Loading...
Searching...
No Matches
vspace::SyncVar< T > Class Template Reference

`#include <vspace.h>`

## Public Member Functions

| SyncVar ()
T | read ()
Result< T > | try_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.

: _set(false) { }
vspace::SyncVar::_set
bool _set
Definition vspace.h:2473

## Member Function Documentation

## read()

template<typename T>

T vspace::SyncVar< T >::read | ( |  | ) |

Definition at line 2517 of file vspace.h.

{
_lock.lock();
if (_set) {
_lock.unlock();
return _value;
}
if (_sem.is_null()) {
_sem = vnew<Semaphore>();
}
// We can't wait inside the lock without deadlocking; but waiting outside
// could cause a race condition with _sem being freed due to being idle.
// Thus, we use start_wait() to insert ourselves into the queue, then
// use wait_signal() outside the lock to complete waiting.
//
// Note: start_wait() will not send a signal to self, as _set is
// false and therefore _sem->value() must be zero.
_sem->start_wait(0);
_lock.unlock();
internals::wait_signal();
_lock.lock();
if (_sem->_idle())
_sem->post();
else {
_sem.free();
_sem = vnull<Semaphore>();
}
_lock.unlock();
return _value;
}
vspace::SyncVar::_value
T _value
Definition vspace.h:2474
vspace::SyncVar::_sem
VRef< Semaphore > _sem
Definition vspace.h:2472
vspace::SyncVar::_lock
FastLock _lock
Definition vspace.h:2471
vspace::internals::wait_signal
ipc_signal_t wait_signal(bool lock)
Definition vspace.cc:1075
vspace::vnew
VRef< T > vnew()
Definition vspace.h:1872
vspace::vnull
VRef< T > vnull()
Definition vspace.h:1867

## 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.

{
_lock.lock();
if (_set) {
internals::send_signal(internals::vmem.current_process, sig);
_lock.unlock();
return true;
}
if (_sem.is_null()) {
_sem = vnew<Semaphore>();
}
bool result = _sem->start_wait(sig);
_lock.unlock();
return result;
}
vspace::SyncVar
Definition vspace.h:2469
vspace::internals::send_signal
bool send_signal(int processno, ipc_signal_t sig, bool lock)
Definition vspace.cc:967

## stop_wait()

template<typename T>

void vspace::SyncVar< T >::stop_wait | ( |  | ) |

private

Definition at line 2506 of file vspace.h.

{
_lock.lock();
if (!_sem.is_null()) {
_sem->stop_wait();
if (!_sem->_idle())
_sem->post();
}
_lock.unlock();
}

## test()

template<typename T>

bool vspace::SyncVar< T >::test | ( |  | ) |

inline

Definition at line 2484 of file vspace.h.

{
return _set;
}

## try_read()

template<typename T>

Result< T > vspace::SyncVar< T >::try_read | ( |  | ) |

Definition at line 2548 of file vspace.h.

{
_lock.lock();
Result<T> result = _set ? Result<T>(_value) : Result<T>();
_lock.unlock();
return result;
}

## write()

template<typename T>

bool vspace::SyncVar< T >::write | ( | T | value | ) |

Definition at line 2556 of file vspace.h.

{
_lock.lock();
if (_set) {
_lock.unlock();
return false;
}
_set = true;
_value = value;
if (!_sem->_idle())
_sem->post();
_lock.unlock();
return true;
}

## Friends And Related Symbol 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:

- kernel/oswrapper/vspace.h