source: libcfa/src/concurrency/locks.hfa @ fe97de26

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since fe97de26 was dff1fd1, checked in by Colby Alexander Parsons <caparsons@…>, 3 years ago

added bool return to timeout routines and removed redundant listed field

  • Property mode set to 100644
File size: 5.8 KB
Line 
1#pragma once
2
3#include <stdbool.h>
4
5#include "bits/algorithm.hfa"
6#include "bits/locks.hfa"
7#include "bits/sequence.hfa"
8#include "bits/containers.hfa"
9
10#include "invoke.h"
11
12#include "time_t.hfa"
13#include "time.hfa"
14#include <sys/time.h>
15#include "alarm.hfa"
16
17///////////////////////////////////////////////////////////////////
18//// is_blocking_lock
19///////////////////////////////////////////////////////////////////
20
21trait is_blocking_lock(dtype L | sized(L)) {
22        void add_( L &, struct $thread * );             // For synchronization locks to use when acquiring
23        void remove_( L & );    // For synchronization locks to use when releasing
24        size_t get_recursion_count( L & ); // to get recursion count for cond lock to reset after waking
25        void set_recursion_count( L &, size_t recursion ); // to set recursion count after getting signalled;
26};
27
28///////////////////////////////////////////////////////////////////
29//// info_thread
30///////////////////////////////////////////////////////////////////
31
32forall(dtype L | is_blocking_lock(L)) {
33        struct info_thread {
34                inline Seqable;
35                struct $thread * t;
36                uintptr_t info;
37                L * lock;
38                bool signalled;                                 // true when signalled and false when timeout wakes thread
39        };
40
41
42        void ?{}( info_thread(L) & this, $thread * t );
43        void ?{}( info_thread(L) & this, $thread * t, uintptr_t info );
44        void ^?{}( info_thread(L) & this );
45
46        info_thread(L) *& Back( info_thread(L) * this );
47        info_thread(L) *& Next( info_thread(L) * this );
48}
49
50///////////////////////////////////////////////////////////////////
51//// Blocking Locks
52///////////////////////////////////////////////////////////////////
53
54struct blocking_lock {
55        // Spin lock used for mutual exclusion
56        __spinlock_t lock;
57
58        // List of blocked threads
59        Sequence( $thread ) blocked_threads;
60
61        // Count of current blocked threads
62        size_t wait_count;
63
64        // Flag if the lock allows multiple acquisition
65        bool multi_acquisition;
66
67        // Flag if lock can be released by non owner
68        bool strict_owner;
69
70        // Current thread owning the lock
71        struct $thread * owner;
72
73        // Number of recursion level
74        size_t recursion_count;
75};
76
77struct single_acquisition_lock {
78        inline blocking_lock;
79};
80
81struct owner_lock {
82        inline blocking_lock;
83};
84
85struct multiple_acquisition_lock {
86        inline blocking_lock;
87};
88
89void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner );
90void ^?{}( blocking_lock & this );
91
92void ?{}( single_acquisition_lock & this );
93void ^?{}( single_acquisition_lock & this );
94
95void ?{}( owner_lock & this );
96void ^?{}( owner_lock & this );
97
98void ?{}( multiple_acquisition_lock & this );
99void ^?{}( multiple_acquisition_lock & this );
100
101void lock( blocking_lock & this );
102bool try_lock( blocking_lock & this );
103void unlock( blocking_lock & this );
104void add_( blocking_lock & this, struct $thread * t );
105void remove_( blocking_lock & this );
106size_t wait_count( blocking_lock & this );
107void set_recursion_count( blocking_lock & this, size_t recursion );
108size_t get_recursion_count( blocking_lock & this );
109
110void lock( single_acquisition_lock & this );
111void unlock( single_acquisition_lock & this );
112void add_( single_acquisition_lock & this, struct $thread * t );
113void remove_( single_acquisition_lock & this );
114void set_recursion_count( single_acquisition_lock & this, size_t recursion );
115size_t get_recursion_count( single_acquisition_lock & this );
116
117void lock( owner_lock & this );
118void unlock( owner_lock & this );
119void add_( owner_lock & this, struct $thread * t );
120void remove_( owner_lock & this );
121void set_recursion_count( owner_lock & this, size_t recursion );
122size_t get_recursion_count( owner_lock & this );
123
124void lock( multiple_acquisition_lock & this );
125void unlock( multiple_acquisition_lock & this );
126void add_( multiple_acquisition_lock & this, struct $thread * t );
127void remove_( multiple_acquisition_lock & this );
128void set_recursion_count( multiple_acquisition_lock & this, size_t recursion );
129size_t get_recursion_count( multiple_acquisition_lock & this );
130
131///////////////////////////////////////////////////////////////////
132//// Synchronization Locks
133///////////////////////////////////////////////////////////////////
134forall(dtype L | is_blocking_lock(L)) {
135        struct condition_variable {
136                // Spin lock used for mutual exclusion
137                __spinlock_t lock;
138
139                info_thread(L) * last_thread;
140
141                // List of blocked threads
142                Sequence( info_thread(L) ) blocked_threads;
143
144                // Count of current blocked threads
145                int count;
146        };
147
148        void ?{}( condition_variable(L) & this );
149        void ^?{}( condition_variable(L) & this );
150
151        struct alarm_node_wrap {
152                alarm_node_t alarm_node;
153
154                condition_variable(L) * cond;
155
156                info_thread(L) * i;
157        };
158
159        void ?{}( alarm_node_wrap(L) & this, Time alarm, Duration period, Alarm_Callback callback );
160        void ^?{}( alarm_node_wrap(L) & this );
161
162        void alarm_node_callback( alarm_node_wrap(L) & this );
163
164        void alarm_node_wrap_cast( alarm_node_t & a );
165
166        bool notify_one( condition_variable(L) & this );
167        bool notify_all( condition_variable(L) & this );
168
169        uintptr_t front( condition_variable(L) & this );
170
171        bool empty( condition_variable(L) & this );
172        int counter( condition_variable(L) & this );
173
174        void wait( condition_variable(L) & this );
175        void wait( condition_variable(L) & this, uintptr_t info );
176        bool wait( condition_variable(L) & this, Duration duration );
177        bool wait( condition_variable(L) & this, uintptr_t info, Duration duration );
178        bool wait( condition_variable(L) & this, Time time );
179        bool wait( condition_variable(L) & this, uintptr_t info, Time time );
180
181        void wait( condition_variable(L) & this, L & l );
182        void wait( condition_variable(L) & this, L & l, uintptr_t info );
183        bool wait( condition_variable(L) & this, L & l, Duration duration );
184        bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration );
185        bool wait( condition_variable(L) & this, L & l, Time time );
186        bool wait( condition_variable(L) & this, L & l, uintptr_t info, Time time );
187}
Note: See TracBrowser for help on using the repository browser.