eina: add Eina_Semaphore API.

Patch by Vincent Torri.


SVN revision: 66693
This commit is contained in:
Cedric BAIL 2011-12-30 13:38:53 +00:00
parent 6d8b053f13
commit d92a903ffe
7 changed files with 163 additions and 0 deletions

View File

@ -173,3 +173,7 @@
2011-12-28 Cedric Bail
* Fix NONNULL argument for eina_hash_find.
2011-12-30 Vincent Torri
* Add Eina_Semaphore abstraction API.

View File

@ -6,6 +6,7 @@ Changes since Eina 1.1.0:
Additions:
* eina_mempool_calloc
* Eina_Semaphore abstraction API
Eina 1.1.0

View File

@ -19,6 +19,15 @@
#ifndef EINA_INLINE_LOCK_POSIX_X_
#define EINA_INLINE_LOCK_POSIX_X_
#ifdef EINA_UNUSED
# undef EINA_UNUSED
#endif
#ifdef __GNUC__
# define EINA_UNUSED __attribute__((unused))
#else
# define EINA_UNUSED
#endif
#include <errno.h>
#ifndef __USE_UNIX98
# define __USE_UNIX98
@ -28,6 +37,8 @@
# include <pthread.h>
#endif
#include <semaphore.h>
#include <sys/time.h>
#include <stdio.h>
@ -46,6 +57,7 @@ typedef struct _Eina_Lock Eina_Lock;
typedef struct _Eina_RWLock Eina_RWLock;
typedef struct _Eina_Condition Eina_Condition;
typedef pthread_key_t Eina_TLS;
typedef sem_t Eina_Semaphore;
struct _Eina_Lock
{
@ -505,4 +517,40 @@ eina_tls_set(Eina_TLS key, const void *data)
return EINA_TRUE;
}
static inline Eina_Bool
eina_semaphore_new(Eina_Semaphore *sem, int count_init)
{
if (!sem || (count_init <= 0))
return EINA_FALSE;
return (sem_init(sem, count_init, 1) == 0) ? EINA_TRUE : EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_free(Eina_Semaphore *sem)
{
if (!sem)
return EINA_FALSE;
return (sem_destroy(sem) == 0) ? EINA_TRUE : EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_lock(Eina_Semaphore *sem)
{
if (!sem)
return EINA_FALSE;
return (sem_wait(sem) == 0) ? EINA_TRUE : EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_release(Eina_Semaphore *sem, int count_release EINA_UNUSED)
{
if (!sem)
return EINA_FALSE;
return (sem_post(sem) == 0) ? EINA_TRUE : EINA_FALSE;
}
#endif

View File

@ -47,6 +47,7 @@ typedef void *Eina_Lock;
typedef void *Eina_RWLock;
typedef void *Eina_Condition;
typedef void *Eina_TLS;
typedef void *Eina_Semaphore;
/**
* @brief Create a new #Eina_Lock.
@ -239,6 +240,31 @@ eina_tls_set(Eina_TLS key EINA_UNUSED, const void *data EINA_UNUSED)
return EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_new(Eina_Semaphore *sem EINA_UNUSED,
int count_init EINA_UNUSED)
{
return EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_free(Eina_Semaphore *sem EINA_UNUSED)
{
return EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_lock(Eina_Semaphore *sem EINA_UNUSED)
{
return EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_release(Eina_Semaphore *sem EINA_UNUSED,
int count_release EINA_UNUSED)
{
return EINA_FALSE;
}
/**
* @}

View File

@ -58,6 +58,8 @@ struct _Eina_Win32_RWLock
typedef DWORD Eina_TLS;
typedef HANDLE Eina_Semaphore;
EAPI extern Eina_Bool _eina_threads_activated;
static inline Eina_Bool
@ -463,4 +465,48 @@ eina_tls_set(Eina_TLS key, const void *data)
return EINA_TRUE;
}
static inline Eina_Bool
eina_semaphore_new(Eina_Semaphore *sem, int count_init)
{
if (!sem || (count_init <= 0))
return EINA_FALSE;
*sem = CreateSemaphore(NULL, count_init, 32767, NULL);
if (!*sem)
return EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_free(Eina_Semaphore *sem)
{
if (!sem)
return EINA_FALSE;
CloseHandle(*sem);
}
static inline Eina_Bool
eina_semaphore_lock(Eina_Semaphore *sem)
{
DWORD res;
if (!sem)
return EINA_FALSE;
res = WaitForSingleObject(ev->shared->lock, 0L);
if (res == WAIT_OBJECT_0)
return EINA_TRUE;
return EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_release(Eina_Semaphore *sem, int count_release)
{
if (!sem)
return EINA_FALSE;
return ReleaseSemaphore(*sem, count_release, NULL) ? EINA_TRUE : EINA_FALSE;
}
#endif

View File

@ -19,6 +19,15 @@
#ifndef EINA_INLINE_LOCK_WIN32_X_
#define EINA_INLINE_LOCK_WIN32_X_
#ifdef EINA_UNUSED
# undef EINA_UNUSED
#endif
#ifdef __GNUC__
# define EINA_UNUSED __attribute__((unused))
#else
# define EINA_UNUSED
#endif
#include <windows.h>
EAPI extern Eina_Bool _threads_activated;
@ -26,6 +35,7 @@ EAPI extern Eina_Bool _threads_activated;
typedef HANDLE Eina_Lock;
typedef Eina_Lock Eina_RWLock;
typedef DWORD Eina_TLS;
typedef void * Eina_Semaphore;
static inline Eina_Bool
eina_lock_new(Eina_Lock *mutex)
@ -173,6 +183,30 @@ eina_tls_set(Eina_TLS key, const void *data)
return EINA_TRUE;
}
static inline Eina_Bool
eina_semaphore_new(Eina_Semaphore *sem EINA_UNUSED,
int count_init EINA_UNUSED)
{
return EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_free(Eina_Semaphore *sem EINA_UNUSED)
{
return EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_lock(Eina_Semaphore *sem EINA_UNUSED)
{
return EINA_FALSE;
}
static inline Eina_Bool
eina_semaphore_release(Eina_Semaphore *sem EINA_UNUSED,
int count_release EINA_UNUSED)
{
return EINA_FALSE;
}
#endif

View File

@ -81,6 +81,10 @@ static inline void eina_tls_free(Eina_TLS key);
static inline void *eina_tls_get(Eina_TLS key);
static inline Eina_Bool eina_tls_set(Eina_TLS key, const void *data);
static inline Eina_Bool eina_semaphore_new(Eina_Semaphore *sem, int count_init);
static inline Eina_Bool eina_semaphore_free(Eina_Semaphore *sem);
static inline Eina_Bool eina_semaphore_lock(Eina_Semaphore *sem);
static inline Eina_Bool eina_semaphore_release(Eina_Semaphore *sem, int count_release);
#ifdef EINA_HAVE_DEBUG_THREADS
# define EINA_MAIN_LOOP_CHECK_RETURN_VAL(val) \