Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / include / common / MsgFifo.h
blobc8b7c8e75d4101d39fcef30a22a8ec4074c7a3b9
1 /*
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
21 #ifndef _MsgFifo_
22 #define _MsgFifo_
24 #ifdef __APPLE__
25 # include <libkern/OSAtomic.h>
26 #endif
28 #ifdef _WIN32
29 # include <winsock2.h>
30 # include <windows.h>
31 #endif
33 /////////////////////////////////////////////////////////////////////
35 template <class MsgType, int N>
36 class MsgFifo
38 public:
39 MsgFifo()
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
52 mItems[next] = data;
53 #ifdef __APPLE__
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);
57 #elif defined(_WIN32)
58 InterlockedExchange(reinterpret_cast<volatile LONG*>(&mWriteHead),next);
59 #else
60 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
61 __sync_synchronize();
62 #endif
63 mWriteHead = next;
64 #endif
65 return true;
68 void Perform() // get next and advance
70 while (HasData()) {
71 unsigned int next = NextPos(mReadHead);
72 mItems[next].Perform();
73 #ifdef __APPLE__
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);
77 #elif defined(_WIN32)
78 InterlockedExchange(reinterpret_cast<volatile LONG*>(&mReadHead),next);
79 #else
80 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
81 __sync_synchronize();
82 #endif
83 mReadHead = next;
84 #endif
87 void Free() // reclaim messages
89 while (NeedsFree()) {
90 unsigned int next = NextPos(mFreeHead);
91 mItems[next].Free();
92 #ifdef __APPLE__
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);
96 #elif defined(_WIN32)
97 InterlockedExchange(reinterpret_cast<volatile LONG*>(&mFreeHead),next);
98 #else
99 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
100 __sync_synchronize();
101 #endif
102 mFreeHead = next;
103 #endif
107 private:
108 int NextPos(int inPos) { return (inPos + 1) & (N - 1); }
110 #ifdef __APPLE__
111 int32_t mReadHead, mWriteHead, mFreeHead;
112 #else
113 volatile int mReadHead, mWriteHead, mFreeHead;
114 #endif
115 MsgType mItems[N];
118 /////////////////////////////////////////////////////////////////////
120 template <class MsgType, int N>
121 class MsgFifoNoFree
123 public:
124 MsgFifoNoFree()
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
137 mItems[next] = data;
138 #ifdef __APPLE__
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);
144 #else
145 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
146 __sync_synchronize();
147 #endif
148 mWriteHead = next;
149 #endif
150 return true;
153 void Perform() // get next and advance
155 while (HasData()) {
156 unsigned int next = NextPos(mReadHead);
157 mItems[next].Perform();
158 #ifdef __APPLE__
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);
164 #else
165 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) && ( !defined(__INTEL_COMPILER) || defined(__ia64) )
166 __sync_synchronize();
167 #endif
168 mReadHead = next;
169 #endif
173 private:
174 int NextPos(int inPos) { return (inPos + 1) & (N - 1); }
175 #ifdef __APPLE__
176 int32_t mReadHead, mWriteHead;
177 #else
178 volatile int mReadHead, mWriteHead;
179 #endif
180 MsgType mItems[N];
183 /////////////////////////////////////////////////////////////////////
186 #endif