[f07e037] | 1 | // -*- Mode: CFA -*-
|
---|
| 2 | //
|
---|
| 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
| 4 | //
|
---|
| 5 | // The contents of this file are covered under the licence agreement in the
|
---|
| 6 | // file "LICENCE" distributed with Cforall.
|
---|
| 7 | //
|
---|
| 8 | // monitor --
|
---|
| 9 | //
|
---|
| 10 | // Author : Thierry Delisle
|
---|
| 11 | // Created On : Thd Feb 23 12:27:26 2017
|
---|
| 12 | // Last Modified By : Thierry Delisle
|
---|
| 13 | // Last Modified On : --
|
---|
| 14 | // Update Count : 0
|
---|
| 15 | //
|
---|
| 16 |
|
---|
| 17 | #ifndef MONITOR_H
|
---|
| 18 | #define MONITOR_H
|
---|
| 19 |
|
---|
[690f13c] | 20 | #include <stddef.h>
|
---|
| 21 |
|
---|
[f07e037] | 22 | #include "assert"
|
---|
| 23 | #include "invoke.h"
|
---|
[2781e65] | 24 | #include "stdlib"
|
---|
[f07e037] | 25 |
|
---|
[84c52a8] | 26 | static inline void ?{}(monitor_desc * this) {
|
---|
[690f13c] | 27 | this->owner = NULL;
|
---|
[cc7f4b1] | 28 | this->recursion = 0;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | struct monitor_guard_t {
|
---|
[84c52a8] | 32 | monitor_desc ** m;
|
---|
[2781e65] | 33 | int count;
|
---|
[4aa2fb2] | 34 | monitor_desc ** prev_mntrs;
|
---|
| 35 | unsigned short prev_count;
|
---|
[51f3798] | 36 | };
|
---|
| 37 |
|
---|
[84c52a8] | 38 | static inline int ?<?(monitor_desc* lhs, monitor_desc* rhs) {
|
---|
[2781e65] | 39 | return ((intptr_t)lhs) < ((intptr_t)rhs);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[5ea06d6] | 42 | void ?{}( monitor_guard_t * this, monitor_desc ** m, int count );
|
---|
| 43 | void ^?{}( monitor_guard_t * this );
|
---|
[51f3798] | 44 |
|
---|
[5ea06d6] | 45 | //-----------------------------------------------------------------------------
|
---|
| 46 | // Internal scheduling
|
---|
[0c78741] | 47 |
|
---|
| 48 | struct __condition_criterion_t {
|
---|
| 49 | bool ready; //Whether or not the criterion is met (True if met)
|
---|
| 50 | monitor_desc * target; //The monitor this criterion concerns
|
---|
| 51 | struct __condition_node_t * owner; //The parent node to which this criterion belongs
|
---|
| 52 | __condition_criterion_t * next; //Intrusive linked list Next field
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | struct __condition_node_t {
|
---|
| 56 | thread_desc * waiting_thread; //Thread that needs to be woken when all criteria are met
|
---|
| 57 | __condition_criterion_t * criteria; //Array of criteria (Criterions are contiguous in memory)
|
---|
| 58 | unsigned short count; //Number of criterions in the criteria
|
---|
| 59 | __condition_node_t * next; //Intrusive linked list Next field
|
---|
[be3d020] | 60 | uintptr_t user_info; //Custom user info accessible before signalling
|
---|
[0c78741] | 61 | };
|
---|
| 62 |
|
---|
| 63 | struct __condition_blocked_queue_t {
|
---|
| 64 | __condition_node_t * head;
|
---|
| 65 | __condition_node_t ** tail;
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | void ?{}( __condition_blocked_queue_t * );
|
---|
| 69 | void append( __condition_blocked_queue_t *, __condition_node_t * );
|
---|
| 70 | __condition_node_t * pop_head( __condition_blocked_queue_t * );
|
---|
| 71 |
|
---|
[5ea06d6] | 72 | struct condition {
|
---|
[0c78741] | 73 | __condition_blocked_queue_t blocked; //Link list which contains the blocked threads as-well as the information needed to unblock them
|
---|
| 74 | monitor_desc ** monitors; //Array of monitor pointers (Monitors are NOT contiguous in memory)
|
---|
| 75 | unsigned short monitor_count; //Number of monitors in the array
|
---|
[5ea06d6] | 76 | };
|
---|
[51f3798] | 77 |
|
---|
[5ea06d6] | 78 | static inline void ?{}( condition * this ) {
|
---|
| 79 | this->monitors = NULL;
|
---|
| 80 | this->monitor_count = 0;
|
---|
| 81 | }
|
---|
[2781e65] | 82 |
|
---|
[1167cd4] | 83 | static inline void ^?{}( condition * this ) {
|
---|
| 84 | free( this->monitors );
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[be3d020] | 87 | void wait( condition * this, uintptr_t user_info = 0 );
|
---|
| 88 | bool signal( condition * this );
|
---|
| 89 | bool signal_block( condition * this );
|
---|
| 90 | static inline bool is_empty( condition * this ) { return !this->blocked.head; }
|
---|
| 91 | uintptr_t front( condition * this );
|
---|
| 92 |
|
---|
[c81ebf9] | 93 | struct __acceptable_t {
|
---|
| 94 | void (*func)(void);
|
---|
| 95 | unsigned short count;
|
---|
| 96 | monitor_desc * monitors[1];
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 | void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) );
|
---|
| 100 |
|
---|
[4aa2fb2] | 101 | #endif //MONITOR_H
|
---|