| 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 Jul 22 09:59:01 2017 | 
|---|
| 13 | // Update Count     : 3 | 
|---|
| 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 | // static inline int ?<?(monitor_desc* lhs, monitor_desc* rhs) { | 
|---|
| 42 | //      return ((intptr_t)lhs) < ((intptr_t)rhs); | 
|---|
| 43 | // } | 
|---|
| 44 |  | 
|---|
| 45 | struct monitor_guard_t { | 
|---|
| 46 | monitor_desc ** m; | 
|---|
| 47 | int count; | 
|---|
| 48 | monitor_desc ** prev_mntrs; | 
|---|
| 49 | unsigned short  prev_count; | 
|---|
| 50 | fptr_t          prev_func; | 
|---|
| 51 | }; | 
|---|
| 52 |  | 
|---|
| 53 | void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, void (*func)() ); | 
|---|
| 54 | void ^?{}( monitor_guard_t & this ); | 
|---|
| 55 |  | 
|---|
| 56 |  | 
|---|
| 57 | struct monitor_dtor_guard_t { | 
|---|
| 58 | monitor_desc * m; | 
|---|
| 59 | monitor_desc ** prev_mntrs; | 
|---|
| 60 | unsigned short  prev_count; | 
|---|
| 61 | fptr_t          prev_func; | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | void ?{}( monitor_dtor_guard_t & this, monitor_desc ** m, void (*func)() ); | 
|---|
| 65 | void ^?{}( monitor_dtor_guard_t & this ); | 
|---|
| 66 |  | 
|---|
| 67 | //----------------------------------------------------------------------------- | 
|---|
| 68 | // Internal scheduling | 
|---|
| 69 |  | 
|---|
| 70 | struct __condition_criterion_t { | 
|---|
| 71 | bool ready;                                             //Whether or not the criterion is met (True if met) | 
|---|
| 72 | monitor_desc * target;                          //The monitor this criterion concerns | 
|---|
| 73 | struct __condition_node_t * owner;              //The parent node to which this criterion belongs | 
|---|
| 74 | __condition_criterion_t * next;         //Intrusive linked list Next field | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | struct __condition_node_t { | 
|---|
| 78 | thread_desc * waiting_thread;                   //Thread that needs to be woken when all criteria are met | 
|---|
| 79 | __condition_criterion_t * criteria;     //Array of criteria (Criterions are contiguous in memory) | 
|---|
| 80 | unsigned short count;                           //Number of criterions in the criteria | 
|---|
| 81 | __condition_node_t * next;                      //Intrusive linked list Next field | 
|---|
| 82 | uintptr_t user_info;                            //Custom user info accessible before signalling | 
|---|
| 83 | }; | 
|---|
| 84 |  | 
|---|
| 85 | struct __condition_blocked_queue_t { | 
|---|
| 86 | __condition_node_t * head; | 
|---|
| 87 | __condition_node_t ** tail; | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 | void ?{}( __condition_blocked_queue_t & ); | 
|---|
| 91 | void append( __condition_blocked_queue_t *, __condition_node_t * ); | 
|---|
| 92 | __condition_node_t * pop_head( __condition_blocked_queue_t * ); | 
|---|
| 93 |  | 
|---|
| 94 | struct condition { | 
|---|
| 95 | __condition_blocked_queue_t blocked;    //Link list which contains the blocked threads as-well as the information needed to unblock them | 
|---|
| 96 | monitor_desc ** monitors;                       //Array of monitor pointers (Monitors are NOT contiguous in memory) | 
|---|
| 97 | unsigned short monitor_count;                   //Number of monitors in the array | 
|---|
| 98 | }; | 
|---|
| 99 |  | 
|---|
| 100 | static inline void ?{}( condition & this ) { | 
|---|
| 101 | this.monitors = NULL; | 
|---|
| 102 | this.monitor_count = 0; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | static inline void ^?{}( condition & this ) { | 
|---|
| 106 | free( this.monitors ); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | void wait( condition * this, uintptr_t user_info = 0 ); | 
|---|
| 110 | bool signal( condition * this ); | 
|---|
| 111 | bool signal_block( condition * this ); | 
|---|
| 112 | static inline bool is_empty( condition * this ) { return !this->blocked.head; } | 
|---|
| 113 | uintptr_t front( condition * this ); | 
|---|
| 114 |  | 
|---|
| 115 | //----------------------------------------------------------------------------- | 
|---|
| 116 | // External scheduling | 
|---|
| 117 |  | 
|---|
| 118 | struct __acceptable_t { | 
|---|
| 119 | __monitor_group_t; | 
|---|
| 120 | bool is_dtor; | 
|---|
| 121 | }; | 
|---|
| 122 |  | 
|---|
| 123 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration ); | 
|---|
| 124 |  | 
|---|
| 125 | // Local Variables: // | 
|---|
| 126 | // mode: c // | 
|---|
| 127 | // tab-width: 4 // | 
|---|
| 128 | // End: // | 
|---|