Commit 4f33f242 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Sem available return now 1 if sem free at one level, -1 for more than one, and 0 if ti is not

parent ea71173c
......@@ -49,7 +49,7 @@ void blc_fatal_pthread_error(const char *name_of_file, const char* name_of_funct
///Return the time difference between the timeval struct and the actual time and set the time to the current time.
long blc_us_time_diff(struct timeval *time);
///Check weather a semaphore is locked. You may use sem_getvalue instead but it does not exist on darwin (Mac OSX).
int blc_sem_is_locked(sem_t *sem);
int blc_sem_available(sem_t *sem);
/**Try to lock the mutex in timeout micro seconds.
@return 1 in case of immediate success, -1 in case of succes after microseconds, 0 in case of failure */
int blc_mutex_trylock_in_time(pthread_mutex_t *mutex, uint32_t microseconds);
......
......@@ -70,18 +70,28 @@ long blc_us_time_diff(struct timeval *previous_time)
return delta_useconds;
}
int blc_sem_is_locked(sem_t *sem)
int blc_sem_available(sem_t *sem)
{
int ret=0;
if( sem==NULL) EXIT_ON_ERROR("sem is NULL");
if (sem_trywait(sem)==0) //Success to lock (it was not lock)
{
SYSTEM_ERROR_CHECK(sem_post(sem), -1, "");
return 0;
if (sem_trywait(sem)==0){
SYSTEM_ERROR_CHECK(sem_post(sem), -1, NULL);
ret=-1; //It is free at at least 2 levels
}
else {
if (errno != EAGAIN) EXIT_ON_SYSTEM_ERROR("sem_trywait");
else ret=1; //Is is free one level
}
SYSTEM_ERROR_CHECK(sem_post(sem), -1, NULL);
return ret;
}
else
{
if (errno != EAGAIN) EXIT_ON_SYSTEM_ERROR("sem_trywait");
return 1;
return 0;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment