source: libcfa/src/concurrency/locks.hfa @ 22b7579

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 22b7579 was 22b7579, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Changed is_blocking_lock trait to use on_wakeup.
Fixed fast_lock to work with conditions.

  • Property mode set to 100644
File size: 10.0 KB
Line 
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
17#pragma once
18
19#include <stdbool.h>
20
21#include "bits/weakso_locks.hfa"
22#include "containers/queueLockFree.hfa"
23
24#include "thread.hfa"
25
26#include "time_t.hfa"
27#include "time.hfa"
28
29//-----------------------------------------------------------------------------
30// Semaphores
31
32// '0-nary' semaphore
33// Similar to a counting semaphore except the value of one is never reached
34// as a consequence, a V() that would bring the value to 1 *spins* until
35// a P consumes it
36struct Semaphore0nary {
37        __spinlock_t lock; // needed to protect
38        mpsc_queue($thread) queue;
39};
40
41static inline bool P(Semaphore0nary & this, $thread * thrd) __attribute__((artificial));
42static inline bool P(Semaphore0nary & this, $thread * thrd) {
43        /* paranoid */ verify(!(thrd->seqable.next));
44        /* paranoid */ verify(!(thrd`next));
45
46        push(this.queue, thrd);
47        return true;
48}
49
50static inline bool P(Semaphore0nary & this) __attribute__((artificial));
51static inline bool P(Semaphore0nary & this) {
52    $thread * thrd = active_thread();
53    P(this, thrd);
54    park();
55    return true;
56}
57
58static inline $thread * V(Semaphore0nary & this, const bool doUnpark = true) __attribute__((artificial));
59static inline $thread * V(Semaphore0nary & this, const bool doUnpark = true) {
60        $thread * next;
61        lock(this.lock __cfaabi_dbg_ctx2);
62                for (;;) {
63                        next = pop(this.queue);
64                        if (next) break;
65                        Pause();
66                }
67        unlock(this.lock);
68
69        if (doUnpark) unpark(next);
70        return next;
71}
72
73// Wrapper used on top of any sempahore to avoid potential locking
74struct BinaryBenaphore {
75        volatile ssize_t counter;
76};
77
78static inline {
79        void ?{}(BinaryBenaphore & this) { this.counter = 0; }
80        void ?{}(BinaryBenaphore & this, zero_t) { this.counter = 0; }
81        void ?{}(BinaryBenaphore & this, one_t ) { this.counter = 1; }
82
83        // returns true if no blocking needed
84        bool P(BinaryBenaphore & this) {
85                return __atomic_fetch_sub(&this.counter, 1, __ATOMIC_SEQ_CST) > 0;
86        }
87
88        bool tryP(BinaryBenaphore & this) {
89                ssize_t c = this.counter;
90                return (c >= 1) && __atomic_compare_exchange_n(&this.counter, &c, c-1, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
91        }
92
93        // returns true if notify needed
94        bool V(BinaryBenaphore & this) {
95                ssize_t c = 0;
96                for () {
97                        if (__atomic_compare_exchange_n(&this.counter, &c, c+1, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
98                                if (c == 0) return true;
99                                /* paranoid */ verify(c < 0);
100                                return false;
101                        } else {
102                                if (c == 1) return true;
103                                /* paranoid */ verify(c < 1);
104                                Pause();
105                        }
106                }
107        }
108}
109
110// Binary Semaphore based on the BinaryBenaphore on top of the 0-nary Semaphore
111struct ThreadBenaphore {
112        BinaryBenaphore ben;
113        Semaphore0nary  sem;
114};
115
116static inline void ?{}(ThreadBenaphore & this) {}
117static inline void ?{}(ThreadBenaphore & this, zero_t) { (this.ben){ 0 }; }
118static inline void ?{}(ThreadBenaphore & this, one_t ) { (this.ben){ 1 }; }
119
120static inline bool P(ThreadBenaphore & this)              { return P(this.ben) ? false : P(this.sem); }
121static inline bool tryP(ThreadBenaphore & this)           { return tryP(this.ben); }
122static inline bool P(ThreadBenaphore & this, bool wait)   { return wait ? P(this) : tryP(this); }
123
124static inline $thread * V(ThreadBenaphore & this, const bool doUnpark = true) {
125        if (V(this.ben)) return 0p;
126        return V(this.sem, doUnpark);
127}
128
129//-----------------------------------------------------------------------------
130// Semaphore
131struct semaphore {
132        __spinlock_t lock;
133        int count;
134        __queue_t($thread) waiting;
135};
136
137void  ?{}(semaphore & this, int count = 1);
138void ^?{}(semaphore & this);
139bool   P (semaphore & this);
140bool   V (semaphore & this);
141bool   V (semaphore & this, unsigned count);
142$thread * V (semaphore & this, bool );
143
144//----------
145struct single_acquisition_lock {
146        inline blocking_lock;
147};
148
149static inline void  ?{}( single_acquisition_lock & this ) {((blocking_lock &)this){ false, false };}
150static inline void ^?{}( single_acquisition_lock & this ) {}
151static inline void   lock     ( single_acquisition_lock & this ) { lock    ( (blocking_lock &)this ); }
152static inline bool   try_lock ( single_acquisition_lock & this ) { return try_lock( (blocking_lock &)this ); }
153static inline void   unlock   ( single_acquisition_lock & this ) { unlock  ( (blocking_lock &)this ); }
154static inline size_t on_wait  ( single_acquisition_lock & this ) { return on_wait ( (blocking_lock &)this ); }
155static inline void   on_wakeup( single_acquisition_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); }
156static inline void   on_notify( single_acquisition_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); }
157
158//----------
159struct owner_lock {
160        inline blocking_lock;
161};
162
163static inline void  ?{}( owner_lock & this ) {((blocking_lock &)this){ true, true };}
164static inline void ^?{}( owner_lock & this ) {}
165static inline void   lock     ( owner_lock & this ) { lock    ( (blocking_lock &)this ); }
166static inline bool   try_lock ( owner_lock & this ) { return try_lock( (blocking_lock &)this ); }
167static inline void   unlock   ( owner_lock & this ) { unlock  ( (blocking_lock &)this ); }
168static inline size_t on_wait  ( owner_lock & this ) { return on_wait ( (blocking_lock &)this ); }
169static inline void   on_wakeup( owner_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); }
170static inline void   on_notify( owner_lock & this, struct $thread * t ) { on_notify( (blocking_lock &)this, t ); }
171
172struct fast_lock {
173        $thread * volatile owner;
174        ThreadBenaphore sem;
175};
176
177static inline bool $try_lock(fast_lock & this, $thread * thrd) {
178    $thread * exp = 0p;
179    return __atomic_compare_exchange_n(&this.owner, &exp, thrd, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
180}
181
182static inline void lock( fast_lock & this ) __attribute__((artificial));
183static inline void lock( fast_lock & this ) {
184        $thread * thrd = active_thread();
185        /* paranoid */verify(thrd != this.owner);
186
187        for (;;) {
188                if ($try_lock(this, thrd)) return;
189                P(this.sem);
190        }
191}
192
193static inline bool try_lock( fast_lock & this ) __attribute__((artificial));
194static inline bool try_lock ( fast_lock & this ) {
195        $thread * thrd = active_thread();
196        /* paranoid */ verify(thrd != this.owner);
197        return $try_lock(this, thrd);
198}
199
200static inline $thread * unlock( fast_lock & this ) __attribute__((artificial));
201static inline $thread * unlock( fast_lock & this ) {
202        $thread * thrd = active_thread();
203        /* paranoid */ verify(thrd == this.owner);
204
205        // open 'owner' before unlocking anyone
206        // so new and unlocked threads don't park incorrectly.
207        this.owner = 0p;
208
209        return V(this.sem,true);
210}
211
212static inline size_t on_wait( fast_lock & this ) __attribute__((artificial));
213static inline void on_wakeup( fast_lock & this, size_t ) __attribute__((artificial));
214static inline void on_notify( fast_lock &, struct $thread * t ) __attribute__((artificial));
215
216static inline size_t on_wait( fast_lock & this ) { unlock(this); return 0; }
217static inline void on_wakeup( fast_lock & this, size_t ) { lock(this); }
218static inline void on_notify( fast_lock &, struct $thread * t ) { unpark(t); }
219
220struct mcs_node {
221        mcs_node * volatile next;
222        single_sem sem;
223};
224
225static inline void ?{}(mcs_node & this) { this.next = 0p; }
226
227static inline mcs_node * volatile & ?`next ( mcs_node * node ) {
228        return node->next;
229}
230
231struct mcs_lock {
232        mcs_queue(mcs_node) queue;
233};
234
235static inline void lock(mcs_lock & l, mcs_node & n) {
236        if(push(l.queue, &n))
237                wait(n.sem);
238}
239
240static inline void unlock(mcs_lock & l, mcs_node & n) {
241        mcs_node * next = advance(l.queue, &n);
242        if(next) post(next->sem);
243}
244
245//-----------------------------------------------------------------------------
246// is_blocking_lock
247trait is_blocking_lock(L & | sized(L)) {
248        // For synchronization locks to use when acquiring
249        void on_notify( L &, struct $thread * );
250
251        // For synchronization locks to use when releasing
252        size_t on_wait( L & );
253
254        // to set recursion count after getting signalled;
255        void on_wakeup( L &, size_t recursion );
256};
257
258//-----------------------------------------------------------------------------
259// info_thread
260// the info thread is a wrapper around a thread used
261// to store extra data for use in the condition variable
262forall(L & | is_blocking_lock(L)) {
263        struct info_thread;
264
265        // for use by sequence
266        info_thread(L) *& Back( info_thread(L) * this );
267        info_thread(L) *& Next( info_thread(L) * this );
268}
269
270//-----------------------------------------------------------------------------
271// Synchronization Locks
272forall(L & | is_blocking_lock(L)) {
273        struct condition_variable {
274                // Spin lock used for mutual exclusion
275                __spinlock_t lock;
276
277                // List of blocked threads
278                Sequence( info_thread(L) ) blocked_threads;
279
280                // Count of current blocked threads
281                int count;
282        };
283
284        void  ?{}( condition_variable(L) & this );
285        void ^?{}( condition_variable(L) & this );
286
287        bool notify_one( condition_variable(L) & this );
288        bool notify_all( condition_variable(L) & this );
289
290        uintptr_t front( condition_variable(L) & this );
291
292        bool empty  ( condition_variable(L) & this );
293        int  counter( condition_variable(L) & this );
294
295        void wait( condition_variable(L) & this );
296        void wait( condition_variable(L) & this, uintptr_t info );
297        bool wait( condition_variable(L) & this, Duration duration );
298        bool wait( condition_variable(L) & this, uintptr_t info, Duration duration );
299        bool wait( condition_variable(L) & this, Time time );
300        bool wait( condition_variable(L) & this, uintptr_t info, Time time );
301
302        void wait( condition_variable(L) & this, L & l );
303        void wait( condition_variable(L) & this, L & l, uintptr_t info );
304        bool wait( condition_variable(L) & this, L & l, Duration duration );
305        bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration );
306        bool wait( condition_variable(L) & this, L & l, Time time );
307        bool wait( condition_variable(L) & this, L & l, uintptr_t info, Time time );
308}
Note: See TracBrowser for help on using the repository browser.