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 1010 of file vspace.h.

Constructor & Destructor Documentation

◆ Semaphore()

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

Definition at line 1031 of file vspace.h.

1031 :
1032 _owner(0), _head(0), _tail(0), _value(value), _lock() {
1033 }
FastLock _lock
Definition: vspace.h:1023
size_t value()
Definition: vspace.h:1034

Member Function Documentation

◆ _idle()

bool vspace::Semaphore::_idle ( )
inlineprivate

Definition at line 1024 of file vspace.h.

1024 {
1025 return _head == _tail;
1026 }

◆ next()

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

Definition at line 1016 of file vspace.h.

1016 {
1018 index = 0;
1019 else
1020 index++;
1021 }
static const int MAX_PROCESS
Definition: vspace.h:86
static int index(p_Length length, p_Ord ord)
Definition: p_Procs_Impl.h:592

◆ post()

void Semaphore::post ( )

Definition at line 449 of file vspace.cc.

449 {
450 int wakeup = -1;
452 _lock.lock();
453 if (_head == _tail) {
454 _value++;
455 } else {
456 // don't increment value, as we'll pass that on to the next process.
457 wakeup = _waiting[_head];
458 sig = _signals[_head];
459 next(_head);
460 }
461 _lock.unlock();
462 if (wakeup >= 0) {
463 internals::send_signal(wakeup, sig);
464 }
465}
int _waiting[internals::MAX_PROCESS+1]
Definition: vspace.h:1013
void next(int &index)
Definition: vspace.h:1016
internals::ipc_signal_t _signals[internals::MAX_PROCESS+1]
Definition: vspace.h:1014
bool send_signal(int processno, ipc_signal_t sig, bool lock)
Definition: vspace.cc:347

◆ start_wait()

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

Definition at line 492 of file vspace.cc.

492 {
493 _lock.lock();
494 if (_value > 0) {
495 if (internals::send_signal(internals::vmem.current_process, sig))
496 _value--;
497 _lock.unlock();
498 return false;
499 }
501 _signals[_tail] = sig;
502 next(_tail);
503 _lock.unlock();
504 return true;
505}
static VMem & vmem
Definition: vspace.h:300

◆ stop_wait()

bool Semaphore::stop_wait ( )

Definition at line 507 of file vspace.cc.

507 {
508 bool result = false;
509 _lock.lock();
510 for (int i = _head; i != _tail; next(i)) {
511 if (_waiting[i] == internals::vmem.current_process) {
512 int last = i;
513 next(i);
514 while (i != _tail) {
517 last = i;
518 next(i);
519 }
520 _tail = last;
521 result = true;
522 break;
523 }
524 }
525 _lock.unlock();
526 return result;
527}
int i
Definition: cfEzgcd.cc:132
return result
Definition: facAbsBiFact.cc:75
STATIC_VAR poly last
Definition: hdegree.cc:1150

◆ try_wait()

bool Semaphore::try_wait ( )

Definition at line 467 of file vspace.cc.

467 {
468 bool result = false;
469 _lock.lock();
470 if (_value > 0) {
471 _value--;
472 result = true;
473 }
474 _lock.unlock();
475 return result;
476}

◆ value()

size_t vspace::Semaphore::value ( )
inline

Definition at line 1034 of file vspace.h.

1034 {
1035 return _value;
1036 }

◆ wait()

void Semaphore::wait ( )

Definition at line 478 of file vspace.cc.

478 {
479 _lock.lock();
480 if (_value > 0) {
481 _value--;
482 _lock.unlock();
483 return;
484 }
486 _signals[_tail] = 0;
487 next(_tail);
488 _lock.unlock();
490}
ipc_signal_t wait_signal(bool lock)
Definition: vspace.cc:413

Friends And Related Function Documentation

◆ SyncVar

template<typename T >
friend class SyncVar
friend

Definition at line 1028 of file vspace.h.

Field Documentation

◆ _head

int vspace::Semaphore::_head
private

Definition at line 1015 of file vspace.h.

◆ _lock

FastLock vspace::Semaphore::_lock
private

Definition at line 1023 of file vspace.h.

◆ _owner

int vspace::Semaphore::_owner
private

Definition at line 1012 of file vspace.h.

◆ _signals

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

Definition at line 1014 of file vspace.h.

◆ _tail

int vspace::Semaphore::_tail
private

Definition at line 1015 of file vspace.h.

◆ _value

size_t vspace::Semaphore::_value
private

Definition at line 1022 of file vspace.h.

◆ _waiting

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

Definition at line 1013 of file vspace.h.


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