BFLibC 0.1
C Library
|
#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"
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 |
#define _BFThreadTypeAsync -1 |
#define _BFThreadTypeAsyncDetached -2 |
#define _BFThreadTypeSync 1 |
#define _GNU_SOURCE |
author: brando date: 10/20/23
#define FLAGS_GET | ( | flags, | |
bit ) (flags & (1 << bit)) |
#define FLAGS_SET_OFF | ( | flags, | |
bit ) flags &= ~(1 << bit) |
#define FLAGS_SET_ON | ( | flags, | |
bit ) flags |= (1 << bit) |
#define IS_CANCELED_GET | ( | flags | ) | FLAGS_GET(flags, 2) |
#define IS_CANCELED_SET_OFF | ( | flags | ) | FLAGS_SET_OFF(flags, 2) |
#define IS_CANCELED_SET_ON | ( | flags | ) | FLAGS_SET_ON(flags, 2) |
#define IS_RUNNING_GET | ( | flags | ) | FLAGS_GET(flags, 0) |
#define IS_RUNNING_SET_OFF | ( | flags | ) | FLAGS_SET_OFF(flags, 0) |
#define IS_RUNNING_SET_ON | ( | flags | ) | FLAGS_SET_ON(flags, 0) |
#define RELEASE_QUEUED_GET | ( | flags | ) | FLAGS_GET(flags, 1) |
#define RELEASE_QUEUED_SET_OFF | ( | flags | ) | FLAGS_SET_OFF(flags, 1) |
#define RELEASE_QUEUED_SET_ON | ( | flags | ) | FLAGS_SET_ON(flags, 1) |
typedef void* _BFThreadSyncID |
sync thread id
start THREAD IDS
void _BFThreadIncrementStartedCount | ( | ) |
void _BFThreadIncrementStoppedCount | ( | ) |
void * _BFThreadStartRoutine | ( | void * | _params | ) |
end THREAD COUNTERS
const void * _ThreadIDTableGetID | ( | ) |
int _ThreadIDTablePopID | ( | ) |
int _ThreadIDTablePushID | ( | void * | bftid, |
char | type ) |
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
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
void BFThreadAsyncDestroy | ( | BFThreadAsyncID | in | ) |
Releases BFThreadAsyncID
int BFThreadAsyncDetach | ( | void(* | callback )(void *), |
void * | args ) |
int BFThreadAsyncError | ( | BFThreadAsyncID | id | ) |
returns the error code, if any, for BFThreadAsync
const BFThreadAsyncID BFThreadAsyncGetID | ( | ) |
returns current thread id
caller is not responsible for the memory
returns 0 if there was an error
bool BFThreadAsyncIDIsValid | ( | BFThreadAsyncID | id | ) |
true if we can safely use id
bool BFThreadAsyncIsCanceled | ( | BFThreadAsyncID | in | ) |
If thread has been canceled
caller can safely call this on running thread
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
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
int BFThreadGetStartedCount | ( | ) |
author: brando date: 10/20/23 returns the count of how many sync/async threads have been launched in current process
int BFThreadGetStoppedCount | ( | ) |
returns the count of how many sync/async threads have been finished in current process
void BFThreadResetStartedCount | ( | ) |
resets the thread launch count
void BFThreadResetStoppedCount | ( | ) |
resets the thread stop count
int BFThreadSync | ( | void(* | callback )(void *), |
void * | args ) |
Launches thread and returns when thread terminates
int _threadsStarted = 0 |
int _threadsStopped = 0 |
_ThreadIDTable _tidtable |