DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Posix thread interface

Functions for starting and managing threads. More...

Files

file  thread.c
 Functions for starting and managing threads.
 

Data Structures

struct  thread_pvt
 thread struct used to create threads data needs to be first element More...
 
struct  threadcontainer
 Global threads data. More...
 

Typedefs

typedef void(* threadcleanup )(void *)
 Function called after thread termination. More...
 
typedef void *(* threadfunc )(void *)
 Thread function. More...
 
typedef int(* threadsighandler )(int, void *)
 Thread signal handler function. More...
 

Enumerations

enum  thread_option_flags { THREAD_OPTION_CANCEL = 1 << 0, THREAD_OPTION_JOINABLE = 1 << 1, THREAD_OPTION_RETURN = 1 << 2 }
 Options supplied to framework_mkthread all defaults are unset. More...
 
enum  threadopt {
  TL_THREAD_NONE = 1 << 0, TL_THREAD_RUN = 1 << 1, TL_THREAD_DONE = 1 << 2, TL_THREAD_JOIN = 1 << 3,
  TL_THREAD_STOP = 1 << 4, TL_THREAD_CAN_CANCEL = 1 << 16, TL_THREAD_JOINABLE = 1 << 17, TL_THREAD_RETURN = 1 << 18
}
 Thread status a thread can be disabled by unsetting TL_THREAD_RUN. More...
 

Functions

int framework_threadok ()
 let threads check there status. More...
 
int startthreads (void)
 Initialise the threadlist and start manager thread. More...
 
void stopthreads (int join)
 Signal manager to stop and cancel all running threads. More...
 
struct thread_pvtframework_mkthread (threadfunc func, threadcleanup cleanup, threadsighandler sig_handler, void *data, int flags)
 create a thread result must be unreferenced More...
 
void jointhreads (void)
 Join the manager thread. More...
 
int thread_signal (int sig)
 Handle signal if its for me. More...
 

Variables

struct threadcontainerthreads = NULL
 Thread control data. More...
 
int thread_can_start = 1
 Automatically start manager thread. More...
 

Detailed Description

Functions for starting and managing threads.

See Also
Thread Interface The thread interface consists of a management thread managing a hashed bucket list of threads running optional clean up when done.

Typedef Documentation

typedef void(* threadcleanup)(void *)

Function called after thread termination.

See Also
framework_mkthread()
Parameters
dataReference of thread data.

Definition at line 238 of file dtsapp.h.

typedef void*(* threadfunc)(void *)

Thread function.

See Also
framework_mkthread()
Parameters
dataPoinnter to reference of thread data.

Definition at line 245 of file dtsapp.h.

typedef int(* threadsighandler)(int, void *)

Thread signal handler function.

See Also
framework_mkthread()
Parameters
dataReference of thread data.

Definition at line 252 of file dtsapp.h.

Enumeration Type Documentation

Options supplied to framework_mkthread all defaults are unset.

Note
this is shifted 16 bits limiting 16 options this maps to high 16 bits of threadopt
Enumerator
THREAD_OPTION_CANCEL 

Flag to enable pthread_cancel calls this is not recomended and can lead to memory leaks.

THREAD_OPTION_JOINABLE 

Create the the thread joinable only do this if you will be joining it cancelable threads are best detached.

THREAD_OPTION_RETURN 

Return reference to thread this must be unreferenced.

Definition at line 118 of file dtsapp.h.

118  {
120  THREAD_OPTION_CANCEL = 1 << 0,
122  THREAD_OPTION_JOINABLE = 1 << 1,
124  THREAD_OPTION_RETURN = 1 << 2
125 };
Create the the thread joinable only do this if you will be joining it cancelable threads are best det...
Definition: dtsapp.h:122
Return reference to thread this must be unreferenced.
Definition: dtsapp.h:124
Flag to enable pthread_cancel calls this is not recomended and can lead to memory leaks...
Definition: dtsapp.h:120
enum threadopt

Thread status a thread can be disabled by unsetting TL_THREAD_RUN.

Note
bits 16-31 are useoptions see thread_option_flags
Enumerator
TL_THREAD_NONE 

No status.

TL_THREAD_RUN 

thread is marked as running

TL_THREAD_DONE 

thread is marked as complete

TL_THREAD_JOIN 

Quit when only manager is left.

Note
This flag is only valid for manager thread
TL_THREAD_STOP 

Quit when only manager is left.

Note
This flag is only valid for manager thread
TL_THREAD_CAN_CANCEL 

Flag to enable pthread_cancel calls.

TL_THREAD_JOINABLE 

Flag to enable pthread_cancel calls.

TL_THREAD_RETURN 

Definition at line 36 of file thread.c.

36  {
38  TL_THREAD_NONE = 1 << 0,
40  TL_THREAD_RUN = 1 << 1,
42  TL_THREAD_DONE = 1 << 2,
45  TL_THREAD_JOIN = 1 << 3,
48  TL_THREAD_STOP = 1 << 4,
49 
51  TL_THREAD_CAN_CANCEL = 1 << 16,
53  TL_THREAD_JOINABLE = 1 << 17,
54  TL_THREAD_RETURN = 1 << 18
55 };
Quit when only manager is left.
Definition: thread.c:48
Quit when only manager is left.
Definition: thread.c:45
No status.
Definition: thread.c:38
Flag to enable pthread_cancel calls.
Definition: thread.c:51
thread is marked as complete
Definition: thread.c:42
thread is marked as running
Definition: thread.c:40
Flag to enable pthread_cancel calls.
Definition: thread.c:53

Function Documentation

struct thread_pvt* framework_mkthread ( threadfunc  func,
threadcleanup  cleanup,
threadsighandler  sig_handler,
void *  data,
int  flags 
)

create a thread result must be unreferenced

Note
If the manager thread has not yet started this will start the manager thread.
Warning
THREAD_OPTION_RETURN flag controls the return of this function.
Threads should periodically check the result of framework_threadok() and cleanup or use THREAD_OPTION_CANCEL
Parameters
funcFunction to run thread on.
cleanupCleanup function to run.
sig_handlerThread signal handler.
dataData to pass to callbacks.
flagsOptions of thread_option_flags passed
Returns
a thread structure that must be un referencend OR NULL depending on flags.

Definition at line 387 of file thread.c.

References addtobucket(), thread_pvt::cleanup, thread_pvt::data, thread_pvt::flags, thread_pvt::func, threadcontainer::list, threadcontainer::manager, objalloc(), objlock(), objref(), objunlock(), objunref(), setflag, thread_pvt::sighandler, startthreads(), testflag, thread_pvt::thr, thread_can_start, TL_THREAD_RETURN, and TL_THREAD_RUN.

Referenced by nf_ctrack_trace(), startthreads(), and unixsocket_server().

387  {
388  struct thread_pvt *thread;
389  struct threadcontainer *tc = NULL;
390 
391  /*Grab a reference for threads in this scope start up if we can*/
392  if (!(tc = (objref(threads)) ? threads : NULL)) {
393  if (!thread_can_start) {
394  return NULL;
395  } else if (!startthreads()) {
396  return NULL;
397  }
398  if (!(tc = (objref(threads)) ? threads : NULL)) {
399  return NULL;
400  }
401  }
402 
403  objlock(tc);
404  /* dont allow threads if no manager or it not started*/
405  if ((!tc->manager || !func) && (func != managethread)) {
406  /*im shuting down*/
407  objunlock(tc);
408  objunref(tc);
409  return NULL;
410  } else if (!(thread = objalloc(sizeof(*thread), free_thread))) {
411  /* could not create*/
412  objunlock(tc);
413  objunref(tc);
414  return NULL;
415  }
416 
417  thread->data = (objref(data)) ? data : NULL;
418  thread->flags = flags << 16;
419  thread->cleanup = cleanup;
420  thread->sighandler = sig_handler;
421  thread->func = func;
422  objunlock(tc);
423 
424  /* start thread and check it*/
425  if (pthread_create(&thread->thr, NULL, threadwrap, thread) || pthread_kill(thread->thr, 0)) {
426  objunref(thread);
427  objunref(tc);
428  return NULL;
429  }
430 
431  /*Activate the thread it needs to be flaged to run or it will die*/
432  objlock(tc);
433  addtobucket(tc->list, thread);
434  setflag(thread, TL_THREAD_RUN);
435  objunlock(tc);
436  objunref(tc);
437 
438  if (testflag(thread, TL_THREAD_RETURN)) {
439  return thread;
440  } else {
441  objunref(thread);
442  return NULL;
443  }
444 }
threadcleanup cleanup
Thread cleanup callback.
Definition: thread.c:65
thread struct used to create threads data needs to be first element
Definition: thread.c:58
int objref(void *data)
Reference a object.
Definition: refobj.c:153
Global threads data.
Definition: thread.c:78
#define testflag(obj, flag)
Atomically test a flag in the flags field of a referenced object.
Definition: dtsapp.h:932
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
threadsighandler sighandler
Thread signal handler.
Definition: thread.c:71
threadfunc func
Thread function.
Definition: thread.c:68
int thread_can_start
Automatically start manager thread.
Definition: thread.c:92
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int startthreads(void)
Initialise the threadlist and start manager thread.
Definition: thread.c:268
#define setflag(obj, flag)
Atomically set a flag in the flags field of a referenced object.
Definition: dtsapp.h:925
struct bucket_list * list
Hashed bucket list of threads.
Definition: thread.c:80
void * data
Reference to data held on thread creation.
Definition: thread.c:60
enum threadopt flags
thread options
Definition: thread.c:74
pthread_t thr
Thread information.
Definition: thread.c:62
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
thread is marked as running
Definition: thread.c:40
struct threadcontainer * threads
Thread control data.
Definition: thread.c:86
struct thread_pvt * manager
Manager thread.
Definition: thread.c:82
int addtobucket(struct bucket_list *blist, void *data)
Add a reference to the bucketlist.
Definition: refobj.c:428
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int framework_threadok ( void  )

let threads check there status.

Returns
0 if the thread should terminate.

Definition at line 143 of file thread.c.

References objunref(), testflag, thread_pvt::thr, and TL_THREAD_RUN.

143  {
144  struct thread_pvt *thr;
145  int ret;
146 
147  thr = get_thread_from_id();
148  ret =(thr) ? testflag(thr, TL_THREAD_RUN) : 0;
149  objunref(thr);
150 
151  return ret;
152 }
thread struct used to create threads data needs to be first element
Definition: thread.c:58
#define testflag(obj, flag)
Atomically test a flag in the flags field of a referenced object.
Definition: dtsapp.h:932
pthread_t thr
Thread information.
Definition: thread.c:62
thread is marked as running
Definition: thread.c:40
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
void jointhreads ( void  )

Join the manager thread.

This will be done when you have issued stopthreads and are waiting or have completed the program and want to let the threads continue. for threads to exit.

Definition at line 450 of file thread.c.

References threadcontainer::manager, objlock(), objref(), objunlock(), objunref(), setflag, thread_pvt::thr, and TL_THREAD_JOIN.

450  {
451  struct threadcontainer *tc;
452 
453  tc = (objref(threads)) ? threads : NULL;
454  if (!tc) {
455  return;
456  }
457 
458  objlock(tc);
459  if (tc->manager) {
461  objunlock(tc);
462  pthread_join(tc->manager->thr, NULL);
463  } else {
464  objunlock(tc);
465  }
466  objunref(tc);
467 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
Global threads data.
Definition: thread.c:78
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
Quit when only manager is left.
Definition: thread.c:45
#define setflag(obj, flag)
Atomically set a flag in the flags field of a referenced object.
Definition: dtsapp.h:925
pthread_t thr
Thread information.
Definition: thread.c:62
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
struct threadcontainer * threads
Thread control data.
Definition: thread.c:86
struct thread_pvt * manager
Manager thread.
Definition: thread.c:82
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int startthreads ( void  )

Initialise the threadlist and start manager thread.

Note
There is no need to call this as it will start when first thread starts.
Returns
1 On success 0 on failure.

Definition at line 268 of file thread.c.

References create_bucketlist(), framework_mkthread(), threadcontainer::list, threadcontainer::manager, objalloc(), objref(), objunref(), THREAD_OPTION_JOINABLE, and THREAD_OPTION_RETURN.

Referenced by framework_mkthread().

268  {
269  struct threadcontainer *tc;
270 
271  tc = (objref(threads)) ? threads : NULL;
272 
273  if (tc) {
274  objunref(tc);
275  return 1;
276  }
277 
278  if (!(tc = objalloc(sizeof(*threads), close_threads))) {
279  return 0;
280  }
281 
282  if (!tc->list && !(tc->list = create_bucketlist(4, hash_thread))) {
283  objunref(tc);
284  return 0;
285  }
286 
287  threads = tc;
288  if (!(tc->manager = framework_mkthread(managethread, manage_clean, manager_sig, NULL, THREAD_OPTION_JOINABLE | THREAD_OPTION_RETURN))) {
289  objunref(tc);
290  return 0;
291  }
292 
293  return 1;
294 }
void * create_bucketlist(int bitmask, blisthash hash_function)
Definition: refobj.c:356
int objref(void *data)
Reference a object.
Definition: refobj.c:153
Create the the thread joinable only do this if you will be joining it cancelable threads are best det...
Definition: dtsapp.h:122
Global threads data.
Definition: thread.c:78
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
struct thread_pvt * framework_mkthread(threadfunc, threadcleanup, threadsighandler, void *data, int flags)
create a thread result must be unreferenced
Definition: thread.c:387
Return reference to thread this must be unreferenced.
Definition: dtsapp.h:124
struct bucket_list * list
Hashed bucket list of threads.
Definition: thread.c:80
struct threadcontainer * threads
Thread control data.
Definition: thread.c:86
struct thread_pvt * manager
Manager thread.
Definition: thread.c:82
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
void stopthreads ( int  join)

Signal manager to stop and cancel all running threads.

This should always be called at shutdown if there have been threads started.

See Also
framework_init()
FRAMEWORK_MAIN()
Parameters
joinA non zero value to join the manager thread after flaging the shutdown.

Definition at line 303 of file thread.c.

References threadcontainer::manager, objlock(), objref(), objunlock(), objunref(), setflag, thread_pvt::thr, TL_THREAD_JOIN, and TL_THREAD_STOP.

Referenced by framework_init().

303  {
304  struct threadcontainer *tc;
305 
306  tc = (objref(threads)) ? threads : NULL;
307  if (!tc) {
308  return;
309  }
310 
311  objlock(tc);
312  if (tc->manager) {
314  if (join) {
316  objunlock(tc);
317  pthread_join(tc->manager->thr, NULL);
318  } else {
319  objunlock(tc);
320  }
321  } else {
322  objunlock(tc);
323  }
324  objunlock(tc);
325  objunref(tc);
326 }
Quit when only manager is left.
Definition: thread.c:48
int objref(void *data)
Reference a object.
Definition: refobj.c:153
Global threads data.
Definition: thread.c:78
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
Quit when only manager is left.
Definition: thread.c:45
#define setflag(obj, flag)
Atomically set a flag in the flags field of a referenced object.
Definition: dtsapp.h:925
pthread_t thr
Thread information.
Definition: thread.c:62
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
struct threadcontainer * threads
Thread control data.
Definition: thread.c:86
struct thread_pvt * manager
Manager thread.
Definition: thread.c:82
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int thread_signal ( int  sig)

Handle signal if its for me.

find the thread the signal was delivered to if the signal was handled returns 1 if the thread could not be handled returns -1 returns 0 if no thread is found NB sending a signal to the current thread while threads is locked will cause a deadlock.

Warning
This is not to be called directly but by the installed system signal handler.
Note
This is not supported on Win32
Parameters
sigSignal to pass.
Returns
1 on success -1 on error.

Definition at line 496 of file thread.c.

References objunref().

496  {
497  int ret = 0;
498 #ifndef __WIN32
499  struct thread_pvt *thread = NULL;
500 
501  if (!(thread = get_thread_from_id())) {
502  return 0;
503  }
504 
505  switch(sig) {
506  case SIGUSR1:
507  case SIGUSR2:
508  case SIGHUP:
509  case SIGALRM:
510  ret = handle_thread_signal(thread, sig);
511  break;
512  case SIGINT:
513  case SIGTERM:
514  ret = handle_thread_signal(thread, sig);
515  }
516  objunref(thread);
517 #endif
518  return ret;
519 }
thread struct used to create threads data needs to be first element
Definition: thread.c:58
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184

Variable Documentation

int thread_can_start = 1

Automatically start manager thread.

If threads have not been started and a thread is created the manager thread will be started. once threads have stoped this will be set to zero manually starting startthreads will be possible.

Definition at line 92 of file thread.c.

Referenced by framework_mkthread().

struct threadcontainer* threads = NULL

Thread control data.

Definition at line 86 of file thread.c.