kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
threadpool.h
Go to the documentation of this file.
1 /*
2 * kinetic-c
3 * Copyright (C) 2015 Seagate Technology.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20 #ifndef THREADPOOL_H
21 #define THREADPOOL_H
22 
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 
27 /* More than a billion tasks in the backlog is likely an error. */
28 #define THREADPOOL_MAX_RINGBUF_SIZE2 30
29 
31 struct threadpool;
32 
35  uint8_t task_ringbuf_size2; //> log2(size) of task ring buffer
36  size_t max_delay; //> max delay, in msec. 0 => default
37  uint8_t max_threads; //> max threads to alloc on demand
38 };
39 
41 typedef void (threadpool_task_cb)(void *udata);
42 
45 typedef void (threadpool_task_cleanup_cb)(void *udata);
46 
49  threadpool_task_cb *task; //> Task function pointer.
50  threadpool_task_cleanup_cb *cleanup; //> Cleanup function pointer.
51  void *udata; //> User data to pass along to function pointer.
52 };
53 
56  uint8_t active_threads;
57  uint8_t dormant_threads;
58  size_t backlog_size;
59 };
60 
62 struct threadpool *Threadpool_Init(struct threadpool_config *cfg);
63 
72 bool Threadpool_Schedule(struct threadpool *t, struct threadpool_task *task,
73  size_t *pushback);
74 
77 void Threadpool_Stats(struct threadpool *t, struct threadpool_info *ti);
78 
86 bool Threadpool_Shutdown(struct threadpool *t, bool kill_all);
87 
91 void Threadpool_Free(struct threadpool *t);
92 
93 #ifdef TEST
94 #include "threadpool_internals.h"
95 #endif
96 
97 #endif
bool Threadpool_Shutdown(struct threadpool *t, bool kill_all)
Notify the threadpool's threads that the system is going to shut down soon.
Definition: threadpool.c:163
void( threadpool_task_cleanup_cb)(void *udata)
Callback to clean up a cancelled task's data, with the arbitrary user-supplied pointer that would hav...
Definition: threadpool.h:45
size_t backlog_size
Definition: threadpool.h:58
threadpool_task_cb * task
Definition: threadpool.h:49
Configuration for thread pool.
Definition: threadpool.h:34
bool Threadpool_Schedule(struct threadpool *t, struct threadpool_task *task, size_t *pushback)
Schedule a task in the threadpool.
Definition: threadpool.c:99
void( threadpool_task_cb)(void *udata)
Callback for a task, with an arbitrary user-supplied pointer.
Definition: threadpool.h:41
Internal threadpool state.
uint8_t active_threads
Definition: threadpool.h:56
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...
Definition: threadpool.c:149
uint8_t task_ringbuf_size2
Definition: threadpool.h:35
Statistics about the current state of the threadpool.
Definition: threadpool.h:55
A task.
Definition: threadpool.h:48
threadpool_task_cleanup_cb * cleanup
Definition: threadpool.h:50
void Threadpool_Free(struct threadpool *t)
Free a threadpool.
Definition: threadpool.c:200
struct threadpool * Threadpool_Init(struct threadpool_config *cfg)
Initialize a threadpool, according to a config.
Definition: threadpool.c:52
uint8_t dormant_threads
Definition: threadpool.h:57
uint8_t max_threads
Definition: threadpool.h:37