[f07e037] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
| 7 | // monitor --
|
---|
| 8 | //
|
---|
| 9 | // Author : Thierry Delisle
|
---|
| 10 | // Created On : Thd Feb 23 12:27:26 2017
|
---|
[91c389a] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[6b0b624] | 12 | // Last Modified On : Sat Jul 22 09:59:01 2017
|
---|
| 13 | // Update Count : 3
|
---|
[f07e037] | 14 | //
|
---|
| 15 |
|
---|
[6b0b624] | 16 | #pragma once
|
---|
[f07e037] | 17 |
|
---|
[690f13c] | 18 | #include <stddef.h>
|
---|
| 19 |
|
---|
[91c389a] | 20 | #include <assert.h>
|
---|
[f07e037] | 21 | #include "invoke.h"
|
---|
[2781e65] | 22 | #include "stdlib"
|
---|
[f07e037] | 23 |
|
---|
[84c52a8] | 24 | static inline void ?{}(monitor_desc * this) {
|
---|
[97e3296] | 25 | (&this->lock){};
|
---|
[690f13c] | 26 | this->owner = NULL;
|
---|
[97e3296] | 27 | (&this->entry_queue){};
|
---|
| 28 | (&this->signal_stack){};
|
---|
[cc7f4b1] | 29 | this->recursion = 0;
|
---|
[97e3296] | 30 | this->acceptables = NULL;
|
---|
| 31 | this->acceptable_count = 0;
|
---|
| 32 | this->accepted_index = -1;
|
---|
| 33 | this->pre_accept = 0;
|
---|
[cc7f4b1] | 34 | }
|
---|
| 35 |
|
---|
| 36 | struct monitor_guard_t {
|
---|
[84c52a8] | 37 | monitor_desc ** m;
|
---|
[2781e65] | 38 | int count;
|
---|
[4aa2fb2] | 39 | monitor_desc ** prev_mntrs;
|
---|
| 40 | unsigned short prev_count;
|
---|
[51f3798] | 41 | };
|
---|
| 42 |
|
---|
[84c52a8] | 43 | static inline int ?<?(monitor_desc* lhs, monitor_desc* rhs) {
|
---|
[2781e65] | 44 | return ((intptr_t)lhs) < ((intptr_t)rhs);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[97e3296] | 47 | void ?{}( monitor_guard_t * this, monitor_desc ** m, int count, void (*func)() );
|
---|
[5ea06d6] | 48 | void ^?{}( monitor_guard_t * this );
|
---|
[51f3798] | 49 |
|
---|
[5ea06d6] | 50 | //-----------------------------------------------------------------------------
|
---|
| 51 | // Internal scheduling
|
---|
[0c78741] | 52 |
|
---|
| 53 | struct __condition_criterion_t {
|
---|
| 54 | bool ready; //Whether or not the criterion is met (True if met)
|
---|
| 55 | monitor_desc * target; //The monitor this criterion concerns
|
---|
| 56 | struct __condition_node_t * owner; //The parent node to which this criterion belongs
|
---|
| 57 | __condition_criterion_t * next; //Intrusive linked list Next field
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | struct __condition_node_t {
|
---|
| 61 | thread_desc * waiting_thread; //Thread that needs to be woken when all criteria are met
|
---|
| 62 | __condition_criterion_t * criteria; //Array of criteria (Criterions are contiguous in memory)
|
---|
| 63 | unsigned short count; //Number of criterions in the criteria
|
---|
| 64 | __condition_node_t * next; //Intrusive linked list Next field
|
---|
[be3d020] | 65 | uintptr_t user_info; //Custom user info accessible before signalling
|
---|
[0c78741] | 66 | };
|
---|
| 67 |
|
---|
| 68 | struct __condition_blocked_queue_t {
|
---|
| 69 | __condition_node_t * head;
|
---|
| 70 | __condition_node_t ** tail;
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 | void ?{}( __condition_blocked_queue_t * );
|
---|
| 74 | void append( __condition_blocked_queue_t *, __condition_node_t * );
|
---|
| 75 | __condition_node_t * pop_head( __condition_blocked_queue_t * );
|
---|
| 76 |
|
---|
[5ea06d6] | 77 | struct condition {
|
---|
[0c78741] | 78 | __condition_blocked_queue_t blocked; //Link list which contains the blocked threads as-well as the information needed to unblock them
|
---|
| 79 | monitor_desc ** monitors; //Array of monitor pointers (Monitors are NOT contiguous in memory)
|
---|
| 80 | unsigned short monitor_count; //Number of monitors in the array
|
---|
[5ea06d6] | 81 | };
|
---|
[51f3798] | 82 |
|
---|
[5ea06d6] | 83 | static inline void ?{}( condition * this ) {
|
---|
| 84 | this->monitors = NULL;
|
---|
| 85 | this->monitor_count = 0;
|
---|
| 86 | }
|
---|
[2781e65] | 87 |
|
---|
[1167cd4] | 88 | static inline void ^?{}( condition * this ) {
|
---|
| 89 | free( this->monitors );
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[be3d020] | 92 | void wait( condition * this, uintptr_t user_info = 0 );
|
---|
| 93 | bool signal( condition * this );
|
---|
| 94 | bool signal_block( condition * this );
|
---|
| 95 | static inline bool is_empty( condition * this ) { return !this->blocked.head; }
|
---|
| 96 | uintptr_t front( condition * this );
|
---|
| 97 |
|
---|
[97e3296] | 98 | //-----------------------------------------------------------------------------
|
---|
| 99 | // External scheduling
|
---|
| 100 |
|
---|
| 101 | typedef void (*void_fptr_t)(void);
|
---|
| 102 |
|
---|
[c81ebf9] | 103 | struct __acceptable_t {
|
---|
[97e3296] | 104 | void_fptr_t func;
|
---|
[c81ebf9] | 105 | unsigned short count;
|
---|
[97e3296] | 106 | monitor_desc ** monitors;
|
---|
| 107 | bool run_preaccept;
|
---|
[c81ebf9] | 108 | };
|
---|
| 109 |
|
---|
[97e3296] | 110 | int __accept_internal( unsigned short count, __acceptable_t * acceptables );
|
---|
[c81ebf9] | 111 |
|
---|
[6b0b624] | 112 | // Local Variables: //
|
---|
| 113 | // mode: c //
|
---|
| 114 | // tab-width: 4 //
|
---|
| 115 | // End: //
|
---|