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

Constructor & Destructor Documentation

◆ SyncVar()

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

Definition at line 1145 of file vspace.h.

1145: _set(false) { }

Member Function Documentation

◆ read()

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

Definition at line 1182 of file vspace.h.

1182 {
1183 _lock.lock();
1184 if (_set) {
1185 _lock.unlock();
1186 return _value;
1187 }
1188 if (_sem.is_null()) {
1189 _sem = vnew<Semaphore>();
1190 }
1191 // We can't wait inside the lock without deadlocking; but waiting outside
1192 // could cause a race condition with _sem being freed due to being idle.
1193 // Thus, we use start_wait() to insert ourselves into the queue, then
1194 // use wait_signal() outside the lock to complete waiting.
1195 //
1196 // Note: start_wait() will not send a signal to self, as _set is
1197 // false and therefore _sem->value() must be zero.
1198 _sem->start_wait(0);
1199 _lock.unlock();
1201 _lock.lock();
1202 if (_sem->_idle())
1203 _sem->post();
1204 else {
1205 _sem.free();
1206 _sem = vnull<Semaphore>();
1207 }
1208 _lock.unlock();
1209 return _value;
1210}
VRef< Semaphore > _sem
Definition: vspace.h:1137
FastLock _lock
Definition: vspace.h:1136
ipc_signal_t wait_signal(bool lock)
Definition: vspace.cc:413

◆ start_wait()

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

Definition at line 1155 of file vspace.h.

1155 {
1156 _lock.lock();
1157 if (_set) {
1158 internals::send_signal(internals::vmem.current_process, sig);
1159 _lock.unlock();
1160 return true;
1161 }
1162 if (_sem.is_null()) {
1163 _sem = vnew<Semaphore>();
1164 }
1165 bool result = _sem->start_wait(sig);
1166 _lock.unlock();
1167 return result;
1168}
return result
Definition: facAbsBiFact.cc:75
static VMem & vmem
Definition: vspace.h:300
bool send_signal(int processno, ipc_signal_t sig, bool lock)
Definition: vspace.cc:347

◆ stop_wait()

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

Definition at line 1171 of file vspace.h.

1171 {
1172 _lock.lock();
1173 if (!_sem.is_null()) {
1174 _sem->stop_wait();
1175 if (!_sem->_idle())
1176 _sem->post();
1177 }
1178 _lock.unlock();
1179}

◆ test()

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

Definition at line 1149 of file vspace.h.

1149 {
1150 return _set;
1151 }

◆ try_read()

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

Definition at line 1213 of file vspace.h.

1213 {
1214 _lock.lock();
1215 Result<T> result = _set ? Result<T>(_value) : Result<T>();
1216 _lock.unlock();
1217 return result;
1218}
STATIC_VAR jList * T
Definition: janet.cc:30

◆ write()

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

Definition at line 1221 of file vspace.h.

1221 {
1222 _lock.lock();
1223 if (_set) {
1224 _lock.unlock();
1225 return false;
1226 }
1227 _set = true;
1228 _value = value;
1229 if (!_sem->_idle())
1230 _sem->post();
1231 _lock.unlock();
1232 return true;
1233}

Friends And Related Function Documentation

◆ SyncReadEvent

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

Definition at line 1141 of file vspace.h.

Field Documentation

◆ _lock

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

Definition at line 1136 of file vspace.h.

◆ _sem

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

Definition at line 1137 of file vspace.h.

◆ _set

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

Definition at line 1138 of file vspace.h.

◆ _value

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

Definition at line 1139 of file vspace.h.


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