alternative new version of the AppleUtility library
[ardour.git] / libs / appleutility / CoreAudio / PublicUtility / CAAtomicStack.h
1 /*
2      File: CAAtomicStack.h
3  Abstract: Part of CoreAudio Utility Classes
4   Version: 1.1
5  
6  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
7  Inc. ("Apple") in consideration of your agreement to the following
8  terms, and your use, installation, modification or redistribution of
9  this Apple software constitutes acceptance of these terms.  If you do
10  not agree with these terms, please do not use, install, modify or
11  redistribute this Apple software.
12  
13  In consideration of your agreement to abide by the following terms, and
14  subject to these terms, Apple grants you a personal, non-exclusive
15  license, under Apple's copyrights in this original Apple software (the
16  "Apple Software"), to use, reproduce, modify and redistribute the Apple
17  Software, with or without modifications, in source and/or binary forms;
18  provided that if you redistribute the Apple Software in its entirety and
19  without modifications, you must retain this notice and the following
20  text and disclaimers in all such redistributions of the Apple Software.
21  Neither the name, trademarks, service marks or logos of Apple Inc. may
22  be used to endorse or promote products derived from the Apple Software
23  without specific prior written permission from Apple.  Except as
24  expressly stated in this notice, no other rights or licenses, express or
25  implied, are granted by Apple herein, including but not limited to any
26  patent rights that may be infringed by your derivative works or by other
27  works in which the Apple Software may be incorporated.
28  
29  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
30  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34  
35  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43  
44  Copyright (C) 2014 Apple Inc. All Rights Reserved.
45  
46 */
47 #ifndef __CAAtomicStack_h__
48 #define __CAAtomicStack_h__
49
50 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
51         #include <libkern/OSAtomic.h>
52 #else
53         #include <CAAtomic.h>
54 #endif
55
56 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
57         #include <CoreServices/CoreServices.h>
58 #endif
59
60 //  linked list LIFO or FIFO (pop_all_reversed) stack, elements are pushed and popped atomically
61 //  class T must implement T *& next().
62 template <class T>
63 class TAtomicStack {
64 public:
65         TAtomicStack() : mHead(NULL) { }
66         
67         // non-atomic routines, for use when initializing/deinitializing, operate NON-atomically
68         void    push_NA(T *item)
69         {
70                 item->next() = mHead;
71                 mHead = item;
72         }
73         
74         T *             pop_NA()
75         {
76                 T *result = mHead;
77                 if (result)
78                         mHead = result->next();
79                 return result;
80         }
81         
82         bool    empty() const { return mHead == NULL; }
83         
84         T *             head() { return mHead; }
85         
86         // atomic routines
87         void    push_atomic(T *item)
88         {
89                 T *head_;
90                 do {
91                         head_ = mHead;
92                         item->next() = head_;
93                 } while (!compare_and_swap(head_, item, &mHead));
94         }
95         
96         void    push_multiple_atomic(T *item)
97                 // pushes entire linked list headed by item
98         {
99                 T *head_, *p = item, *tail;
100                 // find the last one -- when done, it will be linked to head
101                 do {
102                         tail = p;
103                         p = p->next();
104                 } while (p);
105                 do {
106                         head_ = mHead;
107                         tail->next() = head_;
108                 } while (!compare_and_swap(head_, item, &mHead));
109         }
110         
111         T *             pop_atomic_single_reader()
112                 // this may only be used when only one thread may potentially pop from the stack.
113                 // if multiple threads may pop, this suffers from the ABA problem.
114                 // <rdar://problem/4606346> TAtomicStack suffers from the ABA problem
115         {
116                 T *result;
117                 do {
118                         if ((result = mHead) == NULL)
119                                 break;
120                 } while (!compare_and_swap(result, result->next(), &mHead));
121                 return result;
122         }
123         
124         T *             pop_atomic()
125                 // This is inefficient for large linked lists.
126                 // prefer pop_all() to a series of calls to pop_atomic.
127                 // push_multiple_atomic has to traverse the entire list.
128         {
129                 T *result = pop_all();
130                 if (result) {
131                         T *next = result->next();
132                         if (next)
133                                 // push all the remaining items back onto the stack
134                                 push_multiple_atomic(next);
135                 }
136                 return result;
137         }
138         
139         T *             pop_all()
140         {
141                 T *result;
142                 do {
143                         if ((result = mHead) == NULL)
144                                 break;
145                 } while (!compare_and_swap(result, NULL, &mHead));
146                 return result;
147         }
148         
149         T*              pop_all_reversed()
150         {
151                 TAtomicStack<T> reversed;
152                 T *p = pop_all(), *next;
153                 while (p != NULL) {
154                         next = p->next();
155                         reversed.push_NA(p);
156                         p = next;
157                 }
158                 return reversed.mHead;
159         }
160         
161         static bool     compare_and_swap(T *oldvalue, T *newvalue, T **pvalue)
162         {
163 #if TARGET_OS_MAC
164         #if __LP64__
165                         return ::OSAtomicCompareAndSwap64Barrier(int64_t(oldvalue), int64_t(newvalue), (int64_t *)pvalue);
166         #elif MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
167                         return ::OSAtomicCompareAndSwap32Barrier(int32_t(oldvalue), int32_t(newvalue), (int32_t *)pvalue);
168         #else
169                         return ::CompareAndSwap(UInt32(oldvalue), UInt32(newvalue), (UInt32 *)pvalue);
170         #endif
171 #else
172                         //return ::CompareAndSwap(UInt32(oldvalue), UInt32(newvalue), (UInt32 *)pvalue);
173                         return CAAtomicCompareAndSwap32Barrier(SInt32(oldvalue), SInt32(newvalue), (SInt32*)pvalue);
174 #endif
175         }
176         
177 protected:
178         T *             mHead;
179 };
180
181 #if ((MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) && !TARGET_OS_WIN32)
182 #include <libkern/OSAtomic.h>
183
184 class CAAtomicStack {
185 public:
186         CAAtomicStack(size_t nextPtrOffset) : mNextPtrOffset(nextPtrOffset) {
187                 /*OSQueueHead h = OS_ATOMIC_QUEUE_INIT; mHead = h;*/
188                 mHead.opaque1 = 0; mHead.opaque2 = 0;
189         }
190         // a subset of the above
191         void    push_atomic(void *p) { OSAtomicEnqueue(&mHead, p, mNextPtrOffset); }
192         void    push_NA(void *p) { push_atomic(p); }
193
194         void *  pop_atomic() { return OSAtomicDequeue(&mHead, mNextPtrOffset); }
195         void *  pop_atomic_single_reader() { return pop_atomic(); }
196         void *  pop_NA() { return pop_atomic(); }
197         
198 private:
199         OSQueueHead             mHead;
200         size_t                  mNextPtrOffset;
201 };
202
203 // a more efficient subset of TAtomicStack using OSQueue.
204 template <class T>
205 class TAtomicStack2 {
206 public:
207         TAtomicStack2() {
208                 /*OSQueueHead h = OS_ATOMIC_QUEUE_INIT; mHead = h;*/
209                 mHead.opaque1 = 0; mHead.opaque2 = 0;
210                 mNextPtrOffset = -1;
211         }
212         void    push_atomic(T *item) {
213                 if (mNextPtrOffset < 0) {
214                         T **pnext = &item->next();      // hack around offsetof not working with C++
215                         mNextPtrOffset = (Byte *)pnext - (Byte *)item;
216                 }
217                 OSAtomicEnqueue(&mHead, item, mNextPtrOffset);
218         }
219         void    push_NA(T *item) { push_atomic(item); }
220
221         T *             pop_atomic() { return (T *)OSAtomicDequeue(&mHead, mNextPtrOffset); }
222         T *             pop_atomic_single_reader() { return pop_atomic(); }
223         T *             pop_NA() { return pop_atomic(); }
224         
225         // caution: do not try to implement pop_all_reversed here. the writer could add new elements
226         // while the reader is trying to pop old ones!
227         
228 private:
229         OSQueueHead             mHead;
230         ssize_t                 mNextPtrOffset;
231 };
232
233 #else
234
235 #define TAtomicStack2 TAtomicStack
236
237 #endif // MAC_OS_X_VERSION_MAX_ALLOWED && !TARGET_OS_WIN32
238
239 #endif // __CAAtomicStack_h__