| [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
 | 
|---|
| [6b0b624] | 12 | // Last Modified On : Sat Jul 22 09:59:01 2017
 | 
|---|
 | 13 | // Update Count     : 3
 | 
|---|
| [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 | 
 | 
|---|
| [242a902] | 29 | static inline void ?{}(monitor_desc & this) {
 | 
|---|
| [6b224a52] | 30 |         (this.lock){};
 | 
|---|
| [242a902] | 31 |         this.owner = NULL;
 | 
|---|
| [6b224a52] | 32 |         (this.entry_queue){};
 | 
|---|
 | 33 |         (this.signal_stack){};
 | 
|---|
| [242a902] | 34 |         this.recursion = 0;
 | 
|---|
| [6ae8c92] | 35 |         this.mask.accepted = NULL;
 | 
|---|
 | 36 |         this.mask.clauses  = NULL;
 | 
|---|
 | 37 |         this.mask.size     = 0;
 | 
|---|
| [cc7f4b1] | 38 | }
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | struct monitor_guard_t {
 | 
|---|
| [84c52a8] | 41 |         monitor_desc ** m;
 | 
|---|
| [2781e65] | 42 |         int count;
 | 
|---|
| [4aa2fb2] | 43 |         monitor_desc ** prev_mntrs;
 | 
|---|
 | 44 |         unsigned short  prev_count;
 | 
|---|
| [90c4df0] | 45 |         fptr_t          prev_func;
 | 
|---|
| [51f3798] | 46 | };
 | 
|---|
 | 47 | 
 | 
|---|
| [84c52a8] | 48 | static inline int ?<?(monitor_desc* lhs, monitor_desc* rhs) {
 | 
|---|
| [2781e65] | 49 |         return ((intptr_t)lhs) < ((intptr_t)rhs);
 | 
|---|
 | 50 | }
 | 
|---|
 | 51 | 
 | 
|---|
| [bf7b9da7] | 52 | void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, void (*func)() );
 | 
|---|
| [242a902] | 53 | void ^?{}( monitor_guard_t & this );
 | 
|---|
| [51f3798] | 54 | 
 | 
|---|
| [5ea06d6] | 55 | //-----------------------------------------------------------------------------
 | 
|---|
 | 56 | // Internal scheduling
 | 
|---|
| [0c78741] | 57 | 
 | 
|---|
 | 58 | struct __condition_criterion_t {
 | 
|---|
 | 59 |         bool ready;                                             //Whether or not the criterion is met (True if met)
 | 
|---|
 | 60 |         monitor_desc * target;                          //The monitor this criterion concerns
 | 
|---|
 | 61 |         struct __condition_node_t * owner;              //The parent node to which this criterion belongs
 | 
|---|
 | 62 |         __condition_criterion_t * next;         //Intrusive linked list Next field
 | 
|---|
 | 63 | };
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | struct __condition_node_t {
 | 
|---|
 | 66 |         thread_desc * waiting_thread;                   //Thread that needs to be woken when all criteria are met
 | 
|---|
 | 67 |         __condition_criterion_t * criteria;     //Array of criteria (Criterions are contiguous in memory)
 | 
|---|
 | 68 |         unsigned short count;                           //Number of criterions in the criteria
 | 
|---|
 | 69 |         __condition_node_t * next;                      //Intrusive linked list Next field
 | 
|---|
| [be3d020] | 70 |         uintptr_t user_info;                            //Custom user info accessible before signalling
 | 
|---|
| [0c78741] | 71 | };
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 | struct __condition_blocked_queue_t {
 | 
|---|
 | 74 |         __condition_node_t * head;
 | 
|---|
 | 75 |         __condition_node_t ** tail;
 | 
|---|
 | 76 | };
 | 
|---|
 | 77 | 
 | 
|---|
| [242a902] | 78 | void ?{}( __condition_blocked_queue_t & );
 | 
|---|
| [0c78741] | 79 | void append( __condition_blocked_queue_t *, __condition_node_t * );
 | 
|---|
 | 80 | __condition_node_t * pop_head( __condition_blocked_queue_t * );
 | 
|---|
 | 81 | 
 | 
|---|
| [5ea06d6] | 82 | struct condition {
 | 
|---|
| [0c78741] | 83 |         __condition_blocked_queue_t blocked;    //Link list which contains the blocked threads as-well as the information needed to unblock them
 | 
|---|
 | 84 |         monitor_desc ** monitors;                       //Array of monitor pointers (Monitors are NOT contiguous in memory)
 | 
|---|
 | 85 |         unsigned short monitor_count;                   //Number of monitors in the array
 | 
|---|
| [5ea06d6] | 86 | };
 | 
|---|
| [51f3798] | 87 | 
 | 
|---|
| [242a902] | 88 | static inline void ?{}( condition & this ) {
 | 
|---|
 | 89 |         this.monitors = NULL;
 | 
|---|
 | 90 |         this.monitor_count = 0;
 | 
|---|
| [5ea06d6] | 91 | }
 | 
|---|
| [2781e65] | 92 | 
 | 
|---|
| [242a902] | 93 | static inline void ^?{}( condition & this ) {
 | 
|---|
 | 94 |         free( this.monitors );
 | 
|---|
| [1167cd4] | 95 | }
 | 
|---|
 | 96 | 
 | 
|---|
| [be3d020] | 97 | void wait( condition * this, uintptr_t user_info = 0 );
 | 
|---|
 | 98 | bool signal( condition * this );
 | 
|---|
 | 99 | bool signal_block( condition * this );
 | 
|---|
 | 100 | static inline bool is_empty( condition * this ) { return !this->blocked.head; }
 | 
|---|
 | 101 | uintptr_t front( condition * this );
 | 
|---|
 | 102 | 
 | 
|---|
| [97e3296] | 103 | //-----------------------------------------------------------------------------
 | 
|---|
 | 104 | // External scheduling
 | 
|---|
 | 105 | 
 | 
|---|
| [c81ebf9] | 106 | struct __acceptable_t {
 | 
|---|
| [6ae8c92] | 107 |         __monitor_group_t monitors;
 | 
|---|
| [9f5ecf5] | 108 |         bool is_dtor;
 | 
|---|
| [c81ebf9] | 109 | };
 | 
|---|
 | 110 | 
 | 
|---|
| [6ae8c92] | 111 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration );
 | 
|---|
| [c81ebf9] | 112 | 
 | 
|---|
| [6b0b624] | 113 | // Local Variables: //
 | 
|---|
 | 114 | // mode: c //
 | 
|---|
 | 115 | // tab-width: 4 //
 | 
|---|
 | 116 | // End: //
 | 
|---|