]> AND Private Git Repository - loba.git/blob - synchro.h
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
07897e04febe1eea9075db9590c4bf9571dff2dd
[loba.git] / synchro.h
1 #ifndef SYNCHRO_H
2 #define SYNCHRO_H
3
4 #include <xbt/ex.hpp>
5 #include <xbt/synchro.h>
6
7 class mutex_t {
8 public:
9     mutex_t()                   { mutex = xbt_mutex_init(); }
10     ~mutex_t()                  { xbt_mutex_destroy(mutex); }
11     void acquire()              { xbt_mutex_acquire(mutex); }
12     void release()              { xbt_mutex_release(mutex); }
13
14 private:
15     xbt_mutex_t mutex;
16
17     friend class condition_t;
18 };
19
20 class condition_t {
21 public:
22     condition_t()               { cond = xbt_cond_init();           }
23     ~condition_t()              { xbt_cond_destroy(cond);           }
24     void broadcast()            { xbt_cond_broadcast(cond);         }
25     void signal()               { xbt_cond_signal(cond);            }
26     void wait(mutex_t& mutex)   { xbt_cond_wait(cond, mutex.mutex); }
27     bool timedwait(mutex_t& mutex, double delay) {
28         try {
29             xbt_cond_timedwait(cond, mutex.mutex, delay);
30         }
31         catch (const xbt_ex& e) {
32             if (e.category != timeout_error)
33                 throw;
34             mutex.acquire();
35             return false;       // got a timeout
36         }
37         return true;
38     }
39
40 private:
41     xbt_cond_t cond;
42 };
43
44 #endif // !SYNCHRO_H
45
46 // Local variables:
47 // mode: c++
48 // End: