Blender  V3.3
BLI_linklist_lockfree.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2018 Blender Foundation. All rights reserved. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "BLI_linklist_lockfree.h"
11 #include "BLI_strict_flags.h"
12 
13 #include "atomic_ops.h"
14 
16 {
17  list->dummy_node.next = NULL;
18  list->head = list->tail = &list->dummy_node;
19 }
20 
22 {
23  if (free_func != NULL) {
24  /* NOTE: We start from a first user-added node. */
25  LockfreeLinkNode *node = list->head->next;
26  while (node != NULL) {
27  LockfreeLinkNode *node_next = node->next;
28  free_func(node);
29  node = node_next;
30  }
31  }
32 }
33 
35 {
38 }
39 
41 {
42  /* Based on:
43  *
44  * John D. Valois
45  * Implementing Lock-Free Queues
46  *
47  * http://people.csail.mit.edu/bushl2/rpi/portfolio/lockfree-grape/documents/lock-free-linked-lists.pdf
48  */
49  bool keep_working;
50  LockfreeLinkNode *tail_node;
51  node->next = NULL;
52  do {
53  tail_node = list->tail;
54  keep_working = (atomic_cas_ptr((void **)&tail_node->next, NULL, node) != NULL);
55  if (keep_working) {
56  atomic_cas_ptr((void **)&list->tail, tail_node, tail_node->next);
57  }
58  } while (keep_working);
59  atomic_cas_ptr((void **)&list->tail, tail_node, node);
60 }
61 
63 {
64  return list->head->next;
65 }
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)
OperationNode * node
static PyObject * free_func(PyObject *, PyObject *value)
Definition: python.cpp:220
struct LockfreeLinkNode * next