lib<mpsc> 0.1.1
A C POSIX multi-thread based multiple producers, single consumer (MPSC) channel library
|
Go to the source code of this file.
Data Structures | |
struct | mpsc_create_params_t |
Typedefs | |
typedef struct mpsc_s | mpsc_t |
typedef struct mpsc_consumer_s | mpsc_consumer_t |
typedef struct mpsc_producer_s | mpsc_producer_t |
typedef void() | mpsc_producer_thread_callback_t(mpsc_producer_t *producer) |
typedef void() | mpsc_consumer_callback_t(mpsc_consumer_t *consumer, void *data, size_t n, bool closed) |
typedef void() | mpsc_consumer_error_callback_t(mpsc_consumer_t *consumer) |
Enumerations | |
enum | mpsc_register_producer_error_t { MPSC_REGISTER_PRODUCER_ERROR_NONE = 0 , MPSC_REGISTER_PRODUCER_ERROR_CLOSED = 1 , MPSC_REGISTER_PRODUCER_ERROR_N_MAX_PRODUCERS_REACHED = 2 , MPSC_REGISTER_PRODUCER_ERROR_EAGAIN = 3 } |
Functions | |
mpsc_t * | mpsc_create (mpsc_create_params_t params) |
void | mpsc_join (mpsc_t *self) |
mpsc_register_producer_error_t | mpsc_register_producer (mpsc_t *self, mpsc_producer_thread_callback_t callback, void *context) |
mpsc_register_producer_error_t | mpsc_consumer_register_producer (mpsc_consumer_t *self, mpsc_producer_thread_callback_t callback, void *context) |
void | mpsc_consumer_close (mpsc_consumer_t *self) |
bool | mpsc_producer_ping (mpsc_producer_t *self) |
bool | mpsc_producer_send (mpsc_producer_t *self, void *data, size_t n) |
bool | mpsc_producer_send_empty (mpsc_producer_t *self) |
void * | mpsc_producer_context (mpsc_producer_t *self) |
mpsc_register_producer_error_t | mpsc_producer_register_producer (mpsc_producer_t *self, mpsc_producer_thread_callback_t callback, void *context) |
struct mpsc_create_params_t |
The structure that must be passed to mpsc_create to instantiate a new mpsc_t object.
Data Fields | ||
---|---|---|
size_t | buffer_size |
The size (in bytes) of the internal buffer used to transfer a message between a producer and the consumer.
|
mpsc_consumer_callback_t * | consumer_callback | The application defined consumer callback function to be used to received messages for the mpsc_t instance. |
mpsc_consumer_error_callback_t * | consumer_error_callback |
An optional, application defined producer thread callback function used, when error_handling_enabled = true , to deliver potential internal errors to the application, in which case errno should be checked for more information.
|
bool | create_and_join_thread_safety_disabled | A boolean value that can be used to disable the safety feature that prevents mpsc_create and mpsc_join to be called on distinct threads for the same mpsc_t instance. |
bool | error_handling_enabled |
A boolean value indicating whether (true ) error handling should be handed over to the application or whether (false ) errors should be printed to stderr and the process be terminated.
|
size_t | n_max_producers |
The maximum number of producers that can be registered on the mpsc_t instance.
|
typedef void() mpsc_consumer_callback_t(mpsc_consumer_t *consumer, void *data, size_t n, bool closed) |
The signature of the consumer callback function, to be declared and implemented by the application, which is passed as a parameter to the mpsc_create function when instantiating a new channel.
consumer | A pointer to a mpsc_consumer_t instance for which the callback is being executed. |
data | A pointer to dynamically allocated memory containing the message sent by a producer to the consumer. |
n | The size (in bytes) of data . |
closed | A boolean value indicating whether the channel (i.e., the mpsc_t instance) has been marked as closed, in which case the callback won't be called again for the given mpsc_t instance. |
n
is non-zero, data
refers to dynamically allocated memory that is the responsibility of the callback. In other words, as soon as data
is no longer needed, it should be freed using free , else memory will be leaked. closed
argument to be true
: (1) the mpsc_consumer_close function was called on consumer
from inside the callback or (2) the mpsc_join function has been called on the channel object and all producer threads have returned. n
is set to 0, data
will be set to NULL . typedef void() mpsc_consumer_error_callback_t(mpsc_consumer_t *consumer) |
The signature of an optional consumer error callback function, to be declared and implemented by the application, which is passed as a parameter to the mpsc_create function when instantiating a new channel.
error_handling_enabled
is set to false
, in which case, when an error occurs, information about that error is printed to stderr and the process is terminated. consumer
(i.e., the specific mpsc_consumer_t instance), the application should look at errno for information about the encountered error. Currently, the only possible error is ENOMEM, which arises from a failed internal call to malloc . typedef void() mpsc_producer_thread_callback_t(mpsc_producer_t *producer) |
The signature of the producer thread callback function, to be declared and implemented by the application, which is passed as a parameter to the mpsc_register_producer function when registering a producer for a given mpsc_t instance.
producer | A pointer to a mpsc_producer_t instance for which the callback is being executed. |
typedef struct mpsc_s mpsc_t |
An opaque data type used as a container for the MPSC channel data.
The type returned by mpsc_register_producer (as well as by its aliases; i.e., mpsc_producer_register_producer and mpsc_consumer_register_producer ) when trying to register a new producer on a mpsc_t instance, and which indicates whether an error occurred.
Enumerator | |
---|---|
MPSC_REGISTER_PRODUCER_ERROR_NONE | The producer was successfully registered. |
MPSC_REGISTER_PRODUCER_ERROR_CLOSED | The producer could not be registered because the mpsc_t instance has internally been marked as |
MPSC_REGISTER_PRODUCER_ERROR_N_MAX_PRODUCERS_REACHED | The producer could not be registered because the maximum number of producers allowed (i.e., |
MPSC_REGISTER_PRODUCER_ERROR_EAGAIN | The producer could not be registered because a EAGAIN error was observed when, internally, while trying to create a new thread using pthread_create .
|
void mpsc_consumer_close | ( | mpsc_consumer_t * | self | ) |
A function that can be used (from inside the application defined consumer callback implementing mpsc_consumer_callback_t ) on the consumer object self
to notify the channel's internal consumer thread that it should return.
self | A pointer to a mpsc_consumer_t instance whose parent object (a mpsc_t instance) should be marked as closed. |
closed
argument marked as true
. closed = true
if all producer threads have returned and mpsc_join has been called. mpsc_register_producer_error_t mpsc_consumer_register_producer | ( | mpsc_consumer_t * | self, |
mpsc_producer_thread_callback_t | callback, | ||
void * | context | ||
) |
An alias for mpsc_register_producer , but which is used on an object of type mpsc_consumer_t , to try to register a producer for self
's parent channel object.
self | A pointer to the mpsc_consumer_t object for whose parent (i.e., a mpsc_t instance) to register a new producer. |
callback | An application defined thread callback function, which conforms to the mpsc_producer_thread_callback_t interface, to be used by the producer. |
context | An application defined context object that can be retrieved from inside callback by calling the mpsc_producer_context function on the callback 's mpsc_producer_t argument. |
mpsc_t * mpsc_create | ( | mpsc_create_params_t | params | ) |
The function used to create a new channel instance (i.e., a mpsc_t instance).
params | The instance's configurations. (Note: See documentation for mpsc_create_params_t for the details). |
error_handling_enabled = false
, information about the error will be printed to stderr and the process will be terminated, in which case the returned value doesn't need assertion. If, on the other hand, error_handling_enabled = true
, then NULL will be returned and a handleable error will be available on errno . It should be noted, however, that not all errors can be handled by the application: some errors will always, regardless of error_handling_enabled
's value, result in the process being terminated. Currently, the only errors that can be handled by the application are those related to resources exhaustion (i.e., ENOMEM or EAGAIN ), which, internally, can occur when calling malloc , pthread_mutex_init , pthread_cond_init , or pthread_create . The function that must be called on self
to wait for the channel close.
self
's internal resources will be destroyed and the memory freed. self
(i.e., the mpsc_t object). self | A pointer to the mpsc_t instance to be joined. |
void * mpsc_producer_context | ( | mpsc_producer_t * | self | ) |
A function that can be used from inside the producer thread callback function to retrieve the application defined context object passed to mpsc_register_producer when the producer was registered.
self | A pointer to the mpsc_producer_t instance for which to retrieve the application defined context (i.e., the user context). |
bool mpsc_producer_ping | ( | mpsc_producer_t * | self | ) |
A function that can be used from inside a producer thread callback to check whether the channel to which self
belongs is still opened.
self | A pointer to the mpsc_producer_t instance for which to check whether the channel is still open. |
true
) or whether it has been marked as closed (false
). mpsc_register_producer_error_t mpsc_producer_register_producer | ( | mpsc_producer_t * | self, |
mpsc_producer_thread_callback_t | callback, | ||
void * | context | ||
) |
An alias for mpsc_register_producer , but which is used on an object of type mpsc_producer_t , to try to register a producer for self
's parent channel object.
self | A pointer to the mpsc_producer_t object for whose parent (i.e., a mpsc_t instance) to register a new producer. |
callback | An application defined thread callback function, which conforms to the mpsc_producer_thread_callback_t interface, to be used by the producer. |
context | An application defined context object that can be retrieved from inside callback by calling the mpsc_producer_context function on the callback 's mpsc_producer_t argument. |
bool mpsc_producer_send | ( | mpsc_producer_t * | self, |
void * | data, | ||
size_t | n | ||
) |
The function used (from inside a producer thread callback function) to send a message to the channel's consumer.
self | A pointer to the mpsc_producer_t instance for which to send a message down the underlying channel, to be delivered to the consumer. |
data | A pointer to arbitrary bytes ( n bytes) to be sent to the channel's consumer. |
n | the message size, in bytes. |
false
means that the channel has been marked as closed and that, as a consequence, the message could not be delivered. true
means that the message was successfully copied to the internal buffer and will eventually be picked up and copied by the internal consumer thread for delivery to the consumer callback. n
) should never be greater than mpsc_create_params_t 's the buffer_size
parameter value (which was specified when creating the channel using mpsc_create ). If n > buffer_size
, an error message will be printed to stderr and the process will be terminated. data = NULL
and n = 0
, although there is a function called mpsc_producer_send_empty that can be used for that purpose. n
bytes from data
are temporarily copied to the internal buffer. So, if the producer thread callback function implementation uses dynamic memory allocation for data
, it must call free() on that memory when it's no longer needed to avoid a leak. closed
argument set to true
, and the call to mpsc_join will return. bool mpsc_producer_send_empty | ( | mpsc_producer_t * | self | ) |
Similar to mpsc_producer_send , except that this function is used (from inside a producer thread callback function) to send an empty message.
self | A pointer to the mpsc_producer_t instance for which to send a message down the underlying channel, to be delivered to the consumer. |
false
means that the channel has been marked as closed and that, as a consequence, the message could not be delivered. true
means that the message was accepted and will eventually be picked up by the internal consumer thread for delivery to the consumer callback. closed
argument set to true
, and the call to mpsc_join will return. mpsc_register_producer_error_t mpsc_register_producer | ( | mpsc_t * | self, |
mpsc_producer_thread_callback_t | callback, | ||
void * | context | ||
) |
The function used to register a new producer for self
.
self | A pointer to the mpsc_t instance for which to register a new producer. |
callback | An application defined thread callback function, which conforms to the mpsc_producer_thread_callback_t interface, to be used by the producer. |
context | An application defined context object that can be retrieved from inside callback by calling the mpsc_producer_context function on the callback 's mpsc_producer_t argument. |