94 static int32_t hash_thread(
const void *data,
int key) {
96 const pthread_t *th = (key) ? data : &thread->
thr;
97 return jenhash(th,
sizeof(pthread_t), 0);
100 static void close_threads(
void *
data) {
114 static void free_thread(
void *data) {
122 static struct thread_pvt *get_thread_from_id() {
127 if (!(tc = (
objref(threads)) ? threads : NULL)) {
147 thr = get_thread_from_id();
157 static int manager_sig(
int sig,
void *data) {
161 if (!(thread = get_thread_from_id())) {
183 static void manage_clean(
void *data) {
192 static void stop_threads(
void *data,
void *data2) {
197 if (!pthread_equal(man->
thr, thread->
thr)) {
199 pthread_kill(thread->
thr, SIGTERM);
202 pthread_cancel(thread->
thr);
212 static void *managethread(
void *data) {
216 if (!(thread = get_thread_from_id())) {
271 tc = (
objref(threads)) ? threads : NULL;
278 if (!(tc =
objalloc(
sizeof(*threads), close_threads))) {
306 tc = (
objref(threads)) ? threads : NULL;
328 static void thread_cleanup(
void *data) {
345 static void *threadwrap(
void *data) {
360 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
362 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
366 pthread_detach(thread->
thr);
369 pthread_cleanup_push(thread_cleanup, thread);
371 pthread_cleanup_pop(1);
392 if (!(tc = (
objref(threads)) ? threads : NULL)) {
398 if (!(tc = (
objref(threads)) ? threads : NULL)) {
405 if ((!tc->
manager || !func) && (func != managethread)) {
410 }
else if (!(thread =
objalloc(
sizeof(*thread), free_thread))) {
418 thread->
flags = flags << 16;
425 if (pthread_create(&thread->
thr, NULL, threadwrap, thread) || pthread_kill(thread->
thr, 0)) {
453 tc = (
objref(threads)) ? threads : NULL;
471 static int handle_thread_signal(
struct thread_pvt *thread,
int sig) {
501 if (!(thread = get_thread_from_id())) {
510 ret = handle_thread_signal(thread, sig);
514 ret = handle_thread_signal(thread, sig);
threadcleanup cleanup
Thread cleanup callback.
thread struct used to create threads data needs to be first element
void jointhreads(void)
Join the manager thread.
void * create_bucketlist(int bitmask, blisthash hash_function)
Quit when only manager is left.
int objref(void *data)
Reference a object.
Create the the thread joinable only do this if you will be joining it cancelable threads are best det...
#define testflag(obj, flag)
Atomically test a flag in the flags field of a referenced object.
void *(* threadfunc)(void *)
Thread function.
void stopthreads(int join)
Signal manager to stop and cancel all running threads.
int objlock(void *data)
Lock the reference.
threadsighandler sighandler
Thread signal handler.
threadfunc func
Thread function.
int thread_can_start
Automatically start manager thread.
Quit when only manager is left.
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
int startthreads(void)
Initialise the threadlist and start manager thread.
int bucket_list_cnt(struct bucket_list *blist)
Return number of items in the list.
#define setflag(obj, flag)
Atomically set a flag in the flags field of a referenced object.
#define clearflag(obj, flag)
Atomically clear a flag in the flags field of a referenced object.
DTS Application library API Include file.
Flag to enable pthread_cancel calls.
struct thread_pvt * framework_mkthread(threadfunc, threadcleanup, threadsighandler, void *data, int flags)
create a thread result must be unreferenced
Return reference to thread this must be unreferenced.
struct bucket_list * list
Hashed bucket list of threads.
void * data
Reference to data held on thread creation.
int thread_signal(int sig)
Handle signal if its for me.
enum threadopt flags
thread options
pthread_t thr
Thread information.
int objunlock(void *data)
Unlock a reference.
int framework_threadok(void)
let threads check there status.
thread is marked as complete
thread is marked as running
void(* threadcleanup)(void *)
Function called after thread termination.
struct threadcontainer * threads
Thread control data.
threadopt
Thread status a thread can be disabled by unsetting TL_THREAD_RUN.
int(* threadsighandler)(int, void *)
Thread signal handler function.
struct thread_pvt * manager
Manager thread.
void bucketlist_callback(struct bucket_list *blist, blist_cb callback, void *data2)
Run a callback function on all items in the list.
#define jenhash(key, length, initval)
Define jenhash as hashlittle on big endian it should be hashbig.
Flag to enable pthread_cancel calls.
int addtobucket(struct bucket_list *blist, void *data)
Add a reference to the bucketlist.
void * bucket_list_find_key(struct bucket_list *list, const void *key)
Find and return a reference to a item matching supplied key.
void remove_bucket_item(struct bucket_list *blist, void *data)
Remove and unreference a item from the list.
int objunref(void *data)
Drop reference held.
Bucket list, hold hashed objects in buckets.