2011-04-22 14:26:36 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2011 Vincent Torri
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EINA_INLINE_LOCK_POSIX_X_
|
|
|
|
#define EINA_INLINE_LOCK_POSIX_X_
|
|
|
|
|
2013-10-10 01:22:28 -07:00
|
|
|
#ifdef EINA_HAVE_POSIX_SPINLOCK
|
|
|
|
# include <sched.h>
|
|
|
|
#endif
|
|
|
|
|
2011-05-01 06:24:08 -07:00
|
|
|
#include <errno.h>
|
2011-04-24 19:30:42 -07:00
|
|
|
#ifndef __USE_UNIX98
|
|
|
|
# define __USE_UNIX98
|
|
|
|
# include <pthread.h>
|
2011-04-25 01:48:20 -07:00
|
|
|
# undef __USE_UNIX98
|
2011-04-24 19:30:42 -07:00
|
|
|
#else
|
|
|
|
# include <pthread.h>
|
|
|
|
#endif
|
2011-04-22 14:26:36 -07:00
|
|
|
|
2011-12-30 05:38:53 -08:00
|
|
|
#include <semaphore.h>
|
|
|
|
|
2011-07-13 01:51:19 -07:00
|
|
|
#include <sys/time.h>
|
2011-12-06 13:33:39 -08:00
|
|
|
#include <stdio.h>
|
2011-07-13 01:51:19 -07:00
|
|
|
|
2011-05-02 04:25:35 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-04 06:53:22 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2011-05-01 06:24:08 -07:00
|
|
|
#include <execinfo.h>
|
|
|
|
#define EINA_LOCK_DEBUG_BT_NUM 64
|
|
|
|
typedef void (*Eina_Lock_Bt_Func) ();
|
2011-05-04 06:53:22 -07:00
|
|
|
|
|
|
|
#include "eina_inlist.h"
|
2011-05-01 06:24:08 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct _Eina_Lock Eina_Lock;
|
2011-06-23 02:58:54 -07:00
|
|
|
typedef struct _Eina_RWLock Eina_RWLock;
|
2011-05-09 03:32:56 -07:00
|
|
|
typedef struct _Eina_Condition Eina_Condition;
|
2011-10-16 22:06:01 -07:00
|
|
|
typedef pthread_key_t Eina_TLS;
|
2011-12-30 05:38:53 -08:00
|
|
|
typedef sem_t Eina_Semaphore;
|
2013-10-10 01:22:28 -07:00
|
|
|
#ifdef EINA_HAVE_POSIX_SPINLOCK
|
|
|
|
typedef pthread_spinlock_t Eina_Spinlock;
|
|
|
|
#else
|
|
|
|
typedef Eina_Lock Eina_Spinlock;
|
|
|
|
#endif
|
2011-05-01 06:24:08 -07:00
|
|
|
|
|
|
|
struct _Eina_Lock
|
|
|
|
{
|
2011-05-04 06:53:22 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
EINA_INLIST;
|
|
|
|
#endif
|
2011-05-09 03:32:56 -07:00
|
|
|
pthread_mutex_t mutex;
|
2011-05-02 04:25:35 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-01 06:24:08 -07:00
|
|
|
pthread_t lock_thread_id;
|
|
|
|
Eina_Lock_Bt_Func lock_bt[EINA_LOCK_DEBUG_BT_NUM];
|
|
|
|
int lock_bt_num;
|
|
|
|
Eina_Bool locked : 1;
|
2011-05-02 01:24:06 -07:00
|
|
|
#endif
|
2011-05-02 04:25:35 -07:00
|
|
|
};
|
2011-05-02 01:24:06 -07:00
|
|
|
|
2011-05-09 03:32:56 -07:00
|
|
|
struct _Eina_Condition
|
|
|
|
{
|
|
|
|
Eina_Lock *lock;
|
|
|
|
pthread_cond_t condition;
|
|
|
|
};
|
|
|
|
|
2011-06-23 02:58:54 -07:00
|
|
|
struct _Eina_RWLock
|
|
|
|
{
|
|
|
|
pthread_rwlock_t mutex;
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
pthread_t lock_thread_wid;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-04-24 13:22:17 -07:00
|
|
|
EAPI extern Eina_Bool _eina_threads_activated;
|
|
|
|
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
EAPI extern int _eina_threads_debug;
|
2011-05-02 04:25:35 -07:00
|
|
|
EAPI extern pthread_t _eina_main_loop;
|
2011-05-04 06:53:22 -07:00
|
|
|
EAPI extern pthread_mutex_t _eina_tracking_lock;
|
|
|
|
EAPI extern Eina_Inlist *_eina_tracking;
|
2011-04-24 13:22:17 -07:00
|
|
|
#endif
|
2011-04-24 08:54:09 -07:00
|
|
|
|
2011-05-09 05:54:47 -07:00
|
|
|
static inline void
|
|
|
|
eina_lock_debug(const Eina_Lock *mutex)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
printf("lock %p, locked: %i, by %i\n",
|
|
|
|
mutex, (int)mutex->locked, (int)mutex->lock_thread_id);
|
|
|
|
backtrace_symbols_fd((void **)mutex->lock_bt, mutex->lock_bt_num, 1);
|
|
|
|
#else
|
|
|
|
(void) mutex;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-02 06:51:20 -07:00
|
|
|
static inline Eina_Bool
|
2011-04-22 14:26:36 -07:00
|
|
|
eina_lock_new(Eina_Lock *mutex)
|
|
|
|
{
|
2011-04-24 13:22:17 -07:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
|
2011-05-02 04:25:35 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-09-07 03:16:27 -07:00
|
|
|
if (!_eina_threads_activated)
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
2011-05-02 04:25:35 -07:00
|
|
|
#endif
|
|
|
|
|
2011-04-24 13:22:17 -07:00
|
|
|
if (pthread_mutexattr_init(&attr) != 0)
|
|
|
|
return EINA_FALSE;
|
2011-05-04 06:53:22 -07:00
|
|
|
/* NOTE: PTHREAD_MUTEX_RECURSIVE is not allowed at all, you will break on/off
|
|
|
|
feature for sure with that change. */
|
2011-05-30 05:49:04 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-01 06:24:08 -07:00
|
|
|
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0)
|
|
|
|
return EINA_FALSE;
|
|
|
|
memset(mutex, 0, sizeof(Eina_Lock));
|
2011-05-02 04:25:35 -07:00
|
|
|
#endif
|
2011-05-01 06:24:08 -07:00
|
|
|
if (pthread_mutex_init(&(mutex->mutex), &attr) != 0)
|
2011-04-24 13:22:17 -07:00
|
|
|
return EINA_FALSE;
|
|
|
|
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
2011-04-22 14:26:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2011-04-24 09:49:48 -07:00
|
|
|
eina_lock_free(Eina_Lock *mutex)
|
2011-04-22 14:26:36 -07:00
|
|
|
{
|
2011-05-02 04:25:35 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-09-07 03:16:27 -07:00
|
|
|
if (!_eina_threads_activated)
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
2011-05-02 04:25:35 -07:00
|
|
|
#endif
|
|
|
|
|
2011-05-01 06:24:08 -07:00
|
|
|
pthread_mutex_destroy(&(mutex->mutex));
|
2011-05-02 04:25:35 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-01 06:24:08 -07:00
|
|
|
memset(mutex, 0, sizeof(Eina_Lock));
|
2011-05-02 04:25:35 -07:00
|
|
|
#endif
|
2011-04-22 14:26:36 -07:00
|
|
|
}
|
|
|
|
|
2011-05-02 01:24:06 -07:00
|
|
|
static inline Eina_Lock_Result
|
2011-04-24 09:49:48 -07:00
|
|
|
eina_lock_take(Eina_Lock *mutex)
|
2011-04-22 14:26:36 -07:00
|
|
|
{
|
2011-05-02 06:51:20 -07:00
|
|
|
Eina_Lock_Result ret = EINA_LOCK_FAIL;
|
2011-05-02 01:34:22 -07:00
|
|
|
int ok;
|
2011-05-02 04:25:35 -07:00
|
|
|
|
2011-05-02 06:40:28 -07:00
|
|
|
#ifdef EINA_HAVE_ON_OFF_THREADS
|
|
|
|
if (!_eina_threads_activated)
|
|
|
|
{
|
2011-04-24 13:22:17 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-02 06:40:28 -07:00
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
|
|
|
#endif
|
2011-05-04 06:53:22 -07:00
|
|
|
return EINA_LOCK_SUCCEED;
|
2011-05-02 06:40:28 -07:00
|
|
|
}
|
|
|
|
#endif
|
2011-05-02 04:25:35 -07:00
|
|
|
|
2011-05-02 06:40:28 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-01 06:24:08 -07:00
|
|
|
if (_eina_threads_debug)
|
|
|
|
{
|
|
|
|
struct timeval t0, t1;
|
|
|
|
int dt;
|
2011-05-02 04:25:35 -07:00
|
|
|
|
2011-05-01 06:24:08 -07:00
|
|
|
gettimeofday(&t0, NULL);
|
2011-05-04 06:53:22 -07:00
|
|
|
ok = pthread_mutex_lock(&(mutex->mutex));
|
2011-05-01 06:24:08 -07:00
|
|
|
gettimeofday(&t1, NULL);
|
2011-05-02 04:25:35 -07:00
|
|
|
|
2011-05-01 06:24:08 -07:00
|
|
|
dt = (t1.tv_sec - t0.tv_sec) * 1000000;
|
|
|
|
if (t1.tv_usec > t0.tv_usec)
|
|
|
|
dt += (t1.tv_usec - t0.tv_usec);
|
|
|
|
else
|
|
|
|
dt -= t0.tv_usec - t1.tv_usec;
|
|
|
|
dt /= 1000;
|
2011-05-02 04:25:35 -07:00
|
|
|
|
2011-05-01 06:24:08 -07:00
|
|
|
if (dt > _eina_threads_debug) abort();
|
|
|
|
}
|
2011-05-04 06:53:22 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
ok = pthread_mutex_lock(&(mutex->mutex));
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
}
|
2011-04-24 13:22:17 -07:00
|
|
|
#endif
|
2011-05-04 06:53:22 -07:00
|
|
|
|
2011-05-02 06:51:20 -07:00
|
|
|
if (ok == 0) ret = EINA_LOCK_SUCCEED;
|
2011-05-02 01:34:22 -07:00
|
|
|
else if (ok == EDEADLK)
|
|
|
|
{
|
|
|
|
printf("ERROR ERROR: DEADLOCK on lock %p\n", mutex);
|
2011-05-09 05:54:47 -07:00
|
|
|
eina_lock_debug(mutex);
|
2011-05-02 01:34:22 -07:00
|
|
|
ret = EINA_LOCK_DEADLOCK; // magic
|
2011-05-09 06:06:03 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-09 05:54:47 -07:00
|
|
|
if (_eina_threads_debug) abort();
|
2011-05-09 06:06:03 -07:00
|
|
|
#endif
|
2011-05-02 01:34:22 -07:00
|
|
|
}
|
2011-05-04 06:53:22 -07:00
|
|
|
|
2011-05-02 04:25:35 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-01 06:24:08 -07:00
|
|
|
mutex->locked = 1;
|
|
|
|
mutex->lock_thread_id = pthread_self();
|
|
|
|
mutex->lock_bt_num = backtrace((void **)(mutex->lock_bt), EINA_LOCK_DEBUG_BT_NUM);
|
2011-05-04 06:53:22 -07:00
|
|
|
|
|
|
|
pthread_mutex_lock(&_eina_tracking_lock);
|
|
|
|
_eina_tracking = eina_inlist_append(_eina_tracking,
|
|
|
|
EINA_INLIST_GET(mutex));
|
|
|
|
pthread_mutex_unlock(&_eina_tracking_lock);
|
2011-05-02 04:25:35 -07:00
|
|
|
#endif
|
2011-05-04 06:53:22 -07:00
|
|
|
|
2011-05-01 06:24:08 -07:00
|
|
|
return ret;
|
2011-04-22 14:26:36 -07:00
|
|
|
}
|
|
|
|
|
2011-05-02 01:24:06 -07:00
|
|
|
static inline Eina_Lock_Result
|
2011-04-24 09:49:48 -07:00
|
|
|
eina_lock_take_try(Eina_Lock *mutex)
|
2011-04-22 14:26:36 -07:00
|
|
|
{
|
2011-05-02 06:51:20 -07:00
|
|
|
Eina_Lock_Result ret = EINA_LOCK_FAIL;
|
2011-05-01 06:24:08 -07:00
|
|
|
int ok;
|
2011-05-02 04:25:35 -07:00
|
|
|
|
2011-05-02 06:40:28 -07:00
|
|
|
#ifdef EINA_HAVE_ON_OFF_THREADS
|
|
|
|
if (!_eina_threads_activated)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
|
|
|
#endif
|
2011-05-04 06:53:22 -07:00
|
|
|
return EINA_LOCK_SUCCEED;
|
2011-05-02 06:40:28 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-02 04:25:35 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-02 06:40:28 -07:00
|
|
|
if (!_eina_threads_activated)
|
2011-05-02 04:25:35 -07:00
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
|
|
|
#endif
|
|
|
|
|
2011-05-01 06:24:08 -07:00
|
|
|
ok = pthread_mutex_trylock(&(mutex->mutex));
|
2011-05-02 06:51:20 -07:00
|
|
|
if (ok == 0) ret = EINA_LOCK_SUCCEED;
|
2011-05-01 06:24:08 -07:00
|
|
|
else if (ok == EDEADLK)
|
|
|
|
{
|
2011-05-02 01:34:22 -07:00
|
|
|
printf("ERROR ERROR: DEADLOCK on trylock %p\n", mutex);
|
2011-05-02 01:24:06 -07:00
|
|
|
ret = EINA_LOCK_DEADLOCK; // magic
|
2011-05-01 06:24:08 -07:00
|
|
|
}
|
2011-05-02 04:25:35 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-02 06:51:20 -07:00
|
|
|
if (ret == EINA_LOCK_SUCCEED)
|
2011-05-01 06:24:08 -07:00
|
|
|
{
|
|
|
|
mutex->locked = 1;
|
|
|
|
mutex->lock_thread_id = pthread_self();
|
|
|
|
mutex->lock_bt_num = backtrace((void **)(mutex->lock_bt), EINA_LOCK_DEBUG_BT_NUM);
|
2011-05-04 06:53:22 -07:00
|
|
|
|
|
|
|
pthread_mutex_lock(&_eina_tracking_lock);
|
|
|
|
_eina_tracking = eina_inlist_append(_eina_tracking,
|
|
|
|
EINA_INLIST_GET(mutex));
|
|
|
|
pthread_mutex_unlock(&_eina_tracking_lock);
|
2011-05-01 06:24:08 -07:00
|
|
|
}
|
2011-05-02 04:25:35 -07:00
|
|
|
#endif
|
2011-05-01 06:24:08 -07:00
|
|
|
return ret;
|
2011-04-22 14:26:36 -07:00
|
|
|
}
|
|
|
|
|
2011-05-02 01:24:06 -07:00
|
|
|
static inline Eina_Lock_Result
|
2011-04-24 09:49:48 -07:00
|
|
|
eina_lock_release(Eina_Lock *mutex)
|
2011-04-22 14:26:36 -07:00
|
|
|
{
|
2011-05-02 06:40:28 -07:00
|
|
|
Eina_Lock_Result ret;
|
2011-05-02 04:25:35 -07:00
|
|
|
|
2011-05-02 06:40:28 -07:00
|
|
|
#ifdef EINA_HAVE_ON_OFF_THREADS
|
|
|
|
if (!_eina_threads_activated)
|
|
|
|
{
|
2011-05-02 04:25:35 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-02 06:40:28 -07:00
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
|
|
|
#endif
|
2011-05-04 06:53:22 -07:00
|
|
|
return EINA_LOCK_SUCCEED;
|
2011-05-02 06:40:28 -07:00
|
|
|
}
|
2011-05-02 04:25:35 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-04 06:53:22 -07:00
|
|
|
pthread_mutex_lock(&_eina_tracking_lock);
|
|
|
|
_eina_tracking = eina_inlist_remove(_eina_tracking,
|
|
|
|
EINA_INLIST_GET(mutex));
|
|
|
|
pthread_mutex_unlock(&_eina_tracking_lock);
|
|
|
|
|
2011-05-01 06:24:08 -07:00
|
|
|
mutex->locked = 0;
|
|
|
|
mutex->lock_thread_id = 0;
|
|
|
|
memset(mutex->lock_bt, 0, EINA_LOCK_DEBUG_BT_NUM * sizeof(Eina_Lock_Bt_Func));
|
|
|
|
mutex->lock_bt_num = 0;
|
2011-05-02 04:25:35 -07:00
|
|
|
#endif
|
|
|
|
ret = (pthread_mutex_unlock(&(mutex->mutex)) == 0) ?
|
2011-05-02 06:51:20 -07:00
|
|
|
EINA_LOCK_SUCCEED : EINA_LOCK_FAIL;
|
2011-05-01 06:24:08 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-09 03:32:56 -07:00
|
|
|
static inline Eina_Bool
|
|
|
|
eina_condition_new(Eina_Condition *cond, Eina_Lock *mutex)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-05-09 03:42:42 -07:00
|
|
|
assert(mutex != NULL);
|
2011-09-07 03:16:27 -07:00
|
|
|
if (!_eina_threads_activated)
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
2011-05-09 03:32:56 -07:00
|
|
|
memset(cond, 0, sizeof (Eina_Condition));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cond->lock = mutex;
|
|
|
|
if (pthread_cond_init(&cond->condition, NULL) != 0)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
if (errno == EBUSY)
|
|
|
|
printf("eina_condition_new on already initialized Eina_Condition\n");
|
|
|
|
#endif
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
eina_condition_free(Eina_Condition *cond)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-09-07 03:16:27 -07:00
|
|
|
if (!_eina_threads_activated)
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
2011-05-09 03:32:56 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
pthread_cond_destroy(&(cond->condition));
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
memset(cond, 0, sizeof (Eina_Condition));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Bool
|
|
|
|
eina_condition_wait(Eina_Condition *cond)
|
|
|
|
{
|
2011-09-23 06:17:12 -07:00
|
|
|
Eina_Bool r;
|
|
|
|
|
2011-05-09 03:32:56 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
assert(_eina_threads_activated);
|
2011-05-09 07:41:39 -07:00
|
|
|
assert(cond->lock != NULL);
|
2011-09-23 06:17:12 -07:00
|
|
|
|
|
|
|
pthread_mutex_lock(&_eina_tracking_lock);
|
|
|
|
_eina_tracking = eina_inlist_remove(_eina_tracking,
|
|
|
|
EINA_INLIST_GET(cond->lock));
|
|
|
|
pthread_mutex_unlock(&_eina_tracking_lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
r = pthread_cond_wait(&(cond->condition),
|
|
|
|
&(cond->lock->mutex)) == 0 ? EINA_TRUE : EINA_FALSE;
|
|
|
|
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
pthread_mutex_lock(&_eina_tracking_lock);
|
|
|
|
_eina_tracking = eina_inlist_append(_eina_tracking,
|
|
|
|
EINA_INLIST_GET(cond->lock));
|
|
|
|
pthread_mutex_unlock(&_eina_tracking_lock);
|
2011-05-09 03:32:56 -07:00
|
|
|
#endif
|
|
|
|
|
2011-09-23 06:17:12 -07:00
|
|
|
return r;
|
2011-05-30 08:24:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Bool
|
|
|
|
eina_condition_timedwait(Eina_Condition *cond, double t)
|
|
|
|
{
|
|
|
|
struct timespec tv;
|
2011-09-23 06:17:12 -07:00
|
|
|
Eina_Bool r;
|
2011-05-30 08:24:06 -07:00
|
|
|
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
assert(_eina_threads_activated);
|
|
|
|
assert(cond->lock != NULL);
|
2011-09-23 06:17:12 -07:00
|
|
|
|
|
|
|
pthread_mutex_lock(&_eina_tracking_lock);
|
|
|
|
_eina_tracking = eina_inlist_remove(_eina_tracking,
|
|
|
|
EINA_INLIST_GET(cond->lock));
|
|
|
|
pthread_mutex_unlock(&_eina_tracking_lock);
|
2011-05-30 08:24:06 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
tv.tv_sec = t;
|
|
|
|
tv.tv_nsec = (t - (double) tv.tv_sec) * 1000000000;
|
|
|
|
|
2011-09-23 06:17:12 -07:00
|
|
|
r = pthread_cond_timedwait(&(cond->condition),
|
|
|
|
&(cond->lock->mutex),
|
|
|
|
&tv) == 0 ?
|
2011-05-30 08:24:06 -07:00
|
|
|
EINA_TRUE : EINA_FALSE;
|
2011-09-23 06:17:12 -07:00
|
|
|
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
pthread_mutex_lock(&_eina_tracking_lock);
|
|
|
|
_eina_tracking = eina_inlist_append(_eina_tracking,
|
|
|
|
EINA_INLIST_GET(cond->lock));
|
|
|
|
pthread_mutex_unlock(&_eina_tracking_lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return r;
|
2011-05-09 03:32:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Bool
|
|
|
|
eina_condition_broadcast(Eina_Condition *cond)
|
|
|
|
{
|
2011-05-09 07:41:39 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
assert(cond->lock != NULL);
|
|
|
|
#endif
|
|
|
|
|
2011-05-09 03:32:56 -07:00
|
|
|
return pthread_cond_broadcast(&(cond->condition)) == 0 ? EINA_TRUE : EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Bool
|
|
|
|
eina_condition_signal(Eina_Condition *cond)
|
|
|
|
{
|
2011-05-09 07:41:39 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
assert(cond->lock != NULL);
|
|
|
|
#endif
|
|
|
|
|
2011-05-09 03:32:56 -07:00
|
|
|
return pthread_cond_signal(&(cond->condition)) == 0 ? EINA_TRUE : EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-06-23 02:58:54 -07:00
|
|
|
static inline Eina_Bool
|
|
|
|
eina_rwlock_new(Eina_RWLock *mutex)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-09-07 03:16:27 -07:00
|
|
|
if (!_eina_threads_activated)
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
2011-06-23 02:58:54 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (pthread_rwlock_init(&(mutex->mutex), NULL) != 0)
|
|
|
|
return EINA_FALSE;
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
eina_rwlock_free(Eina_RWLock *mutex)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-09-07 03:16:27 -07:00
|
|
|
if (!_eina_threads_activated)
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
2011-06-23 02:58:54 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
pthread_rwlock_destroy(&(mutex->mutex));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Lock_Result
|
|
|
|
eina_rwlock_take_read(Eina_RWLock *mutex)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_ON_OFF_THREADS
|
|
|
|
if (!_eina_threads_activated)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
|
|
|
#endif
|
|
|
|
return EINA_LOCK_SUCCEED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (pthread_rwlock_rdlock(&(mutex->mutex)) != 0)
|
|
|
|
return EINA_LOCK_FAIL;
|
|
|
|
return EINA_LOCK_SUCCEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Lock_Result
|
|
|
|
eina_rwlock_take_write(Eina_RWLock *mutex)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_ON_OFF_THREADS
|
|
|
|
if (!_eina_threads_activated)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
|
|
|
#endif
|
|
|
|
return EINA_LOCK_SUCCEED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (pthread_rwlock_wrlock(&(mutex->mutex)) != 0)
|
|
|
|
return EINA_LOCK_FAIL;
|
|
|
|
return EINA_LOCK_SUCCEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Lock_Result
|
|
|
|
eina_rwlock_release(Eina_RWLock *mutex)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_ON_OFF_THREADS
|
|
|
|
if (!_eina_threads_activated)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
|
|
|
assert(pthread_equal(_eina_main_loop, pthread_self()));
|
|
|
|
#endif
|
|
|
|
return EINA_LOCK_SUCCEED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (pthread_rwlock_unlock(&(mutex->mutex)) != 0)
|
|
|
|
return EINA_LOCK_FAIL;
|
|
|
|
return EINA_LOCK_SUCCEED;
|
|
|
|
}
|
2011-05-09 03:32:56 -07:00
|
|
|
|
2011-10-16 22:06:01 -07:00
|
|
|
static inline Eina_Bool
|
|
|
|
eina_tls_new(Eina_TLS *key)
|
|
|
|
{
|
|
|
|
if (pthread_key_create(key, NULL) != 0)
|
|
|
|
return EINA_FALSE;
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
eina_tls_free(Eina_TLS key)
|
|
|
|
{
|
|
|
|
pthread_key_delete(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *
|
|
|
|
eina_tls_get(Eina_TLS key)
|
|
|
|
{
|
|
|
|
return pthread_getspecific(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Bool
|
|
|
|
eina_tls_set(Eina_TLS key, const void *data)
|
|
|
|
{
|
|
|
|
if (pthread_setspecific(key, data) != 0)
|
|
|
|
return EINA_FALSE;
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-12-30 05:38:53 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-16 20:55:35 -07:00
|
|
|
#ifdef EINA_HAVE_PTHREAD_BARRIER
|
|
|
|
typedef struct _Eina_Barrier Eina_Barrier;
|
|
|
|
|
|
|
|
struct _Eina_Barrier
|
|
|
|
{
|
|
|
|
pthread_barrier_t barrier;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline Eina_Bool
|
|
|
|
eina_barrier_new(Eina_Barrier *barrier, int needed)
|
|
|
|
{
|
|
|
|
if (!pthread_barrier_init(&(barrier->barrier), NULL, needed))
|
|
|
|
return EINA_TRUE;
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
eina_barrier_free(Eina_Barrier *barrier)
|
|
|
|
{
|
|
|
|
pthread_barrier_destroy(&(barrier->barrier));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Bool
|
|
|
|
eina_barrier_wait(Eina_Barrier *barrier)
|
|
|
|
{
|
|
|
|
pthread_barrier_wait(&(barrier->barrier));
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include "eina_inline_lock_barrier.x"
|
|
|
|
#endif
|
|
|
|
|
2013-10-10 01:22:28 -07:00
|
|
|
static inline Eina_Bool
|
|
|
|
eina_spinlock_new(Eina_Spinlock *spinlock)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_POSIX_SPINLOCK
|
|
|
|
return pthread_spin_init(spinlock, PTHREAD_PROCESS_PRIVATE) == 0 ? EINA_TRUE : EINA_FALSE;
|
|
|
|
#else
|
|
|
|
return eina_lock_new(spinlock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Lock_Result
|
|
|
|
eina_spinlock_take(Eina_Spinlock *spinlock)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_POSIX_SPINLOCK
|
|
|
|
int t;
|
|
|
|
|
|
|
|
do {
|
|
|
|
t = pthread_spin_trylock(spinlock);
|
|
|
|
if (t != 0)
|
|
|
|
{
|
2013-10-11 01:28:09 -07:00
|
|
|
if (errno == EBUSY) sched_yield();
|
|
|
|
else if (errno == EDEADLK) return EINA_LOCK_DEADLOCK;
|
2013-10-10 01:22:28 -07:00
|
|
|
}
|
2013-10-11 01:28:09 -07:00
|
|
|
} while (t != 0);
|
2013-10-10 01:22:28 -07:00
|
|
|
|
Revert "eina: fix CID 1106340: Logically dead code (DEADCODE) reported by coverity."
This reverts commit 1c1b48ca64c4d13cfd3677cde1ee99e4cf6c2c15.
this commit is causing all sorts of hell and crashes, hangs and what
not. like:
Thread 1 (Thread 0x7feb7389e780 (LWP 7149)):
No symbol table info available.
No symbol table info available.
at lib/eina/eina_inlist.c:422
l = 0xf38a50
__FUNCTION__ = "eina_inlist_demote"
#3 0x00007feb6a809867 in
_eina_chained_mempool_alloc_in (
pool=pool@entry=0x8650a0, p=p@entry=0xf38a50)
at modules/eina/mp/chained_pool/eina_chained_mempool.c:197
mem = 0xf3a690
#4 0x00007feb6a809dff in eina_chained_mempool_malloc
(data=0x8650a0,
size=<optimized out>)
at modules/eina/mp/chained_pool/eina_chained_mempool.c:300
pool = 0x8650a0
p = 0xf38a50
mem = <optimized out>
#5 0x00007feb6a7e451f in
eina_mempool_malloc (size=40, mp=<optimized out>)
at lib/eina/eina_inline_mempool.x:103
No locals.
#6 _eina_list_mempool_list_new (list=0x7feb5402a510)
at lib/eina/eina_list.c:199
No locals.
#7 eina_list_append (list=0x7feb5402a510, data=0xf41720)
at lib/eina/eina_list.c:534
l = <optimized out>
__FUNCTION__ = "eina_list_append"
#8 0x0000000000445d1a in
e_bindings_signal_add (ctxt=<optimized out>,
sig=0x947e6c "mouse,clicked,[12]", src=0x92248c "e.event.close",
mod=E_BINDING_MODIFIER_NONE, any_mod=1, action=0x93479c
"window_close",
params=0x0) at e_bindings.c:1036
binding = 0xf41720
#9 0x00000000004469d4 in e_bindings_init () at
e_bindings.c:68
ebs = 0x947df0
ebm = <optimized out>
ebw = <optimized out>
ebe = <optimized out>
ebk = <optimized out>
eba = <optimized out>
l = 0x9532e0
#10
0x0000000000437516 in main (argc=<optimized out>, argv=<optimized out>)
at e_main.c:941
safe_mode = 0 '\000'
after_restart = 1 '\001'
waslocked = 0 '\000'
t = <optimized out>
tstart = <optimized out>
s = <optimized out>
buff =
"1381800872.3", '\000' <repeats 19 times>
action = {__sigaction_handler = {
sa_handler = 0x4fca00 <e_sigabrt_act>,
sa_sigaction = 0x4fca00
<e_sigabrt_act>}, sa_mask = {__val = {
0 <repeats 16 times>}}, sa_flags = -1073741820,
sa_restorer = 0x0}
__FUNCTION__ = "main"
Detaching from program:
/usr/local/bin/enlightenment, process 7149
2013-10-14 18:35:10 -07:00
|
|
|
return t ? EINA_LOCK_FAIL : EINA_LOCK_SUCCEED;
|
2013-10-10 01:22:28 -07:00
|
|
|
#else
|
|
|
|
return eina_lock_take(spinlock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Lock_Result
|
|
|
|
eina_spinlock_take_try(Eina_Spinlock *spinlock)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_POSIX_SPINLOCK
|
|
|
|
int t;
|
|
|
|
|
|
|
|
t = pthread_spin_trylock(spinlock);
|
|
|
|
return t ? EINA_LOCK_FAIL : EINA_LOCK_SUCCEED;
|
|
|
|
#else
|
|
|
|
return eina_lock_take_try(spinlock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Lock_Result
|
|
|
|
eina_spinlock_release(Eina_Spinlock *spinlock)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_POSIX_SPINLOCK
|
|
|
|
return pthread_spin_unlock(spinlock) ? EINA_LOCK_FAIL : EINA_LOCK_SUCCEED;
|
|
|
|
#else
|
|
|
|
return eina_lock_release(spinlock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
eina_spinlock_free(Eina_Spinlock *spinlock)
|
|
|
|
{
|
|
|
|
#ifdef EINA_HAVE_POSIX_SPINLOCK
|
|
|
|
pthread_spin_destroy(spinlock);
|
|
|
|
#else
|
|
|
|
eina_lock_free(spinlock);
|
|
|
|
#endif
|
|
|
|
}
|
2012-10-16 20:55:35 -07:00
|
|
|
|
2011-04-22 14:26:36 -07:00
|
|
|
#endif
|