[09800e9] | 1 | |
---|
| 2 | // -*- Mode: CFA -*- |
---|
| 3 | // |
---|
| 4 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 5 | // |
---|
| 6 | // The contents of this file are covered under the licence agreement in the |
---|
| 7 | // file "LICENCE" distributed with Cforall. |
---|
| 8 | // |
---|
| 9 | // mutex -- |
---|
| 10 | // |
---|
| 11 | // Author : Thierry Delisle |
---|
| 12 | // Created On : Fri May 25 01:24:09 2018 |
---|
[121be3e] | 13 | // Last Modified By : Peter A. Buhr |
---|
| 14 | // Last Modified On : Wed Dec 4 09:16:53 2019 |
---|
| 15 | // Update Count : 1 |
---|
[09800e9] | 16 | // |
---|
| 17 | |
---|
| 18 | #pragma once |
---|
| 19 | |
---|
| 20 | #include <stdbool.h> |
---|
| 21 | |
---|
[58b6d1b] | 22 | #include "bits/algorithm.hfa" |
---|
[73abe95] | 23 | #include "bits/locks.hfa" |
---|
[09800e9] | 24 | |
---|
| 25 | #include "invoke.h" |
---|
[73abe95] | 26 | #include "time_t.hfa" |
---|
[09800e9] | 27 | |
---|
| 28 | //----------------------------------------------------------------------------- |
---|
| 29 | // Locks |
---|
| 30 | |
---|
| 31 | // Exclusive lock - non-recursive |
---|
| 32 | // --- |
---|
| 33 | struct mutex_lock { |
---|
| 34 | // Spin lock used for mutual exclusion |
---|
| 35 | __spinlock_t lock; |
---|
| 36 | |
---|
| 37 | // List of blocked threads |
---|
[ac2b598] | 38 | __queue_t(struct $thread) blocked_threads; |
---|
[09800e9] | 39 | |
---|
| 40 | // Locked flag |
---|
| 41 | bool is_locked; |
---|
| 42 | }; |
---|
| 43 | |
---|
[0edd11a] | 44 | void ?{}(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 45 | void ^?{}(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 46 | void lock(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 47 | bool try_lock(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 48 | void unlock(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
[09800e9] | 49 | |
---|
| 50 | // Exclusive lock - recursive |
---|
| 51 | // --- |
---|
| 52 | struct recursive_mutex_lock{ |
---|
| 53 | // Spin lock used for mutual exclusion |
---|
| 54 | __spinlock_t lock; |
---|
| 55 | |
---|
| 56 | // List of blocked threads |
---|
[ac2b598] | 57 | __queue_t(struct $thread) blocked_threads; |
---|
[09800e9] | 58 | |
---|
| 59 | // Current thread owning the lock |
---|
[ac2b598] | 60 | struct $thread * owner; |
---|
[09800e9] | 61 | |
---|
| 62 | // Number of recursion level |
---|
| 63 | size_t recursion_count; |
---|
| 64 | }; |
---|
| 65 | |
---|
[0edd11a] | 66 | void ?{}(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 67 | void ^?{}(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 68 | void lock(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 69 | bool try_lock(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 70 | void unlock(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
[09800e9] | 71 | |
---|
| 72 | trait is_lock(dtype L | sized(L)) { |
---|
| 73 | void lock (L &); |
---|
| 74 | void unlock(L &); |
---|
| 75 | }; |
---|
| 76 | |
---|
| 77 | //----------------------------------------------------------------------------- |
---|
| 78 | // Condition variables |
---|
| 79 | |
---|
| 80 | struct condition_variable { |
---|
| 81 | // Spin lock used for mutual exclusion |
---|
| 82 | __spinlock_t lock; |
---|
| 83 | |
---|
| 84 | // List of blocked threads |
---|
[ac2b598] | 85 | __queue_t(struct $thread) blocked_threads; |
---|
[09800e9] | 86 | }; |
---|
| 87 | |
---|
[0edd11a] | 88 | void ?{}(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 89 | void ^?{}(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
[09800e9] | 90 | |
---|
[0edd11a] | 91 | void notify_one(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
| 92 | void notify_all(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
[09800e9] | 93 | |
---|
[0edd11a] | 94 | void wait(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
[09800e9] | 95 | |
---|
| 96 | forall(dtype L | is_lock(L)) |
---|
[0edd11a] | 97 | void wait(condition_variable & this, L & l) __attribute__((deprecated("use concurrency/locks.hfa instead"))); |
---|
[09800e9] | 98 | |
---|
| 99 | //----------------------------------------------------------------------------- |
---|
| 100 | // Scopes |
---|
| 101 | forall(dtype L | is_lock(L)) { |
---|
| 102 | #if !defined( __TUPLE_ARRAYS_EXIST__ ) |
---|
| 103 | void lock ( L * locks [], size_t count); |
---|
| 104 | void unlock( L * locks [], size_t count); |
---|
| 105 | |
---|
| 106 | struct lock_scope { |
---|
| 107 | L ** locks; |
---|
| 108 | size_t count; |
---|
| 109 | }; |
---|
| 110 | |
---|
| 111 | static inline void ?{}(lock_scope(L) & this) { |
---|
[121be3e] | 112 | this.locks = 0p; |
---|
[09800e9] | 113 | this.count = 0; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | static inline void ^?{}(lock_scope(L) & this) { |
---|
| 117 | if(this.count > 0) { |
---|
| 118 | unlock(this.locks, this.count); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | static inline lock_scope(L) lock( L * locks [], size_t count, lock_scope(L) & scope) { |
---|
| 123 | lock(locks, count); |
---|
| 124 | scope.locks = locks; |
---|
| 125 | scope.count = count; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | static inline void unlock( lock_scope(L) & this ) { |
---|
| 129 | unlock(this.locks, this.count); |
---|
| 130 | this.count = 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | static inline void release( lock_scope(L) & this ) { |
---|
| 134 | this.count = 0; |
---|
| 135 | } |
---|
| 136 | #else |
---|
| 137 | void lock( [L &...] locks ); |
---|
| 138 | void unlock( [L &...] locks ); |
---|
| 139 | |
---|
| 140 | forall(size_t N) |
---|
| 141 | struct lock_scope { |
---|
| 142 | bool released; |
---|
| 143 | [L &... N] locks; |
---|
| 144 | }; |
---|
| 145 | |
---|
| 146 | void ?{}(lock_scope(L) & this) = void; |
---|
| 147 | void ?{}(lock_scope(L) & this, lock_scope(L) other) = void; |
---|
| 148 | void ?move?(lock_scope(L) & this, lock_scope(L) & other) = default; |
---|
| 149 | |
---|
| 150 | static inline void ^?{}(lock_scope(L) & this) { |
---|
| 151 | if( !this.released ) { |
---|
| 152 | unlock(this.locks); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | forall(size_t N) |
---|
| 157 | static inline lock_scope(L, N) lock( [L &...] locks ) { |
---|
| 158 | lock(locks); |
---|
| 159 | return @{false, locks}; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | static inline void unlock( lock_scope(L) & this ) { |
---|
| 163 | unlock(this.locks); |
---|
| 164 | this.released = true |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | static inline void release( lock_scope(L) & this ) { |
---|
| 168 | this.released = true; |
---|
| 169 | } |
---|
| 170 | #endif |
---|
| 171 | } |
---|