source: src/libcfa/concurrency/monitor.c @ bff227f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since bff227f was 53a8e68, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Update several more library files for references

  • Property mode set to 100644
File size: 15.3 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 "libhdr.h"
22#include "kernel_private.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_yield( &this->lock DEBUG_CTX2 );
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                        verify( 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                        BlockInternal( &this->lock );
66
67                        //BlockInternal 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_yield( &this->lock DEBUG_CTX2 );
79
80                // LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i). ", this_thread, this, this->owner, this->recursion);
81                verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread, this->owner, this->recursion );
82
83                //Leaving a recursion level, decrement the counter
84                this->recursion -= 1;
85
86                //If we haven't left the last level of recursion
87                //it means we don't need to do anything
88                if( this->recursion != 0) {
89                        unlock( &this->lock );
90                        return;
91                }
92
93                thread_desc * new_owner = next_thread( this );
94
95                //We can now let other threads in safely
96                unlock( &this->lock );
97
98                // LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
99
100                //We need to wake-up the thread
101                WakeThread( new_owner );
102        }
103
104        void __leave_thread_monitor( thread_desc * thrd ) {
105                monitor_desc * this = &thrd->mon;
106                lock_yield( &this->lock DEBUG_CTX2 );
107
108                disable_interrupts();
109
110                thrd->cor.state = Halted;
111
112                verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i)", thrd, this->owner, this->recursion );
113
114                //Leaving a recursion level, decrement the counter
115                this->recursion -= 1;
116
117                //If we haven't left the last level of recursion
118                //it means we don't need to do anything
119                if( this->recursion != 0) {
120                        unlock( &this->lock );
121                        return;
122                }
123
124                thread_desc * new_owner = next_thread( this );
125
126                LeaveThread( &this->lock, new_owner );
127        }
128}
129
130static inline void enter(monitor_desc ** monitors, int count) {
131        for(int i = 0; i < count; i++) {
132                __enter_monitor_desc( monitors[i] );
133        }
134}
135
136static inline void leave(monitor_desc ** monitors, int count) {
137        for(int i = count - 1; i >= 0; i--) {
138                __leave_monitor_desc( monitors[i] );
139        }
140}
141
142void ?{}( monitor_guard_t & this, monitor_desc ** m, int count ) {
143        this.m = m;
144        this.count = count;
145        qsort(this.m, count);
146        enter( this.m, this.count );
147
148        this.prev_mntrs = this_thread->current_monitors;
149        this.prev_count = this_thread->current_monitor_count;
150
151        this_thread->current_monitors      = m;
152        this_thread->current_monitor_count = count;
153}
154
155void ^?{}( monitor_guard_t & this ) {
156        leave( this.m, this.count );
157
158        this_thread->current_monitors      = this.prev_mntrs;
159        this_thread->current_monitor_count = this.prev_count;
160}
161
162void ?{}(__condition_node_t & this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ) {
163        this.waiting_thread = waiting_thread;
164        this.count = count;
165        this.next = NULL;
166        this.user_info = user_info;
167}
168
169void ?{}(__condition_criterion_t & this ) {
170        this.ready  = false;
171        this.target = NULL;
172        this.owner  = NULL;
173        this.next   = NULL;
174}
175
176void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t * owner ) {
177        this.ready  = false;
178        this.target = target;
179        this.owner  = owner;
180        this.next   = NULL;
181}
182
183//-----------------------------------------------------------------------------
184// Internal scheduling
185void wait( condition * this, uintptr_t user_info = 0 ) {
186        // LIB_DEBUG_PRINT_SAFE("Waiting\n");
187
188        brand_condition( this );
189
190        //Check that everything is as expected
191        assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
192        verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
193        verifyf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
194
195        unsigned short count = this->monitor_count;
196        unsigned int recursions[ count ];               //Save the current recursion levels to restore them later
197        spinlock *   locks     [ count ];               //We need to pass-in an array of locks to BlockInternal
198
199        // LIB_DEBUG_PRINT_SAFE("count %i\n", count);
200
201        __condition_node_t waiter = { (thread_desc*)this_thread, count, user_info };
202
203        __condition_criterion_t criteria[count];
204        for(int i = 0; i < count; i++) {
205                (criteria[i]){ this->monitors[i], &waiter };
206                // LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
207        }
208
209        waiter.criteria = criteria;
210        append( &this->blocked, &waiter );
211
212        lock_all( this->monitors, locks, count );
213        save_recursion( this->monitors, recursions, count );
214        //DON'T unlock, ask the kernel to do it
215
216        //Find the next thread(s) to run
217        unsigned short thread_count = 0;
218        thread_desc * threads[ count ];
219        for(int i = 0; i < count; i++) {
220                threads[i] = 0;
221        }
222
223        for( int i = 0; i < count; i++) {
224                thread_desc * new_owner = next_thread( this->monitors[i] );
225                thread_count = insert_unique( threads, thread_count, new_owner );
226        }
227
228        // LIB_DEBUG_PRINT_SAFE("Will unblock: ");
229        for(int i = 0; i < thread_count; i++) {
230                // LIB_DEBUG_PRINT_SAFE("%p ", threads[i]);
231        }
232        // LIB_DEBUG_PRINT_SAFE("\n");
233
234        // Everything is ready to go to sleep
235        BlockInternal( locks, count, threads, thread_count );
236
237
238        //WE WOKE UP
239
240
241        //We are back, restore the owners and recursions
242        lock_all( locks, count );
243        restore_recursion( this->monitors, recursions, count );
244        unlock_all( locks, count );
245}
246
247bool signal( condition * this ) {
248        if( is_empty( this ) ) {
249                // LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
250                return false;
251        }
252
253        //Check that everything is as expected
254        verify( this->monitors );
255        verify( this->monitor_count != 0 );
256
257        unsigned short count = this->monitor_count;
258
259        //Some more checking in debug
260        LIB_DEBUG_DO(
261                thread_desc * this_thrd = this_thread;
262                if ( this->monitor_count != this_thrd->current_monitor_count ) {
263                        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 );
264                } // if
265
266                for(int i = 0; i < this->monitor_count; i++) {
267                        if ( this->monitors[i] != this_thrd->current_monitors[i] ) {
268                                abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->current_monitors[i] );
269                        } // if
270                }
271        );
272
273        //Lock all the monitors
274        lock_all( this->monitors, NULL, count );
275        // LIB_DEBUG_PRINT_SAFE("Signalling");
276
277        //Pop the head of the waiting queue
278        __condition_node_t * node = pop_head( &this->blocked );
279
280        //Add the thread to the proper AS stack
281        for(int i = 0; i < count; i++) {
282                __condition_criterion_t * crit = &node->criteria[i];
283                // LIB_DEBUG_PRINT_SAFE(" %p", crit->target);
284                assert( !crit->ready );
285                push( &crit->target->signal_stack, crit );
286        }
287
288        // LIB_DEBUG_PRINT_SAFE("\n");
289
290        //Release
291        unlock_all( this->monitors, count );
292
293        return true;
294}
295
296bool signal_block( condition * this ) {
297        if( !this->blocked.head ) {
298                LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
299                return false;
300        }
301
302        //Check that everything is as expected
303        verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
304        verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
305
306        unsigned short count = this->monitor_count;
307        unsigned int recursions[ count ];               //Save the current recursion levels to restore them later
308        spinlock *   locks     [ count ];               //We need to pass-in an array of locks to BlockInternal
309
310        lock_all( this->monitors, locks, count );
311
312        //create creteria
313        __condition_node_t waiter = { (thread_desc*)this_thread, count, 0 };
314
315        __condition_criterion_t criteria[count];
316        for(int i = 0; i < count; i++) {
317                (criteria[i]){ this->monitors[i], &waiter };
318                // LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
319                push( &criteria[i].target->signal_stack, &criteria[i] );
320        }
321
322        waiter.criteria = criteria;
323
324        //save contexts
325        save_recursion( this->monitors, recursions, count );
326
327        //Find the thread to run
328        thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
329        for(int i = 0; i < count; i++) {
330                set_owner( this->monitors[i], signallee );
331        }
332
333        LIB_DEBUG_PRINT_SAFE( "Waiting on signal block\n" );
334
335        //Everything is ready to go to sleep
336        BlockInternal( locks, count, &signallee, 1 );
337
338
339
340
341        LIB_DEBUG_PRINT_SAFE( "Back from signal block\n" );
342
343        //We are back, restore the owners and recursions
344        lock_all( locks, count );
345        restore_recursion( this->monitors, recursions, count );
346        unlock_all( locks, count );
347
348        return true;
349}
350
351uintptr_t front( condition * this ) {
352        verifyf( !is_empty(this),
353                "Attempt to access user data on an empty condition.\n"
354                "Possible cause is not checking if the condition is empty before reading stored data."
355        );
356        return this->blocked.head->user_info;
357}
358
359//-----------------------------------------------------------------------------
360// Internal scheduling
361void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) ) {
362        // thread_desc * this = this_thread;
363
364        // unsigned short count = this->current_monitor_count;
365        // unsigned int recursions[ count ];            //Save the current recursion levels to restore them later
366        // spinlock *   locks     [ count ];            //We need to pass-in an array of locks to BlockInternal
367
368        // lock_all( this->current_monitors, locks, count );
369
370
371
372
373
374        // // // Everything is ready to go to sleep
375        // // BlockInternal( locks, count, threads, thread_count );
376
377
378        // //WE WOKE UP
379
380
381        // //We are back, restore the owners and recursions
382        // lock_all( locks, count );
383        // restore_recursion( this->monitors, recursions, count );
384        // unlock_all( locks, count );
385}
386
387//-----------------------------------------------------------------------------
388// Utilities
389
390static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
391        //Pass the monitor appropriately
392        this->owner = owner;
393
394        //We are passing the monitor to someone else, which means recursion level is not 0
395        this->recursion = owner ? 1 : 0;
396}
397
398static inline thread_desc * next_thread( monitor_desc * this ) {
399        //Check the signaller stack
400        __condition_criterion_t * urgent = pop( &this->signal_stack );
401        if( urgent ) {
402                //The signaller stack is not empty,
403                //regardless of if we are ready to baton pass,
404                //we need to set the monitor as in use
405                set_owner( this,  urgent->owner->waiting_thread );
406
407                return check_condition( urgent );
408        }
409
410        // No signaller thread
411        // Get the next thread in the entry_queue
412        thread_desc * new_owner = pop_head( &this->entry_queue );
413        set_owner( this, new_owner );
414
415        return new_owner;
416}
417
418static inline void lock_all( spinlock ** locks, unsigned short count ) {
419        for( int i = 0; i < count; i++ ) {
420                lock_yield( locks[i] DEBUG_CTX2 );
421        }
422}
423
424static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
425        for( int i = 0; i < count; i++ ) {
426                spinlock * l = &source[i]->lock;
427                lock_yield( l DEBUG_CTX2 );
428                if(locks) locks[i] = l;
429        }
430}
431
432static inline void unlock_all( spinlock ** locks, unsigned short count ) {
433        for( int i = 0; i < count; i++ ) {
434                unlock( locks[i] );
435        }
436}
437
438static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
439        for( int i = 0; i < count; i++ ) {
440                unlock( &locks[i]->lock );
441        }
442}
443
444
445static inline void save_recursion   ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count ) {
446        for( int i = 0; i < count; i++ ) {
447                recursions[i] = ctx[i]->recursion;
448        }
449}
450
451static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count ) {
452        for( int i = 0; i < count; i++ ) {
453                ctx[i]->recursion = recursions[i];
454        }
455}
456
457// Function has 2 different behavior
458// 1 - Marks a monitors as being ready to run
459// 2 - Checks if all the monitors are ready to run
460//     if so return the thread to run
461static inline thread_desc * check_condition( __condition_criterion_t * target ) {
462        __condition_node_t * node = target->owner;
463        unsigned short count = node->count;
464        __condition_criterion_t * criteria = node->criteria;
465
466        bool ready2run = true;
467
468        for(    int i = 0; i < count; i++ ) {
469
470                // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
471                if( &criteria[i] == target ) {
472                        criteria[i].ready = true;
473                        // LIB_DEBUG_PRINT_SAFE( "True\n" );
474                }
475
476                ready2run = criteria[i].ready && ready2run;
477        }
478
479        // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
480        return ready2run ? node->waiting_thread : NULL;
481}
482
483static inline void brand_condition( condition * this ) {
484        thread_desc * thrd = this_thread;
485        if( !this->monitors ) {
486                // LIB_DEBUG_PRINT_SAFE("Branding\n");
487                assertf( thrd->current_monitors != NULL, "No current monitor to brand condition", thrd->current_monitors );
488                this->monitor_count = thrd->current_monitor_count;
489
490                this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
491                for( int i = 0; i < this->monitor_count; i++ ) {
492                        this->monitors[i] = thrd->current_monitors[i];
493                }
494        }
495}
496
497static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) {
498        if( !val ) return end;
499
500        for(int i = 0; i <= end; i++) {
501                if( thrds[i] == val ) return end;
502        }
503
504        thrds[end] = val;
505        return end + 1;
506}
507
508void ?{}( __condition_blocked_queue_t & this ) {
509        this.head = NULL;
510        this.tail = &this.head;
511}
512
513void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
514        verify(this->tail != NULL);
515        *this->tail = c;
516        this->tail = &c->next;
517}
518
519__condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
520        __condition_node_t * head = this->head;
521        if( head ) {
522                this->head = head->next;
523                if( !head->next ) {
524                        this->tail = &this->head;
525                }
526                head->next = NULL;
527        }
528        return head;
529}
Note: See TracBrowser for help on using the repository browser.