source: libcfa/src/concurrency/mutex.hfa @ 9f99799

ADTast-experimentalpthread-emulation
Last change on this file since 9f99799 was e84ab3d, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Step 1 of changing $thread to thread$

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[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// ---
33struct mutex_lock {
34        // Spin lock used for mutual exclusion
35        __spinlock_t lock;
36
37        // List of blocked threads
[e84ab3d]38        __queue_t(struct thread$) blocked_threads;
[09800e9]39
40        // Locked flag
41        bool is_locked;
42};
43
[0edd11a]44void ?{}(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
45void ^?{}(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
46void lock(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
47bool try_lock(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
48void unlock(mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
[09800e9]49
50// Exclusive lock - recursive
51// ---
52struct recursive_mutex_lock{
53        // Spin lock used for mutual exclusion
54        __spinlock_t lock;
55
56        // List of blocked threads
[e84ab3d]57        __queue_t(struct thread$) blocked_threads;
[09800e9]58
59        // Current thread owning the lock
[e84ab3d]60        struct thread$ * owner;
[09800e9]61
62        // Number of recursion level
63        size_t recursion_count;
64};
65
[0edd11a]66void ?{}(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
67void ^?{}(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
68void lock(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
69bool try_lock(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
70void unlock(recursive_mutex_lock & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
[09800e9]71
[fd54fef]72trait is_lock(L & | sized(L)) {
[09800e9]73        void lock  (L &);
74        void unlock(L &);
75};
76
77//-----------------------------------------------------------------------------
78// Condition variables
79
80struct condition_variable {
81        // Spin lock used for mutual exclusion
82        __spinlock_t lock;
83
84        // List of blocked threads
[e84ab3d]85        __queue_t(struct thread$) blocked_threads;
[09800e9]86};
87
[0edd11a]88void ?{}(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
89void ^?{}(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
[09800e9]90
[0edd11a]91void notify_one(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
92void notify_all(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
[09800e9]93
[0edd11a]94void wait(condition_variable & this) __attribute__((deprecated("use concurrency/locks.hfa instead")));
[09800e9]95
[fd54fef]96forall(L & | is_lock(L))
[0edd11a]97void wait(condition_variable & this, L & l) __attribute__((deprecated("use concurrency/locks.hfa instead")));
[09800e9]98
99//-----------------------------------------------------------------------------
100// Scopes
[fd54fef]101forall(L & | is_lock(L)) {
[09800e9]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}
Note: See TracBrowser for help on using the repository browser.