source: src/libcfa/concurrency/monitor.c@ 0db6fc0

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 0db6fc0 was be3d020, checked in by Thierry Delisle <tdelisle@…>, 8 years ago
  • signal/signal_block now returns true if the queue was not empty
  • wait now supports passing in a user define value
  • Property mode set to 100644
File size: 13.8 KB
Line 
1// -*- Mode: CFA -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// monitor_desc.c --
9//
10// Author : Thierry Delisle
11// Created On : Thd Feb 23 12:27:26 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count : 0
15//
16
17#include "monitor"
18
19#include <stdlib>
20
21#include "kernel_private.h"
22#include "libhdr.h"
23
24//-----------------------------------------------------------------------------
25// Forward declarations
26static inline void set_owner( monitor_desc * this, thread_desc * owner );
27static inline thread_desc * next_thread( monitor_desc * this );
28
29static inline void lock_all( spinlock ** locks, unsigned short count );
30static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count );
31static inline void unlock_all( spinlock ** locks, unsigned short count );
32static inline void unlock_all( monitor_desc ** locks, unsigned short count );
33
34static inline void save_recursion ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count );
35static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count );
36
37static inline thread_desc * check_condition( __condition_criterion_t * );
38static inline void brand_condition( condition * );
39static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val );
40
41//-----------------------------------------------------------------------------
42// Enter/Leave routines
43
44
45extern "C" {
46 void __enter_monitor_desc(monitor_desc * this) {
47 lock( &this->lock );
48 thread_desc * thrd = this_thread();
49
50 LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
51
52 if( !this->owner ) {
53 //No one has the monitor, just take it
54 set_owner( this, thrd );
55 }
56 else if( this->owner == thrd) {
57 //We already have the monitor, just not how many times we took it
58 assert( this->recursion > 0 );
59 this->recursion += 1;
60 }
61 else {
62 //Some one else has the monitor, wait in line for it
63 append( &this->entry_queue, thrd );
64 LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
65 ScheduleInternal( &this->lock );
66
67 //ScheduleInternal will unlock spinlock, no need to unlock ourselves
68 return;
69 }
70
71 unlock( &this->lock );
72 return;
73 }
74
75 // leave pseudo code :
76 // TODO
77 void __leave_monitor_desc(monitor_desc * this) {
78 lock( &this->lock );
79
80 thread_desc * thrd = this_thread();
81
82 LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
83 assertf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i)", thrd, this->owner, this->recursion );
84
85 //Leaving a recursion level, decrement the counter
86 this->recursion -= 1;
87
88 //If we haven't left the last level of recursion
89 //it means we don't need to do anything
90 if( this->recursion != 0) {
91 unlock( &this->lock );
92 return;
93 }
94
95 thread_desc * new_owner = next_thread( this );
96
97 //We can now let other threads in safely
98 unlock( &this->lock );
99
100 LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
101
102 //We need to wake-up the thread
103 ScheduleThread( new_owner );
104 }
105}
106
107static inline void enter(monitor_desc ** monitors, int count) {
108 for(int i = 0; i < count; i++) {
109 __enter_monitor_desc( monitors[i] );
110 }
111}
112
113static inline void leave(monitor_desc ** monitors, int count) {
114 for(int i = count - 1; i >= 0; i--) {
115 __leave_monitor_desc( monitors[i] );
116 }
117}
118
119void ?{}( monitor_guard_t * this, monitor_desc ** m, int count ) {
120 this->m = m;
121 this->count = count;
122 qsort(this->m, count);
123 enter( this->m, this->count );
124
125 this->prev_mntrs = this_thread()->current_monitors;
126 this->prev_count = this_thread()->current_monitor_count;
127
128 this_thread()->current_monitors = m;
129 this_thread()->current_monitor_count = count;
130}
131
132void ^?{}( monitor_guard_t * this ) {
133 leave( this->m, this->count );
134
135 this_thread()->current_monitors = this->prev_mntrs;
136 this_thread()->current_monitor_count = this->prev_count;
137}
138
139void ?{}(__condition_node_t * this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ) {
140 this->waiting_thread = waiting_thread;
141 this->count = count;
142 this->next = NULL;
143 this->user_info = user_info;
144}
145
146void ?{}(__condition_criterion_t * this ) {
147 this->ready = false;
148 this->target = NULL;
149 this->owner = NULL;
150 this->next = NULL;
151}
152
153void ?{}(__condition_criterion_t * this, monitor_desc * target, __condition_node_t * owner ) {
154 this->ready = false;
155 this->target = target;
156 this->owner = owner;
157 this->next = NULL;
158}
159
160//-----------------------------------------------------------------------------
161// Internal scheduling
162void wait( condition * this, uintptr_t user_info = 0 ) {
163 LIB_DEBUG_PRINT_SAFE("Waiting\n");
164
165 brand_condition( this );
166
167 //Check that everything is as expected
168 assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
169 assertf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
170 assertf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
171
172 unsigned short count = this->monitor_count;
173 unsigned int recursions[ count ]; //Save the current recursion levels to restore them later
174 spinlock * locks [ count ]; //We need to pass-in an array of locks to ScheduleInternal
175
176 LIB_DEBUG_PRINT_SAFE("count %i\n", count);
177
178 __condition_node_t waiter = { this_thread(), count, user_info };
179
180 __condition_criterion_t criteria[count];
181 for(int i = 0; i < count; i++) {
182 (&criteria[i]){ this->monitors[i], &waiter };
183 LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
184 }
185
186 waiter.criteria = criteria;
187 append( &this->blocked, &waiter );
188
189 lock_all( this->monitors, locks, count );
190 save_recursion( this->monitors, recursions, count );
191 //DON'T unlock, ask the kernel to do it
192
193 //Find the next thread(s) to run
194 unsigned short thread_count = 0;
195 thread_desc * threads[ count ];
196 for(int i = 0; i < count; i++) {
197 threads[i] = 0;
198 }
199
200 for( int i = 0; i < count; i++) {
201 thread_desc * new_owner = next_thread( this->monitors[i] );
202 thread_count = insert_unique( threads, thread_count, new_owner );
203 }
204
205 LIB_DEBUG_PRINT_SAFE("Will unblock: ");
206 for(int i = 0; i < thread_count; i++) {
207 LIB_DEBUG_PRINT_SAFE("%p ", threads[i]);
208 }
209 LIB_DEBUG_PRINT_SAFE("\n");
210
211 // Everything is ready to go to sleep
212 ScheduleInternal( locks, count, threads, thread_count );
213
214 //WE WOKE UP
215
216
217 //We are back, restore the owners and recursions
218 lock_all( locks, count );
219 restore_recursion( this->monitors, recursions, count );
220 unlock_all( locks, count );
221}
222
223bool signal( condition * this ) {
224 if( is_empty( this ) ) {
225 LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
226 return false;
227 }
228
229 //Check that everything is as expected
230 assert( this->monitors );
231 assert( this->monitor_count != 0 );
232
233 unsigned short count = this->monitor_count;
234
235 //Some more checking in debug
236 LIB_DEBUG_DO(
237 thread_desc * this_thrd = this_thread();
238 if ( this->monitor_count != this_thrd->current_monitor_count ) {
239 abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->current_monitor_count );
240 } // if
241
242 for(int i = 0; i < this->monitor_count; i++) {
243 if ( this->monitors[i] != this_thrd->current_monitors[i] ) {
244 abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->current_monitors[i] );
245 } // if
246 }
247 );
248
249 //Lock all the monitors
250 lock_all( this->monitors, NULL, count );
251 LIB_DEBUG_PRINT_SAFE("Signalling");
252
253 //Pop the head of the waiting queue
254 __condition_node_t * node = pop_head( &this->blocked );
255
256 //Add the thread to the proper AS stack
257 for(int i = 0; i < count; i++) {
258 __condition_criterion_t * crit = &node->criteria[i];
259 LIB_DEBUG_PRINT_SAFE(" %p", crit->target);
260 assert( !crit->ready );
261 push( &crit->target->signal_stack, crit );
262 }
263
264 LIB_DEBUG_PRINT_SAFE("\n");
265
266 //Release
267 unlock_all( this->monitors, count );
268
269 return true;
270}
271
272bool signal_block( condition * this ) {
273 if( !this->blocked.head ) {
274 LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
275 return false;
276 }
277
278 //Check that everything is as expected
279 assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
280 assertf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
281
282 unsigned short count = this->monitor_count;
283 unsigned int recursions[ count ]; //Save the current recursion levels to restore them later
284 spinlock * locks [ count ]; //We need to pass-in an array of locks to ScheduleInternal
285
286 lock_all( this->monitors, locks, count );
287
288 //create creteria
289 __condition_node_t waiter = { this_thread(), count, 0 };
290
291 __condition_criterion_t criteria[count];
292 for(int i = 0; i < count; i++) {
293 (&criteria[i]){ this->monitors[i], &waiter };
294 LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
295 push( &criteria[i].target->signal_stack, &criteria[i] );
296 }
297
298 waiter.criteria = criteria;
299
300 //save contexts
301 save_recursion( this->monitors, recursions, count );
302
303 //Find the thread to run
304 thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
305 for(int i = 0; i < count; i++) {
306 set_owner( this->monitors[i], signallee );
307 }
308
309 LIB_DEBUG_PRINT_SAFE( "Waiting on signal block\n" );
310
311 //Everything is ready to go to sleep
312 ScheduleInternal( locks, count, &signallee, 1 );
313
314 LIB_DEBUG_PRINT_SAFE( "Back from signal block\n" );
315
316 //We are back, restore the owners and recursions
317 lock_all( locks, count );
318 restore_recursion( this->monitors, recursions, count );
319 unlock_all( locks, count );
320
321 return true;
322}
323
324uintptr_t front( condition * this ) {
325 LIB_DEBUG_DO(
326 if( is_empty(this) ) {
327 abortf( "Attempt to access user data on an empty condition.\n"
328 "Possible cause is not checking if the condition is empty before reading stored data." );
329 }
330 );
331 return this->blocked.head->user_info;
332}
333
334//-----------------------------------------------------------------------------
335// Utilities
336
337static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
338 //Pass the monitor appropriately
339 this->owner = owner;
340
341 //We are passing the monitor to someone else, which means recursion level is not 0
342 this->recursion = owner ? 1 : 0;
343}
344
345static inline thread_desc * next_thread( monitor_desc * this ) {
346 //Check the signaller stack
347 __condition_criterion_t * urgent = pop( &this->signal_stack );
348 if( urgent ) {
349 //The signaller stack is not empty,
350 //regardless of if we are ready to baton pass,
351 //we need to set the monitor as in use
352 set_owner( this, urgent->owner->waiting_thread );
353
354 return check_condition( urgent );
355 }
356
357 // No signaller thread
358 // Get the next thread in the entry_queue
359 thread_desc * new_owner = pop_head( &this->entry_queue );
360 set_owner( this, new_owner );
361
362 return new_owner;
363}
364
365static inline void lock_all( spinlock ** locks, unsigned short count ) {
366 for( int i = 0; i < count; i++ ) {
367 lock( locks[i] );
368 }
369}
370
371static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
372 for( int i = 0; i < count; i++ ) {
373 spinlock * l = &source[i]->lock;
374 lock( l );
375 if(locks) locks[i] = l;
376 }
377}
378
379static inline void unlock_all( spinlock ** locks, unsigned short count ) {
380 for( int i = 0; i < count; i++ ) {
381 unlock( locks[i] );
382 }
383}
384
385static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
386 for( int i = 0; i < count; i++ ) {
387 unlock( &locks[i]->lock );
388 }
389}
390
391
392static inline void save_recursion ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count ) {
393 for( int i = 0; i < count; i++ ) {
394 recursions[i] = ctx[i]->recursion;
395 }
396}
397
398static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count ) {
399 for( int i = 0; i < count; i++ ) {
400 ctx[i]->recursion = recursions[i];
401 }
402}
403
404// Function has 2 different behavior
405// 1 - Marks a monitors as being ready to run
406// 2 - Checks if all the monitors are ready to run
407// if so return the thread to run
408static inline thread_desc * check_condition( __condition_criterion_t * target ) {
409 __condition_node_t * node = target->owner;
410 unsigned short count = node->count;
411 __condition_criterion_t * criteria = node->criteria;
412
413 bool ready2run = true;
414
415 for( int i = 0; i < count; i++ ) {
416
417 LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
418 if( &criteria[i] == target ) {
419 criteria[i].ready = true;
420 LIB_DEBUG_PRINT_SAFE( "True\n" );
421 }
422
423 ready2run = criteria[i].ready && ready2run;
424 }
425
426 LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
427 return ready2run ? node->waiting_thread : NULL;
428}
429
430static inline void brand_condition( condition * this ) {
431 thread_desc * thrd = this_thread();
432 if( !this->monitors ) {
433 LIB_DEBUG_PRINT_SAFE("Branding\n");
434 assertf( thrd->current_monitors != NULL, "No current monitor to brand condition", thrd->current_monitors );
435 this->monitor_count = thrd->current_monitor_count;
436
437 this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
438 for( int i = 0; i < this->monitor_count; i++ ) {
439 this->monitors[i] = thrd->current_monitors[i];
440 }
441 }
442}
443
444static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) {
445 if( !val ) return end;
446
447 for(int i = 0; i <= end; i++) {
448 if( thrds[i] == val ) return end;
449 }
450
451 thrds[end] = val;
452 return end + 1;
453}
454
455void ?{}( __condition_blocked_queue_t * this ) {
456 this->head = NULL;
457 this->tail = &this->head;
458}
459
460void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
461 assert(this->tail != NULL);
462 *this->tail = c;
463 this->tail = &c->next;
464}
465
466__condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
467 __condition_node_t * head = this->head;
468 if( head ) {
469 this->head = head->next;
470 if( !head->next ) {
471 this->tail = &this->head;
472 }
473 head->next = NULL;
474 }
475 return head;
476}
Note: See TracBrowser for help on using the repository browser.