#pragma once #include "locks.hfa" #include #include "bits/algorithm.hfa" #include "bits/locks.hfa" #include "bits/containers.hfa" #include "invoke.h" #include "time_t.hfa" #include "time.hfa" #include #include "alarm.hfa" /////////////////////////////////////////////////////////////////// //// Semaphores /////////////////////////////////////////////////////////////////// forall(dtype L | is_blocking_lock(L)) { struct base_semaphore { // internal counter for the semaphore int count; // Spin lock used for mutual exclusion __spinlock_t lock; // List of blocked threads __queue_t(info_thread(L)) blocked_threads; // Flag if the semaphor is binary bool is_binary; }; // A semaphore that is binary // If the current owner P's this semaphore it will not block struct binary_semaphore { inline base_semaphore(L); }; // A semaphore that maintains a counter. // If a thread P's this semaphore it always decreases the counter struct counting_semaphore { inline base_semaphore(L); }; void ?{}(base_semaphore(L) & this, int count, bool is_binary); void ^?{}(base_semaphore(L) & this); void ?{}(binary_semaphore(L) & this); void ?{}(binary_semaphore(L) & this, int count); void ^?{}(binary_semaphore(L) & this); void ?{}(counting_semaphore(L) & this); void ?{}(counting_semaphore(L) & this, int count); void ^?{}(counting_semaphore(L) & this); struct alarm_node_semaphore { alarm_node_t alarm_node; base_semaphore(L) * sem; info_thread(L) ** i; }; void ?{}( alarm_node_semaphore(L) & this, $thread * thrd, Time alarm, Duration period, Alarm_Callback callback ); void ^?{}( alarm_node_semaphore(L) & this ); void add_( base_semaphore(L) & this, struct $thread * t ); void remove_( base_semaphore(L) & this ); // TODO: look into changing timout routines to return bool showing if signalled or woken by kernel void P(base_semaphore(L) & this); void P(base_semaphore(L) & this, uintptr_t info); void P(base_semaphore(L) & this, Duration duration); void P(base_semaphore(L) & this, uintptr_t info, Duration duration); void P(base_semaphore(L) & this, Time time); void P(base_semaphore(L) & this, uintptr_t info, Time time); void P(base_semaphore(L) & this, base_semaphore(L) & s); void P(base_semaphore(L) & this, base_semaphore(L) & s, uintptr_t info); void P(base_semaphore(L) & this, base_semaphore(L) & s, Duration duration ); void P(base_semaphore(L) & this, base_semaphore(L) & s, uintptr_t info, Duration duration); void P(base_semaphore(L) & this, base_semaphore(L) & s, Time time); void P(base_semaphore(L) & this, base_semaphore(L) & s, uintptr_t info, Time time); bool tryP(base_semaphore(L) & this); void V(base_semaphore(L) & this); void V(base_semaphore(L) & this, int times); // void ?`++(base_semaphore(L) & this); // void ?`--(base_semaphore(L) & this); // void ?`V(base_semaphore(L) & this); // void ?`P(base_semaphore(L) & this); uintptr_t front(base_semaphore(L) & this); bool empty(base_semaphore(L) & this); int counter(base_semaphore(L) & this); // these are to satisfy the is_blocking_lock trait so that // semaphores can be released by condition variables and vice versa void set_recursion_count( base_semaphore(L) & this, size_t recursion ); size_t get_recursion_count( base_semaphore(L) & this ); }