cthreadcomm · Section 3
ccircq_create_mp
NAME
ccircq_create_mp - create a circular queue with custom memory management
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cthreadcomm.h>
ccircq *ccircq_create_mp(size_t max_size,
ccol_memmgmt_procs *mmgmt_procs, char **err_str);
DESCRIPTION
Creates a fixed-size queue on top of a circular buffer. A send on a full queue blocks until there is free space, and a new queue starts with sends turned on. Every allocation the queue makes, at creation and for the rest of its life, goes through mmgmt_procs. The queue keeps its own copy of the procedures and frees itself through them, so the caller's ccol_memmgmt_procs doesn't have to outlive this call. NULL selects the default malloc and free, which is what the ccircq_create() macro passes. The queue lives in one array of max_size * sizeof(cmessage) bytes that is allocated up front, so its memory use stays the same as messages come and go.
PARAMETERS
max_size Maximum number of messages that the queue can hold: At
least 1, no larger than ccol_max_elem_count, and no larger
than SIZE_MAX / sizeof(cmessage), above which the size
of the backing array would overflow.
mmgmt_procs Custom memory management procedures, or NULL to use the
default malloc and free. If the value is not NULL, all of
malloc, calloc, realloc and free must be set.
err_str Optional pointer that gets an error string on failure. Pass
NULL to ignore it.
RETURN VALUE
On success, a pointer to the new circular queue, and *err_str is set to NULL when err_str is not NULL. On failure, NULL, and *err_str describes the reason when err_str is not NULL. The call fails when max_size is 0, when it is larger than ccol_max_elem_count or than SIZE_MAX / sizeof(cmessage), when mmgmt_procs is malformed because it sets some but not all of malloc, calloc, realloc and free, when an allocation fails, when the queue's mutex or one of its condition variables cannot be initialized, and, in a build with fork-safety protection compiled in, when the queue's mutex cannot be registered with the fork-safety registry. That registration also fails once the library's process-exit destructors have run and the last queue is gone, for example in an application destructor that runs after them. None of these failures leaves anything allocated.
NOTES
All operations on this queue are thread-safe. When you are done with the queue, destroy it with ccircq_destroy(). fork(2) and this queue can be combined in two supported ways: Call fork(2) before you create the queue, or create it and then call fork(2) followed at once by exec(3). A program that keeps running with no exec(3) after the fork while a queue from before the fork is live is not supported. As a defense in depth beyond that rule, the child never inherits a lock that another thread held, in the locked state, at the instant of the fork. The queue owns no thread of its own either, so a queue that a child inherits this way stays fully usable.
SEE ALSO
ccircq_create(3), ccircq_destroy(3), ccircq_send(3), ccircq_recv(3)