summaryrefslogtreecommitdiff
path: root/g_src/mail.hpp
blob: b158e31af6996c49cc28e5a2f4df96baed4f2126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef MAIL_H
#define MAIL_H

#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <queue>

template <typename T>
class MBox {
  T val;
  SDL_sem *fill, *empty;
public:
  bool try_read(T &r) { // Attempt to read the mbox. Returns true if read succeeded.
    if (SDL_SemTryWait(fill) == 0) {
      r = val;
      SDL_SemPost(empty);
      return true;
    } else
      return false;
  }
  void read(T &r) {
    SDL_SemWait(fill);
    r = val;
    SDL_SemPost(empty);
  }
  void write(const T &v) {
    SDL_SemWait(empty);
    val = v;
    SDL_SemPost(fill);
  }
  bool try_write(const T &v) { // Returns true if the write succeeded
    if (SDL_SemTryWait(empty) == 0) {
      val = v;
      SDL_SemPost(fill);
      return true;
    } else
      return false;
  }
  MBox(T &v) {
    MBox();
    write(v);
  }
  MBox() {
    fill  = SDL_CreateSemaphore(0);
    empty = SDL_CreateSemaphore(1);
  }
  ~MBox() {
    SDL_DestroySemaphore(fill);
    SDL_DestroySemaphore(empty);
  }
};

template <typename T>
class MVar {
  SDL_sem *s;
public:
  T val;
  void lock() { SDL_SemWait(s); }
  void unlock() { SDL_SemPost(s); }
  MVar() { s = SDL_CreateSemaphore(1); }
  ~MVar() { SDL_DestroySemaphore(s); }
  void write(const T &w) { lock(); val = w; unlock(); }
  void read(T &r) { lock(); r = val; unlock(); }
  T read() { T r; read(r); return r; }
};
  

template<bool start_locked = false>
class Lock {
  SDL_sem *s;
public:
  void lock() { SDL_SemWait(s); }
  void unlock() { SDL_SemPost(s); }
  Lock() { s = SDL_CreateSemaphore(start_locked ? 0 : 1); }
  ~Lock() { SDL_DestroySemaphore(s); }
};


template<typename T>
class Chan {
  MVar<std::queue<T> > vals;
  SDL_sem *fill;
public:
  bool try_read(T &r) {
    if (SDL_SemTryWait(fill) == 0) {
      vals.lock();
      r = vals.val.front();
      vals.val.pop();
      vals.unlock();
      return true;
    } else
      return false;
  }
  void read(T &r) {
    SDL_SemWait(fill);
    vals.lock();
    r = vals.val.front();
    vals.val.pop();
    vals.unlock();
  }
  void write(const T &w) {
    vals.lock();
    vals.val.push(w);
    vals.unlock();
    SDL_SemPost(fill);
  }
  Chan() {
    fill = SDL_CreateSemaphore(0);
  }
  ~Chan() {
    SDL_DestroySemaphore(fill);
  }
};

template<>
class Chan<void> {
  SDL_sem *fill;
public:
  bool try_read() {
    if (SDL_SemTryWait(fill) == 0)
      return true;
    return false;
  }
  void read() {
    SDL_SemWait(fill);
  }
  void write() {
    SDL_SemPost(fill);
  }
  Chan() {
    fill = SDL_CreateSemaphore(0);
  }
  ~Chan() {
    SDL_DestroySemaphore(fill);
  }
};

template<typename L, typename R>
struct Either {
  bool isL;
  union {
    L left;
    R right;
  };
  Either(const L &l) {
    isL = true;
    left = l;
  }
  Either(const R &r) {
    isL = false;
    right = r;
  }
};

#endif