os: fix Mac Semaphore error
This commit is contained in:
parent
679145dcb6
commit
70ea1235df
|
@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 3.0)
|
||||||
|
|
||||||
set(CMAKE_VERBOSE_MAKEFILE OFF)
|
set(CMAKE_VERBOSE_MAKEFILE OFF)
|
||||||
|
|
||||||
SET(BUILD_SHARED_LIBS "OFF")
|
|
||||||
|
|
||||||
#set output directory
|
#set output directory
|
||||||
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build/lib)
|
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build/lib)
|
||||||
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build/bin)
|
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build/bin)
|
||||||
|
|
|
@ -1,3 +1,19 @@
|
||||||
|
SET(PREPARE_ENV_CMD "prepare_env_cmd")
|
||||||
|
SET(PREPARE_ENV_TARGET "prepare_env_target")
|
||||||
|
ADD_CUSTOM_COMMAND(OUTPUT ${PREPARE_ENV_CMD}
|
||||||
|
POST_BUILD
|
||||||
|
COMMAND echo "make test directory"
|
||||||
|
DEPENDS taosd
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/cfg/
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/log/
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/data/
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E echo dataDir ${TD_TESTS_OUTPUT_DIR}/data > ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E echo logDir ${TD_TESTS_OUTPUT_DIR}/log >> ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E echo charset UTF-8 >> ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E echo monitor 0 >> ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg
|
||||||
|
COMMENT "prepare taosd environment")
|
||||||
|
ADD_CUSTOM_TARGET(${PREPARE_ENV_TARGET} ALL WORKING_DIRECTORY ${TD_EXECUTABLE_OUTPUT_PATH} DEPENDS ${PREPARE_ENV_CMD})
|
||||||
|
|
||||||
IF (TD_LINUX)
|
IF (TD_LINUX)
|
||||||
SET(TD_MAKE_INSTALL_SH "${TD_SOURCE_DIR}/packaging/tools/make_install.sh")
|
SET(TD_MAKE_INSTALL_SH "${TD_SOURCE_DIR}/packaging/tools/make_install.sh")
|
||||||
INSTALL(CODE "MESSAGE(\"make install script: ${TD_MAKE_INSTALL_SH}\")")
|
INSTALL(CODE "MESSAGE(\"make install script: ${TD_MAKE_INSTALL_SH}\")")
|
||||||
|
|
|
@ -90,6 +90,12 @@ ELSE ()
|
||||||
ENDIF ()
|
ENDIF ()
|
||||||
ENDIF ()
|
ENDIF ()
|
||||||
|
|
||||||
|
option(
|
||||||
|
BUILD_SHARED_LIBS
|
||||||
|
""
|
||||||
|
OFF
|
||||||
|
)
|
||||||
|
|
||||||
option(
|
option(
|
||||||
RUST_BINDINGS
|
RUST_BINDINGS
|
||||||
"If build with rust-bindings"
|
"If build with rust-bindings"
|
||||||
|
|
|
@ -23,10 +23,9 @@ extern "C" {
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
|
|
||||||
#if defined(_TD_DARWIN_64)
|
#if defined(_TD_DARWIN_64)
|
||||||
|
#include <dispatch/dispatch.h>
|
||||||
// typedef struct tsem_s *tsem_t;
|
// typedef struct tsem_s *tsem_t;
|
||||||
typedef struct bosal_sem_t *tsem_t;
|
typedef dispatch_semaphore_t tsem_t;
|
||||||
|
|
||||||
|
|
||||||
int tsem_init(tsem_t *sem, int pshared, unsigned int value);
|
int tsem_init(tsem_t *sem, int pshared, unsigned int value);
|
||||||
int tsem_wait(tsem_t *sem);
|
int tsem_wait(tsem_t *sem);
|
||||||
|
|
|
@ -392,179 +392,32 @@ int32_t tsem_timewait(tsem_t* sem, int64_t nanosecs) {
|
||||||
// *sem = NULL;
|
// *sem = NULL;
|
||||||
// return 0;
|
// return 0;
|
||||||
// }
|
// }
|
||||||
typedef struct {
|
|
||||||
pthread_mutex_t count_lock;
|
|
||||||
pthread_cond_t count_bump;
|
|
||||||
unsigned int count;
|
|
||||||
} bosal_sem_t;
|
|
||||||
|
|
||||||
int tsem_init(tsem_t *psem, int flags, unsigned int count) {
|
int tsem_init(tsem_t *psem, int flags, unsigned int count) {
|
||||||
bosal_sem_t *pnewsem;
|
*psem = dispatch_semaphore_create(count);
|
||||||
int result;
|
if (*psem == NULL) return -1;
|
||||||
|
|
||||||
pnewsem = (bosal_sem_t *)malloc(sizeof(bosal_sem_t));
|
|
||||||
if (!pnewsem) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
result = pthread_mutex_init(&pnewsem->count_lock, NULL);
|
|
||||||
if (result) {
|
|
||||||
free(pnewsem);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
result = pthread_cond_init(&pnewsem->count_bump, NULL);
|
|
||||||
if (result) {
|
|
||||||
pthread_mutex_destroy(&pnewsem->count_lock);
|
|
||||||
free(pnewsem);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
pnewsem->count = count;
|
|
||||||
*psem = (tsem_t)pnewsem;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tsem_destroy(tsem_t *psem) {
|
int tsem_destroy(tsem_t *psem) {
|
||||||
bosal_sem_t *poldsem;
|
|
||||||
|
|
||||||
if (!psem) {
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
poldsem = (bosal_sem_t *)*psem;
|
|
||||||
|
|
||||||
pthread_mutex_destroy(&poldsem->count_lock);
|
|
||||||
pthread_cond_destroy(&poldsem->count_bump);
|
|
||||||
free(poldsem);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tsem_post(tsem_t *psem) {
|
int tsem_post(tsem_t *psem) {
|
||||||
bosal_sem_t *pxsem;
|
if (psem == NULL || *psem == NULL) return -1;
|
||||||
int result, xresult;
|
dispatch_semaphore_signal(*psem);
|
||||||
|
|
||||||
if (!psem) {
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
pxsem = (bosal_sem_t *)*psem;
|
|
||||||
|
|
||||||
result = pthread_mutex_lock(&pxsem->count_lock);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
pxsem->count = pxsem->count + 1;
|
|
||||||
|
|
||||||
xresult = pthread_cond_signal(&pxsem->count_bump);
|
|
||||||
|
|
||||||
result = pthread_mutex_unlock(&pxsem->count_lock);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if (xresult) {
|
|
||||||
errno = xresult;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int tsem_trywait(tsem_t *psem) {
|
|
||||||
bosal_sem_t *pxsem;
|
|
||||||
int result, xresult;
|
|
||||||
|
|
||||||
if (!psem) {
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
pxsem = (bosal_sem_t *)*psem;
|
|
||||||
|
|
||||||
result = pthread_mutex_lock(&pxsem->count_lock);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
xresult = 0;
|
|
||||||
|
|
||||||
if (pxsem->count > 0) {
|
|
||||||
pxsem->count--;
|
|
||||||
} else {
|
|
||||||
xresult = EAGAIN;
|
|
||||||
}
|
|
||||||
result = pthread_mutex_unlock(&pxsem->count_lock);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if (xresult) {
|
|
||||||
errno = xresult;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tsem_wait(tsem_t *psem) {
|
int tsem_wait(tsem_t *psem) {
|
||||||
bosal_sem_t *pxsem;
|
if (psem == NULL || *psem == NULL) return -1;
|
||||||
int result, xresult;
|
dispatch_semaphore_wait(*psem, DISPATCH_TIME_FOREVER);
|
||||||
|
|
||||||
if (!psem) {
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
pxsem = (bosal_sem_t *)*psem;
|
|
||||||
|
|
||||||
result = pthread_mutex_lock(&pxsem->count_lock);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
xresult = 0;
|
|
||||||
|
|
||||||
if (pxsem->count == 0) {
|
|
||||||
xresult = pthread_cond_wait(&pxsem->count_bump, &pxsem->count_lock);
|
|
||||||
}
|
|
||||||
if (!xresult) {
|
|
||||||
if (pxsem->count > 0) {
|
|
||||||
pxsem->count--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result = pthread_mutex_unlock(&pxsem->count_lock);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if (xresult) {
|
|
||||||
errno = xresult;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tsem_timewait(tsem_t *psem, int64_t nanosecs) {
|
int tsem_timewait(tsem_t *psem, int64_t nanosecs) {
|
||||||
struct timespec abstim = {
|
if (psem == NULL || *psem == NULL) return -1;
|
||||||
.tv_sec = 0,
|
dispatch_semaphore_wait(*psem, nanosecs);
|
||||||
.tv_nsec = nanosecs,
|
|
||||||
};
|
|
||||||
|
|
||||||
bosal_sem_t *pxsem;
|
|
||||||
int result, xresult;
|
|
||||||
|
|
||||||
if (!psem) {
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
pxsem = (bosal_sem_t *)*psem;
|
|
||||||
|
|
||||||
result = pthread_mutex_lock(&pxsem->count_lock);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
xresult = 0;
|
|
||||||
|
|
||||||
if (pxsem->count == 0) {
|
|
||||||
xresult = pthread_cond_timedwait(&pxsem->count_bump, &pxsem->count_lock, &abstim);
|
|
||||||
}
|
|
||||||
if (!xresult) {
|
|
||||||
if (pxsem->count > 0) {
|
|
||||||
pxsem->count--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result = pthread_mutex_unlock(&pxsem->count_lock);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if (xresult) {
|
|
||||||
errno = xresult;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue