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

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