source: libcfa/src/concurrency/locks.hfa@ 231b18f

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 231b18f was f4e35326, checked in by Colby Alexander Parsons <caparsons@…>, 5 years ago

a few small changes to locks.*

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