osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr) { UINT32 uwRet; UINT32 uwSemId; UNUSED(attr); if (OS_INT_ACTIVE) { return (osSemaphoreId_t)NULL; } if(1 == max_count) { uwRet = LOS_BinarySemCreate((UINT16)initial_count, &uwSemId); } else { uwRet = LOS_SemCreate((UINT16)initial_count, &uwSemId); } if (uwRet == LOS_OK) { return (osSemaphoreId_t)(GET_SEM(uwSemId)); } else { return (osSemaphoreId_t)NULL; } }
tsk_semaphore_handle_t* tsk_semaphore_create_2(int initial_val) { SEMAPHORE_T handle = tsk_null; #if TSK_UNDER_WINDOWS # if TSK_UNDER_WINDOWS_RT handle = CreateSemaphoreEx(NULL, initial_val, 0x7FFFFFFF, NULL, 0x00000000, SEMAPHORE_ALL_ACCESS); # else handle = CreateSemaphore(NULL, initial_val, 0x7FFFFFFF, NULL); # endif #else handle = tsk_calloc(1, sizeof(SEMAPHORE_S)); #if TSK_USE_NAMED_SEM named_sem_t * nsem = (named_sem_t*)handle; snprintf(nsem->name, (sizeof(nsem->name)/sizeof(nsem->name[0])) - 1, "/sem/%llu/%d.", tsk_time_epoch(), rand() ^ rand()); if ((nsem->sem = sem_open(nsem->name, O_CREAT /*| O_EXCL*/, S_IRUSR | S_IWUSR, initial_val)) == SEM_FAILED) { #else if (sem_init((SEMAPHORE_T)handle, 0, initial_val)) { #endif TSK_FREE(handle); TSK_DEBUG_ERROR("Failed to initialize the new semaphore (errno=%d).", errno); } #endif if (!handle) { TSK_DEBUG_ERROR("Failed to create new semaphore"); } return handle; } /**@ingroup tsk_semaphore_group * Increments a semaphore. * @param handle The semaphore to increment. * @retval Zero if succeed and otherwise the function returns -1 and sets errno to indicate the error. * @sa @ref tsk_semaphore_decrement. */ int tsk_semaphore_increment(tsk_semaphore_handle_t* handle) { int ret = EINVAL; if (handle) { #if TSK_UNDER_WINDOWS if((ret = ReleaseSemaphore((SEMAPHORE_T)handle, 1L, NULL) ? 0 : -1)) #else if((ret = sem_post((SEMAPHORE_T)GET_SEM(handle)))) #endif { TSK_DEBUG_ERROR("sem_post function failed: %d", ret); } } return ret; }
/**@ingroup tsk_semaphore_group * Destroy a semaphore previously created using @ref tsk_semaphore_create. * @param handle The semaphore to free. * @sa @ref tsk_semaphore_create */ void tsk_semaphore_destroy(tsk_semaphore_handle_t** handle) { if(handle && *handle) { #if TSK_UNDER_WINDOWS CloseHandle((SEMAPHORE_T)*handle); *handle = tsk_null; #else # if TSK_USE_NAMED_SEM named_sem_t * nsem = ((named_sem_t*)*handle); sem_close(nsem->sem); #else sem_destroy((SEMAPHORE_T)GET_SEM(*handle)); #endif /* TSK_USE_NAMED_SEM */ tsk_free(handle); #endif } else{ TSK_DEBUG_WARN("Cannot free an uninitialized semaphore object"); } }
void hl_semaphore_destroy(hl_semaphore_handle_t** handle) { if (handle && *handle) { #if HL_UNDER_WINDOWS CloseHandle((SEMAPHORE_T)*handle); *handle = hl_null; #else # if HL_USE_NAMED_SEM named_sem_t * nsem = ((named_sem_t*)*handle); sem_close(nsem->sem); HL_MEMORY_FREE(nsem->name); #else sem_destroy((SEMAPHORE_T)GET_SEM(*handle)); #endif /* HL_USE_NAMED_SEM */ HL_MEMORY_FREE(handle); #endif } else { HL_DEBUG_WARN("Cannot free an uninitialized semaphore object"); } }
/**@ingroup tsk_semaphore_group * Decrements a semaphore. * @param handle The semaphore to decrement. * @retval Zero if succeed and otherwise the function returns -1 and sets errno to indicate the error. * @sa @ref tsk_semaphore_increment. */ int tsk_semaphore_decrement(tsk_semaphore_handle_t* handle) { int ret = EINVAL; if (handle) { #if TSK_UNDER_WINDOWS # if TSK_UNDER_WINDOWS_RT ret = (WaitForSingleObjectEx((SEMAPHORE_T)handle, INFINITE, TRUE) == WAIT_OBJECT_0) ? 0 : -1; # else ret = (WaitForSingleObject((SEMAPHORE_T)handle, INFINITE) == WAIT_OBJECT_0) ? 0 : -1; #endif if (ret) { TSK_DEBUG_ERROR("sem_wait function failed: %d", ret); } #else do { ret = sem_wait((SEMAPHORE_T)GET_SEM(handle)); } while ( errno == EINTR ); if(ret) TSK_DEBUG_ERROR("sem_wait function failed: %d", errno); #endif } return ret; }
hl_semaphore_handle_t* hl_semaphore_create_2(int initial_val) { SEMAPHORE_T handle = hl_null; #if HL_UNDER_WINDOWS # if HL_UNDER_WINDOWS_RT handle = CreateSemaphoreEx(NULL, initial_val, 0x7FFFFFFF, NULL, 0x00000000, SEMAPHORE_ALL_ACCESS); # else handle = CreateSemaphore(NULL, initial_val, 0x7FFFFFFF, NULL); # endif #else handle = hl_memory_calloc(1, sizeof(SEMAPHORE_S)); #if HL_USE_NAMED_SEM named_sem_t * nsem = (named_sem_t*)handle; hl_sprintf(&(nsem->name), "/sem-%d", sem_count++); if ((nsem->sem = sem_open(nsem->name, O_CREAT /*| O_EXCL*/, S_IRUSR | S_IWUSR, initial_val)) == SEM_FAILED) { HL_MEMORY_FREE(nsem->name); #else if (sem_init((SEMAPHORE_T)handle, 0, initial_val)) { #endif HL_MEMORY_FREE(handle); HL_DEBUG_ERROR("Failed to initialize the new semaphore (errno=%d).", errno); } #endif if (!handle) { HL_DEBUG_ERROR("Failed to create new semaphore"); } return handle; } int hl_semaphore_increment(hl_semaphore_handle_t* handle) { int ret = EINVAL; if (handle) { #if HL_UNDER_WINDOWS if ((ret = ReleaseSemaphore((SEMAPHORE_T)handle, 1L, NULL) ? 0 : -1)) { #else if ((ret = sem_post((SEMAPHORE_T)GET_SEM(handle)))) { #endif HL_DEBUG_ERROR("sem_post function failed: %d", ret); } } return ret; } int hl_semaphore_decrement(hl_semaphore_handle_t* handle) { int ret = EINVAL; if (handle) { #if HL_UNDER_WINDOWS # if HL_UNDER_WINDOWS_RT ret = (WaitForSingleObjectEx((SEMAPHORE_T)handle, INFINITE, TRUE) == WAIT_OBJECT_0) ? 0 : -1; # else ret = (WaitForSingleObject((SEMAPHORE_T)handle, INFINITE) == WAIT_OBJECT_0) ? 0 : -1; #endif if(ret) { HL_DEBUG_ERROR("sem_wait function failed: %d", ret); } #else do { ret = sem_wait((SEMAPHORE_T)GET_SEM(handle)); } while ( errno == EINTR ); if (ret) { HL_DEBUG_ERROR("sem_wait function failed: %d", errno); } #endif } return ret; }