]> git.proxmox.com Git - rustc.git/blob - src/rt/rust_scheduler.h
Imported Upstream version 0.6
[rustc.git] / src / rt / rust_scheduler.h
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /**
12 The rust scheduler. Schedulers may be added to the kernel
13 dynamically and they run until there are no more tasks to
14 schedule. Most of the scheduler work is carried out in worker
15 threads by rust_sched_loop.
16 */
17
18 #ifndef RUST_SCHEDULER_H
19 #define RUST_SCHEDULER_H
20
21 #include "rust_globals.h"
22 #include "util/array_list.h"
23 #include "rust_kernel.h"
24 #include "rust_refcount.h"
25
26 class rust_sched_launcher;
27 class rust_sched_launcher_factory;
28
29 class rust_scheduler : public kernel_owned<rust_scheduler> {
30 RUST_ATOMIC_REFCOUNT();
31 // FIXME (#2693): Make these private
32 public:
33 rust_kernel *kernel;
34 private:
35 // Protects live_threads, live_tasks, cur_thread, may_exit
36 lock_and_signal lock;
37 // When this hits zero we'll tell the kernel to release us
38 uintptr_t live_threads;
39 // When this hits zero we'll tell the threads to exit
40 uintptr_t live_tasks;
41 size_t cur_thread;
42 bool may_exit;
43 bool killed;
44
45 rust_sched_launcher_factory *launchfac;
46 array_list<rust_sched_launcher *> threads;
47 const size_t max_num_threads;
48
49 rust_sched_id id;
50
51 void destroy_task_threads();
52
53 rust_sched_launcher *create_task_thread(int id);
54 void destroy_task_thread(rust_sched_launcher *thread);
55
56 void exit();
57
58 // Called when refcount reaches zero
59 void delete_this();
60
61 private:
62 // private and undefined to disable copying
63 rust_scheduler(const rust_scheduler& rhs);
64 rust_scheduler& operator=(const rust_scheduler& rhs);
65
66 public:
67 rust_scheduler(rust_kernel *kernel, size_t max_num_threads,
68 rust_sched_id id, bool allow_exit, bool killed,
69 rust_sched_launcher_factory *launchfac);
70
71 void start_task_threads();
72 void join_task_threads();
73 void kill_all_tasks();
74 rust_task* create_task(rust_task *spawner, const char *name);
75
76 void release_task();
77
78 size_t max_number_of_threads();
79 size_t number_of_threads();
80 // Called by each thread when it terminates. When all threads
81 // terminate the scheduler does as well.
82 void release_task_thread();
83
84 rust_sched_id get_id() { return id; }
85 // Tells the scheduler that as soon as it runs out of tasks
86 // to run it should exit
87 void allow_exit();
88 void disallow_exit();
89 };
90
91 #endif /* RUST_SCHEDULER_H */