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