My Project
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 1007 of file vspace.cc.

1007  {
1008  int wakeup = -1;
1010  _lock.lock();
1011  if (_head == _tail) {
1012  _value++;
1013  } else {
1014  // don't increment value, as we'll pass that on to the next process.
1015  wakeup = _waiting[_head];
1016  sig = _signals[_head];
1017  next(_head);
1018  }
1019  _lock.unlock();
1020  if (wakeup >= 0) {
1021  internals::send_signal(wakeup, sig);
1022  }
1023 }
int _waiting[internals::MAX_PROCESS+1]
Definition: vspace.h:2348
void next(int &index)
Definition: vspace.h:2351
internals::ipc_signal_t _signals[internals::MAX_PROCESS+1]
Definition: vspace.h:2349
bool send_signal(int processno, ipc_signal_t sig, bool lock)
Definition: vspace.cc:905

◆ start_wait()

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

Definition at line 1050 of file vspace.cc.

1050  {
1051  _lock.lock();
1052  if (_value > 0) {
1053  if (internals::send_signal(internals::vmem.current_process, sig))
1054  _value--;
1055  _lock.unlock();
1056  return false;
1057  }
1059  _signals[_tail] = sig;
1060  next(_tail);
1061  _lock.unlock();
1062  return true;
1063 }
static VMem & vmem
Definition: vspace.h:1635

◆ stop_wait()

bool Semaphore::stop_wait ( )

Definition at line 1065 of file vspace.cc.

1065  {
1066  bool result = false;
1067  _lock.lock();
1068  for (int i = _head; i != _tail; next(i)) {
1069  if (_waiting[i] == internals::vmem.current_process) {
1070  int last = i;
1071  next(i);
1072  while (i != _tail) {
1073  _waiting[last] = _waiting[i];
1074  _signals[last] = _signals[i];
1075  last = i;
1076  next(i);
1077  }
1078  _tail = last;
1079  result = true;
1080  break;
1081  }
1082  }
1083  _lock.unlock();
1084  return result;
1085 }
int i
Definition: cfEzgcd.cc:132
return result
Definition: facAbsBiFact.cc:75
STATIC_VAR poly last
Definition: hdegree.cc:1151

◆ try_wait()

bool Semaphore::try_wait ( )

Definition at line 1025 of file vspace.cc.

1025  {
1026  bool result = false;
1027  _lock.lock();
1028  if (_value > 0) {
1029  _value--;
1030  result = true;
1031  }
1032  _lock.unlock();
1033  return result;
1034 }

◆ 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 1036 of file vspace.cc.

1036  {
1037  _lock.lock();
1038  if (_value > 0) {
1039  _value--;
1040  _lock.unlock();
1041  return;
1042  }
1044  _signals[_tail] = 0;
1045  next(_tail);
1046  _lock.unlock();
1048 }
ipc_signal_t wait_signal(bool lock)
Definition: vspace.cc:971

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: