medfall

A super great game engine
Log | Files | Refs

scheduler.hpp (9106B)


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

#include "base.hpp"
#include "context_base.hpp"


namespace rl
{


enum thread_state_e
{
    thread_state_running,
    thread_state_blocked,
    thread_state_finished,
};

enum thread_finish_result
{
    thread_finish_result_normal,
    thread_finish_result_last,
    thread_finish_result_deadlock,
};



struct scheduler_thread_info
{
    thread_id_t             index_;
    unsigned                block_count_;
    thread_state_e          state_;

    void reset(test_params& /*params*/)
    {
        block_count_ = 0;
        state_ = thread_state_running;
    }
};




template<typename derived_t, typename thread_info_type, thread_id_t thread_count>
class scheduler : nocopy<>
{
public:
    typedef thread_info_type                    thread_info_t;

    struct shared_context_t
    {
        typedef typename derived_t::task_t      task_t;
        //CRITICAL_SECTION                        guard_;
        queue<task_t>                           queue_;
    };

    scheduler(test_params& params, shared_context_t& ctx, thread_id_t dynamic_thread_count)
        : params_(params)
        , ctx_(ctx)
        , total_dynamic_threads_(dynamic_thread_count)
        , iter_()
        , thread_()
    {
        for (thread_id_t i = 0; i != thread_count; ++i)
        {
            threads_[i].index_ = i;
        }
    }

    thread_id_t iteration_begin(iteration_t iter)
    {
        iter_ = iter;
        running_threads_count = thread_count;
        finished_thread_count_ = 0;
        timed_thread_count_ = 0;
        spurious_thread_count_ = 0;
        dynamic_thread_count_ = 0;

        for (thread_id_t i = 0; i != thread_count; ++i)
        {
            running_threads.push_back(i);
            threads_[i].reset(params_);
        }

        for (thread_id_t i = thread_count - total_dynamic_threads_; i != thread_count; ++i)
        {
            dynamic_threads_[dynamic_thread_count_++] = &threads_[i];
            block_thread(i, false);
        }

        thread_id_t const th = self().iteration_begin_impl();
    
        thread_ = &threads_[th];

        return th;
    }

    bool iteration_end()
    {
        bool const finish = self().iteration_end_impl();

        thread_ = 0;

        return finish;
    }

    thread_id_t schedule(unpark_reason& reason, unsigned yield)
    {
        thread_id_t const th = self().schedule_impl(reason, yield);

        RL_VERIFY(threads_[th].state_ == thread_state_running);
        thread_ = &threads_[th];

        return th;
    }

    RL_INLINE
    unsigned rand(unsigned limit, sched_type t)
    {
        RL_VERIFY(limit);
        return self().rand_impl(limit, t);
    }

    iteration_t iteration_count()
    {
        return self().iteration_count_impl();
    }

    bool park_current_thread(bool is_timed, bool allow_spurious_wakeup)
    {
        if (is_timed)
        {
            timed_threads_[timed_thread_count_++] = thread_;
            RL_VERIFY(timed_thread_count_ <= thread_count);
        }

        if (allow_spurious_wakeup)
        {
            spurious_threads_[spurious_thread_count_++] = thread_;
            RL_VERIFY(spurious_thread_count_ <= thread_count);
        }

        block_thread(thread_->index_, true);

        return is_deadlock() ? false : true;
    }

    void unpark_thread(thread_id_t th, bool do_switch = false)
    {
        (void)do_switch;
        unblock_thread(th);

        thread_info_t& t = threads_[th];

        //!!! store flag as to whether thread is spurious blocked in thread object
        // (to eliminate iteration over all threads)
        for (thread_id_t i = 0; i != spurious_thread_count_; ++i)
        {
            if (spurious_threads_[i] == &t)
            {
                for (thread_id_t j = i + 1; j != spurious_thread_count_; ++j)
                    spurious_threads_[j - 1] = spurious_threads_[j];
                spurious_thread_count_ -= 1;
                break;
            }
        }

        //!!! store flag as to whether thread is spurious blocked in thread object
        for (thread_id_t i = 0; i != timed_thread_count_; ++i)
        {
            if (timed_threads_[i] == &t)
            {
                for (thread_id_t j = i + 1; j != timed_thread_count_; ++j)
                    timed_threads_[j - 1] = timed_threads_[j];
                timed_thread_count_ -= 1;
                break;
            }
        }
    }

    thread_finish_result thread_finished()
    {
        RL_VERIFY(thread_->state_ == thread_state_running);
        block_thread(thread_->index_, false);
        thread_->state_ = thread_state_finished;
        finished_thread_count_ += 1;
        self().thread_finished_impl();
retry:
        if (finished_thread_count_ == thread_count)
        {
            return thread_finish_result_last;
        }
        else if (is_deadlock())
        {
            if (dynamic_thread_count_)
            {
                while (dynamic_thread_count_)
                {
                    thread_info_t* th = dynamic_threads_[--dynamic_thread_count_];
                    unblock_thread(th->index_);
                }
                goto retry;
            }
            return thread_finish_result_deadlock;
        }
        else
        {
            return thread_finish_result_normal;
        }
    }

    thread_id_t create_thread()
    {
        RL_VERIFY(dynamic_thread_count_);
        thread_info_t* th = dynamic_threads_[--dynamic_thread_count_];
        unblock_thread(th->index_);
        return th->index_;
    }

    void get_state(std::ostream& ss)
    {
        self().get_state_impl(ss);
    }

    void set_state(std::istream& ss)
    {
        self().set_state_impl(ss);
    }

protected:
    test_params&                    params_;
    shared_context_t&               ctx_;
    thread_id_t const               total_dynamic_threads_;
    iteration_t                     iter_;

    aligned<thread_info_t>          threads_ [thread_count];
    thread_info_t*                  thread_;

    vector<thread_id_t>::type       running_threads;
    thread_id_t                     running_threads_count;
    thread_id_t                     finished_thread_count_;

    //!!! doesn't timed/spurious waits must belong to full scheduler?
    // hyphotesis: random scheduler can ignore timed/spurious waits
    // (however must detect deadlock with spurious threads)
    thread_info_t*                  timed_threads_ [thread_count];
    thread_id_t                     timed_thread_count_;

    thread_info_t*                  spurious_threads_ [thread_count];
    thread_id_t                     spurious_thread_count_;

    thread_info_t*                  dynamic_threads_ [thread_count];
    thread_id_t                     dynamic_thread_count_;

    void block_thread(thread_id_t th, bool yield)
    {
        RL_VERIFY(th < thread_count);
        thread_info_t& t = threads_[th];
        RL_VERIFY(t.state_ != thread_state_finished);
        if (t.block_count_++)
            return;

        for (thread_id_t i = 0; i != running_threads_count; ++i)
        {
            if (running_threads[i] == th)
            {
                running_threads.erase(running_threads.begin() + i);
                running_threads_count -= 1;
                t.state_ = thread_state_blocked;
                self().on_thread_block(th, yield);
                return;
            }
        }
        RL_VERIFY(false);
    }

    bool unblock_thread(thread_id_t th)
    {
        RL_VERIFY(th < thread_count);
        thread_info_t& t = threads_[th];
        RL_VERIFY(t.state_ == thread_state_blocked);
        if (--t.block_count_)
            return false;

        running_threads.push_back(th);
        running_threads_count += 1;
        t.state_ = thread_state_running;
        return true;
    }

private:
    derived_t& self()
    {
        return *static_cast<derived_t*>(this);
    }

    bool is_deadlock()
    {
        if ((0 == running_threads_count) && (0 == timed_thread_count_))
        {
            self().purge_blocked_threads();
            if ((0 == running_threads_count) && (0 == timed_thread_count_))
                return true;
        }
        return false;
    }

    void thread_finished_impl()
    {
    }

    void purge_blocked_threads()
    {
    }
};


}

#endif