| 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 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Sat Oct  7 18:06:45 2017 | 
|---|
| 13 | // Update Count     : 10 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <stddef.h> | 
|---|
| 19 |  | 
|---|
| 20 | #include <assert.h> | 
|---|
| 21 | #include "invoke.h" | 
|---|
| 22 | #include "stdlib" | 
|---|
| 23 |  | 
|---|
| 24 | trait is_monitor(dtype T) { | 
|---|
| 25 | monitor_desc * get_monitor( T & ); | 
|---|
| 26 | void ^?{}( T & mutex ); | 
|---|
| 27 | }; | 
|---|
| 28 |  | 
|---|
| 29 | static inline void ?{}(monitor_desc & this) { | 
|---|
| 30 | (this.lock){}; | 
|---|
| 31 | (this.entry_queue){}; | 
|---|
| 32 | (this.signal_stack){}; | 
|---|
| 33 | this.owner         = NULL; | 
|---|
| 34 | this.recursion     = 0; | 
|---|
| 35 | this.mask.accepted = NULL; | 
|---|
| 36 | this.mask.clauses  = NULL; | 
|---|
| 37 | this.mask.size     = 0; | 
|---|
| 38 | this.dtor_node     = NULL; | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | struct monitor_guard_t { | 
|---|
| 42 | monitor_desc ** m; | 
|---|
| 43 | int count; | 
|---|
| 44 | monitor_desc ** prev_mntrs; | 
|---|
| 45 | unsigned short  prev_count; | 
|---|
| 46 | fptr_t          prev_func; | 
|---|
| 47 | }; | 
|---|
| 48 |  | 
|---|
| 49 | void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, void (*func)() ); | 
|---|
| 50 | void ^?{}( monitor_guard_t & this ); | 
|---|
| 51 |  | 
|---|
| 52 | struct monitor_dtor_guard_t { | 
|---|
| 53 | monitor_desc * m; | 
|---|
| 54 | monitor_desc ** prev_mntrs; | 
|---|
| 55 | unsigned short  prev_count; | 
|---|
| 56 | fptr_t          prev_func; | 
|---|
| 57 | }; | 
|---|
| 58 |  | 
|---|
| 59 | void ?{}( monitor_dtor_guard_t & this, monitor_desc ** m, void (*func)() ); | 
|---|
| 60 | void ^?{}( monitor_dtor_guard_t & this ); | 
|---|
| 61 |  | 
|---|
| 62 | static inline forall( dtype T | sized(T) | { void ^?{}( T & mutex ); } ) | 
|---|
| 63 | void delete( T * th ) { | 
|---|
| 64 | ^(*th){}; | 
|---|
| 65 | free( th ); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | //----------------------------------------------------------------------------- | 
|---|
| 69 | // Internal scheduling | 
|---|
| 70 |  | 
|---|
| 71 | struct __condition_criterion_t { | 
|---|
| 72 | // Whether or not the criterion is met (True if met) | 
|---|
| 73 | bool ready; | 
|---|
| 74 |  | 
|---|
| 75 | // The monitor this criterion concerns | 
|---|
| 76 | monitor_desc * target; | 
|---|
| 77 |  | 
|---|
| 78 | // The parent node to which this criterion belongs | 
|---|
| 79 | struct __condition_node_t * owner; | 
|---|
| 80 |  | 
|---|
| 81 | // Intrusive linked list Next field | 
|---|
| 82 | __condition_criterion_t * next; | 
|---|
| 83 |  | 
|---|
| 84 | }; | 
|---|
| 85 |  | 
|---|
| 86 | struct __condition_node_t { | 
|---|
| 87 | // Thread that needs to be woken when all criteria are met | 
|---|
| 88 | thread_desc * waiting_thread; | 
|---|
| 89 |  | 
|---|
| 90 | // Array of criteria (Criterions are contiguous in memory) | 
|---|
| 91 | __condition_criterion_t * criteria; | 
|---|
| 92 |  | 
|---|
| 93 | // Number of criterions in the criteria | 
|---|
| 94 | unsigned short count; | 
|---|
| 95 |  | 
|---|
| 96 | // Intrusive linked list Next field | 
|---|
| 97 | __condition_node_t * next; | 
|---|
| 98 |  | 
|---|
| 99 | // Custom user info accessible before signalling | 
|---|
| 100 | uintptr_t user_info; | 
|---|
| 101 |  | 
|---|
| 102 | }; | 
|---|
| 103 |  | 
|---|
| 104 | struct __condition_blocked_queue_t { | 
|---|
| 105 | __condition_node_t * head; | 
|---|
| 106 | __condition_node_t ** tail; | 
|---|
| 107 | }; | 
|---|
| 108 |  | 
|---|
| 109 | void ?{}(__condition_node_t & this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ); | 
|---|
| 110 | void ?{}(__condition_criterion_t & this ); | 
|---|
| 111 | void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t * owner ); | 
|---|
| 112 |  | 
|---|
| 113 | void ?{}( __condition_blocked_queue_t & ); | 
|---|
| 114 | void append( __condition_blocked_queue_t &, __condition_node_t * ); | 
|---|
| 115 | __condition_node_t * pop_head( __condition_blocked_queue_t & ); | 
|---|
| 116 |  | 
|---|
| 117 | struct condition { | 
|---|
| 118 | // Link list which contains the blocked threads as-well as the information needed to unblock them | 
|---|
| 119 | __condition_blocked_queue_t blocked; | 
|---|
| 120 |  | 
|---|
| 121 | // Array of monitor pointers (Monitors are NOT contiguous in memory) | 
|---|
| 122 | monitor_desc ** monitors; | 
|---|
| 123 |  | 
|---|
| 124 | // Number of monitors in the array | 
|---|
| 125 | unsigned short monitor_count; | 
|---|
| 126 |  | 
|---|
| 127 | }; | 
|---|
| 128 |  | 
|---|
| 129 | static inline void ?{}( condition & this ) { | 
|---|
| 130 | this.monitors = NULL; | 
|---|
| 131 | this.monitor_count = 0; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | static inline void ^?{}( condition & this ) { | 
|---|
| 135 | free( this.monitors ); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | void wait        ( condition & this, uintptr_t user_info = 0 ); | 
|---|
| 139 | bool signal      ( condition & this ); | 
|---|
| 140 | bool signal_block( condition & this ); | 
|---|
| 141 | static inline bool is_empty    ( condition & this ) { return !this.blocked.head; } | 
|---|
| 142 | uintptr_t front       ( condition & this ); | 
|---|
| 143 |  | 
|---|
| 144 | //----------------------------------------------------------------------------- | 
|---|
| 145 | // External scheduling | 
|---|
| 146 |  | 
|---|
| 147 | struct __acceptable_t { | 
|---|
| 148 | __monitor_group_t; | 
|---|
| 149 | bool is_dtor; | 
|---|
| 150 | }; | 
|---|
| 151 |  | 
|---|
| 152 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration ); | 
|---|
| 153 |  | 
|---|
| 154 | // Local Variables: // | 
|---|
| 155 | // mode: c // | 
|---|
| 156 | // tab-width: 4 // | 
|---|
| 157 | // End: // | 
|---|