| [c2409fd] | 1 | #pragma once
 | 
|---|
 | 2 | 
 | 
|---|
 | 3 | #include "locks.hfa"
 | 
|---|
 | 4 | #include <stdbool.h>
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | #include "bits/algorithm.hfa"
 | 
|---|
 | 7 | #include "bits/locks.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 | //// Semaphores
 | 
|---|
 | 19 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 20 | forall(dtype L | is_blocking_lock(L)) {
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 |         struct base_semaphore {
 | 
|---|
 | 23 |                 // internal counter for the semaphore
 | 
|---|
 | 24 |                 int count;
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 |                 // Spin lock used for mutual exclusion
 | 
|---|
 | 27 |                 __spinlock_t lock;
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 |                 // List of blocked threads
 | 
|---|
 | 30 |                 __queue_t(info_thread(L)) blocked_threads;
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 |                 // Flag if the semaphor is binary
 | 
|---|
 | 33 |                 bool is_binary;
 | 
|---|
 | 34 |         };
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 |         // A semaphore that is binary
 | 
|---|
 | 37 |         // If the current owner P's this semaphore it will not block
 | 
|---|
 | 38 |         struct binary_semaphore {
 | 
|---|
 | 39 |                 inline base_semaphore(L);
 | 
|---|
 | 40 |         };
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 |         // A semaphore that maintains a counter.
 | 
|---|
 | 43 |         // If a thread P's this semaphore it always decreases the counter
 | 
|---|
 | 44 |         struct counting_semaphore {
 | 
|---|
 | 45 |                 inline base_semaphore(L);
 | 
|---|
 | 46 |         };
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 |         void ?{}(base_semaphore(L) & this, int count, bool is_binary);
 | 
|---|
 | 49 |         void ^?{}(base_semaphore(L) & this);
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 |         void ?{}(binary_semaphore(L) & this);
 | 
|---|
 | 52 |         void ?{}(binary_semaphore(L) & this, int count);
 | 
|---|
 | 53 |         void ^?{}(binary_semaphore(L) & this);
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |         void ?{}(counting_semaphore(L) & this);
 | 
|---|
 | 56 |         void ?{}(counting_semaphore(L) & this, int count);
 | 
|---|
 | 57 |         void ^?{}(counting_semaphore(L) & this);
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 |         struct alarm_node_semaphore {
 | 
|---|
 | 60 |                 alarm_node_t alarm_node;
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |                 base_semaphore(L) * sem;
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |                 info_thread(L) ** i;
 | 
|---|
 | 65 |         };
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |         void ?{}( alarm_node_semaphore(L) & this, $thread * thrd, Time alarm, Duration period, Alarm_Callback callback );
 | 
|---|
 | 68 |         void ^?{}( alarm_node_semaphore(L) & this );
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |         void add_( base_semaphore(L) & this, struct $thread * t );
 | 
|---|
 | 71 |         void remove_( base_semaphore(L) & this );
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |         // TODO: look into changing timout routines to return bool showing if signalled or woken by kernel
 | 
|---|
 | 74 |         void P(base_semaphore(L) & this);
 | 
|---|
 | 75 |         void P(base_semaphore(L) & this, uintptr_t info);
 | 
|---|
 | 76 |         void P(base_semaphore(L) & this, Duration duration);
 | 
|---|
 | 77 |         void P(base_semaphore(L) & this, uintptr_t info, Duration duration);
 | 
|---|
 | 78 |         void P(base_semaphore(L) & this, Time time);
 | 
|---|
 | 79 |         void P(base_semaphore(L) & this, uintptr_t info, Time time);
 | 
|---|
 | 80 |         void P(base_semaphore(L) & this, base_semaphore(L) & s);
 | 
|---|
 | 81 |         void P(base_semaphore(L) & this, base_semaphore(L) & s, uintptr_t info);
 | 
|---|
 | 82 |         void P(base_semaphore(L) & this, base_semaphore(L) & s, Duration duration );
 | 
|---|
 | 83 |         void P(base_semaphore(L) & this, base_semaphore(L) & s, uintptr_t info, Duration duration);
 | 
|---|
 | 84 |         void P(base_semaphore(L) & this, base_semaphore(L) & s, Time time);
 | 
|---|
 | 85 |         void P(base_semaphore(L) & this, base_semaphore(L) & s, uintptr_t info, Time time);
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 |         bool tryP(base_semaphore(L) & this);
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |         void V(base_semaphore(L) & this);
 | 
|---|
 | 90 |         void V(base_semaphore(L) & this, int times);
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |         // void ?`++(base_semaphore(L) & this);
 | 
|---|
 | 93 |         // void ?`--(base_semaphore(L) & this);
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 |         // void ?`V(base_semaphore(L) & this);
 | 
|---|
 | 96 |         // void ?`P(base_semaphore(L) & this);
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |         uintptr_t front(base_semaphore(L) & this);
 | 
|---|
 | 99 |         bool empty(base_semaphore(L) & this);
 | 
|---|
 | 100 |         int counter(base_semaphore(L) & this);
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 |         // these are to satisfy the is_blocking_lock trait so that 
 | 
|---|
 | 104 |         // semaphores can be released by condition variables and vice versa
 | 
|---|
 | 105 |         void set_recursion_count( base_semaphore(L) & this, size_t recursion );
 | 
|---|
 | 106 |         size_t get_recursion_count( base_semaphore(L) & this );
 | 
|---|
 | 107 | }
 | 
|---|