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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since dff1fd1 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
RevLine 
[f4e35326]1#pragma once
2
[848439f]3#include <stdbool.h>
4
5#include "bits/algorithm.hfa"
6#include "bits/locks.hfa"
[cad1df1]7#include "bits/sequence.hfa"
[848439f]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>
[eeb5023]15#include "alarm.hfa"
[848439f]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 {
[cad1df1]34                inline Seqable;
[848439f]35                struct $thread * t;
36                uintptr_t info;
37                L * lock;
[dff1fd1]38                bool signalled;                                 // true when signalled and false when timeout wakes thread
[848439f]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 );
[c131a02]45
46        info_thread(L) *& Back( info_thread(L) * this );
47        info_thread(L) *& Next( info_thread(L) * this );
[848439f]48}
49
50///////////////////////////////////////////////////////////////////
51//// Blocking Locks
52///////////////////////////////////////////////////////////////////
[6a8882c]53
[848439f]54struct blocking_lock {
55        // Spin lock used for mutual exclusion
56        __spinlock_t lock;
57
58        // List of blocked threads
[c131a02]59        Sequence( $thread ) blocked_threads;
[848439f]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
[6a8882c]77struct single_acquisition_lock {
[848439f]78        inline blocking_lock;
79};
80
81struct owner_lock {
82        inline blocking_lock;
83};
84
[6a8882c]85struct multiple_acquisition_lock {
[848439f]86        inline blocking_lock;
87};
88
89void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner );
90void ^?{}( blocking_lock & this );
91
[6a8882c]92void ?{}( single_acquisition_lock & this );
93void ^?{}( single_acquisition_lock & this );
[848439f]94
95void ?{}( owner_lock & this );
96void ^?{}( owner_lock & this );
97
[6a8882c]98void ?{}( multiple_acquisition_lock & this );
99void ^?{}( multiple_acquisition_lock & this );
[848439f]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
[6a8882c]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 );
[848439f]130
131///////////////////////////////////////////////////////////////////
132//// Synchronization Locks
133///////////////////////////////////////////////////////////////////
134forall(dtype L | is_blocking_lock(L)) {
[eeb5023]135        struct condition_variable {
[848439f]136                // Spin lock used for mutual exclusion
137                __spinlock_t lock;
138
[cad1df1]139                info_thread(L) * last_thread;
140
[848439f]141                // List of blocked threads
[cad1df1]142                Sequence( info_thread(L) ) blocked_threads;
[848439f]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
[eeb5023]151        struct alarm_node_wrap {
152                alarm_node_t alarm_node;
[848439f]153
[eeb5023]154                condition_variable(L) * cond;
[848439f]155
[cad1df1]156                info_thread(L) * i;
[eeb5023]157        };
[848439f]158
[4aeaee5]159        void ?{}( alarm_node_wrap(L) & this, Time alarm, Duration period, Alarm_Callback callback );
[eeb5023]160        void ^?{}( alarm_node_wrap(L) & this );
[848439f]161
[eeb5023]162        void alarm_node_callback( alarm_node_wrap(L) & this );
[848439f]163
[eeb5023]164        void alarm_node_wrap_cast( alarm_node_t & a );
[848439f]165
[eeb5023]166        bool notify_one( condition_variable(L) & this );
167        bool notify_all( condition_variable(L) & this );
[848439f]168
[eeb5023]169        uintptr_t front( condition_variable(L) & this );
[848439f]170
[eeb5023]171        bool empty( condition_variable(L) & this );
172        int counter( condition_variable(L) & this );
[848439f]173
[eeb5023]174        void wait( condition_variable(L) & this );
175        void wait( condition_variable(L) & this, uintptr_t info );
[dff1fd1]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 );
[848439f]180
[eeb5023]181        void wait( condition_variable(L) & this, L & l );
182        void wait( condition_variable(L) & this, L & l, uintptr_t info );
[dff1fd1]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 );
[eeb5023]187}
Note: See TracBrowser for help on using the repository browser.