2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 # include <libkern/OSAtomic.h>
29 # include <winsock2.h>
33 /////////////////////////////////////////////////////////////////////
35 template <class MsgType
, int N
>
40 : mReadHead(0), mWriteHead(0), mFreeHead(0)
43 void MakeEmpty() { mFreeHead
= mReadHead
= mWriteHead
; }
44 bool IsEmpty() { return mReadHead
== mWriteHead
; }
45 bool HasData() { return mReadHead
!= mWriteHead
; }
46 bool NeedsFree() { return mFreeHead
!= mReadHead
; }
48 bool Write(MsgType
& data
)
50 unsigned int next
= NextPos(mWriteHead
);
51 if (next
== mFreeHead
) return false; // fifo is full
54 // we don't really need a compare and swap, but this happens to call
55 // the PowerPC memory barrier instruction lwsync.
56 OSAtomicCompareAndSwap32Barrier(mWriteHead
, next
, &mWriteHead
);
58 InterlockedExchange(reinterpret_cast<volatile LONG
*>(&mWriteHead
),next
);
60 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
68 void Perform() // get next and advance
71 unsigned int next
= NextPos(mReadHead
);
72 mItems
[next
].Perform();
74 // we don't really need a compare and swap, but this happens to call
75 // the PowerPC memory barrier instruction lwsync.
76 OSAtomicCompareAndSwap32Barrier(mReadHead
, next
, &mReadHead
);
78 InterlockedExchange(reinterpret_cast<volatile LONG
*>(&mReadHead
),next
);
80 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
87 void Free() // reclaim messages
90 unsigned int next
= NextPos(mFreeHead
);
93 // we don't really need a compare and swap, but this happens to call
94 // the PowerPC memory barrier instruction lwsync.
95 OSAtomicCompareAndSwap32Barrier(mFreeHead
, next
, &mFreeHead
);
97 InterlockedExchange(reinterpret_cast<volatile LONG
*>(&mFreeHead
),next
);
99 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
100 __sync_synchronize();
108 int NextPos(int inPos
) { return (inPos
+ 1) & (N
- 1); }
111 int32_t mReadHead
, mWriteHead
, mFreeHead
;
113 volatile int mReadHead
, mWriteHead
, mFreeHead
;
118 /////////////////////////////////////////////////////////////////////
120 template <class MsgType
, int N
>
125 : mReadHead(0), mWriteHead(0)
129 void MakeEmpty() { mReadHead
= mWriteHead
; }
130 bool IsEmpty() { return mReadHead
== mWriteHead
; }
131 bool HasData() { return mReadHead
!= mWriteHead
; }
133 bool Write(MsgType
& data
)
135 unsigned int next
= NextPos(mWriteHead
);
136 if (next
== mReadHead
) return false; // fifo is full
139 // we don't really need a compare and swap, but this happens to call
140 // the PowerPC memory barrier instruction lwsync.
141 OSAtomicCompareAndSwap32Barrier(mWriteHead
, next
, &mWriteHead
);
142 #elif defined(_WIN32)
143 InterlockedExchange(reinterpret_cast<volatile LONG
*>(&mWriteHead
),next
);
145 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
146 __sync_synchronize();
153 void Perform() // get next and advance
156 unsigned int next
= NextPos(mReadHead
);
157 mItems
[next
].Perform();
159 // we don't really need a compare and swap, but this happens to call
160 // the PowerPC memory barrier instruction lwsync.
161 OSAtomicCompareAndSwap32Barrier(mReadHead
, next
, &mReadHead
);
162 #elif defined(_WIN32)
163 InterlockedExchange(reinterpret_cast<volatile LONG
*>(&mReadHead
),next
);
165 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
166 __sync_synchronize();
174 int NextPos(int inPos
) { return (inPos
+ 1) & (N
- 1); }
176 int32_t mReadHead
, mWriteHead
;
178 volatile int mReadHead
, mWriteHead
;
183 /////////////////////////////////////////////////////////////////////