Blender  V2.93
BLI_linklist_lockfree.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2018 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "BLI_linklist_lockfree.h"
27 #include "BLI_strict_flags.h"
28 
29 #include "atomic_ops.h"
30 
32 {
33  list->dummy_node.next = NULL;
34  list->head = list->tail = &list->dummy_node;
35 }
36 
38 {
39  if (free_func != NULL) {
40  /* NOTE: We start from a first user-added node. */
41  LockfreeLinkNode *node = list->head->next;
42  while (node != NULL) {
43  LockfreeLinkNode *node_next = node->next;
44  free_func(node);
45  node = node_next;
46  }
47  }
48 }
49 
51 {
54 }
55 
57 {
58  /* Based on:
59  *
60  * John D. Valois
61  * Implementing Lock-Free Queues
62  *
63  * http://people.csail.mit.edu/bushl2/rpi/portfolio/lockfree-grape/documents/lock-free-linked-lists.pdf
64  */
65  bool keep_working;
66  LockfreeLinkNode *tail_node;
67  node->next = NULL;
68  do {
69  tail_node = list->tail;
70  keep_working = (atomic_cas_ptr((void **)&tail_node->next, NULL, node) != NULL);
71  if (keep_working) {
72  atomic_cas_ptr((void **)&list->tail, tail_node, tail_node->next);
73  }
74  } while (keep_working);
75  atomic_cas_ptr((void **)&list->tail, tail_node, node);
76 }
77 
79 {
80  return list->head->next;
81 }
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
Read Guarded memory(de)allocation.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE void * atomic_cas_ptr(void **v, void *old, void *_new)
static PyObject * free_func(PyObject *, PyObject *value)
OperationNode * node
struct LockfreeLinkNode * next