| [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 | 
 | 
|---|
| [fd54fef] | 24 | trait is_monitor(T &) {
 | 
|---|
| [e84ab3d] | 25 |         monitor$ * get_monitor( T & );
 | 
|---|
| [1dcd9554] | 26 |         void ^?{}( T & mutex );
 | 
|---|
 | 27 | };
 | 
|---|
 | 28 | 
 | 
|---|
| [e84ab3d] | 29 | static inline void ?{}(monitor$ & this) with( this ) {
 | 
|---|
| [c40e7c5] | 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 | 
 | 
|---|
| [e84ab3d] | 41 | static inline void ^?{}(monitor$ & ) {}
 | 
|---|
| [6c3a5ac1] | 42 | 
 | 
|---|
| [cc7f4b1] | 43 | struct monitor_guard_t {
 | 
|---|
| [e84ab3d] | 44 |         monitor$ **     m;
 | 
|---|
| [0cf5b79] | 45 |         __lock_size_t           count;
 | 
|---|
 | 46 |         __monitor_group_t prev;
 | 
|---|
| [51f3798] | 47 | };
 | 
|---|
 | 48 | 
 | 
|---|
| [e84ab3d] | 49 | void ?{}( monitor_guard_t & this, monitor$ ** m, __lock_size_t count, void (*func)() );
 | 
|---|
| [6cebfef] | 50 | void ?{}( monitor_guard_t & this, monitor$ ** m, __lock_size_t count );
 | 
|---|
| [242a902] | 51 | void ^?{}( monitor_guard_t & this );
 | 
|---|
| [51f3798] | 52 | 
 | 
|---|
| [549c006] | 53 | struct monitor_dtor_guard_t {
 | 
|---|
| [e84ab3d] | 54 |         monitor$ *    m;
 | 
|---|
| [0cf5b79] | 55 |         __monitor_group_t prev;
 | 
|---|
| [77a2994] | 56 |         bool join;
 | 
|---|
| [549c006] | 57 | };
 | 
|---|
 | 58 | 
 | 
|---|
| [e84ab3d] | 59 | void ?{}( monitor_dtor_guard_t & this, monitor$ ** m, void (*func)(), bool join );
 | 
|---|
| [549c006] | 60 | void ^?{}( monitor_dtor_guard_t & this );
 | 
|---|
 | 61 | 
 | 
|---|
| [fd54fef] | 62 | static inline forall( T & | sized(T) | { void ^?{}( T & mutex ); } )
 | 
|---|
| [751d963] | 63 | void delete( T * th ) {
 | 
|---|
| [e0b8d66d] | 64 |         if(th) ^(*th){};
 | 
|---|
| [751d963] | 65 |         free( th );
 | 
|---|
 | 66 | }
 | 
|---|
 | 67 | 
 | 
|---|
| [5ea06d6] | 68 | //-----------------------------------------------------------------------------
 | 
|---|
 | 69 | // Internal scheduling
 | 
|---|
| [0c78741] | 70 | 
 | 
|---|
 | 71 | struct __condition_criterion_t {
 | 
|---|
| [025278e] | 72 |         // Whether or not the criterion is met (True if met)
 | 
|---|
 | 73 |         bool ready;
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 |         // The monitor this criterion concerns
 | 
|---|
| [e84ab3d] | 76 |         monitor$ * target;
 | 
|---|
| [025278e] | 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;
 | 
|---|
| [0c78741] | 83 | };
 | 
|---|
 | 84 | 
 | 
|---|
| [0cf5b79] | 85 | static inline __condition_criterion_t * & get_next( __condition_criterion_t & this ) {
 | 
|---|
 | 86 |         return this.next;
 | 
|---|
 | 87 | }
 | 
|---|
 | 88 | 
 | 
|---|
| [0c78741] | 89 | struct __condition_node_t {
 | 
|---|
| [025278e] | 90 |         // Thread that needs to be woken when all criteria are met
 | 
|---|
| [e84ab3d] | 91 |         thread$ * waiting_thread;
 | 
|---|
| [025278e] | 92 | 
 | 
|---|
 | 93 |         // Array of criteria (Criterions are contiguous in memory)
 | 
|---|
 | 94 |         __condition_criterion_t * criteria;
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 |         // Number of criterions in the criteria
 | 
|---|
| [c1a9c86] | 97 |         __lock_size_t count;
 | 
|---|
| [025278e] | 98 | 
 | 
|---|
 | 99 |         // Intrusive linked list Next field
 | 
|---|
 | 100 |         __condition_node_t * next;
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 |         // Custom user info accessible before signalling
 | 
|---|
 | 103 |         uintptr_t user_info;
 | 
|---|
| [0c78741] | 104 | };
 | 
|---|
 | 105 | 
 | 
|---|
| [0cf5b79] | 106 | static inline __condition_node_t * & get_next( __condition_node_t & this ) {
 | 
|---|
 | 107 |         return this.next;
 | 
|---|
 | 108 | }
 | 
|---|
| [0c78741] | 109 | 
 | 
|---|
| [e84ab3d] | 110 | void ?{}(__condition_node_t & this, thread$ * waiting_thread, __lock_size_t count, uintptr_t user_info );
 | 
|---|
| [e5c8488] | 111 | void ?{}(__condition_criterion_t & this );
 | 
|---|
| [e84ab3d] | 112 | void ?{}(__condition_criterion_t & this, monitor$ * target, __condition_node_t * owner );
 | 
|---|
| [e5c8488] | 113 | 
 | 
|---|
| [5ea06d6] | 114 | struct condition {
 | 
|---|
| [025278e] | 115 |         // Link list which contains the blocked threads as-well as the information needed to unblock them
 | 
|---|
| [0cf5b79] | 116 |         __queue_t(__condition_node_t) blocked;
 | 
|---|
| [025278e] | 117 | 
 | 
|---|
 | 118 |         // Array of monitor pointers (Monitors are NOT contiguous in memory)
 | 
|---|
| [e84ab3d] | 119 |         monitor$ ** monitors;
 | 
|---|
| [025278e] | 120 | 
 | 
|---|
 | 121 |         // Number of monitors in the array
 | 
|---|
| [c1a9c86] | 122 |         __lock_size_t monitor_count;
 | 
|---|
| [5ea06d6] | 123 | };
 | 
|---|
| [51f3798] | 124 | 
 | 
|---|
| [242a902] | 125 | static inline void ?{}( condition & this ) {
 | 
|---|
| [121be3e] | 126 |         this.monitors = 0p;
 | 
|---|
| [242a902] | 127 |         this.monitor_count = 0;
 | 
|---|
| [5ea06d6] | 128 | }
 | 
|---|
| [2781e65] | 129 | 
 | 
|---|
| [242a902] | 130 | static inline void ^?{}( condition & this ) {
 | 
|---|
 | 131 |         free( this.monitors );
 | 
|---|
| [1167cd4] | 132 | }
 | 
|---|
 | 133 | 
 | 
|---|
| [8fc45b7] | 134 |               void wait        ( condition & this, uintptr_t user_info = 0 );
 | 
|---|
| [1f58c62] | 135 | static inline bool is_empty    ( condition & this ) { return this.blocked.head == 1p; }
 | 
|---|
| [8fc45b7] | 136 |               bool signal      ( condition & this );
 | 
|---|
 | 137 |               bool signal_block( condition & this );
 | 
|---|
| [1f58c62] | 138 | static inline bool signal_all  ( condition & this ) { bool ret = false; while(!is_empty(this)) { ret = signal(this) || ret; } return ret; }
 | 
|---|
| [8fc45b7] | 139 |          uintptr_t front       ( condition & this );
 | 
|---|
| [be3d020] | 140 | 
 | 
|---|
| [97e3296] | 141 | //-----------------------------------------------------------------------------
 | 
|---|
 | 142 | // External scheduling
 | 
|---|
 | 143 | 
 | 
|---|
| [c81ebf9] | 144 | struct __acceptable_t {
 | 
|---|
| [b1672e1] | 145 |         inline struct __monitor_group_t;
 | 
|---|
| [9f5ecf5] | 146 |         bool is_dtor;
 | 
|---|
| [c81ebf9] | 147 | };
 | 
|---|
 | 148 | 
 | 
|---|
| [6ae8c92] | 149 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration );
 | 
|---|
| [c81ebf9] | 150 | 
 | 
|---|
| [af67ee1] | 151 | // lock and unlock routines for mutex statements to use
 | 
|---|
 | 152 | void lock( monitor$ * this );
 | 
|---|
 | 153 | void unlock( monitor$ * this );
 | 
|---|
 | 154 | 
 | 
|---|
| [6b0b624] | 155 | // Local Variables: //
 | 
|---|
 | 156 | // mode: c //
 | 
|---|
 | 157 | // tab-width: 4 //
 | 
|---|
| [6cebfef] | 158 | // End: //
 | 
|---|