]>
git.saurik.com Git - apple/libplatform.git/blob - include/libkern/OSAtomicQueue.h
103f1e86ba616b7e0f68ef88a54bc8fdd902215c
2 * Copyright (c) 2004-2016 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _OSATOMICQUEUE_H_
25 #define _OSATOMICQUEUE_H_
28 #include <sys/cdefs.h>
32 #include <Availability.h>
34 /*! @header Lockless atomic enqueue and dequeue
35 * These routines manipulate singly-linked LIFO lists.
40 /*! @abstract The data structure for a queue head.
42 You should always initialize a queue head structure with the
43 initialization vector {@link OS_ATOMIC_QUEUE_INIT} before use.
47 typedef volatile struct {
50 } __attribute__ ((aligned (16))) OSQueueHead
;
54 typedef volatile struct {
61 /*! @abstract The initialization vector for a queue head. */
62 #define OS_ATOMIC_QUEUE_INIT { NULL, 0 }
64 /*! @abstract Enqueue an element onto a list.
66 Memory barriers are incorporated as needed to permit thread-safe access
69 The list on which you want to enqueue the element.
73 The "offset" parameter is the offset (in bytes) of the link field
74 from the beginning of the data structure being queued (<code>__new</code>).
75 The link field should be a pointer type.
76 The <code>__offset</code> value needs to be same for all enqueuing and
77 dequeuing operations on the same list, even if different structure types
78 are enqueued on that list. The use of <code>offsetset()</code>, defined in
79 <code>stddef.h</code> is the common way to specify the <code>__offset</code>
82 __OSX_AVAILABLE_STARTING(__MAC_10_5
, __IPHONE_4_0
)
83 void OSAtomicEnqueue( OSQueueHead
*__list
, void *__new
, size_t __offset
);
86 /*! @abstract Dequeue an element from a list.
88 Memory barriers are incorporated as needed to permit thread-safe access
91 The list from which you want to dequeue an element.
93 The "offset" parameter is the offset (in bytes) of the link field
94 from the beginning of the data structure being dequeued (<code>__new</code>).
95 The link field should be a pointer type.
96 The <code>__offset</code> value needs to be same for all enqueuing and
97 dequeuing operations on the same list, even if different structure types
98 are enqueued on that list. The use of <code>offsetset()</code>, defined in
99 <code>stddef.h</code> is the common way to specify the <code>__offset</code>
101 IMPORTANT: the memory backing the link field of a queue element must not be
102 unmapped after OSAtomicDequeue() returns until all concurrent calls to
103 OSAtomicDequeue() for the same list on other threads have also returned,
104 as they may still be accessing that memory location.
106 Returns the most recently enqueued element, or <code>NULL</code> if the
109 __OSX_AVAILABLE_STARTING(__MAC_10_5
, __IPHONE_4_0
)
110 void* OSAtomicDequeue( OSQueueHead
*__list
, size_t __offset
);
112 #if defined(__x86_64__) || defined(__i386__)
114 /*! @group Lockless atomic fifo enqueue and dequeue
115 * These routines manipulate singly-linked FIFO lists.
118 /*! @abstract The data structure for a fifo queue head.
120 You should always initialize a fifo queue head structure with the
121 initialization vector {@link OS_ATOMIC_FIFO_QUEUE_INIT} before use.
123 #if defined(__x86_64__)
125 typedef volatile struct {
129 } __attribute__ ((aligned (16))) OSFifoQueueHead
;
133 typedef volatile struct {
141 /*! @abstract The initialization vector for a fifo queue head. */
142 #define OS_ATOMIC_FIFO_QUEUE_INIT { NULL, NULL, 0 }
144 /*! @abstract Enqueue an element onto a list.
146 Memory barriers are incorporated as needed to permit thread-safe access
147 to the queue element.
149 The list on which you want to enqueue the element.
153 The "offset" parameter is the offset (in bytes) of the link field
154 from the beginning of the data structure being queued (<code>__new</code>).
155 The link field should be a pointer type.
156 The <code>__offset</code> value needs to be same for all enqueuing and
157 dequeuing operations on the same list, even if different structure types
158 are enqueued on that list. The use of <code>offsetset()</code>, defined in
159 <code>stddef.h</code> is the common way to specify the <code>__offset</code>
162 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
)
163 void OSAtomicFifoEnqueue( OSFifoQueueHead
*__list
, void *__new
, size_t __offset
);
165 /*! @abstract Dequeue an element from a list.
167 Memory barriers are incorporated as needed to permit thread-safe access
168 to the queue element.
170 The list from which you want to dequeue an element.
172 The "offset" parameter is the offset (in bytes) of the link field
173 from the beginning of the data structure being dequeued (<code>__new</code>).
174 The link field should be a pointer type.
175 The <code>__offset</code> value needs to be same for all enqueuing and
176 dequeuing operations on the same list, even if different structure types
177 are enqueued on that list. The use of <code>offsetset()</code>, defined in
178 <code>stddef.h</code> is the common way to specify the <code>__offset</code>
181 Returns the oldest enqueued element, or <code>NULL</code> if the
184 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
)
185 void* OSAtomicFifoDequeue( OSFifoQueueHead
*__list
, size_t __offset
);
187 #endif /* __i386__ || __x86_64__ */
191 #endif /* _OSATOMICQUEUE_H_ */