5 #include <SDL/SDL_thread.h>
11 SDL_sem
*fill
, *empty
;
13 bool try_read(T
&r
) { // Attempt to read the mbox. Returns true if read succeeded.
14 if (SDL_SemTryWait(fill
) == 0) {
26 void write(const T
&v
) {
31 bool try_write(const T
&v
) { // Returns true if the write succeeded
32 if (SDL_SemTryWait(empty
) == 0) {
44 fill
= SDL_CreateSemaphore(0);
45 empty
= SDL_CreateSemaphore(1);
48 SDL_DestroySemaphore(fill
);
49 SDL_DestroySemaphore(empty
);
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>
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
); }
81 MVar
<std::queue
<T
> > vals
;
85 if (SDL_SemTryWait(fill
) == 0) {
101 void write(const T
&w
) {
108 fill
= SDL_CreateSemaphore(0);
111 SDL_DestroySemaphore(fill
);
120 if (SDL_SemTryWait(fill
) == 0)
131 fill
= SDL_CreateSemaphore(0);
134 SDL_DestroySemaphore(fill
);
138 template<typename L
, typename R
>