[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 | |
---|
[84c52a8] | 24 | static inline void ?{}(monitor_desc * this) { |
---|
[690f13c] | 25 | this->owner = NULL; |
---|
[cc7f4b1] | 26 | this->recursion = 0; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | struct monitor_guard_t { |
---|
[84c52a8] | 30 | monitor_desc ** m; |
---|
[2781e65] | 31 | int count; |
---|
[4aa2fb2] | 32 | monitor_desc ** prev_mntrs; |
---|
| 33 | unsigned short prev_count; |
---|
[51f3798] | 34 | }; |
---|
| 35 | |
---|
[84c52a8] | 36 | static inline int ?<?(monitor_desc* lhs, monitor_desc* rhs) { |
---|
[2781e65] | 37 | return ((intptr_t)lhs) < ((intptr_t)rhs); |
---|
| 38 | } |
---|
| 39 | |
---|
[5ea06d6] | 40 | void ?{}( monitor_guard_t * this, monitor_desc ** m, int count ); |
---|
| 41 | void ^?{}( monitor_guard_t * this ); |
---|
[51f3798] | 42 | |
---|
[5ea06d6] | 43 | //----------------------------------------------------------------------------- |
---|
| 44 | // Internal scheduling |
---|
[0c78741] | 45 | |
---|
| 46 | struct __condition_criterion_t { |
---|
| 47 | bool ready; //Whether or not the criterion is met (True if met) |
---|
| 48 | monitor_desc * target; //The monitor this criterion concerns |
---|
| 49 | struct __condition_node_t * owner; //The parent node to which this criterion belongs |
---|
| 50 | __condition_criterion_t * next; //Intrusive linked list Next field |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | struct __condition_node_t { |
---|
| 54 | thread_desc * waiting_thread; //Thread that needs to be woken when all criteria are met |
---|
| 55 | __condition_criterion_t * criteria; //Array of criteria (Criterions are contiguous in memory) |
---|
| 56 | unsigned short count; //Number of criterions in the criteria |
---|
| 57 | __condition_node_t * next; //Intrusive linked list Next field |
---|
[be3d020] | 58 | uintptr_t user_info; //Custom user info accessible before signalling |
---|
[0c78741] | 59 | }; |
---|
| 60 | |
---|
| 61 | struct __condition_blocked_queue_t { |
---|
| 62 | __condition_node_t * head; |
---|
| 63 | __condition_node_t ** tail; |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | void ?{}( __condition_blocked_queue_t * ); |
---|
| 67 | void append( __condition_blocked_queue_t *, __condition_node_t * ); |
---|
| 68 | __condition_node_t * pop_head( __condition_blocked_queue_t * ); |
---|
| 69 | |
---|
[5ea06d6] | 70 | struct condition { |
---|
[0c78741] | 71 | __condition_blocked_queue_t blocked; //Link list which contains the blocked threads as-well as the information needed to unblock them |
---|
| 72 | monitor_desc ** monitors; //Array of monitor pointers (Monitors are NOT contiguous in memory) |
---|
| 73 | unsigned short monitor_count; //Number of monitors in the array |
---|
[5ea06d6] | 74 | }; |
---|
[51f3798] | 75 | |
---|
[5ea06d6] | 76 | static inline void ?{}( condition * this ) { |
---|
| 77 | this->monitors = NULL; |
---|
| 78 | this->monitor_count = 0; |
---|
| 79 | } |
---|
[2781e65] | 80 | |
---|
[1167cd4] | 81 | static inline void ^?{}( condition * this ) { |
---|
| 82 | free( this->monitors ); |
---|
| 83 | } |
---|
| 84 | |
---|
[be3d020] | 85 | void wait( condition * this, uintptr_t user_info = 0 ); |
---|
| 86 | bool signal( condition * this ); |
---|
| 87 | bool signal_block( condition * this ); |
---|
| 88 | static inline bool is_empty( condition * this ) { return !this->blocked.head; } |
---|
| 89 | uintptr_t front( condition * this ); |
---|
| 90 | |
---|
[c81ebf9] | 91 | struct __acceptable_t { |
---|
| 92 | void (*func)(void); |
---|
| 93 | unsigned short count; |
---|
| 94 | monitor_desc * monitors[1]; |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) ); |
---|
| 98 | |
---|
[6b0b624] | 99 | // Local Variables: // |
---|
| 100 | // mode: c // |
---|
| 101 | // tab-width: 4 // |
---|
| 102 | // End: // |
---|