PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/src/rt/rust_scheduler.h

http://github.com/jruderman/rust
C Header | 77 lines | 46 code | 16 blank | 15 comment | 0 complexity | e9026b5565ff0424db511b4e5596b6b9 MD5 | raw file
 1/**
 2   The rust scheduler. Schedulers may be added to the kernel
 3   dynamically and they run until there are no more tasks to
 4   schedule. Most of the scheduler work is carried out in worker
 5   threads by rust_sched_loop.
 6 */
 7
 8#ifndef RUST_SCHEDULER_H
 9#define RUST_SCHEDULER_H
10
11#include "rust_globals.h"
12#include "util/array_list.h"
13#include "rust_kernel.h"
14#include "rust_refcount.h"
15
16class rust_sched_launcher;
17class rust_sched_launcher_factory;
18
19class rust_scheduler : public kernel_owned<rust_scheduler> {
20    RUST_ATOMIC_REFCOUNT();
21    // FIXME (#2693): Make these private
22public:
23    rust_kernel *kernel;
24private:
25    // Protects live_threads, live_tasks, cur_thread, may_exit
26    lock_and_signal lock;
27    // When this hits zero we'll tell the kernel to release us
28    uintptr_t live_threads;
29    // When this hits zero we'll tell the threads to exit
30    uintptr_t live_tasks;
31    size_t cur_thread;
32    bool may_exit;
33
34    array_list<rust_sched_launcher *> threads;
35    const size_t num_threads;
36
37    rust_sched_id id;
38
39    void create_task_threads(rust_sched_launcher_factory *launchfac,
40                             bool killed);
41    void destroy_task_threads();
42
43    rust_sched_launcher *
44    create_task_thread(rust_sched_launcher_factory *launchfac, int id,
45                       bool killed);
46    void destroy_task_thread(rust_sched_launcher *thread);
47
48    void exit();
49
50    // Called when refcount reaches zero
51    void delete_this();
52
53public:
54    rust_scheduler(rust_kernel *kernel, size_t num_threads,
55                   rust_sched_id id, bool allow_exit, bool killed,
56                   rust_sched_launcher_factory *launchfac);
57
58    void start_task_threads();
59    void join_task_threads();
60    void kill_all_tasks();
61    rust_task* create_task(rust_task *spawner, const char *name);
62
63    void release_task();
64
65    size_t number_of_threads();
66    // Called by each thread when it terminates. When all threads
67    // terminate the scheduler does as well.
68    void release_task_thread();
69
70    rust_sched_id get_id() { return id; }
71    // Tells the scheduler that as soon as it runs out of tasks
72    // to run it should exit
73    void allow_exit();
74    void disallow_exit();
75};
76
77#endif /* RUST_SCHEDULER_H */