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

@ -5,9 +5,11 @@
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
static pthread_mutex_t mutex;
static pthread_cond_t cond;
static sem_t *sem;
static void *
thread(void *arg)
@ -24,6 +26,7 @@ thread(void *arg)
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
sem_post(sem);
printf("thread exit \n");
@ -45,10 +48,18 @@ main(int argc, char *argv[])
goto fail1;
}
// O_CREAT and S_IRGRPS_IRGRP | S_IWGRP on linux (glibc), initial value is 0
if (!(sem = sem_open("tessstsem", 0100, 0x10 | 0x20, 0))) {
printf("Failed to open sem. %p\n", sem);
goto fail2;
}
pthread_mutex_lock(&mutex);
if (pthread_create(&tid, NULL, thread, &num) != 0) {
printf("Failed to create thread.\n");
goto fail2;
pthread_mutex_unlock(&mutex);
goto fail3;
}
printf("cond wait start\n");
@ -56,12 +67,22 @@ main(int argc, char *argv[])
pthread_mutex_unlock(&mutex);
printf("cond wait success.\n");
if (sem_wait(sem) != 0) {
printf("Failed to wait sem.\n");
}
else {
printf("sem wait success.\n");
}
if (pthread_join(tid, NULL) != 0) {
printf("Failed to join thread.\n");
}
ret = 0;
fail3:
sem_close(sem);
sem_unlink("tessstsem");
fail2:
pthread_cond_destroy(&cond);
fail1: