1 | |
---|
2 | // -*- Mode: CFA -*- |
---|
3 | // |
---|
4 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
5 | // |
---|
6 | // The contents of this file are covered under the licence agreement in the |
---|
7 | // file "LICENCE" distributed with Cforall. |
---|
8 | // |
---|
9 | // mutex.c -- |
---|
10 | // |
---|
11 | // Author : Thierry Delisle |
---|
12 | // Created On : Fri May 25 01:37:11 2018 |
---|
13 | // Last Modified By : Peter A. Buhr |
---|
14 | // Last Modified On : Wed Dec 4 09:16:39 2019 |
---|
15 | // Update Count : 1 |
---|
16 | // |
---|
17 | |
---|
18 | #define __cforall_thread__ |
---|
19 | #define _GNU_SOURCE |
---|
20 | |
---|
21 | #include "mutex.hfa" |
---|
22 | |
---|
23 | #include "kernel/private.hfa" |
---|
24 | |
---|
25 | //----------------------------------------------------------------------------- |
---|
26 | // Locks |
---|
27 | |
---|
28 | // Exclusive lock - non-recursive |
---|
29 | // --- |
---|
30 | void ?{}(mutex_lock & this) { |
---|
31 | this.lock{}; |
---|
32 | this.blocked_threads{}; |
---|
33 | this.is_locked = false; |
---|
34 | } |
---|
35 | |
---|
36 | void ^?{}(mutex_lock & this) { |
---|
37 | // default |
---|
38 | } |
---|
39 | |
---|
40 | void lock(mutex_lock & this) with(this) { |
---|
41 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
42 | if( is_locked ) { |
---|
43 | append( blocked_threads, active_thread() ); |
---|
44 | unlock( lock ); |
---|
45 | park(); |
---|
46 | } |
---|
47 | else { |
---|
48 | is_locked = true; |
---|
49 | unlock( lock ); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | bool try_lock(mutex_lock & this) with(this) { |
---|
54 | bool ret = false; |
---|
55 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
56 | if( !is_locked ) { |
---|
57 | ret = true; |
---|
58 | is_locked = true; |
---|
59 | } |
---|
60 | unlock( lock ); |
---|
61 | return ret; |
---|
62 | } |
---|
63 | |
---|
64 | void unlock(mutex_lock & this) { |
---|
65 | lock( this.lock __cfaabi_dbg_ctx2 ); |
---|
66 | this.is_locked = (this.blocked_threads != 0); |
---|
67 | unpark( |
---|
68 | pop_head( this.blocked_threads ) |
---|
69 | ); |
---|
70 | unlock( this.lock ); |
---|
71 | } |
---|
72 | |
---|
73 | // Exclusive lock - non-recursive |
---|
74 | // --- |
---|
75 | void ?{}(recursive_mutex_lock & this) { |
---|
76 | this.lock{}; |
---|
77 | this.blocked_threads{}; |
---|
78 | this.owner = 0p; |
---|
79 | this.recursion_count = 0; |
---|
80 | } |
---|
81 | |
---|
82 | void ^?{}(recursive_mutex_lock & this) { |
---|
83 | // default |
---|
84 | } |
---|
85 | |
---|
86 | void lock(recursive_mutex_lock & this) with(this) { |
---|
87 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
88 | if( owner == 0p ) { |
---|
89 | owner = active_thread(); |
---|
90 | recursion_count = 1; |
---|
91 | unlock( lock ); |
---|
92 | } |
---|
93 | else if( owner == active_thread() ) { |
---|
94 | recursion_count++; |
---|
95 | unlock( lock ); |
---|
96 | } |
---|
97 | else { |
---|
98 | append( blocked_threads, active_thread() ); |
---|
99 | unlock( lock ); |
---|
100 | park(); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | bool try_lock(recursive_mutex_lock & this) with(this) { |
---|
105 | bool ret = false; |
---|
106 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
107 | if( owner == 0p ) { |
---|
108 | owner = active_thread(); |
---|
109 | recursion_count = 1; |
---|
110 | ret = true; |
---|
111 | } |
---|
112 | else if( owner == active_thread() ) { |
---|
113 | recursion_count++; |
---|
114 | ret = true; |
---|
115 | } |
---|
116 | unlock( lock ); |
---|
117 | return ret; |
---|
118 | } |
---|
119 | |
---|
120 | void unlock(recursive_mutex_lock & this) with(this) { |
---|
121 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
122 | recursion_count--; |
---|
123 | if( recursion_count == 0 ) { |
---|
124 | thread$ * thrd = pop_head( blocked_threads ); |
---|
125 | owner = thrd; |
---|
126 | recursion_count = (thrd ? 1 : 0); |
---|
127 | unpark( thrd ); |
---|
128 | } |
---|
129 | unlock( lock ); |
---|
130 | } |
---|
131 | |
---|
132 | //----------------------------------------------------------------------------- |
---|
133 | // Conditions |
---|
134 | void ?{}(condition_variable & this) { |
---|
135 | this.blocked_threads{}; |
---|
136 | } |
---|
137 | |
---|
138 | void ^?{}(condition_variable & this) { |
---|
139 | // default |
---|
140 | } |
---|
141 | |
---|
142 | void notify_one(condition_variable & this) with(this) { |
---|
143 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
144 | unpark( |
---|
145 | pop_head( this.blocked_threads ) |
---|
146 | ); |
---|
147 | unlock( lock ); |
---|
148 | } |
---|
149 | |
---|
150 | void notify_all(condition_variable & this) with(this) { |
---|
151 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
152 | while(this.blocked_threads) { |
---|
153 | unpark( |
---|
154 | pop_head( this.blocked_threads ) |
---|
155 | ); |
---|
156 | } |
---|
157 | unlock( lock ); |
---|
158 | } |
---|
159 | |
---|
160 | void wait(condition_variable & this) { |
---|
161 | lock( this.lock __cfaabi_dbg_ctx2 ); |
---|
162 | append( this.blocked_threads, active_thread() ); |
---|
163 | unlock( this.lock ); |
---|
164 | park(); |
---|
165 | } |
---|
166 | |
---|
167 | forall(L & | is_lock(L)) |
---|
168 | void wait(condition_variable & this, L & l) { |
---|
169 | lock( this.lock __cfaabi_dbg_ctx2 ); |
---|
170 | append( this.blocked_threads, active_thread() ); |
---|
171 | unlock(l); |
---|
172 | unlock(this.lock); |
---|
173 | park(); |
---|
174 | lock(l); |
---|
175 | } |
---|
176 | |
---|
177 | //----------------------------------------------------------------------------- |
---|
178 | // Scopes |
---|
179 | forall(L & | is_lock(L)) |
---|
180 | void lock_all ( L * locks[], size_t count) { |
---|
181 | // Sort locks based on addresses |
---|
182 | __libcfa_small_sort(locks, count); |
---|
183 | |
---|
184 | // Lock all |
---|
185 | for(size_t i = 0; i < count; i++) { |
---|
186 | L * l = locks[i]; |
---|
187 | lock( *l ); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | forall(L & | is_lock(L)) |
---|
192 | void unlock_all( L * locks[], size_t count) { |
---|
193 | // Lock all |
---|
194 | for(size_t i = 0; i < count; i++) { |
---|
195 | L * l = locks[i]; |
---|
196 | unlock( *l ); |
---|
197 | } |
---|
198 | } |
---|