kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
Data Structures | Macros | Typedefs | Functions
threadpool.h File Reference
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  threadpool_config
 Configuration for thread pool. More...
 
struct  threadpool_task
 A task. More...
 
struct  threadpool_info
 Statistics about the current state of the threadpool. More...
 

Macros

#define THREADPOOL_MAX_RINGBUF_SIZE2   30
 

Typedefs

typedef void( threadpool_task_cb )(void *udata)
 Callback for a task, with an arbitrary user-supplied pointer. More...
 
typedef void( threadpool_task_cleanup_cb )(void *udata)
 Callback to clean up a cancelled task's data, with the arbitrary user-supplied pointer that would have gone to the task. More...
 

Functions

struct threadpoolThreadpool_Init (struct threadpool_config *cfg)
 Initialize a threadpool, according to a config. More...
 
bool Threadpool_Schedule (struct threadpool *t, struct threadpool_task *task, size_t *pushback)
 Schedule a task in the threadpool. More...
 
void Threadpool_Stats (struct threadpool *t, struct threadpool_info *ti)
 If TI is non-NULL, fill out some statistics about the operating state of the thread pool. More...
 
bool Threadpool_Shutdown (struct threadpool *t, bool kill_all)
 Notify the threadpool's threads that the system is going to shut down soon. More...
 
void Threadpool_Free (struct threadpool *t)
 Free a threadpool. More...
 

Macro Definition Documentation

#define THREADPOOL_MAX_RINGBUF_SIZE2   30

Definition at line 28 of file threadpool.h.

Typedef Documentation

typedef void( threadpool_task_cb)(void *udata)

Callback for a task, with an arbitrary user-supplied pointer.

Definition at line 41 of file threadpool.h.

typedef void( threadpool_task_cleanup_cb)(void *udata)

Callback to clean up a cancelled task's data, with the arbitrary user-supplied pointer that would have gone to the task.

Definition at line 45 of file threadpool.h.

Function Documentation

struct threadpool* Threadpool_Init ( struct threadpool_config cfg)
bool Threadpool_Schedule ( struct threadpool t,
struct threadpool_task task,
size_t *  pushback 
)

Schedule a task in the threadpool.

Returns whether the task was successfully registered or not. If Threadpool_Shutdown has been called, this function will always return false, due to API misuse.

If *pushback is non-NULL, it will be set to the number of tasks in the backlog, so code upstream can provide counterpressure.

TASK is copied into the threadpool by value.

Definition at line 99 of file threadpool.c.

References ATOMIC_BOOL_COMPARE_AND_SWAP, threadpool_task::cleanup, marked_task::cleanup, commit_current_task(), notify_new_task(), threadpool::shutting_down, threadpool_task::task, marked_task::task, threadpool::task_release_head, threadpool::task_reserve_head, threadpool::task_ringbuf_size, threadpool::tasks, threadpool_task::udata, and marked_task::udata.

void Threadpool_Stats ( struct threadpool t,
struct threadpool_info ti 
)
bool Threadpool_Shutdown ( struct threadpool t,
bool  kill_all 
)

Notify the threadpool's threads that the system is going to shut down soon.

Returns whether all previously active threads have shut down.

kill_all will eventually permit immediately shutting down all active threads (possibly with memory leaks), but is not yet implemented.

Returns whether everything has halted.

Definition at line 163 of file threadpool.c.

References ATOMIC_BOOL_COMPARE_AND_SWAP, marked_task::cleanup, threadpool::live_threads, notify_shutdown(), threadpool::shutting_down, SPIN_ADJ, thread_info::status, STATUS_SHUTDOWN, thread_info::t, threadpool::task_commit_head, threadpool::task_release_head, threadpool::task_request_head, threadpool::task_ringbuf_mask, threadpool::tasks, threadpool::threads, and marked_task::udata.

void Threadpool_Free ( struct threadpool t)

Free a threadpool.

Assumes either Threadpool_Shutdown() has been repeatedly called already, or leaking memory and other resources is acceptable.

Definition at line 200 of file threadpool.c.

References threadpool::tasks, and threadpool::threads.