libcmutils 0.6.5
Multi-platform C99 utility library
Loading...
Searching...
No Matches
Threads, locks and synchronization primitives.

Data Structures

struct  CMUTIL_Cond
 Platform independent condition definition for concurrency control. More...
 
struct  CMUTIL_Mutex
 Platform independent mutex implementation. More...
 
struct  CMUTIL_Thread
 Platform independent thread object. More...
 
struct  CMUTIL_ThreadPool
 A threadpool object. More...
 
struct  CMUTIL_Semaphore
 Platform independent semaphore object. More...
 
struct  CMUTIL_RWLock
 Platform independent read/write lock object. More...
 

Macros

#define CMSync(a, ...)
 Synchronized block macro.
 

Functions

CMUTIL_CondCMUTIL_CondCreate (CMBool manual_reset)
 Creates a condition object.
 
CMUTIL_MutexCMUTIL_MutexCreate (void)
 Creates a mutex object.
 
CMUTIL_ThreadCMUTIL_ThreadCreate (void *(*proc)(void *), void *udata, const char *name)
 Creates a thread object.
 
uint32_t CMUTIL_ThreadSelfId (void)
 Get the ID of the current thread. This id is not system thread id, just an internal thread index.
 
CMUTIL_ThreadCMUTIL_ThreadSelf (void)
 Get the current thread context.
 
uint64_t CMUTIL_ThreadSystemSelfId (void)
 Get system dependent thread id.
 
CMUTIL_ThreadPoolCMUTIL_ThreadPoolCreate (int pool_size, const char *name)
 Creates a new threadpool object.
 
CMUTIL_SemaphoreCMUTIL_SemaphoreCreate (int initcnt)
 Creates a semaphore object.
 
CMUTIL_RWLockCMUTIL_RWLockCreate (void)
 Create a read-write lock object.
 

Detailed Description

Macro Definition Documentation

◆ CMSync

#define CMSync (   a,
  ... 
)
Value:
do { \
CMCall((a), Lock); \
__VA_ARGS__ \
CMCall((a), Unlock); \
} while(0)

Synchronized block macro.

This macro will lock the given mutex object, execute given statements, and unlock the given mutex object.

Note that do not use 'return', 'break', 'continue' or "goto" statements inside synchronized block, because these statements will bypass unlocking mutex operation, which will cause deadlock.

Parameters
aMutex object to be locked.
...Statements to be executed in synchronized block.

Function Documentation

◆ CMUTIL_CondCreate()

CMUTIL_Cond * CMUTIL_CondCreate ( CMBool  manual_reset)

Creates a condition object.

Creates a manual or auto-resetting condition object.

Parameters
manual_resetIf this parameter is CMTrue, the function creates a manual-reset condition object, which requires the use of the Reset method to set the event state to nonsignaled. If this parameter is CMFalse, the function creates an auto-reset condition object, and the system automatically resets the event state to nonsignaled after a single waiting thread has been released.
Returns
Created a conditional object.

◆ CMUTIL_MutexCreate()

CMUTIL_Mutex * CMUTIL_MutexCreate ( void  )

Creates a mutex object.

Any thread of the calling process can use created mutex-object in a call to lock methods. When a lock method returns, the waiting thread is released to continue its execution. This function creates a 'recursive lockable' mutex object.

Returns
Created a mutex object.

◆ CMUTIL_ThreadCreate()

CMUTIL_Thread * CMUTIL_ThreadCreate ( void *(*)(void *)  proc,
void *  udata,
const char *  name 
)

Creates a thread object.

Created thread does not start automatically. Call the Start method to start this thread. The returned object must be freed by calling the Join method, even if this thread is not started.

Parameters
procThe start routine of the created thread.
udataThis argument is passed as the sole argument of 'proc'
nameThread name, any name could be assigned and can also duplicable. But must not exceed 200 bytes.
Returns
Created a thread object.

◆ CMUTIL_ThreadSelfId()

uint32_t CMUTIL_ThreadSelfId ( void  )

Get the ID of the current thread. This id is not system thread id, just an internal thread index.

Returns
ID of the current thread.

◆ CMUTIL_ThreadSelf()

CMUTIL_Thread * CMUTIL_ThreadSelf ( void  )

Get the current thread context.

Returns
Current thread context.

◆ CMUTIL_ThreadSystemSelfId()

uint64_t CMUTIL_ThreadSystemSelfId ( void  )

Get system dependent thread id.

Returns
System dependent thread id.

◆ CMUTIL_ThreadPoolCreate()

CMUTIL_ThreadPool * CMUTIL_ThreadPoolCreate ( int  pool_size,
const char *  name 
)

Creates a new threadpool object.

This function will create a new threadpool object with the initial size. If pool_size is zero or negative value, this threadpool object's pool size will not be fixed, will be increased one by one if needed.

Parameters
pool_sizeThread count of this threadpool if positive, otherwise this object will increase pool size automatically.
nameName of this threadpool object, any name could be assigned and can also duplicable. But must not exceed 200 bytes.
Returns
A new threadpool object.

◆ CMUTIL_SemaphoreCreate()

CMUTIL_Semaphore * CMUTIL_SemaphoreCreate ( int  initcnt)

Creates a semaphore object.

Create an in-process semaphore object.

Parameters
initcntInitial semaphore ownership count.
Returns
Created a semaphore object.

◆ CMUTIL_RWLockCreate()

CMUTIL_RWLock * CMUTIL_RWLockCreate ( void  )

Create a read-write lock object.

Returns
A new read-write lock, which must be destroyed after use.