BFLibC 0.1
C Library
Loading...
Searching...
No Matches
thread.c File Reference
#include "thread.h"
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdbool.h>
#include "free.h"
#include "lock.h"
Include dependency graph for thread.c:

Data Structures

struct  _BFThreadAsyncID
 
struct  _ThreadIDEntry
 
struct  _ThreadIDTable
 
struct  _BFThreadRoutineParams
 

Macros

#define _GNU_SOURCE
 
#define _BFThreadTypeSync   1
 
#define _BFThreadTypeAsync   -1
 
#define _BFThreadTypeAsyncDetached   -2
 
#define FLAGS_GET(flags, bit)   (flags & (1 << bit))
 
#define FLAGS_SET_ON(flags, bit)   flags |= (1 << bit)
 
#define FLAGS_SET_OFF(flags, bit)   flags &= ~(1 << bit)
 
#define IS_RUNNING_GET(flags)   FLAGS_GET(flags, 0)
 
#define IS_RUNNING_SET_ON(flags)   FLAGS_SET_ON(flags, 0)
 
#define IS_RUNNING_SET_OFF(flags)   FLAGS_SET_OFF(flags, 0)
 
#define RELEASE_QUEUED_GET(flags)   FLAGS_GET(flags, 1)
 
#define RELEASE_QUEUED_SET_ON(flags)   FLAGS_SET_ON(flags, 1)
 
#define RELEASE_QUEUED_SET_OFF(flags)   FLAGS_SET_OFF(flags, 1)
 
#define IS_CANCELED_GET(flags)   FLAGS_GET(flags, 2)
 
#define IS_CANCELED_SET_ON(flags)   FLAGS_SET_ON(flags, 2)
 
#define IS_CANCELED_SET_OFF(flags)   FLAGS_SET_OFF(flags, 2)
 

Typedefs

typedef void * _BFThreadSyncID
 sync thread id
 

Functions

int _ThreadIDTablePushID (void *bftid, char type)
 
int _ThreadIDTablePopID ()
 
const void * _ThreadIDTableGetID ()
 
int BFThreadGetStartedCount ()
 
void _BFThreadIncrementStartedCount ()
 
void BFThreadResetStartedCount ()
 
int BFThreadGetStoppedCount ()
 
void _BFThreadIncrementStoppedCount ()
 
void BFThreadResetStoppedCount ()
 
void * _BFThreadStartRoutine (void *_params)
 
const BFThreadAsyncID BFThreadAsyncGetID ()
 
void BFThreadAsyncDestroy (BFThreadAsyncID in)
 
BFThreadAsyncID BFThreadAsync (void(*callback)(void *), void *args)
 
bool BFThreadAsyncIDIsValid (BFThreadAsyncID id)
 
int BFThreadAsyncError (BFThreadAsyncID id)
 
bool BFThreadAsyncIsRunning (BFThreadAsyncID in)
 
int BFThreadAsyncWait (BFThreadAsyncID in)
 
int BFThreadAsyncCancel (BFThreadAsyncID in)
 
bool BFThreadAsyncIsCanceled (BFThreadAsyncID in)
 
int BFThreadSync (void(*callback)(void *), void *args)
 
int BFThreadAsyncDetach (void(*callback)(void *), void *args)
 

Variables

_ThreadIDTable _tidtable
 
int _threadsStarted = 0
 
int _threadsStopped = 0
 

Macro Definition Documentation

◆ _BFThreadTypeAsync

#define _BFThreadTypeAsync   -1

◆ _BFThreadTypeAsyncDetached

#define _BFThreadTypeAsyncDetached   -2

◆ _BFThreadTypeSync

#define _BFThreadTypeSync   1

◆ _GNU_SOURCE

#define _GNU_SOURCE

author: brando date: 10/20/23

◆ FLAGS_GET

#define FLAGS_GET ( flags,
bit )   (flags & (1 << bit))

◆ FLAGS_SET_OFF

#define FLAGS_SET_OFF ( flags,
bit )   flags &= ~(1 << bit)

◆ FLAGS_SET_ON

#define FLAGS_SET_ON ( flags,
bit )   flags |= (1 << bit)

◆ IS_CANCELED_GET

#define IS_CANCELED_GET ( flags)    FLAGS_GET(flags, 2)

◆ IS_CANCELED_SET_OFF

#define IS_CANCELED_SET_OFF ( flags)    FLAGS_SET_OFF(flags, 2)

◆ IS_CANCELED_SET_ON

#define IS_CANCELED_SET_ON ( flags)    FLAGS_SET_ON(flags, 2)

◆ IS_RUNNING_GET

#define IS_RUNNING_GET ( flags)    FLAGS_GET(flags, 0)

◆ IS_RUNNING_SET_OFF

#define IS_RUNNING_SET_OFF ( flags)    FLAGS_SET_OFF(flags, 0)

◆ IS_RUNNING_SET_ON

#define IS_RUNNING_SET_ON ( flags)    FLAGS_SET_ON(flags, 0)

◆ RELEASE_QUEUED_GET

#define RELEASE_QUEUED_GET ( flags)    FLAGS_GET(flags, 1)

◆ RELEASE_QUEUED_SET_OFF

#define RELEASE_QUEUED_SET_OFF ( flags)    FLAGS_SET_OFF(flags, 1)

◆ RELEASE_QUEUED_SET_ON

#define RELEASE_QUEUED_SET_ON ( flags)    FLAGS_SET_ON(flags, 1)

Typedef Documentation

◆ _BFThreadSyncID

typedef void* _BFThreadSyncID

sync thread id

start THREAD IDS

Function Documentation

◆ _BFThreadIncrementStartedCount()

void _BFThreadIncrementStartedCount ( )

◆ _BFThreadIncrementStoppedCount()

void _BFThreadIncrementStoppedCount ( )

◆ _BFThreadStartRoutine()

void * _BFThreadStartRoutine ( void * _params)

end THREAD COUNTERS

Here is the call graph for this function:

◆ _ThreadIDTableGetID()

const void * _ThreadIDTableGetID ( )

◆ _ThreadIDTablePopID()

int _ThreadIDTablePopID ( )

◆ _ThreadIDTablePushID()

int _ThreadIDTablePushID ( void * bftid,
char type )

◆ BFThreadAsync()

BFThreadAsyncID BFThreadAsync ( void(* callback )(void *),
void * args )

Launches and detaches callback on a separate thread

will return while thread is running

us async id to query the async thread

To destroy use BFThreadAsyncIDDestroy

Here is the call graph for this function:

◆ BFThreadAsyncCancel()

int BFThreadAsyncCancel ( BFThreadAsyncID in)

Sets a flag that is readable

If called, BFThreadAsyncIsCanceled will always return true

The result of the function is not reversible

◆ BFThreadAsyncDestroy()

void BFThreadAsyncDestroy ( BFThreadAsyncID in)

Releases BFThreadAsyncID

Here is the call graph for this function:

◆ BFThreadAsyncDetach()

int BFThreadAsyncDetach ( void(* callback )(void *),
void * args )

◆ BFThreadAsyncError()

int BFThreadAsyncError ( BFThreadAsyncID id)

returns the error code, if any, for BFThreadAsync

◆ BFThreadAsyncGetID()

const BFThreadAsyncID BFThreadAsyncGetID ( )

returns current thread id

caller is not responsible for the memory

returns 0 if there was an error

Here is the call graph for this function:

◆ BFThreadAsyncIDIsValid()

bool BFThreadAsyncIDIsValid ( BFThreadAsyncID id)

true if we can safely use id

◆ BFThreadAsyncIsCanceled()

bool BFThreadAsyncIsCanceled ( BFThreadAsyncID in)

If thread has been canceled

caller can safely call this on running thread

◆ BFThreadAsyncIsRunning()

bool BFThreadAsyncIsRunning ( BFThreadAsyncID in)

true if callback from BFThreadAsync is still running

thread safe. you can use this function to poll the async thread if it's still running

◆ BFThreadAsyncWait()

int BFThreadAsyncWait ( BFThreadAsyncID in)

Waits for thread to finished if BFThreadAsyncIsRunning is true

this will block function until thread is finished

be careful when you call this. be sure that you know the thread WILL end soon or else this will hang

Here is the call graph for this function:

◆ BFThreadGetStartedCount()

int BFThreadGetStartedCount ( )

author: brando date: 10/20/23 returns the count of how many sync/async threads have been launched in current process

◆ BFThreadGetStoppedCount()

int BFThreadGetStoppedCount ( )

returns the count of how many sync/async threads have been finished in current process

◆ BFThreadResetStartedCount()

void BFThreadResetStartedCount ( )

resets the thread launch count

◆ BFThreadResetStoppedCount()

void BFThreadResetStoppedCount ( )

resets the thread stop count

◆ BFThreadSync()

int BFThreadSync ( void(* callback )(void *),
void * args )

Launches thread and returns when thread terminates

Here is the call graph for this function:

Variable Documentation

◆ _threadsStarted

int _threadsStarted = 0

◆ _threadsStopped

int _threadsStopped = 0

◆ _tidtable

_ThreadIDTable _tidtable
Initial value:
= {
.mut = PTHREAD_MUTEX_INITIALIZER,
.entries = NULL,
.size = 0
}