Explicitly include a boost "windows" folder even on linux
[supercollider.git] / include / common / SC_FIFO.h
blob4f8b5b606cc08b8c794c404f6ca6f41d317488c0
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 SC_FIFO_H_INCLUDED
22 #define SC_FIFO_H_INCLUDED
24 #ifdef __APPLE__
25 # include <libkern/OSAtomic.h>
26 #endif
28 #ifdef _WIN32
29 # include <winsock2.h>
30 # include <windows.h>
31 #endif
33 template <class T, int N>
34 class SC_FIFO
36 public:
37 SC_FIFO()
38 : mMask(N - 1),
39 mReadHead(0),
40 mWriteHead(0)
43 void MakeEmpty() { mReadHead = mWriteHead; }
44 bool IsEmpty() { return mReadHead == mWriteHead; }
45 bool HasData() { return mReadHead != mWriteHead; }
47 bool Put(T data)
49 long next = NextPos(mWriteHead);
50 if (next == mReadHead) return false; // fifo is full
51 mItems[next] = data;
52 #ifdef __APPLE__
53 // we don't really need a compare and swap, but this happens to call
54 // the PowerPC memory barrier instruction lwsync.
55 OSAtomicCompareAndSwap32Barrier(mWriteHead, next, &mWriteHead);
56 #elif defined(_WIN32)
57 InterlockedExchange(reinterpret_cast<volatile LONG*>(&mWriteHead),next);
58 #else
59 mWriteHead = next;
60 #endif
61 return true;
64 T Get()
66 //assert(HasData());
67 long next = NextPos(mReadHead);
68 T out = mItems[next];
69 #ifdef __APPLE__
70 // we don't really need a compare and swap, but this happens to call
71 // the PowerPC memory barrier instruction lwsync.
72 OSAtomicCompareAndSwap32Barrier(mReadHead, next, &mReadHead);
73 #elif defined(_WIN32)
74 InterlockedExchange(reinterpret_cast<volatile LONG*>(&mReadHead),next);
75 #else
76 mReadHead = next;
77 #endif
78 return out;
81 int Capacity() const { return N; }
83 private:
84 int NextPos(int inPos) { return (inPos + 1) & mMask; }
86 long mMask;
88 #ifdef __APPLE__
89 int32_t mReadHead, mWriteHead;
90 #else
91 volatile int mReadHead, mWriteHead;
92 #endif
93 T mItems[N];
96 #endif // SC_FIFO_H_INCLUDED