medfall

A super great game engine
Log | Files | Refs

condition_variable.hpp (10345B)


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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*  Relacy Race Detector
 *  Copyright (c) 2008-2010, Dmitry S. Vyukov
 *  All rights reserved.
 *  This software is provided AS-IS with no warranty, either express or implied.
 *  This software is distributed under a license and may not be copied,
 *  modified or distributed except as expressly authorized under the
 *  terms of the license contained in the file LICENSE.TXT in this distribution.
 */

#ifndef RL_CONDITION_VARIABLE_HPP
#define RL_CONDITION_VARIABLE_HPP
#ifdef _MSC_VER
#   pragma once
#endif

#include "../base.hpp"
#include "../context_base.hpp"
#include "../waitset.hpp"
#include "../signature.hpp"


namespace rl
{

struct mutex_wrapper
{
    virtual void lock(debug_info_param info) const = 0;
    virtual void unlock(debug_info_param info) const = 0;
    virtual ~mutex_wrapper() {}
};

template<typename mutex_t>
class mutex_wrapper_impl : public mutex_wrapper
{
public:
    mutex_wrapper_impl(mutex_t& m)
        : m_(m)
    {
    }

private:
    mutex_t& m_;

    virtual void lock(debug_info_param info) const
    {
        m_.lock(info);
    }

    virtual void unlock(debug_info_param info) const
    {
        m_.unlock(info);
    }

    RL_NOCOPY(mutex_wrapper_impl);
};

struct pred_wrapper
{
    virtual bool exec() const = 0;
    virtual ~pred_wrapper() {}
};

template<typename pred_t>
class pred_wrapper_impl : public pred_wrapper
{
public:
    pred_wrapper_impl(pred_t p)
        : p_(p)
    {
    }

private:
    mutable pred_t p_;

    virtual bool exec() const
    {
        return p_();
    }

    RL_NOCOPY(pred_wrapper_impl);
};


struct condvar_data
{
    virtual void notify_one(debug_info_param info) = 0;
    virtual void notify_all(debug_info_param info) = 0;
    virtual sema_wakeup_reason wait(mutex_wrapper const& lock, bool is_timed, debug_info_param info) = 0;
    virtual bool wait(mutex_wrapper const& lock, pred_wrapper const& pred, bool is_timed, debug_info_param info) = 0;
    virtual ~condvar_data() {} // just to calm down gcc
};

template<thread_id_t thread_count>
class condvar_data_impl : public condvar_data
{
public:
    condvar_data_impl(bool allow_spurious_wakeups)
    {
        spurious_wakeup_limit_ = 0;
        if (allow_spurious_wakeups && ctx().is_random_sched())
            spurious_wakeup_limit_ = 10;
    }

    ~condvar_data_impl()
    {
        //!!! detect destoy when there are blocked threads
    }

private:
    waitset<thread_count>           ws_;
    signature<0xc0ffe3ad>           sign_;
    int                             spurious_wakeup_limit_;

    struct event_t
    {
        enum type_e
        {
            type_notify_one,
            type_notify_all,
            type_wait_enter,
            type_wait_exit,
            type_wait_pred_enter,
            type_wait_pred_exit,
        };

        condvar_data_impl const*    var_addr_;
        type_e                      type_;
        thread_id_t                 thread_count_;
        unpark_reason               reason_;

        void output(std::ostream& s) const
        {
            s << "<" << std::hex << var_addr_ << std::dec << "> cond_var: ";
            switch (type_)
            {
            case type_notify_one:
                s << "notify one total_blocked=" << thread_count_ << " unblocked=" << (thread_count_ ? 1 : 0);
                break;
            case type_notify_all:
                s << "notify all unblocked=" << thread_count_;
                break;
            case type_wait_enter: s << "wait enter"; break;
            case type_wait_exit:
                s << "wait exit";
                if (unpark_reason_normal == reason_)
                    s << " due to notified";
                else if (unpark_reason_timeout == reason_)
                    s << " due to timeout";
                else if (unpark_reason_spurious == reason_)
                    s << " spuriously";
                break;
            case type_wait_pred_enter: s << "wait pred enter"; break;
            case type_wait_pred_exit: s << "wait pred exit"; break;
            }
        }
    };

    virtual void notify_one(debug_info_param info)
    {
        context& c = ctx();
        //??? do I need this scheduler call?
        c.sched();
        sign_.check(info);
        RL_HIST(event_t) {this, event_t::type_notify_one, ws_.size()} RL_HIST_END();
        ws_.unpark_one(c, info);
    }

    virtual void notify_all(debug_info_param info)
    {
        context& c = ctx();
        //??? do I need this scheduler call?
        c.sched();
        sign_.check(info);
        RL_HIST(event_t) {this, event_t::type_notify_all, ws_.size()} RL_HIST_END();
        ws_.unpark_all(c, info);
    }

    virtual sema_wakeup_reason wait(mutex_wrapper const& lock, bool is_timed, debug_info_param info)
    {
        //!!! detect whether mutex is the same
        context& c = ctx();
        sign_.check(info);
        RL_HIST(event_t) {this, event_t::type_wait_enter} RL_HIST_END();
        lock.unlock(info);
        sign_.check(info);
        bool allow_spurious_wakeup = (spurious_wakeup_limit_ > 0);
        unpark_reason reason = ws_.park_current(c, is_timed, allow_spurious_wakeup, false, info);
        if (reason == unpark_reason_spurious)
            spurious_wakeup_limit_ -= 1;
        RL_HIST(event_t) {this, event_t::type_wait_exit, 0, reason} RL_HIST_END();
        lock.lock(info);
        sign_.check(info);
        if (reason == unpark_reason_normal)
            return sema_wakeup_reason_success;
        else if (reason == unpark_reason_spurious)
            return sema_wakeup_reason_spurious;
        else //if (reason == unpark_reason_timeout)
            return sema_wakeup_reason_timeout;
    }

    virtual bool wait(mutex_wrapper const& lock, pred_wrapper const& pred, bool is_timed, debug_info_param info)
    {
        context& c = ctx();
        sign_.check(info);
        RL_HIST(event_t) {this, event_t::type_wait_pred_enter} RL_HIST_END();
        while (!pred.exec())
        {
            sema_wakeup_reason reason = wait(lock, is_timed, info);
            if (reason == sema_wakeup_reason_timeout)
            {
                RL_HIST(event_t) {this, event_t::type_wait_pred_exit} RL_HIST_END();
                return pred.exec();
            }
        }
        RL_HIST(event_t) {this, event_t::type_wait_pred_exit} RL_HIST_END();
        return true;
    }
};


template<typename tag_t>
class condvar
{
public:
    condvar()
        : impl_()
    {
    }

    condvar(condvar const&)
        : impl_()
    {
    }

    condvar& operator = (condvar const&)
    {
        return *this;
    }

    ~condvar()
    {
    }

    void init(bool allow_spurious_wakeups, debug_info_param info)
    {
        context& c = ctx();
        RL_ASSERT_IMPL(0 == impl_, test_result_double_initialization_of_condvar, "", info);
        sign_.check(info);
        impl_ = c.condvar_ctor(allow_spurious_wakeups);
    }

    void deinit(debug_info_param info)
    {
        context& c = ctx();
        check(info);
        c.condvar_dtor(impl_);
        impl_ = 0;
    }

    void notify_one(debug_info_param info)
    {
        check(info);
        impl_->notify_one(info);
    }

    void notify_all(debug_info_param info)
    {
        check(info);
        impl_->notify_all(info);
    }

    template<typename lock_t>
    sema_wakeup_reason wait(lock_t& lock, bool is_timed, debug_info_param info)
    {
        check(info);
        mutex_wrapper_impl<lock_t> w (lock);
        return impl_->wait(w, is_timed, info);
    }

    template<typename lock_t, typename pred_t>
    bool wait(mutex_wrapper const& lock, pred_wrapper const& pred, bool is_timed, debug_info_param info)
    {
        check(info);
        return impl_->wait(mutex_wrapper_impl<lock_t>(lock), pred_wrapper_impl<pred_t>(pred), is_timed, info);
    }

private:
    condvar_data* impl_;
    signature<0xbadc0ffe> sign_;

    void check(debug_info_param info)
    {
        RL_ASSERT_IMPL(impl_, test_result_usage_of_non_initialized_condvar, "", info);
        sign_.check(info);
    }
};



template<typename tag_t>
class condition_variable_std : condvar<tag_t>
{
public:
    condition_variable_std()
    {
        condvar<tag_t>::init(true, $);
    }

    ~condition_variable_std()
    {
        condvar<tag_t>::deinit($);
    }

    void notify_one(debug_info_param info)
    {
        condvar<tag_t>::notify_one(info);
    }

    void notify_all(debug_info_param info)
    {
        condvar<tag_t>::notify_all(info);
    }

    template<typename lock_t>
    void wait(lock_t& lock, debug_info_param info)
    {
        condvar<tag_t>::wait(lock, false, info);
    }

    template<typename lock_t, typename pred_t>
    void wait(lock_t& lock, pred_t pred, debug_info_param info)
    {
        condvar<tag_t>::wait(lock, pred, false, info);
    }

    template<typename lock_t, typename abs_time_t>
    bool wait_until(lock_t& lock, abs_time_t const&, debug_info_param info)
    {
        return condvar<tag_t>::wait(lock, true, info);
    }

    template<typename lock_t, typename abs_time_t, typename pred_t>
    bool wait_until(lock_t& lock, abs_time_t const&, pred_t pred, debug_info_param info)
    {
        return condvar<tag_t>::wait(lock, pred, true, info);
    }
    
    template<typename lock_t, typename rel_time_t>
    bool wait_for(lock_t& lock, rel_time_t const&, debug_info_param info)
    {
        sema_wakeup_reason reason = condvar<tag_t>::wait(lock, true, info);
        return reason == sema_wakeup_reason_success;
    }

    template<typename lock_t, typename rel_time_t, typename pred_t>
    bool wait_for(lock_t& lock, rel_time_t const&, pred_t pred, debug_info_param info)
    {
        return condvar<tag_t>::wait(lock, pred, true, info);
    }

    RL_NOCOPY(condition_variable_std);
};


struct condvar_tag_std;
typedef condition_variable_std<condvar_tag_std> condition_variable;
struct condvar_tag_std_any;
typedef condition_variable_std<condvar_tag_std_any> condition_variable_any;

}

#endif