summaryrefslogtreecommitdiffstats
path: root/client/read_write_mutex.h
blob: 721422e53220a9fc2f4432a1262c008262a9fad3 (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
/*
   Copyright (C) 2009 Red Hat, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _H_READ_WRITE_MUTEX
#define _H_READ_WRITE_MUTEX

#include "threads.h"

class ReadWriteMutex {
public:
    ReadWriteMutex()
    {
        _state.read_count = 0;
        _state.write = false;
        _state.write_waiting = false;
    }

    virtual ~ReadWriteMutex()
    {
    }

    void read_lock()
    {
        Lock lock(_state_mutex);

        while (_state.write || _state.write_waiting) {
            _read_cond.wait(lock);
        }

        ++_state.read_count;
    }

    bool try_read_lock()
    {
        Lock lock(_state_mutex);
        if (_state.write || _state.write_waiting) {
            return false;
        } else {
            ++_state.read_count;
            return true;
        }
    }

    void read_unlock()
    {
        Lock lock(_state_mutex);
        --_state.read_count;

        if (!_state.read_count) { // last reader
            _state.write_waiting = false;
            release_waiters();
        }
    }

    void write_lock()
    {
        Lock lock(_state_mutex);

        while (_state.read_count || _state.write) {
            _state.write_waiting = true;
            _write_cond.wait(lock);
        }
        _state.write = true;
    }

    bool try_write_lock()
    {
        Lock lock(_state_mutex);

        if (_state.read_count || _state.write) {
            return false;
        } else {
            _state.write = true;
            return true;
        }
    }

    void write_unlock()
    {
        Lock lock(_state_mutex);
        _state.write = false;
        _state.write_waiting = false;
        release_waiters();
    }

private:
    void release_waiters()
    {
        _write_cond.notify_one();
        _read_cond.notify_all();
    }

private:
    struct {
        unsigned int read_count;
        bool write;
        bool write_waiting;
    } _state;

    Mutex _state_mutex;
    Condition _read_cond;
    Condition _write_cond;
};

#endif