vxx2 / vlib / x / executor / owner.v
44 lines · 39 sloc · 1023 bytes · e80386b6af8a071a2e13fd654ebcdb2105fb9202
Raw
1module executor
2
3import sync
4
5// is_owner_thread reports whether the caller is currently inside an active owner pump.
6pub fn (e &Executor) is_owner_thread() bool {
7 e.mutex.lock()
8 is_owner := e.owner_active && e.owner_thread_id == sync.thread_id()
9 e.mutex.unlock()
10 return is_owner
11}
12
13// assert_owner_thread returns an error when called outside the active owner pump.
14pub fn (e &Executor) assert_owner_thread() ! {
15 if !e.is_owner_thread() {
16 return error(err_not_owner_thread)
17 }
18}
19
20fn (mut e Executor) begin_owner_pump(long_running bool) ! {
21 e.mutex.lock()
22 if e.stopped && long_running {
23 e.mutex.unlock()
24 return error(err_executor_stopped)
25 }
26 if e.pumping {
27 e.mutex.unlock()
28 return error(err_owner_pump_running)
29 }
30 e.pumping = true
31 e.run_active = long_running
32 e.owner_active = true
33 e.owner_thread_id = sync.thread_id()
34 e.mutex.unlock()
35}
36
37fn (mut e Executor) end_owner_pump() {
38 e.mutex.lock()
39 e.pumping = false
40 e.run_active = false
41 e.owner_active = false
42 e.owner_thread_id = 0
43 e.mutex.unlock()
44}
45