1 // SPDX-FileCopyrightText: 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: LGPL-2.1-or-later
5 #ifndef _URCU_WFQUEUE_STATIC_H
6 #define _URCU_WFQUEUE_STATIC_H
9 * Userspace RCU library - Queue with Wait-Free Enqueue/Blocking Dequeue
11 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See wfqueue.h for linking
12 * dynamically with the userspace rcu library.
17 #include <urcu/assert.h>
18 #include <urcu/compiler.h>
19 #include <urcu/uatomic.h>
26 * Queue with wait-free enqueue/blocking dequeue.
27 * This implementation adds a dummy head node when the queue is empty to ensure
28 * we can always update the queue locklessly.
30 * Inspired from half-wait-free/half-blocking queue implementation done by
34 #define WFQ_ADAPT_ATTEMPTS 10 /* Retry if being set */
35 #define WFQ_WAIT 10 /* Wait 10 ms if being set */
37 static inline void _cds_wfq_node_init(struct cds_wfq_node
*node
)
42 static inline void _cds_wfq_init(struct cds_wfq_queue
*q
)
46 _cds_wfq_node_init(&q
->dummy
);
47 /* Set queue head and tail */
49 q
->tail
= &q
->dummy
.next
;
50 ret
= pthread_mutex_init(&q
->lock
, NULL
);
51 urcu_posix_assert(!ret
);
54 static inline void _cds_wfq_destroy(struct cds_wfq_queue
*q
)
56 int ret
= pthread_mutex_destroy(&q
->lock
);
57 urcu_posix_assert(!ret
);
60 static inline void _cds_wfq_enqueue(struct cds_wfq_queue
*q
,
61 struct cds_wfq_node
*node
)
63 struct cds_wfq_node
**old_tail
;
66 * uatomic_xchg() implicit memory barrier orders earlier stores to data
67 * structure containing node and setting node->next to NULL before
70 old_tail
= uatomic_xchg(&q
->tail
, &node
->next
);
72 * At this point, dequeuers see a NULL old_tail->next, which indicates
73 * that the queue is being appended to. The following store will append
74 * "node" to the queue from a dequeuer perspective.
76 CMM_STORE_SHARED(*old_tail
, node
);
80 * Waiting for enqueuer to complete enqueue and return the next node
82 static inline struct cds_wfq_node
*
83 ___cds_wfq_node_sync_next(struct cds_wfq_node
*node
)
85 struct cds_wfq_node
*next
;
89 * Adaptative busy-looping waiting for enqueuer to complete enqueue.
91 while ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
92 if (++attempt
>= WFQ_ADAPT_ATTEMPTS
) {
93 (void) poll(NULL
, 0, WFQ_WAIT
); /* Wait for 10ms */
103 * It is valid to reuse and free a dequeued node immediately.
105 * No need to go on a waitqueue here, as there is no possible state in which the
106 * list could cause dequeue to busy-loop needlessly while waiting for another
107 * thread to be scheduled. The queue appears empty until tail->next is set by
110 static inline struct cds_wfq_node
*
111 ___cds_wfq_dequeue_blocking(struct cds_wfq_queue
*q
)
113 struct cds_wfq_node
*node
, *next
;
116 * Queue is empty if it only contains the dummy node.
118 if (q
->head
== &q
->dummy
&& CMM_LOAD_SHARED(q
->tail
) == &q
->dummy
.next
)
122 next
= ___cds_wfq_node_sync_next(node
);
125 * Move queue head forward.
129 * Requeue dummy node if we just dequeued it.
131 if (node
== &q
->dummy
) {
132 _cds_wfq_node_init(node
);
133 _cds_wfq_enqueue(q
, node
);
134 return ___cds_wfq_dequeue_blocking(q
);
139 static inline struct cds_wfq_node
*
140 _cds_wfq_dequeue_blocking(struct cds_wfq_queue
*q
)
142 struct cds_wfq_node
*retnode
;
145 ret
= pthread_mutex_lock(&q
->lock
);
146 urcu_posix_assert(!ret
);
147 retnode
= ___cds_wfq_dequeue_blocking(q
);
148 ret
= pthread_mutex_unlock(&q
->lock
);
149 urcu_posix_assert(!ret
);
157 #endif /* _URCU_WFQUEUE_STATIC_H */
This page took 0.034803 seconds and 4 git commands to generate.