source: src/libcfa/concurrency/monitor.c @ 44264c5

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 44264c5 was 44264c5, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Working implementation of internal scheduling, TODO some cleanup

  • Property mode set to 100644
File size: 13.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 "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 debug_break() __attribute__(( noinline ))
140{
141       
142}
143
144//-----------------------------------------------------------------------------
145// Internal scheduling
146void wait( condition * this ) {
147        LIB_DEBUG_PRINT_SAFE("Waiting\n");
148
149        brand_condition( this );
150
151        //Check that everything is as expected
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 );
154        assertf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
155
156        unsigned short count = this->monitor_count;
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
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        }
175
176        waiter.criteria = criteria;
177        append( &this->blocked, &waiter );
178
179        lock_all( this->monitors, locks, count );
180        save_recursion( this->monitors, recursions, count );
181        //DON'T unlock, ask the kernel to do it
182
183        //Find the next thread(s) to run
184        unsigned short thread_count = 0;
185        thread_desc * threads[ count ];
186        for(int i = 0; i < count; i++) {
187                threads[i] = 0;
188        }
189
190        for( int i = 0; i < count; i++) {
191                thread_desc * new_owner = next_thread( this->monitors[i] );
192                thread_count = insert_unique( threads, thread_count, new_owner );
193        }
194
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");
200
201        // Everything is ready to go to sleep
202        ScheduleInternal( locks, count, threads, thread_count );
203
204        debug_break();
205        //WE WOKE UP
206
207
208        //We are back, restore the owners and recursions
209        lock_all( locks, count );
210        restore_recursion( this->monitors, recursions, count );
211        unlock_all( locks, count );
212}
213
214void signal( condition * this ) {
215        if( !this->blocked.head ) {
216                LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
217                return;
218        }
219
220        //Check that everything is as expected
221        assert( this->monitors );
222        assert( this->monitor_count != 0 );
223
224        unsigned short count = this->monitor_count;
225       
226        //Some more checking in debug
227        LIB_DEBUG_DO(
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 );
231                } // if
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                }
238        );
239
240        //Lock all the monitors
241        lock_all( this->monitors, NULL, count );
242        LIB_DEBUG_PRINT_SAFE("Signalling");
243
244        //Pop the head of the waiting queue
245        __condition_node_t * node = pop_head( &this->blocked );
246
247        //Add the thread to the proper AS stack
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 );
253        }
254
255        LIB_DEBUG_PRINT_SAFE("\n");
256
257        //Release
258        unlock_all( this->monitors, count );
259}
260
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
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++ ) {
401
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;
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                }
426        }
427}
428
429static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) {
430        if( !val ) return end;
431
432        for(int i = 0; i <= end; i++) {
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;
461}
Note: See TracBrowser for help on using the repository browser.