Implement POSIX semaphore support for linux platform (#1345)

Implement POSIX semaphore support for linux platform
This commit is contained in:
Huang Qi
2022-08-08 19:59:46 +08:00
committed by GitHub
parent e8f0c9580b
commit f3f8d684b3
19 changed files with 434 additions and 1 deletions

View File

@ -55,6 +55,7 @@ typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef sem_t korp_sem;
#define os_thread_local_attribute __thread

View File

@ -195,6 +195,48 @@ os_cond_wait(korp_cond *cond, korp_mutex *mutex)
return BHT_OK;
}
korp_sem *
os_sem_open(const char *name, int oflags, int mode, int val)
{
return sem_open(name, oflags, mode, val);
}
int
os_sem_close(korp_sem *sem)
{
return sem_close(sem);
}
int
os_sem_wait(korp_sem *sem)
{
return sem_wait(sem);
}
int
os_sem_trywait(korp_sem *sem)
{
return sem_trywait(sem);
}
int
os_sem_post(korp_sem *sem)
{
return sem_post(sem);
}
int
os_sem_getvalue(korp_sem *sem, int *sval)
{
return sem_getvalue(sem, sval);
}
int
os_sem_unlink(const char *name)
{
return sem_unlink(name);
}
static void
msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
{

View File

@ -56,6 +56,7 @@ typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef sem_t korp_sem;
#define os_thread_local_attribute __thread

View File

@ -36,6 +36,7 @@ typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef unsigned int korp_sem;
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)

View File

@ -195,6 +195,91 @@ os_cond_signal(korp_cond *cond);
int
os_cond_broadcast(korp_cond *cond);
/**
* Creates a new POSIX-like semaphore or opens an existing
* semaphore. The semaphore is identified by name. For details of
* the construction of name, please refer to
* https://man7.org/linux/man-pages/man3/sem_open.3.html.
*
* @param name semaphore name
* @param oflasg specifies flags that control the operation of the call
* @param mode permission flags
* @param val initial value of the named semaphore.
*
* @return korp_sem * if success, NULL otherwise
*/
korp_sem *
os_sem_open(const char *name, int oflags, int mode, int val);
/**
* Closes the named semaphore referred to by sem,
* allowing any resources that the system has allocated to the
* calling process for this semaphore to be freed.
*
* @param sem
*
* @return 0 if success
*/
int
os_sem_close(korp_sem *sem);
/**
* Decrements (locks) the semaphore pointed to by sem.
* If the semaphore's value is greater than zero, then the decrement
* proceeds, and the function returns, immediately. If the
* semaphore currently has the value zero, then the call blocks
* until either it becomes possible to perform the decrement (i.e.,
* the semaphore value rises above zero), or a signal handler
* interrupts the call.
*
* @return 0 if success
*/
int
os_sem_wait(korp_sem *sem);
/**
* Is the same as sem_wait(), except that if the
* decrement cannot be immediately performed, then call returns an
* error (errno set to EAGAIN) instead of blocking.
*
* @return 0 if success
*/
int
os_sem_trywait(korp_sem *sem);
/**
* Increments (unlocks) the semaphore pointed to by sem.
* If the semaphore's value consequently becomes greater than zero,
* then another process or thread blocked in a sem_wait(3) call will
* be woken up and proceed to lock the semaphore.
*
* @return 0 if success
*/
int
os_sem_post(korp_sem *sem);
/**
* Places the current value of the semaphore pointed
* to sem into the integer pointed to by sval.
*
* @return 0 if success
*/
int
os_sem_getvalue(korp_sem *sem, int *sval);
/**
* Remove the named semaphore referred to by name.
* The semaphore name is removed immediately. The semaphore is
* destroyed once all other processes that have the semaphore open
* close it.
*
* @param name semaphore name
*
* @return 0 if success
*/
int
os_sem_unlink(const char *name);
/****************************************************
* Section 2 *
* Socket support *

View File

@ -50,6 +50,7 @@ typedef pthread_t korp_thread;
typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef unsigned int korp_sem;
typedef void (*os_print_function_t)(const char *message);
void

View File

@ -55,6 +55,7 @@ typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef sem_t korp_sem;
#define os_thread_local_attribute __thread

View File

@ -25,6 +25,7 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <semaphore.h>
#ifdef __cplusplus
extern "C" {
@ -38,6 +39,7 @@ typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef sem_t korp_sem;
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)

View File

@ -38,6 +38,7 @@
typedef thread_t korp_thread;
typedef kernel_pid_t korp_tid;
typedef mutex_t korp_mutex;
typedef unsigned int korp_sem;
/* typedef sema_t korp_sem; */

View File

@ -36,6 +36,7 @@ typedef rt_thread_t korp_tid;
typedef struct rt_mutex korp_mutex;
typedef struct rt_thread korp_cond;
typedef struct rt_thread korp_thread;
typedef unsigned int korp_sem;
typedef rt_uint8_t uint8_t;
typedef rt_int8_t int8_t;

View File

@ -54,6 +54,7 @@ typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef sem_t korp_sem;
#define os_thread_local_attribute __thread

View File

@ -49,6 +49,7 @@
typedef struct k_thread korp_thread;
typedef korp_thread *korp_tid;
typedef struct k_mutex korp_mutex;
typedef unsigned int korp_sem;
struct os_thread_wait_node;
typedef struct os_thread_wait_node *os_thread_wait_list;