Cheetah Software  1.0
SharedMemorySemaphore Class Reference

#include <SharedMemory.h>

+ Collaboration diagram for SharedMemorySemaphore:

Public Member Functions

void init (unsigned int value)
 
void increment ()
 
void decrement ()
 
bool tryDecrement ()
 
bool decrementTimeout (u64 seconds, u64 nanoseconds)
 
void destroy ()
 

Private Attributes

sem_t _sem
 
bool _init = false
 

Detailed Description

A POSIX semaphore for shared memory. See https://linux.die.net/man/7/sem_overview for more deatils

Definition at line 27 of file SharedMemory.h.

Member Function Documentation

void SharedMemorySemaphore::decrement ( )
inline

If the semaphore's value is > 0, decrement the value. Otherwise, wait until its value is > 0, then decrement.

Definition at line 59 of file SharedMemory.h.

References _sem.

59 { sem_wait(&_sem); }

+ Here is the caller graph for this function:

bool SharedMemorySemaphore::decrementTimeout ( u64  seconds,
u64  nanoseconds 
)
inline

Like decrement, but after waiting ms milliseconds, will give up Returns true if the semaphore is successfully decremented

Definition at line 72 of file SharedMemory.h.

References _sem.

72  {
73  struct timespec ts;
74  clock_gettime(CLOCK_REALTIME, &ts);
75  ts.tv_nsec += nanoseconds;
76  ts.tv_sec += seconds;
77  ts.tv_sec += ts.tv_nsec / 1000000000;
78  ts.tv_nsec %= 1000000000;
79  return (sem_timedwait(&_sem, &ts) == 0);
80  }
void SharedMemorySemaphore::destroy ( )
inline

Delete the semaphore. Note that deleting a semaphore in one process while another is still using it results in very strange behavior.

Definition at line 86 of file SharedMemory.h.

References _sem.

86 { sem_destroy(&_sem); }
void SharedMemorySemaphore::increment ( )
inline

Increment the value of the semaphore.

Definition at line 53 of file SharedMemory.h.

References _sem.

53 { sem_post(&_sem); }
void SharedMemorySemaphore::init ( unsigned int  value)
inline

If semaphore is unitialized, initialize it and set its value. This can be called as many times as you want safely. It must be called at least once. Only one process needs to call this, even if it is used in multiple processes.

Note that if init() is called after the semaphore has been initialized, it will not change its value.

Parameters
valueThe initial value of the semaphore.

Definition at line 39 of file SharedMemory.h.

References _init, and _sem.

39  {
40  if (!_init) {
41  if (sem_init(&_sem, 1, value)) {
42  printf("[ERROR] Failed to initialize shared memory semaphore: %s\n",
43  strerror(errno));
44  } else {
45  _init = true;
46  }
47  }
48  }

+ Here is the caller graph for this function:

bool SharedMemorySemaphore::tryDecrement ( )
inline

If the semaphore's value is > 0, decrement the value and return true Otherwise, return false (doesn't decrement or wait)

Returns

Definition at line 66 of file SharedMemory.h.

References _sem.

66 { return (sem_trywait(&_sem)) == 0; }

Member Data Documentation

bool SharedMemorySemaphore::_init = false
private

Definition at line 90 of file SharedMemory.h.

sem_t SharedMemorySemaphore::_sem
private

Definition at line 89 of file SharedMemory.h.


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