summary refs log tree commit diff
path: root/src/rt/sync/lock_and_signal.h
blob: 927b0c5b0ff211ea97ce64fb57a21425f6a3ac9b (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
// -*- c++ -*-
#ifndef LOCK_AND_SIGNAL_H
#define LOCK_AND_SIGNAL_H

#include "rust_globals.h"

#ifndef RUST_NDEBUG
#define DEBUG_LOCKS
#endif

class lock_and_signal {
#if defined(__WIN32__)
    HANDLE _event;
    CRITICAL_SECTION _cs;
#if defined(DEBUG_LOCKS)
    DWORD _holding_thread;
#endif
#else
    pthread_cond_t _cond;
    pthread_mutex_t _mutex;
#if defined(DEBUG_LOCKS)
    pthread_t _holding_thread;
#endif
#endif

#if defined(DEBUG_LOCKS)
    bool lock_held_by_current_thread();
#endif

    void must_not_be_locked();

public:
    lock_and_signal();
    virtual ~lock_and_signal();

    void lock();
    void unlock();
    void wait();
    void signal();

    void must_have_lock();
    void must_not_have_lock();
};

class scoped_lock {
  lock_and_signal &lock;

public:
  scoped_lock(lock_and_signal &lock);
  ~scoped_lock();
};

#endif /* LOCK_AND_SIGNAL_H */