acl/lib_fiber/c/include/fiber/fiber_sem.h

85 lines
2.8 KiB
C
Raw Normal View History

#ifndef FIBER_SEM_INCLUDE_H
2018-11-30 14:38:22 +08:00
#define FIBER_SEM_INCLUDE_H
#include "fiber_define.h"
#ifdef __cplusplus
extern "C" {
#endif
2022-04-01 14:39:54 +08:00
/* Fiber semaphore, thread unsafety, one semaphore can only be used in one
2018-11-30 14:38:22 +08:00
* thread, if used in different threads, result is unpredictable */
typedef struct ACL_FIBER_SEM ACL_FIBER_SEM;
/**
2022-04-01 14:39:54 +08:00
* Create one fiber semaphore, and binding it with the current thread
2018-11-30 14:38:22 +08:00
* @param num {int} the initial value of the semaphore, must >= 0
* @return {ACL_FIBER_SEM *}
*/
FIBER_API ACL_FIBER_SEM* acl_fiber_sem_create(int num);
/**
2022-04-01 14:39:54 +08:00
* Free fiber semaphore
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_SEM *}
*/
FIBER_API void acl_fiber_sem_free(ACL_FIBER_SEM* sem);
/**
2022-04-01 14:39:54 +08:00
* Get the thread binding the specificed fiber sem
2018-11-30 14:38:22 +08:00
* @param sem {ACL_FIBER_SEM*} created by acl_fiber_sem_create
* @return {unsigned long} thread ID of the thread binding the semaphore
*/
#if !defined(_WIN32) && !defined(_WIN64)
FIBER_API unsigned long acl_fiber_sem_get_tid(ACL_FIBER_SEM* sem);
#endif
/**
2022-04-01 14:39:54 +08:00
* Set the thread ID the semaphore belongs to, changing the owner of the fiber
2018-11-30 14:38:22 +08:00
* semaphore, when this function was called, the value of the semaphore must
* be zero, otherwise fatal will happen.
* @param sem {ACL_FIBER_SEM*} created by acl_fiber_sem_create
* @param {unsigned long} the thread ID to be specificed with the semaphore
*/
FIBER_API void acl_fiber_sem_set_tid(ACL_FIBER_SEM* sem, unsigned long tid);
/**
2022-04-01 14:39:54 +08:00
* Wait for semaphore until > 0, semaphore will be -1 when returned
2018-11-30 14:38:22 +08:00
* @param sem {ACL_FIBER_SEM *} created by acl_fiber_sem_create
* @return {int} the semaphore value returned, if the caller's thread isn't
* same as the semaphore owner's thread, -1 will be returned
*/
FIBER_API int acl_fiber_sem_wait(ACL_FIBER_SEM* sem);
/**
2022-04-01 14:39:54 +08:00
* Try to wait semaphore until > 0, if semaphore is 0, -1 returned immediately,
2018-11-30 14:38:22 +08:00
* otherwise semaphore will be decreased 1 and the semaphore's value is returned
* @param sem {ACL_FIBER_SEM *} created by acl_fiber_sem_create
* @return {int} value(>=0) returned when waiting ok, otherwise -1 will be
* returned if the caller's thread isn't same as the semaphore thread or the
* semaphore's value is 0
*/
FIBER_API int acl_fiber_sem_trywait(ACL_FIBER_SEM* sem);
/**
2022-04-01 14:39:54 +08:00
* Add 1 to the semaphore, if there are other fibers waiting for semaphore,
2018-11-30 14:38:22 +08:00
* one waiter will be wakeuped
* @param sem {ACL_FIBER_SEM *} created by acl_fiber_sem_create
* @return {int} the current semaphore value returned, -1 returned if the
* current thread ID is not same as the semaphore's owner ID
*/
FIBER_API int acl_fiber_sem_post(ACL_FIBER_SEM* sem);
/**
2022-04-01 14:39:54 +08:00
* Get the specificed semaphore's value
2018-11-30 14:38:22 +08:00
* @param sem {ACL_FIBER_SEM*} created by acl_fiber_sem_create
* @retur {int} current semaphore's value returned
*/
FIBER_API int acl_fiber_sem_num(ACL_FIBER_SEM* sem);
#ifdef __cplusplus
}
#endif
#endif