| [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 | 
|---|
| [751d963] | 12 | // Last Modified On : Sat Oct  7 18:06:45 2017 | 
|---|
|  | 13 | // Update Count     : 10 | 
|---|
| [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 |  | 
|---|
| [1dcd9554] | 24 | trait is_monitor(dtype T) { | 
|---|
|  | 25 | monitor_desc * get_monitor( T & ); | 
|---|
|  | 26 | void ^?{}( T & mutex ); | 
|---|
|  | 27 | }; | 
|---|
|  | 28 |  | 
|---|
| [c40e7c5] | 29 | static inline void ?{}(monitor_desc & this) with( this ) { | 
|---|
|  | 30 | lock{}; | 
|---|
|  | 31 | entry_queue{}; | 
|---|
|  | 32 | signal_stack{}; | 
|---|
|  | 33 | owner         = NULL; | 
|---|
|  | 34 | recursion     = 0; | 
|---|
|  | 35 | mask.accepted = NULL; | 
|---|
|  | 36 | mask.data     = NULL; | 
|---|
|  | 37 | mask.size     = 0; | 
|---|
|  | 38 | dtor_node     = NULL; | 
|---|
| [cc7f4b1] | 39 | } | 
|---|
|  | 40 |  | 
|---|
|  | 41 | struct monitor_guard_t { | 
|---|
| [0cf5b79] | 42 | monitor_desc **         m; | 
|---|
|  | 43 | __lock_size_t           count; | 
|---|
|  | 44 | __monitor_group_t prev; | 
|---|
| [51f3798] | 45 | }; | 
|---|
|  | 46 |  | 
|---|
| [59a0bde] | 47 | void ?{}( monitor_guard_t & this, monitor_desc ** m, __lock_size_t count, void (*func)() ); | 
|---|
| [242a902] | 48 | void ^?{}( monitor_guard_t & this ); | 
|---|
| [51f3798] | 49 |  | 
|---|
| [549c006] | 50 | struct monitor_dtor_guard_t { | 
|---|
| [0cf5b79] | 51 | monitor_desc *    m; | 
|---|
|  | 52 | __monitor_group_t prev; | 
|---|
| [549c006] | 53 | }; | 
|---|
|  | 54 |  | 
|---|
|  | 55 | void ?{}( monitor_dtor_guard_t & this, monitor_desc ** m, void (*func)() ); | 
|---|
|  | 56 | void ^?{}( monitor_dtor_guard_t & this ); | 
|---|
|  | 57 |  | 
|---|
| [751d963] | 58 | static inline forall( dtype T | sized(T) | { void ^?{}( T & mutex ); } ) | 
|---|
|  | 59 | void delete( T * th ) { | 
|---|
|  | 60 | ^(*th){}; | 
|---|
|  | 61 | free( th ); | 
|---|
|  | 62 | } | 
|---|
|  | 63 |  | 
|---|
| [5ea06d6] | 64 | //----------------------------------------------------------------------------- | 
|---|
|  | 65 | // Internal scheduling | 
|---|
| [0c78741] | 66 |  | 
|---|
|  | 67 | struct __condition_criterion_t { | 
|---|
| [025278e] | 68 | // Whether or not the criterion is met (True if met) | 
|---|
|  | 69 | bool ready; | 
|---|
|  | 70 |  | 
|---|
|  | 71 | // The monitor this criterion concerns | 
|---|
|  | 72 | monitor_desc * target; | 
|---|
|  | 73 |  | 
|---|
|  | 74 | // The parent node to which this criterion belongs | 
|---|
|  | 75 | struct __condition_node_t * owner; | 
|---|
|  | 76 |  | 
|---|
|  | 77 | // Intrusive linked list Next field | 
|---|
|  | 78 | __condition_criterion_t * next; | 
|---|
| [0c78741] | 79 | }; | 
|---|
|  | 80 |  | 
|---|
| [0cf5b79] | 81 | static inline __condition_criterion_t * & get_next( __condition_criterion_t & this ) { | 
|---|
|  | 82 | return this.next; | 
|---|
|  | 83 | } | 
|---|
|  | 84 |  | 
|---|
| [0c78741] | 85 | struct __condition_node_t { | 
|---|
| [025278e] | 86 | // Thread that needs to be woken when all criteria are met | 
|---|
|  | 87 | thread_desc * waiting_thread; | 
|---|
|  | 88 |  | 
|---|
|  | 89 | // Array of criteria (Criterions are contiguous in memory) | 
|---|
|  | 90 | __condition_criterion_t * criteria; | 
|---|
|  | 91 |  | 
|---|
|  | 92 | // Number of criterions in the criteria | 
|---|
| [c1a9c86] | 93 | __lock_size_t count; | 
|---|
| [025278e] | 94 |  | 
|---|
|  | 95 | // Intrusive linked list Next field | 
|---|
|  | 96 | __condition_node_t * next; | 
|---|
|  | 97 |  | 
|---|
|  | 98 | // Custom user info accessible before signalling | 
|---|
|  | 99 | uintptr_t user_info; | 
|---|
| [0c78741] | 100 | }; | 
|---|
|  | 101 |  | 
|---|
| [0cf5b79] | 102 | static inline __condition_node_t * & get_next( __condition_node_t & this ) { | 
|---|
|  | 103 | return this.next; | 
|---|
|  | 104 | } | 
|---|
| [0c78741] | 105 |  | 
|---|
| [59a0bde] | 106 | void ?{}(__condition_node_t & this, thread_desc * waiting_thread, __lock_size_t count, uintptr_t user_info ); | 
|---|
| [e5c8488] | 107 | void ?{}(__condition_criterion_t & this ); | 
|---|
|  | 108 | void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t * owner ); | 
|---|
|  | 109 |  | 
|---|
| [5ea06d6] | 110 | struct condition { | 
|---|
| [025278e] | 111 | // Link list which contains the blocked threads as-well as the information needed to unblock them | 
|---|
| [0cf5b79] | 112 | __queue_t(__condition_node_t) blocked; | 
|---|
| [025278e] | 113 |  | 
|---|
|  | 114 | // Array of monitor pointers (Monitors are NOT contiguous in memory) | 
|---|
|  | 115 | monitor_desc ** monitors; | 
|---|
|  | 116 |  | 
|---|
|  | 117 | // Number of monitors in the array | 
|---|
| [c1a9c86] | 118 | __lock_size_t monitor_count; | 
|---|
| [5ea06d6] | 119 | }; | 
|---|
| [51f3798] | 120 |  | 
|---|
| [242a902] | 121 | static inline void ?{}( condition & this ) { | 
|---|
|  | 122 | this.monitors = NULL; | 
|---|
|  | 123 | this.monitor_count = 0; | 
|---|
| [5ea06d6] | 124 | } | 
|---|
| [2781e65] | 125 |  | 
|---|
| [242a902] | 126 | static inline void ^?{}( condition & this ) { | 
|---|
|  | 127 | free( this.monitors ); | 
|---|
| [1167cd4] | 128 | } | 
|---|
|  | 129 |  | 
|---|
| [8fc45b7] | 130 | void wait        ( condition & this, uintptr_t user_info = 0 ); | 
|---|
|  | 131 | bool signal      ( condition & this ); | 
|---|
|  | 132 | bool signal_block( condition & this ); | 
|---|
|  | 133 | static inline bool is_empty    ( condition & this ) { return !this.blocked.head; } | 
|---|
|  | 134 | uintptr_t front       ( condition & this ); | 
|---|
| [be3d020] | 135 |  | 
|---|
| [97e3296] | 136 | //----------------------------------------------------------------------------- | 
|---|
|  | 137 | // External scheduling | 
|---|
|  | 138 |  | 
|---|
| [c81ebf9] | 139 | struct __acceptable_t { | 
|---|
| [aaa4f93] | 140 | __monitor_group_t; | 
|---|
| [9f5ecf5] | 141 | bool is_dtor; | 
|---|
| [c81ebf9] | 142 | }; | 
|---|
|  | 143 |  | 
|---|
| [6ae8c92] | 144 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration ); | 
|---|
| [c81ebf9] | 145 |  | 
|---|
| [6b0b624] | 146 | // Local Variables: // | 
|---|
|  | 147 | // mode: c // | 
|---|
|  | 148 | // tab-width: 4 // | 
|---|
|  | 149 | // End: // | 
|---|