pick up CXXFLAGS passed on commandline
[rofl0r-df-libgraphics.git] / g_src / mail.hpp
blobb158e31af6996c49cc28e5a2f4df96baed4f2126
1 #ifndef MAIL_H
2 #define MAIL_H
4 #include <SDL/SDL.h>
5 #include <SDL/SDL_thread.h>
6 #include <queue>
8 template <typename T>
9 class MBox {
10 T val;
11 SDL_sem *fill, *empty;
12 public:
13 bool try_read(T &r) { // Attempt to read the mbox. Returns true if read succeeded.
14 if (SDL_SemTryWait(fill) == 0) {
15 r = val;
16 SDL_SemPost(empty);
17 return true;
18 } else
19 return false;
21 void read(T &r) {
22 SDL_SemWait(fill);
23 r = val;
24 SDL_SemPost(empty);
26 void write(const T &v) {
27 SDL_SemWait(empty);
28 val = v;
29 SDL_SemPost(fill);
31 bool try_write(const T &v) { // Returns true if the write succeeded
32 if (SDL_SemTryWait(empty) == 0) {
33 val = v;
34 SDL_SemPost(fill);
35 return true;
36 } else
37 return false;
39 MBox(T &v) {
40 MBox();
41 write(v);
43 MBox() {
44 fill = SDL_CreateSemaphore(0);
45 empty = SDL_CreateSemaphore(1);
47 ~MBox() {
48 SDL_DestroySemaphore(fill);
49 SDL_DestroySemaphore(empty);
53 template <typename T>
54 class MVar {
55 SDL_sem *s;
56 public:
57 T val;
58 void lock() { SDL_SemWait(s); }
59 void unlock() { SDL_SemPost(s); }
60 MVar() { s = SDL_CreateSemaphore(1); }
61 ~MVar() { SDL_DestroySemaphore(s); }
62 void write(const T &w) { lock(); val = w; unlock(); }
63 void read(T &r) { lock(); r = val; unlock(); }
64 T read() { T r; read(r); return r; }
68 template<bool start_locked = false>
69 class Lock {
70 SDL_sem *s;
71 public:
72 void lock() { SDL_SemWait(s); }
73 void unlock() { SDL_SemPost(s); }
74 Lock() { s = SDL_CreateSemaphore(start_locked ? 0 : 1); }
75 ~Lock() { SDL_DestroySemaphore(s); }
79 template<typename T>
80 class Chan {
81 MVar<std::queue<T> > vals;
82 SDL_sem *fill;
83 public:
84 bool try_read(T &r) {
85 if (SDL_SemTryWait(fill) == 0) {
86 vals.lock();
87 r = vals.val.front();
88 vals.val.pop();
89 vals.unlock();
90 return true;
91 } else
92 return false;
94 void read(T &r) {
95 SDL_SemWait(fill);
96 vals.lock();
97 r = vals.val.front();
98 vals.val.pop();
99 vals.unlock();
101 void write(const T &w) {
102 vals.lock();
103 vals.val.push(w);
104 vals.unlock();
105 SDL_SemPost(fill);
107 Chan() {
108 fill = SDL_CreateSemaphore(0);
110 ~Chan() {
111 SDL_DestroySemaphore(fill);
115 template<>
116 class Chan<void> {
117 SDL_sem *fill;
118 public:
119 bool try_read() {
120 if (SDL_SemTryWait(fill) == 0)
121 return true;
122 return false;
124 void read() {
125 SDL_SemWait(fill);
127 void write() {
128 SDL_SemPost(fill);
130 Chan() {
131 fill = SDL_CreateSemaphore(0);
133 ~Chan() {
134 SDL_DestroySemaphore(fill);
138 template<typename L, typename R>
139 struct Either {
140 bool isL;
141 union {
142 L left;
143 R right;
145 Either(const L &l) {
146 isL = true;
147 left = l;
149 Either(const R &r) {
150 isL = false;
151 right = r;
155 #endif