Valkka
1.6.1
OpenSource Video Management
|
Interprocess shared memory ring buffer synchronized with posix semaphores. More...
#include <sharedmem.h>
Public Member Functions | |
SharedMemRingBufferBase (const char *name, int n_cells, std::size_t n_bytes, int mstimeout=0, bool is_server=false) | |
Default constructor. More... | |
virtual | ~SharedMemRingBufferBase () |
Default destructor. | |
int | getValue () |
Returns the current index (next to be read) of the shmem buffer. | |
bool | getClientState () |
Are the shmem segments available for client? | |
void | serverPush (std::vector< uint8_t > &inp_payload, void *meta) |
Copies payload to ring buffer. More... | |
void | serverUseFd (EventFd &event_fd) |
Activate the file descriptor api for usage with select and poll. | |
bool | clientPull (int &index_out, void *meta) |
Returns the index of SharedMemoryRingBuffer::shmems that was just written. More... | |
bool | clientPullThread (int &index_out, void *meta) |
multithreading version: releases GIL | |
PyObject * | getBufferListPy () |
void | clientUseFd (EventFd &event_fd) |
Activate the file descriptor api for usage with select and poll. | |
Public Attributes | |
std::vector< SharedMemSegment * > | shmems |
Shared memory segments - can be exposed as numpy arrays. | |
uint8_t ** | cache |
One can expose a cache instead as numpy arrays - but this requires an additional copy step - enable with flag USE_SHMEM_CACHE. | |
Protected Member Functions | |
void | setFlag () |
Server: call this to indicate a ring-buffer overflow. | |
bool | flagIsSet () |
Client: call this to see if there has been a ring-buffer overflow. | |
void | clearFlag () |
Client: call this after handling the ring-buffer overflow. | |
int | getFlagValue () |
Used by SharedMemoryRingBuffer::flagIsSet() | |
void | zero () |
Force reset. Semaphore value is set to 0. | |
void | setEventFd () |
Set event file descriptor. | |
void | clearEventFd () |
Clear event file descriptor. | |
Protected Attributes | |
std::string | name |
int | n_cells |
int | n_bytes |
Parameters defining the shmem ring buffer (number, size) | |
int | mstimeout |
Semaphore timeout in milliseconds. | |
bool | is_server |
Are we on the server side or not? | |
sem_t * | sema |
sem_t * | flagsema |
Posix semaphore objects (semaphore counter, semaphore for the overflow flag) | |
std::string | sema_name |
Name to identify the posix semaphore counter. | |
std::string | flagsema_name |
Name to identify the posix semaphore used for the overflow flag. | |
int | index |
The index of cell that has just been written. Remember: server and client see their own copies of this. | |
struct timespec | ts |
Timespec for semaphore timeouts. | |
int | fd |
A file descriptor for poll and select. | |
Interprocess shared memory ring buffer synchronized with posix semaphores.
Create first a server instance. Then, in another process, create the client instance with the same name, but with is_server=false.
This class is only for multiprocessing. Don't never ever use it in the context of multithreading. And don't start the server and client side from the same process.
Don't expect this shmem ring buffer to work for high-throughput media streaming. It's good for sending a few frames per second between multiprocesses. For high-throughput cases, use multithreading instead.
SharedMemRingBufferBase::SharedMemRingBufferBase | ( | const char * | name, |
int | n_cells, | ||
std::size_t | n_bytes, | ||
int | mstimeout = 0 , |
||
bool | is_server = false |
||
) |
Default constructor.
name | Name of the ring buffer. This name is used as unique identifier for the posix semaphores and shmem segments. Don't use weird characters. |
n_cells | Number of cells in the ring buffer |
n_bytes | Size of each ring buffer cell in bytes |
mstimeout | Semaphore timeout in milliseconds. SharedMemRingBuffer::clientPull returns after this many milliseconds even if data was not received. Default 0 = waits forever. |
is_server | Set this to true if you are starting this from the server multiprocess |
Child classes should reserve correct SharedMemSegment type
bool SharedMemRingBufferBase::clientPull | ( | int & | index_out, |
void * | meta | ||
) |
Returns the index of SharedMemoryRingBuffer::shmems that was just written.
index_out | : returns the index of the shmem ringbuffer just written |
size_out | : returns the size of the payload written to the shmem ringbuffer |
returns true if data was obtained, false if semaphore timed out shmem index, metadata object to be filled
void SharedMemRingBufferBase::serverPush | ( | std::vector< uint8_t > & | inp_payload, |
void * | meta | ||
) |
Copies payload to ring buffer.
inp_payload | : std::vector bytebuffer (passed by reference) |
After copying the payload, releases (increases) the semaphore.