My Project
Public Member Functions | Private Member Functions | Private Attributes | Friends
vspace::SyncVar< T > Class Template Reference

#include <vspace.h>

Public Member Functions

 SyncVar ()
 
T read ()
 
Result< Ttry_read ()
 
bool write (T value)
 
bool test ()
 

Private Member Functions

bool start_wait (internals::ipc_signal_t sig)
 
void stop_wait ()
 

Private Attributes

FastLock _lock
 
VRef< Semaphore_sem
 
bool _set
 
T _value
 

Friends

template<typename U >
class SyncReadEvent
 

Detailed Description

template<typename T>
class vspace::SyncVar< T >

Definition at line 2469 of file vspace.h.

Constructor & Destructor Documentation

◆ SyncVar()

template<typename T >
vspace::SyncVar< T >::SyncVar ( )
inline

Definition at line 2480 of file vspace.h.

2480 : _set(false) { }

Member Function Documentation

◆ read()

template<typename T >
T vspace::SyncVar< T >::read

Definition at line 2517 of file vspace.h.

2517  {
2518  _lock.lock();
2519  if (_set) {
2520  _lock.unlock();
2521  return _value;
2522  }
2523  if (_sem.is_null()) {
2524  _sem = vnew<Semaphore>();
2525  }
2526  // We can't wait inside the lock without deadlocking; but waiting outside
2527  // could cause a race condition with _sem being freed due to being idle.
2528  // Thus, we use start_wait() to insert ourselves into the queue, then
2529  // use wait_signal() outside the lock to complete waiting.
2530  //
2531  // Note: start_wait() will not send a signal to self, as _set is
2532  // false and therefore _sem->value() must be zero.
2533  _sem->start_wait(0);
2534  _lock.unlock();
2536  _lock.lock();
2537  if (_sem->_idle())
2538  _sem->post();
2539  else {
2540  _sem.free();
2541  _sem = vnull<Semaphore>();
2542  }
2543  _lock.unlock();
2544  return _value;
2545 }
VRef< Semaphore > _sem
Definition: vspace.h:2472
FastLock _lock
Definition: vspace.h:2471
ipc_signal_t wait_signal(bool lock)
Definition: vspace.cc:987

◆ start_wait()

template<typename T >
bool vspace::SyncVar< T >::start_wait ( internals::ipc_signal_t  sig)
private

Definition at line 2490 of file vspace.h.

2490  {
2491  _lock.lock();
2492  if (_set) {
2493  internals::send_signal(internals::vmem.current_process, sig);
2494  _lock.unlock();
2495  return true;
2496  }
2497  if (_sem.is_null()) {
2498  _sem = vnew<Semaphore>();
2499  }
2500  bool result = _sem->start_wait(sig);
2501  _lock.unlock();
2502  return result;
2503 }
return result
Definition: facAbsBiFact.cc:75
static VMem & vmem
Definition: vspace.h:1635
bool send_signal(int processno, ipc_signal_t sig, bool lock)
Definition: vspace.cc:921

◆ stop_wait()

template<typename T >
void vspace::SyncVar< T >::stop_wait
private

Definition at line 2506 of file vspace.h.

2506  {
2507  _lock.lock();
2508  if (!_sem.is_null()) {
2509  _sem->stop_wait();
2510  if (!_sem->_idle())
2511  _sem->post();
2512  }
2513  _lock.unlock();
2514 }

◆ test()

template<typename T >
bool vspace::SyncVar< T >::test ( )
inline

Definition at line 2484 of file vspace.h.

2484  {
2485  return _set;
2486  }

◆ try_read()

template<typename T >
Result< T > vspace::SyncVar< T >::try_read

Definition at line 2548 of file vspace.h.

2548  {
2549  _lock.lock();
2550  Result<T> result = _set ? Result<T>(_value) : Result<T>();
2551  _lock.unlock();
2552  return result;
2553 }
STATIC_VAR jList * T
Definition: janet.cc:30

◆ write()

template<typename T >
bool vspace::SyncVar< T >::write ( T  value)

Definition at line 2556 of file vspace.h.

2556  {
2557  _lock.lock();
2558  if (_set) {
2559  _lock.unlock();
2560  return false;
2561  }
2562  _set = true;
2563  _value = value;
2564  if (!_sem->_idle())
2565  _sem->post();
2566  _lock.unlock();
2567  return true;
2568 }

Friends And Related Function Documentation

◆ SyncReadEvent

template<typename T >
template<typename U >
friend class SyncReadEvent
friend

Definition at line 2476 of file vspace.h.

Field Documentation

◆ _lock

template<typename T >
FastLock vspace::SyncVar< T >::_lock
private

Definition at line 2471 of file vspace.h.

◆ _sem

template<typename T >
VRef<Semaphore> vspace::SyncVar< T >::_sem
private

Definition at line 2472 of file vspace.h.

◆ _set

template<typename T >
bool vspace::SyncVar< T >::_set
private

Definition at line 2473 of file vspace.h.

◆ _value

template<typename T >
T vspace::SyncVar< T >::_value
private

Definition at line 2474 of file vspace.h.


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