3.3. Threads utilitiesΒΆ

Thread utilities functions.

Defines

THREAD_CANCELED

Thread return value when canceled.

MUTEX_INIT

Mutex static initializer.

RWLOCK_INIT

Read-write lock static initializer.

Typedefs

typedef pthread_t thread_t

Opaque thread type.

typedef pthread_mutex_t mutex_t

Opaque mutex type.

typedef pthread_spinlock_t spinlock_t

Opaque spinlock type.

typedef pthread_rwlock_t rwlock_t

Opaque read-write lock type.

typedef sem_t semaphore_t

Opaque semaphore type.

typedef pthread_barrier_t barrier_t

Opaque barrier type.

typedef pthread_key_t local_storage_t

Opaque local storage type.

typedef uint32 atomic_t

32 bit atomic opaque type.

typedef uint64 atomic64_t

64 bit atomic opaque type.

Enums

thread_cancel_t enum

Thread cancel mode.

Values:

  • THREAD_CANCEL_DEFERRED -

    Deferred mode.

  • THREAD_CANCEL_ASYNCHRONOUS -

    Asynchronous mode.

Functions

int thread_get_packet_capture_cpu_count()

Get the number of thread to be used by the application for packet capture.

void thread_set_packet_capture_cpu_count(int count)

Set the number of thread to be used by the application for packet capture.

Parameters
  • count -

    Number of thread to use for capture.

int thread_get_cpu_count()

Get the number of CPUs.

bool thread_create(thread_t * thread, void *(*)(void *) main, void * param)

Start a new thread.

thread_t thread_current()

Get the current thread.

bool thread_join(thread_t thread, void ** ret)

Join a thread.

bool thread_cancel(thread_t thread)

Cancel a running thread.

int thread_getid()

Get current thread id.

void thread_setid(int id)

Set current thread id.

Parameters
  • id -

    New thread identifier.

bool thread_sigmask(int how, sigset_t * set, sigset_t * oldset)

Set signal mask on a thread.

bool thread_signal(thread_t thread, int sig)

Send a signal to the given thread.

bool thread_setcancelstate(bool enable)

Change thread support for cancel.

bool thread_setcanceltype(enum thread_cancel_t type)

Change thread cancel mode.

void thread_testcancel()

Check for cancel.

void thread_protect(void(*)(void *) run, void * runarg, void(*)(void *) finish, void * finisharg)

Run a function in protected mode. If a cancel is raised, then a cleanup function is called before leaving the thread.

thread_t thread_main()

Get the main thread handle.

thread_t thread_self()

Get the current thread.

bool thread_equal(thread_t a, thread_t b)

Check if two threads are the same.

bool thread_kill(thread_t thread, int sig)

Raise a signal on the given thread.

bool mutex_init(mutex_t * mutex, bool recursive)

bool mutex_destroy(mutex_t * mutex)

bool mutex_lock(mutex_t * mutex)

bool mutex_trylock(mutex_t * mutex)

bool mutex_unlock(mutex_t * mutex)

bool spinlock_init(spinlock_t * lock)

bool spinlock_destroy(spinlock_t * lock)

bool spinlock_lock(spinlock_t * lock)

bool spinlock_trylock(spinlock_t * lock)

bool spinlock_unlock(spinlock_t * lock)

bool rwlock_init(rwlock_t * rwlock)

bool rwlock_destroy(rwlock_t * rwlock)

bool rwlock_readlock(rwlock_t * rwlock)

bool rwlock_tryreadlock(rwlock_t * rwlock)

bool rwlock_writelock(rwlock_t * rwlock)

bool rwlock_trywritelock(rwlock_t * rwlock)

bool rwlock_unlock(rwlock_t * rwlock)

bool semaphore_init(semaphore_t * semaphore, uint32 initial)

bool semaphore_destroy(semaphore_t * semaphore)

bool semaphore_wait(semaphore_t * semaphore)

bool semaphore_post(semaphore_t * semaphore)

bool barrier_init(barrier_t * barrier, uint32 count)

bool barrier_destroy(barrier_t * barrier)

bool barrier_wait(barrier_t * barrier)

bool local_storage_init(local_storage_t * key, void(*)(void *) destructor)

Initialize thread local storage. The parameter destructor allows to set a cleanup function to call when the storage is destroyed.

Return
True on success. Use clear_error() to get details about the error.

bool local_storage_destroy(local_storage_t * key)

Destroy a thread local storage.

Return
True on success. Use clear_error() to get details about the error.

void * local_storage_get(local_storage_t * key)

Get the value of the thread local storage.

Return
The value associated with the current thread.

bool local_storage_set(local_storage_t * key, const void * value)

Set the value of the thread local storage.

Return
True on success. Use clear_error() to get details about the error.

INLINE uint32 atomic_inc(atomic_t * v)

Increment an atomic counter.

Return
The new value after increment.

INLINE uint32 atomic_dec(atomic_t * v)

Decrement an atomic counter.

Return
The new value after decrement.

INLINE uint32 atomic_get(atomic_t * v)

Get the value of an atomic counter.

INLINE void atomic_set(atomic_t * v, uint32 x)

Set the current value of an atomic counter.

INLINE void atomic64_set(atomic64_t * v, uint64 x)

Set the current value of a 64 bit atomic counter.

INLINE void atomic64_init(atomic64_t * v, uint64 x)

Initialize a 64 bit atomic counter.

INLINE void atomic64_destroy(atomic64_t * v)

Destroy a 64 bit atomic counter.

INLINE uint64 atomic64_inc(atomic64_t * v)

Increment a 64 bit atomic counter.

Return
The new value after increment.

INLINE uint64 atomic64_dec(atomic64_t * v)

Decrement a 64 bit atomic counter.

Return
The new value after decrement.

INLINE uint64 atomic64_get(atomic64_t * v)

Get the value of a 64 bit atomic counter.