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

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 d997f8e was 44264c5, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Working implementation of internal scheduling, TODO some cleanup

  • Property mode set to 100644
File size: 13.3 KB
RevLine 
[f07e037]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//
[84c52a8]8// monitor_desc.c --
[f07e037]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
[a933dcf4]19#include <stdlib>
20
[f07e037]21#include "kernel_private.h"
[5ea06d6]22#include "libhdr.h"
[f07e037]23
[0c78741]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
[690f13c]43
44
[cb0e6de]45extern "C" {
[0c78741]46        void __enter_monitor_desc(monitor_desc * this) {
[cb0e6de]47                lock( &this->lock );
48                thread_desc * thrd = this_thread();
[f07e037]49
[0c78741]50                LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
[5ea06d6]51
[cb0e6de]52                if( !this->owner ) {
53                        //No one has the monitor, just take it
[cd348e7]54                        set_owner( this, thrd );
[cb0e6de]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 );
[44264c5]64                        LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
[cb0e6de]65                        ScheduleInternal( &this->lock );
[cc7f4b1]66
[cb0e6de]67                        //ScheduleInternal will unlock spinlock, no need to unlock ourselves
68                        return; 
69                }
[f07e037]70
[cb0e6de]71                unlock( &this->lock );
[5ea06d6]72                return;
[cb0e6de]73        }
[f07e037]74
[690f13c]75        // leave pseudo code :
[0c78741]76        //      TODO
77        void __leave_monitor_desc(monitor_desc * this) {
[cb0e6de]78                lock( &this->lock );
[f07e037]79
[cb0e6de]80                thread_desc * thrd = this_thread();
[0c78741]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 );
[cc7f4b1]84
[cb0e6de]85                //Leaving a recursion level, decrement the counter
86                this->recursion -= 1;
[f07e037]87
[690f13c]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                }
[f07e037]94
[0c78741]95                thread_desc * new_owner = next_thread( this );
[5ea06d6]96
[690f13c]97                //We can now let other threads in safely
[cb0e6de]98                unlock( &this->lock );
[51f3798]99
[44264c5]100                LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
101
[690f13c]102                //We need to wake-up the thread
103                ScheduleThread( new_owner );
[cc7f4b1]104        }
[2781e65]105}
106
[5ea06d6]107static inline void enter(monitor_desc ** monitors, int count) {
[0c78741]108        for(int i = 0; i < count; i++) {
109                __enter_monitor_desc( monitors[i] );
[2781e65]110        }
111}
112
[5ea06d6]113static inline void leave(monitor_desc ** monitors, int count) {
[0c78741]114        for(int i = count - 1; i >= 0; i--) {
115                __leave_monitor_desc( monitors[i] );
[2781e65]116        }
[5ea06d6]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
[ad1a8dd]139void debug_break() __attribute__(( noinline ))
140{
141       
142}
143
[5ea06d6]144//-----------------------------------------------------------------------------
145// Internal scheduling
146void wait( condition * this ) {
[0c78741]147        LIB_DEBUG_PRINT_SAFE("Waiting\n");
[5ea06d6]148
[0c78741]149        brand_condition( this );
[5ea06d6]150
151        //Check that everything is as expected
[0c78741]152        assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
153        assertf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
[44264c5]154        assertf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
[5ea06d6]155
[0c78741]156        unsigned short count = this->monitor_count;
[9c59cd4]157        unsigned int recursions[ count ];               //Save the current recursion levels to restore them later
158        spinlock *   locks     [ count ];               //We need to pass-in an array of locks to ScheduleInternal
[0c78741]159
160        LIB_DEBUG_PRINT_SAFE("count %i\n", count);
161
162        __condition_node_t waiter;
163        waiter.waiting_thread = this_thread();
164        waiter.count = count;
165        waiter.next = NULL;
166
167        __condition_criterion_t criteria[count];
168        for(int i = 0; i < count; i++) {
169                criteria[i].ready  = false;
170                criteria[i].target = this->monitors[i];
171                criteria[i].owner  = &waiter;
172                criteria[i].next   = NULL;
173                LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
174        }
[5ea06d6]175
[0c78741]176        waiter.criteria = criteria;
177        append( &this->blocked, &waiter );
[5ea06d6]178
[9c59cd4]179        lock_all( this->monitors, locks, count );
180        save_recursion( this->monitors, recursions, count );
[0c78741]181        //DON'T unlock, ask the kernel to do it
[5ea06d6]182
[0c78741]183        //Find the next thread(s) to run
[ad1a8dd]184        unsigned short thread_count = 0;
[0c78741]185        thread_desc * threads[ count ];
[ad1a8dd]186        for(int i = 0; i < count; i++) {
187                threads[i] = 0;
188        }
189
[0c78741]190        for( int i = 0; i < count; i++) {
191                thread_desc * new_owner = next_thread( this->monitors[i] );
[ad1a8dd]192                thread_count = insert_unique( threads, thread_count, new_owner );
[5ea06d6]193        }
194
[0c78741]195        LIB_DEBUG_PRINT_SAFE("Will unblock: ");
196        for(int i = 0; i < thread_count; i++) {
197                LIB_DEBUG_PRINT_SAFE("%p ", threads[i]);
198        }
199        LIB_DEBUG_PRINT_SAFE("\n");
[5ea06d6]200
[9c59cd4]201        // Everything is ready to go to sleep
202        ScheduleInternal( locks, count, threads, thread_count );
[5ea06d6]203
[44264c5]204        debug_break();
[5ea06d6]205        //WE WOKE UP
206
207
208        //We are back, restore the owners and recursions
[9c59cd4]209        lock_all( locks, count );
210        restore_recursion( this->monitors, recursions, count );
211        unlock_all( locks, count );
[5ea06d6]212}
213
[0c78741]214void signal( condition * this ) {
215        if( !this->blocked.head ) {
216                LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
217                return;
218        }
[5ea06d6]219
220        //Check that everything is as expected
221        assert( this->monitors );
222        assert( this->monitor_count != 0 );
[0c78741]223
224        unsigned short count = this->monitor_count;
[5ea06d6]225       
[44264c5]226        //Some more checking in debug
[5ea06d6]227        LIB_DEBUG_DO(
[0c78741]228                thread_desc * this_thrd = this_thread();
229                if ( this->monitor_count != this_thrd->current_monitor_count ) {
230                        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 );
[5ea06d6]231                } // if
[0c78741]232
233                for(int i = 0; i < this->monitor_count; i++) {
234                        if ( this->monitors[i] != this_thrd->current_monitors[i] ) {
235                                abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->current_monitors[i] );
236                        } // if
237                }
[5ea06d6]238        );
239
[44264c5]240        //Lock all the monitors
[0c78741]241        lock_all( this->monitors, NULL, count );
242        LIB_DEBUG_PRINT_SAFE("Signalling");
243
[44264c5]244        //Pop the head of the waiting queue
[0c78741]245        __condition_node_t * node = pop_head( &this->blocked );
[44264c5]246
247        //Add the thread to the proper AS stack
[0c78741]248        for(int i = 0; i < count; i++) {
249                __condition_criterion_t * crit = &node->criteria[i];
250                LIB_DEBUG_PRINT_SAFE(" %p", crit->target);
251                assert( !crit->ready );
252                push( &crit->target->signal_stack, crit );
[5ea06d6]253        }
[0c78741]254
255        LIB_DEBUG_PRINT_SAFE("\n");
256
[44264c5]257        //Release
[0c78741]258        unlock_all( this->monitors, count );
[5ea06d6]259}
260
[44264c5]261void signal_block( condition * this ) {
262        if( !this->blocked.head ) {
263                LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
264                return;
265        }
266
267        //Check that everything is as expected
268        assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
269        assertf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
270
271        unsigned short count = this->monitor_count;
272        unsigned int recursions[ count ];               //Save the current recursion levels to restore them later
273        spinlock *   locks     [ count ];               //We need to pass-in an array of locks to ScheduleInternal
274
275        lock_all( this->monitors, locks, count );
276
277        //create creteria
278        __condition_node_t waiter;
279        waiter.waiting_thread = this_thread();
280        waiter.count = count;
281        waiter.next = NULL;
282
283        __condition_criterion_t criteria[count];
284        for(int i = 0; i < count; i++) {
285                LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
286                criteria[i].ready  = false;
287                criteria[i].owner  = &waiter;
288                criteria[i].next   = NULL;
289                criteria[i].target = this->monitors[i];
290                push( &criteria[i].target->signal_stack, &criteria[i] );
291        }
292
293        waiter.criteria = criteria;
294
295        //save contexts
296        save_recursion( this->monitors, recursions, count );
297
298        //Find the thread to run
299        thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
300        for(int i = 0; i < count; i++) {
301                set_owner( this->monitors[i], signallee );
302        }
303
304        LIB_DEBUG_PRINT_SAFE( "Waiting on signal block\n" );
305        debug_break();
306
307        //Everything is ready to go to sleep
308        ScheduleInternal( locks, count, &signallee, 1 );
309
310        debug_break();
311        LIB_DEBUG_PRINT_SAFE( "Back from signal block\n" );
312
313        //We are back, restore the owners and recursions
314        lock_all( locks, count );
315        restore_recursion( this->monitors, recursions, count );
316        unlock_all( locks, count );
317}
318
[0c78741]319//-----------------------------------------------------------------------------
320// Utilities
321
322static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
323        //Pass the monitor appropriately
324        this->owner = owner;
325
326        //We are passing the monitor to someone else, which means recursion level is not 0
327        this->recursion = owner ? 1 : 0;
328}
329
330static inline thread_desc * next_thread( monitor_desc * this ) {
331        //Check the signaller stack
332        __condition_criterion_t * urgent = pop( &this->signal_stack );
333        if( urgent ) {
334                //The signaller stack is not empty,
335                //regardless of if we are ready to baton pass,
336                //we need to set the monitor as in use
337                set_owner( this,  urgent->owner->waiting_thread );
338
339                return check_condition( urgent );
340        }
341
342        // No signaller thread
343        // Get the next thread in the entry_queue
344        thread_desc * new_owner = pop_head( &this->entry_queue );
345        set_owner( this, new_owner );
346
347        return new_owner;
348}
349
350static inline void lock_all( spinlock ** locks, unsigned short count ) {
351        for( int i = 0; i < count; i++ ) {
352                lock( locks[i] );
353        }
354}
355
356static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
357        for( int i = 0; i < count; i++ ) {
358                spinlock * l = &source[i]->lock;
359                lock( l );
360                if(locks) locks[i] = l;
361        }
362}
363
364static inline void unlock_all( spinlock ** locks, unsigned short count ) {
365        for( int i = 0; i < count; i++ ) {
366                unlock( locks[i] );
367        }
368}
369
370static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
371        for( int i = 0; i < count; i++ ) {
372                unlock( &locks[i]->lock );
373        }
374}
375
376
377static inline void save_recursion   ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count ) {
378        for( int i = 0; i < count; i++ ) {
379                recursions[i] = ctx[i]->recursion;
380        }
381}
382
383static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count ) {
384        for( int i = 0; i < count; i++ ) {
385                ctx[i]->recursion = recursions[i];
386        }
387}
388
389// Function has 2 different behavior
390// 1 - Marks a monitors as being ready to run
391// 2 - Checks if all the monitors are ready to run
392//     if so return the thread to run
393static inline thread_desc * check_condition( __condition_criterion_t * target ) {
394        __condition_node_t * node = target->owner;
395        unsigned short count = node->count;
396        __condition_criterion_t * criteria = node->criteria;
397
398        bool ready2run = true;
399
400        for(    int i = 0; i < count; i++ ) {
[44264c5]401
[0c78741]402                LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
403                if( &criteria[i] == target ) {
404                        criteria[i].ready = true;
405                        LIB_DEBUG_PRINT_SAFE( "True\n" );
406                }
407
408                ready2run = criteria[i].ready && ready2run;
409        }
410
411        LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
412        return ready2run ? node->waiting_thread : NULL;
413}
414
415static inline void brand_condition( condition * this ) {
416        thread_desc * thrd = this_thread();
417        if( !this->monitors ) {
418                LIB_DEBUG_PRINT_SAFE("Branding\n");
419                assertf( thrd->current_monitors != NULL, "No current monitor to brand condition", thrd->current_monitors );
420                this->monitor_count = thrd->current_monitor_count;
[a933dcf4]421
422                this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
423                for( int i = 0; i < this->monitor_count; i++ ) {
424                        this->monitors[i] = thrd->current_monitors[i];
425                }
[0c78741]426        }
427}
428
429static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) {
[ad1a8dd]430        if( !val ) return end;
431
432        for(int i = 0; i <= end; i++) {
[0c78741]433                if( thrds[i] == val ) return end;
434        }
435
436        thrds[end] = val;
437        return end + 1;
438}
439
440void ?{}( __condition_blocked_queue_t * this ) {
441        this->head = NULL;
442        this->tail = &this->head;
443}
444
445void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
446        assert(this->tail != NULL);
447        *this->tail = c;
448        this->tail = &c->next;
449}
450
451__condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
452        __condition_node_t * head = this->head;
453        if( head ) {
454                this->head = head->next;
455                if( !head->next ) {
456                        this->tail = &this->head;
457                }
458                head->next = NULL;
459        }
460        return head;
[f07e037]461}
Note: See TracBrowser for help on using the repository browser.