My Project
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Friends
vspace::Semaphore Class Reference

#include <vspace.h>

Public Member Functions

 Semaphore (size_t value=0)
 
size_t value ()
 
void post ()
 
bool try_wait ()
 
void wait ()
 
bool start_wait (internals::ipc_signal_t sig=0)
 
bool stop_wait ()
 

Private Member Functions

void next (int &index)
 
bool _idle ()
 

Private Attributes

int _owner
 
int _waiting [internals::MAX_PROCESS+1]
 
internals::ipc_signal_t _signals [internals::MAX_PROCESS+1]
 
int _head
 
int _tail
 
size_t _value
 
FastLock _lock
 

Friends

template<typename T >
class SyncVar
 

Detailed Description

Definition at line 2345 of file vspace.h.

Constructor & Destructor Documentation

◆ Semaphore()

vspace::Semaphore::Semaphore ( size_t  value = 0)
inline

Definition at line 2366 of file vspace.h.

2366 :
2367 _owner(0), _head(0), _tail(0), _value(value), _lock() {
2368 }
FastLock _lock
Definition: vspace.h:2358
size_t value()
Definition: vspace.h:2369

Member Function Documentation

◆ _idle()

bool vspace::Semaphore::_idle ( )
inlineprivate

Definition at line 2359 of file vspace.h.

2359 {
2360 return _head == _tail;
2361 }

◆ next()

void vspace::Semaphore::next ( int &  index)
inlineprivate

Definition at line 2351 of file vspace.h.

2351 {
2353 index = 0;
2354 else
2355 index++;
2356 }
static const int MAX_PROCESS
Definition: vspace.h:1419
static int index(p_Length length, p_Ord ord)
Definition: p_Procs_Impl.h:592

◆ post()

void Semaphore::post ( )

Definition at line 1023 of file vspace.cc.

1023 {
1024 int wakeup = -1;
1026 _lock.lock();
1027 if (_head == _tail) {
1028 _value++;
1029 } else {
1030 // don't increment value, as we'll pass that on to the next process.
1031 wakeup = _waiting[_head];
1032 sig = _signals[_head];
1033 next(_head);
1034 }
1035 _lock.unlock();
1036 if (wakeup >= 0) {
1037 internals::send_signal(wakeup, sig);
1038 }
1039}
int _waiting[internals::MAX_PROCESS+1]
Definition: vspace.h:2348
internals::ipc_signal_t _signals[internals::MAX_PROCESS+1]
Definition: vspace.h:2349
ListNode * next
Definition: janet.h:31
bool send_signal(int processno, ipc_signal_t sig, bool lock)
Definition: vspace.cc:921

◆ start_wait()

bool Semaphore::start_wait ( internals::ipc_signal_t  sig = 0)

Definition at line 1066 of file vspace.cc.

1066 {
1067 _lock.lock();
1068 if (_value > 0) {
1069 if (internals::send_signal(internals::vmem.current_process, sig))
1070 _value--;
1071 _lock.unlock();
1072 return false;
1073 }
1075 _signals[_tail] = sig;
1076 next(_tail);
1077 _lock.unlock();
1078 return true;
1079}
static VMem & vmem
Definition: vspace.h:1635

◆ stop_wait()

bool Semaphore::stop_wait ( )

Definition at line 1081 of file vspace.cc.

1081 {
1082 bool result = false;
1083 _lock.lock();
1084 for (int i = _head; i != _tail; next(i)) {
1085 if (_waiting[i] == internals::vmem.current_process) {
1086 int last = i;
1087 next(i);
1088 while (i != _tail) {
1089 _waiting[last] = _waiting[i];
1090 _signals[last] = _signals[i];
1091 last = i;
1092 next(i);
1093 }
1094 _tail = last;
1095 result = true;
1096 break;
1097 }
1098 }
1099 _lock.unlock();
1100 return result;
1101}
int i
Definition: cfEzgcd.cc:132
return result
Definition: facAbsBiFact.cc:75
STATIC_VAR poly last
Definition: hdegree.cc:1173

◆ try_wait()

bool Semaphore::try_wait ( )

Definition at line 1041 of file vspace.cc.

1041 {
1042 bool result = false;
1043 _lock.lock();
1044 if (_value > 0) {
1045 _value--;
1046 result = true;
1047 }
1048 _lock.unlock();
1049 return result;
1050}

◆ value()

size_t vspace::Semaphore::value ( )
inline

Definition at line 2369 of file vspace.h.

2369 {
2370 return _value;
2371 }

◆ wait()

void Semaphore::wait ( )

Definition at line 1052 of file vspace.cc.

1052 {
1053 _lock.lock();
1054 if (_value > 0) {
1055 _value--;
1056 _lock.unlock();
1057 return;
1058 }
1060 _signals[_tail] = 0;
1061 next(_tail);
1062 _lock.unlock();
1064}
ipc_signal_t wait_signal(bool lock)
Definition: vspace.cc:987

Friends And Related Function Documentation

◆ SyncVar

template<typename T >
friend class SyncVar
friend

Definition at line 2363 of file vspace.h.

Field Documentation

◆ _head

int vspace::Semaphore::_head
private

Definition at line 2350 of file vspace.h.

◆ _lock

FastLock vspace::Semaphore::_lock
private

Definition at line 2358 of file vspace.h.

◆ _owner

int vspace::Semaphore::_owner
private

Definition at line 2347 of file vspace.h.

◆ _signals

internals::ipc_signal_t vspace::Semaphore::_signals[internals::MAX_PROCESS+1]
private

Definition at line 2349 of file vspace.h.

◆ _tail

int vspace::Semaphore::_tail
private

Definition at line 2350 of file vspace.h.

◆ _value

size_t vspace::Semaphore::_value
private

Definition at line 2357 of file vspace.h.

◆ _waiting

int vspace::Semaphore::_waiting[internals::MAX_PROCESS+1]
private

Definition at line 2348 of file vspace.h.


The documentation for this class was generated from the following files: