Make sure the semaphore name is unique across processes

This commit is contained in:
bossiel 2015-05-22 00:43:46 +00:00
parent a2fa230552
commit d208a36f4d
1 changed files with 121 additions and 132 deletions

View File

@ -1,42 +1,38 @@
/* /*
* Copyright (C) 2010-2011 Mamadou Diop. * Copyright (C) 2010-2015 Mamadou DIOP.
* *
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org> * This file is part of Open Source Doubango Framework.
* *
* This file is part of Open Source Doubango Framework. * DOUBANGO is free software: you can redistribute it and/or modify
* * it under the terms of the GNU General Public License as published by
* DOUBANGO is free software: you can redistribute it and/or modify * the Free Software Foundation, either version 3 of the License, or
* it under the terms of the GNU General Public License as published by * (at your option) any later version.
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. * DOUBANGO is distributed in the hope that it will be useful,
* * but WITHOUT ANY WARRANTY; without even the implied warranty of
* DOUBANGO is distributed in the hope that it will be useful, * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* but WITHOUT ANY WARRANTY; without even the implied warranty of * GNU General Public License for more details.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. * You should have received a copy of the GNU General Public License
* * along with DOUBANGO.
* You should have received a copy of the GNU General Public License *
* along with DOUBANGO. */
*
*/
/**@file tsk_semaphore.c /**@file tsk_semaphore.c
* @brief Pthread/Windows Semaphore utility functions. * @brief Pthread/Windows Semaphore utility functions.
* * @date Created: Sat Nov 8 16:54:58 2009 mdiop
* @author Mamadou Diop <diopmamadou(at)doubango[dot]org> */
*
* @date Created: Sat Nov 8 16:54:58 2009 mdiop
*/
#include "tsk_semaphore.h" #include "tsk_semaphore.h"
#include "tsk_memory.h" #include "tsk_memory.h"
#include "tsk_debug.h" #include "tsk_debug.h"
#include "tsk_string.h" #include "tsk_string.h"
#include "tsk_time.h"
/* Apple claims that they fully support POSIX semaphore but ... /* Apple claims that they fully support POSIX semaphore but ...
*/ */
#if defined(__APPLE__) /* Mac OSX/Darwin/Iphone/Ipod Touch */ #if defined(__APPLE__) /* Mac OSX/Darwin/Iphone/Ipod Touch */
# define TSK_USE_NAMED_SEM 1 # define TSK_USE_NAMED_SEM 1
#else #else
# define TSK_USE_NAMED_SEM 0 # define TSK_USE_NAMED_SEM 0
#endif #endif
@ -45,7 +41,7 @@
# include <windows.h> # include <windows.h>
# include "tsk_errno.h" # include "tsk_errno.h"
# define SEMAPHORE_S void # define SEMAPHORE_S void
typedef HANDLE SEMAPHORE_T; typedef HANDLE SEMAPHORE_T;
# if TSK_UNDER_WINDOWS_RT # if TSK_UNDER_WINDOWS_RT
# if !defined(CreateSemaphoreEx) # if !defined(CreateSemaphoreEx)
# define CreateSemaphoreEx CreateSemaphoreExW # define CreateSemaphoreEx CreateSemaphoreExW
@ -62,19 +58,18 @@
# include <fcntl.h> /* O_CREAT */ # include <fcntl.h> /* O_CREAT */
# include <sys/stat.h> /* S_IRUSR, S_IWUSR*/ # include <sys/stat.h> /* S_IRUSR, S_IWUSR*/
static int sem_count = 0; typedef struct named_sem_s
typedef struct named_sem_s {
{ sem_t* sem;
sem_t* sem; char name [NAME_MAX + 1];
char* name; } named_sem_t;
} named_sem_t;
# define SEMAPHORE_S named_sem_t # define SEMAPHORE_S named_sem_t
# define GET_SEM(PSEM) (((named_sem_t*)(PSEM))->sem) # define GET_SEM(PSEM) (((named_sem_t*)(PSEM))->sem)
# else # else
# define SEMAPHORE_S sem_t # define SEMAPHORE_S sem_t
# define GET_SEM(PSEM) ((PSEM)) # define GET_SEM(PSEM) ((PSEM))
# endif /* TSK_USE_NAMED_SEM */ # endif /* TSK_USE_NAMED_SEM */
typedef sem_t* SEMAPHORE_T; typedef sem_t* SEMAPHORE_T;
#endif #endif
@ -85,132 +80,126 @@
/**@defgroup tsk_semaphore_group Pthread/Windows Semaphore functions. /**@defgroup tsk_semaphore_group Pthread/Windows Semaphore functions.
*/ */
/**@ingroup tsk_semaphore_group /**@ingroup tsk_semaphore_group
* Creates new semaphore handle. * Creates new semaphore handle.
* @retval A New semaphore handle. * @retval A New semaphore handle.
* You MUST call @ref tsk_semaphore_destroy to free the semaphore. * You MUST call @ref tsk_semaphore_destroy to free the semaphore.
* @sa @ref tsk_semaphore_destroy * @sa @ref tsk_semaphore_destroy
*/ */
tsk_semaphore_handle_t* tsk_semaphore_create() tsk_semaphore_handle_t* tsk_semaphore_create()
{ {
return tsk_semaphore_create_2(0); return tsk_semaphore_create_2(0);
} }
tsk_semaphore_handle_t* tsk_semaphore_create_2(int initial_val) tsk_semaphore_handle_t* tsk_semaphore_create_2(int initial_val)
{ {
SEMAPHORE_T handle = tsk_null; SEMAPHORE_T handle = tsk_null;
#if TSK_UNDER_WINDOWS #if TSK_UNDER_WINDOWS
# if TSK_UNDER_WINDOWS_RT # if TSK_UNDER_WINDOWS_RT
handle = CreateSemaphoreEx(NULL, initial_val, 0x7FFFFFFF, NULL, 0x00000000, SEMAPHORE_ALL_ACCESS); handle = CreateSemaphoreEx(NULL, initial_val, 0x7FFFFFFF, NULL, 0x00000000, SEMAPHORE_ALL_ACCESS);
# else # else
handle = CreateSemaphore(NULL, initial_val, 0x7FFFFFFF, NULL); handle = CreateSemaphore(NULL, initial_val, 0x7FFFFFFF, NULL);
# endif # endif
#else #else
handle = tsk_calloc(1, sizeof(SEMAPHORE_S)); handle = tsk_calloc(1, sizeof(SEMAPHORE_S));
#if TSK_USE_NAMED_SEM #if TSK_USE_NAMED_SEM
named_sem_t * nsem = (named_sem_t*)handle; named_sem_t * nsem = (named_sem_t*)handle;
tsk_sprintf(&(nsem->name), "/sem-%d", sem_count++); 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) if ((nsem->sem = sem_open(nsem->name, O_CREAT /*| O_EXCL*/, S_IRUSR | S_IWUSR, initial_val)) == SEM_FAILED) {
{
TSK_FREE(nsem->name);
#else #else
if(sem_init((SEMAPHORE_T)handle, 0, initial_val)) if (sem_init((SEMAPHORE_T)handle, 0, initial_val)) {
{
#endif #endif
TSK_FREE(handle); TSK_FREE(handle);
TSK_DEBUG_ERROR("Failed to initialize the new semaphore (errno=%d).", errno); TSK_DEBUG_ERROR("Failed to initialize the new semaphore (errno=%d).", errno);
} }
#endif #endif
if (!handle) {
if(!handle){ TSK_DEBUG_ERROR("Failed to create new semaphore");
TSK_DEBUG_ERROR("Failed to create new semaphore"); }
} return handle;
return handle; }
}
/**@ingroup tsk_semaphore_group
/**@ingroup tsk_semaphore_group * Increments a semaphore.
* Increments a semaphore. * @param handle The semaphore to increment.
* @param handle The semaphore to increment. * @retval Zero if succeed and otherwise the function returns -1 and sets errno to indicate the error.
* @retval Zero if succeed and otherwise the function returns -1 and sets errno to indicate the error. * @sa @ref tsk_semaphore_decrement.
* @sa @ref tsk_semaphore_decrement. */
*/ int tsk_semaphore_increment(tsk_semaphore_handle_t* handle)
int tsk_semaphore_increment(tsk_semaphore_handle_t* handle) {
{ int ret = EINVAL;
int ret = EINVAL; if (handle) {
if(handle)
{
#if TSK_UNDER_WINDOWS #if TSK_UNDER_WINDOWS
if((ret = ReleaseSemaphore((SEMAPHORE_T)handle, 1L, NULL) ? 0 : -1)) if((ret = ReleaseSemaphore((SEMAPHORE_T)handle, 1L, NULL) ? 0 : -1))
#else #else
if((ret = sem_post((SEMAPHORE_T)GET_SEM(handle)))) if((ret = sem_post((SEMAPHORE_T)GET_SEM(handle))))
#endif #endif
{ {
TSK_DEBUG_ERROR("sem_post function failed: %d", ret); TSK_DEBUG_ERROR("sem_post function failed: %d", ret);
} }
} }
return ret; return ret;
} }
/**@ingroup tsk_semaphore_group /**@ingroup tsk_semaphore_group
* Decrements a semaphore. * Decrements a semaphore.
* @param handle The semaphore to decrement. * @param handle The semaphore to decrement.
* @retval Zero if succeed and otherwise the function returns -1 and sets errno to indicate the error. * @retval Zero if succeed and otherwise the function returns -1 and sets errno to indicate the error.
* @sa @ref tsk_semaphore_increment. * @sa @ref tsk_semaphore_increment.
*/ */
int tsk_semaphore_decrement(tsk_semaphore_handle_t* handle) int tsk_semaphore_decrement(tsk_semaphore_handle_t* handle)
{ {
int ret = EINVAL; int ret = EINVAL;
if (handle) { if (handle) {
#if TSK_UNDER_WINDOWS #if TSK_UNDER_WINDOWS
# if TSK_UNDER_WINDOWS_RT # if TSK_UNDER_WINDOWS_RT
ret = (WaitForSingleObjectEx((SEMAPHORE_T)handle, INFINITE, TRUE) == WAIT_OBJECT_0) ? 0 : -1; ret = (WaitForSingleObjectEx((SEMAPHORE_T)handle, INFINITE, TRUE) == WAIT_OBJECT_0) ? 0 : -1;
# else # else
ret = (WaitForSingleObject((SEMAPHORE_T)handle, INFINITE) == WAIT_OBJECT_0) ? 0 : -1; ret = (WaitForSingleObject((SEMAPHORE_T)handle, INFINITE) == WAIT_OBJECT_0) ? 0 : -1;
#endif #endif
if (ret) { if (ret) {
TSK_DEBUG_ERROR("sem_wait function failed: %d", ret); TSK_DEBUG_ERROR("sem_wait function failed: %d", ret);
} }
#else #else
do { do {
ret = sem_wait((SEMAPHORE_T)GET_SEM(handle)); ret = sem_wait((SEMAPHORE_T)GET_SEM(handle));
} }
while ( errno == EINTR ); while ( errno == EINTR );
if(ret) TSK_DEBUG_ERROR("sem_wait function failed: %d", errno); if(ret) TSK_DEBUG_ERROR("sem_wait function failed: %d", errno);
#endif #endif
} }
return ret; return ret;
} }
/**@ingroup tsk_semaphore_group /**@ingroup tsk_semaphore_group
* Destroy a semaphore previously created using @ref tsk_semaphore_create. * Destroy a semaphore previously created using @ref tsk_semaphore_create.
* @param handle The semaphore to free. * @param handle The semaphore to free.
* @sa @ref tsk_semaphore_create * @sa @ref tsk_semaphore_create
*/ */
void tsk_semaphore_destroy(tsk_semaphore_handle_t** handle) void tsk_semaphore_destroy(tsk_semaphore_handle_t** handle)
{ {
if(handle && *handle) if(handle && *handle)
{ {
#if TSK_UNDER_WINDOWS #if TSK_UNDER_WINDOWS
CloseHandle((SEMAPHORE_T)*handle); CloseHandle((SEMAPHORE_T)*handle);
*handle = tsk_null; *handle = tsk_null;
#else #else
# if TSK_USE_NAMED_SEM # if TSK_USE_NAMED_SEM
named_sem_t * nsem = ((named_sem_t*)*handle); named_sem_t * nsem = ((named_sem_t*)*handle);
sem_close(nsem->sem); sem_close(nsem->sem);
TSK_FREE(nsem->name);
#else #else
sem_destroy((SEMAPHORE_T)GET_SEM(*handle)); sem_destroy((SEMAPHORE_T)GET_SEM(*handle));
#endif /* TSK_USE_NAMED_SEM */ #endif /* TSK_USE_NAMED_SEM */
tsk_free(handle); tsk_free(handle);
#endif #endif
} }
else{ else{
TSK_DEBUG_WARN("Cannot free an uninitialized semaphore object"); TSK_DEBUG_WARN("Cannot free an uninitialized semaphore object");
} }
} }