medfall

A super great game engine
Log | Files | Refs

dyn_thread_ctx.hpp (2955B)


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
/*  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_DYN_THREAD_CTX_HPP
#define RL_DYN_THREAD_CTX_HPP
#ifdef _MSC_VER
#   pragma once
#endif


#include "base.hpp"
#include "waitset.hpp"
#include "sync_var.hpp"
#include "stdlib/semaphore.hpp"


namespace rl
{


template<thread_id_t thread_count>
class thread_sync_object : public win_waitable_object
{
public:
    thread_sync_object()
    {
    }

    void iteration_begin()
    {
        finished_ = false;
        sync_.iteration_begin();
        RL_VERIFY(!ws_);
    }

    void on_create()
    {
        sync_.release(ctx().threadx_);
    }

    void on_start()
    {
        RL_VERIFY(finished_ == false);
        context& c = ctx();
        sync_.acquire(c.threadx_);
    }
    
    void on_finish()
    {
        RL_VERIFY(finished_ == false);
        context& c = ctx();
        finished_ = true;
        sync_.release(c.threadx_);
        ws_.unpark_all(c, $);
    }

private:
    bool finished_;
    waitset<thread_count> ws_;
    sync_var<thread_count> sync_;

    virtual void deinit(debug_info_param info)
    {
        (void)info;
    }

    virtual sema_wakeup_reason wait(bool try_wait, bool is_timed, debug_info_param info)
    {
        context& c = ctx();
        if (finished_)
        {
            sync_.acquire(c.threadx_);
            return sema_wakeup_reason_success;
        }
        else if (try_wait)
        {
            sync_.acquire(c.threadx_);
            return sema_wakeup_reason_failed;
        }
        else
        {
            unpark_reason reason = ws_.park_current(c, is_timed, false, false, info);
            sync_.acquire(c.threadx_);
            if (reason == unpark_reason_normal)
                return sema_wakeup_reason_success;
            else if (reason == unpark_reason_timeout)
                return sema_wakeup_reason_timeout;
            RL_VERIFY(false);
            return sema_wakeup_reason_failed;
        }
    }

    virtual bool signal(debug_info_param info)
    {
        RL_ASSERT_IMPL(false, test_result_thread_signal, "trying to signal a thread", info);
        return false;
    }

    virtual bool is_signaled(debug_info_param info)
    {
        (void)info;
        return finished_;
    }

    virtual void memory_acquire(debug_info_param info)
    {
        (void)info;
        sync_.acquire(ctx().threadx_);
    }

    virtual void* prepare_wait(debug_info_param info)
    {
        (void)info;
        return &ws_;
    }
};


}

#endif