[ab1b971] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2021 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 | // locks.hfa -- PUBLIC |
---|
| 8 | // Runtime locks that used with the runtime thread system. |
---|
| 9 | // |
---|
| 10 | // Author : Colby Alexander Parsons |
---|
| 11 | // Created On : Thu Jan 21 19:46:50 2021 |
---|
| 12 | // Last Modified By : |
---|
| 13 | // Last Modified On : |
---|
| 14 | // Update Count : |
---|
| 15 | // |
---|
| 16 | |
---|
[f4e35326] | 17 | #pragma once |
---|
| 18 | |
---|
[848439f] | 19 | #include <stdbool.h> |
---|
| 20 | |
---|
[ab1b971] | 21 | #include "bits/weakso_locks.hfa" |
---|
[848439f] | 22 | |
---|
| 23 | #include "time_t.hfa" |
---|
| 24 | #include "time.hfa" |
---|
| 25 | |
---|
[ab1b971] | 26 | //---------- |
---|
| 27 | struct single_acquisition_lock { |
---|
| 28 | inline blocking_lock; |
---|
| 29 | }; |
---|
| 30 | |
---|
| 31 | static inline void ?{}( single_acquisition_lock & this ) {((blocking_lock &)this){ false, false };} |
---|
| 32 | static inline void ^?{}( single_acquisition_lock & this ) {} |
---|
| 33 | static inline void lock ( single_acquisition_lock & this ) { lock ( (blocking_lock &)this ); } |
---|
| 34 | static inline void unlock ( single_acquisition_lock & this ) { unlock ( (blocking_lock &)this ); } |
---|
| 35 | static inline void on_wait ( single_acquisition_lock & this ) { on_wait( (blocking_lock &)this ); } |
---|
| 36 | static inline void on_notify ( single_acquisition_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); } |
---|
| 37 | static inline void set_recursion_count( single_acquisition_lock & this, size_t recursion ) { set_recursion_count( (blocking_lock &)this, recursion ); } |
---|
| 38 | static inline size_t get_recursion_count( single_acquisition_lock & this ) { return get_recursion_count( (blocking_lock &)this ); } |
---|
| 39 | |
---|
| 40 | //---------- |
---|
| 41 | struct owner_lock { |
---|
| 42 | inline blocking_lock; |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | static inline void ?{}( owner_lock & this ) {((blocking_lock &)this){ true, true };} |
---|
| 46 | static inline void ^?{}( owner_lock & this ) {} |
---|
| 47 | static inline void lock ( owner_lock & this ) { lock ( (blocking_lock &)this ); } |
---|
| 48 | static inline void unlock ( owner_lock & this ) { unlock ( (blocking_lock &)this ); } |
---|
| 49 | static inline void on_wait ( owner_lock & this ) { on_wait( (blocking_lock &)this ); } |
---|
| 50 | static inline void on_notify( owner_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); } |
---|
| 51 | static inline void set_recursion_count( owner_lock & this, size_t recursion ) { set_recursion_count( (blocking_lock &)this, recursion ); } |
---|
| 52 | static inline size_t get_recursion_count( owner_lock & this ) { return get_recursion_count( (blocking_lock &)this ); } |
---|
| 53 | |
---|
[ac5816d] | 54 | //----------------------------------------------------------------------------- |
---|
| 55 | // is_blocking_lock |
---|
[fd54fef] | 56 | trait is_blocking_lock(L & | sized(L)) { |
---|
[ac5816d] | 57 | // For synchronization locks to use when acquiring |
---|
| 58 | void on_notify( L &, struct $thread * ); |
---|
| 59 | |
---|
| 60 | // For synchronization locks to use when releasing |
---|
| 61 | void on_wait( L & ); |
---|
[848439f] | 62 | |
---|
[ac5816d] | 63 | // to get recursion count for cond lock to reset after waking |
---|
| 64 | size_t get_recursion_count( L & ); |
---|
| 65 | |
---|
| 66 | // to set recursion count after getting signalled; |
---|
| 67 | void set_recursion_count( L &, size_t recursion ); |
---|
| 68 | }; |
---|
[848439f] | 69 | |
---|
[ac5816d] | 70 | //----------------------------------------------------------------------------- |
---|
| 71 | // info_thread |
---|
[797a193] | 72 | // the info thread is a wrapper around a thread used |
---|
| 73 | // to store extra data for use in the condition variable |
---|
[fd54fef] | 74 | forall(L & | is_blocking_lock(L)) { |
---|
[ac5816d] | 75 | struct info_thread; |
---|
[c131a02] | 76 | |
---|
[797a193] | 77 | // for use by sequence |
---|
[c131a02] | 78 | info_thread(L) *& Back( info_thread(L) * this ); |
---|
| 79 | info_thread(L) *& Next( info_thread(L) * this ); |
---|
[848439f] | 80 | } |
---|
| 81 | |
---|
[ac5816d] | 82 | //----------------------------------------------------------------------------- |
---|
| 83 | // Synchronization Locks |
---|
[fd54fef] | 84 | forall(L & | is_blocking_lock(L)) { |
---|
[eeb5023] | 85 | struct condition_variable { |
---|
[848439f] | 86 | // Spin lock used for mutual exclusion |
---|
| 87 | __spinlock_t lock; |
---|
| 88 | |
---|
| 89 | // List of blocked threads |
---|
[cad1df1] | 90 | Sequence( info_thread(L) ) blocked_threads; |
---|
[848439f] | 91 | |
---|
| 92 | // Count of current blocked threads |
---|
| 93 | int count; |
---|
| 94 | }; |
---|
| 95 | |
---|
[ac5816d] | 96 | void ?{}( condition_variable(L) & this ); |
---|
[848439f] | 97 | void ^?{}( condition_variable(L) & this ); |
---|
| 98 | |
---|
[eeb5023] | 99 | bool notify_one( condition_variable(L) & this ); |
---|
| 100 | bool notify_all( condition_variable(L) & this ); |
---|
[848439f] | 101 | |
---|
[eeb5023] | 102 | uintptr_t front( condition_variable(L) & this ); |
---|
[848439f] | 103 | |
---|
[ac5816d] | 104 | bool empty ( condition_variable(L) & this ); |
---|
| 105 | int counter( condition_variable(L) & this ); |
---|
[848439f] | 106 | |
---|
[eeb5023] | 107 | void wait( condition_variable(L) & this ); |
---|
| 108 | void wait( condition_variable(L) & this, uintptr_t info ); |
---|
[dff1fd1] | 109 | bool wait( condition_variable(L) & this, Duration duration ); |
---|
| 110 | bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ); |
---|
| 111 | bool wait( condition_variable(L) & this, Time time ); |
---|
| 112 | bool wait( condition_variable(L) & this, uintptr_t info, Time time ); |
---|
[848439f] | 113 | |
---|
[eeb5023] | 114 | void wait( condition_variable(L) & this, L & l ); |
---|
| 115 | void wait( condition_variable(L) & this, L & l, uintptr_t info ); |
---|
[dff1fd1] | 116 | bool wait( condition_variable(L) & this, L & l, Duration duration ); |
---|
| 117 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ); |
---|
| 118 | bool wait( condition_variable(L) & this, L & l, Time time ); |
---|
| 119 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Time time ); |
---|
[eeb5023] | 120 | } |
---|
[454f478] | 121 | |
---|
| 122 | //----------------------------------------------------------------------------- |
---|
| 123 | // Semaphore |
---|
| 124 | struct semaphore { |
---|
| 125 | __spinlock_t lock; |
---|
| 126 | int count; |
---|
| 127 | __queue_t($thread) waiting; |
---|
| 128 | }; |
---|
| 129 | |
---|
| 130 | void ?{}(semaphore & this, int count = 1); |
---|
| 131 | void ^?{}(semaphore & this); |
---|
| 132 | bool P (semaphore & this); |
---|
| 133 | bool V (semaphore & this); |
---|
| 134 | bool V (semaphore & this, unsigned count); |
---|