2003-07-13 22:17:52 -04:00
|
|
|
/* threads.c: Thread Abstraction Functions
|
|
|
|
*
|
2014-12-05 04:31:08 -05:00
|
|
|
* Copyright (C) 2014 Michael Smith <msmith@icecast.org>,
|
|
|
|
* Brendan Cully <brendan@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>,
|
|
|
|
* Jack Moffitt <jack@icecast.org>,
|
2019-04-28 05:40:47 -04:00
|
|
|
* Ed "oddsock" Zaleski <oddsock@xiph.org>,
|
|
|
|
* Copyright (C) 2015-2019 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
|
2003-07-13 22:17:52 -04:00
|
|
|
*
|
2014-12-05 04:31:08 -05:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2003-07-13 22:17:52 -04:00
|
|
|
*
|
2014-12-05 04:31:08 -05:00
|
|
|
* 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2003-07-13 22:17:52 -04:00
|
|
|
*
|
|
|
|
*/
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-08 11:05:38 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2002-03-05 18:59:38 -05:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
2001-10-20 01:35:30 -04:00
|
|
|
#include <sys/time.h>
|
2002-02-06 20:04:09 -05:00
|
|
|
#else
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winbase.h>
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
2003-03-09 17:56:46 -05:00
|
|
|
#include <thread/thread.h>
|
|
|
|
#include <avl/avl.h>
|
2002-08-16 10:23:17 -04:00
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-09 17:56:46 -05:00
|
|
|
#include <log/log.h>
|
2002-08-16 10:23:17 -04:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-02-06 20:04:09 -05:00
|
|
|
#ifdef _WIN32
|
2002-03-05 18:59:38 -05:00
|
|
|
#define __FUNCTION__ __FILE__
|
2002-02-06 20:04:09 -05:00
|
|
|
#endif
|
|
|
|
|
2002-08-16 10:23:17 -04:00
|
|
|
#ifdef THREAD_DEBUG
|
2001-09-09 22:24:39 -04:00
|
|
|
#define CATMODULE "thread"
|
2002-08-03 04:14:56 -04:00
|
|
|
#define LOG_ERROR(y) log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y)
|
|
|
|
#define LOG_ERROR3(y, z1, z2, z3) log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y, z1, z2, z3)
|
|
|
|
#define LOG_ERROR7(y, z1, z2, z3, z4, z5, z6, z7) log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5, z6, z7)
|
2001-10-20 01:35:30 -04:00
|
|
|
|
2002-08-03 04:14:56 -04:00
|
|
|
#define LOG_WARN(y) log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y)
|
|
|
|
#define LOG_WARN3(y, z1, z2, z3) log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y, z1, z2, z3)
|
|
|
|
#define LOG_WARN5(y, z1, z2, z3, z4, z5) log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
|
|
|
|
#define LOG_WARN7(y, z1, z2, z3, z4, z5, z6, z7) log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5, z6, z7)
|
2001-10-20 01:35:30 -04:00
|
|
|
|
2002-08-03 04:14:56 -04:00
|
|
|
#define LOG_INFO(y) log_write(_logid, 3, CATMODULE "/", __FUNCTION__, y)
|
|
|
|
#define LOG_INFO4(y, z1, z2, z3, z4) log_write(_logid, 3, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4)
|
|
|
|
#define LOG_INFO5(y, z1, z2, z3, z4, z5) log_write(_logid, 3, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
|
2001-10-20 01:35:30 -04:00
|
|
|
|
2002-08-03 04:14:56 -04:00
|
|
|
#define LOG_DEBUG(y) log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y)
|
|
|
|
#define LOG_DEBUG2(y, z1, z2) log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y, z1, z2)
|
|
|
|
#define LOG_DEBUG5(y, z1, z2, z3, z4, z5) log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
|
2002-08-16 10:23:17 -04:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* thread starting structure */
|
|
|
|
typedef struct thread_start_tag {
|
2003-03-14 21:10:19 -05:00
|
|
|
/* the real start routine and arg */
|
|
|
|
void *(*start_routine)(void *);
|
|
|
|
void *arg;
|
|
|
|
|
|
|
|
/* the other stuff we need to make sure this thread is inserted into
|
|
|
|
** the thread tree
|
|
|
|
*/
|
|
|
|
thread_type *thread;
|
|
|
|
pthread_t sys_thread;
|
2001-09-09 22:24:39 -04:00
|
|
|
} thread_start_t;
|
|
|
|
|
|
|
|
static long _next_thread_id = 0;
|
|
|
|
static int _initialized = 0;
|
|
|
|
static avl_tree *_threadtree = NULL;
|
2003-03-04 10:31:34 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG_MUTEXES
|
2016-03-16 06:11:18 -04:00
|
|
|
static mutex_t _threadtree_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
|
2003-01-17 04:01:04 -05:00
|
|
|
PTHREAD_MUTEX_INITIALIZER};
|
2003-03-04 10:31:34 -05:00
|
|
|
#else
|
|
|
|
static mutex_t _threadtree_mutex = { PTHREAD_MUTEX_INITIALIZER };
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-04 10:31:34 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG_MUTEXES
|
|
|
|
static int _logid = -1;
|
2001-09-09 22:24:39 -04:00
|
|
|
static long _next_mutex_id = 0;
|
2003-03-04 10:31:34 -05:00
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
static avl_tree *_mutextree = NULL;
|
2003-01-17 04:01:04 -05:00
|
|
|
static mutex_t _mutextree_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
|
|
|
|
PTHREAD_MUTEX_INITIALIZER};
|
2003-03-04 10:31:34 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG_MUTEXES
|
2003-01-17 04:01:04 -05:00
|
|
|
static mutex_t _library_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
|
|
|
|
PTHREAD_MUTEX_INITIALIZER};
|
2003-03-04 10:31:34 -05:00
|
|
|
#else
|
|
|
|
static mutex_t _library_mutex = { PTHREAD_MUTEX_INITIALIZER };
|
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* INTERNAL FUNCTIONS */
|
|
|
|
|
|
|
|
/* avl tree functions */
|
2003-03-04 10:31:34 -05:00
|
|
|
#ifdef DEBUG_MUTEXES
|
2001-09-09 22:24:39 -04:00
|
|
|
static int _compare_mutexes(void *compare_arg, void *a, void *b);
|
|
|
|
static int _free_mutex(void *key);
|
2003-03-04 10:31:34 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int _compare_threads(void *compare_arg, void *a, void *b);
|
2001-09-09 22:24:39 -04:00
|
|
|
static int _free_thread(void *key);
|
|
|
|
|
|
|
|
/* mutex fuctions */
|
|
|
|
static void _mutex_create(mutex_t *mutex);
|
|
|
|
static void _mutex_lock(mutex_t *mutex);
|
|
|
|
static void _mutex_unlock(mutex_t *mutex);
|
|
|
|
|
|
|
|
/* misc thread stuff */
|
|
|
|
static void *_start_routine(void *arg);
|
|
|
|
static void _catch_signals(void);
|
|
|
|
static void _block_signals(void);
|
|
|
|
|
|
|
|
/* LIBRARY INITIALIZATION */
|
|
|
|
|
|
|
|
void thread_initialize(void)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_type *thread;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* set up logging */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-14 21:10:19 -05:00
|
|
|
log_initialize();
|
|
|
|
_logid = log_open("thread.log");
|
|
|
|
log_set_level(_logid, THREAD_DEBUG);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
2003-03-04 10:31:34 -05:00
|
|
|
#ifdef DEBUG_MUTEXES
|
2003-03-14 21:10:19 -05:00
|
|
|
/* create all the internal mutexes, and initialize the mutex tree */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutextree = avl_tree_new(_compare_mutexes, NULL);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* we have to create this one by hand, because there's no
|
2016-03-16 06:11:18 -04:00
|
|
|
** mutextree_mutex to lock yet!
|
2003-03-14 21:10:19 -05:00
|
|
|
*/
|
|
|
|
_mutex_create(&_mutextree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutextree_mutex.mutex_id = _next_mutex_id++;
|
|
|
|
avl_insert(_mutextree, (void *)&_mutextree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_create(&_threadtree_mutex);
|
2016-03-16 06:11:18 -04:00
|
|
|
thread_mutex_create(&_library_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* initialize the thread tree and insert the main thread */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_threadtree = avl_tree_new(_compare_threads, NULL);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
thread = (thread_type *)malloc(sizeof(thread_type));
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
thread->thread_id = _next_thread_id++;
|
|
|
|
thread->line = 0;
|
|
|
|
thread->file = strdup("main.c");
|
|
|
|
thread->sys_thread = pthread_self();
|
|
|
|
thread->create_time = time(NULL);
|
|
|
|
thread->name = strdup("Main Thread");
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_insert(_threadtree, (void *)thread);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_catch_signals();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_initialized = 1;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_shutdown(void)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
if (_initialized == 1) {
|
|
|
|
thread_mutex_destroy(&_library_mutex);
|
|
|
|
thread_mutex_destroy(&_threadtree_mutex);
|
2003-03-04 10:31:34 -05:00
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_mutex_destroy(&_mutextree_mutex);
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree_free(_mutextree, _free_mutex);
|
2003-03-04 10:31:34 -05:00
|
|
|
#endif
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree_free(_threadtree, _free_thread);
|
2009-01-08 22:18:03 -05:00
|
|
|
_threadtree = NULL;
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-14 21:10:19 -05:00
|
|
|
log_close(_logid);
|
|
|
|
log_shutdown();
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Signals should be handled by the main thread, nowhere else.
|
|
|
|
* I'm using POSIX signal interface here, until someone tells me
|
|
|
|
* that I should use signal/sigset instead
|
|
|
|
*
|
|
|
|
* This function only valid for non-Win32
|
|
|
|
*/
|
|
|
|
static void _block_signals(void)
|
|
|
|
{
|
2016-03-18 04:51:27 -04:00
|
|
|
#if !defined(_WIN32) && !defined(__ANDROID__)
|
2001-09-09 22:24:39 -04:00
|
|
|
sigset_t ss;
|
|
|
|
|
|
|
|
sigfillset(&ss);
|
|
|
|
|
|
|
|
/* These ones we want */
|
|
|
|
sigdelset(&ss, SIGKILL);
|
|
|
|
sigdelset(&ss, SIGSTOP);
|
|
|
|
sigdelset(&ss, SIGSEGV);
|
2009-01-08 22:18:03 -05:00
|
|
|
sigdelset(&ss, SIGCHLD);
|
2001-09-09 22:24:39 -04:00
|
|
|
sigdelset(&ss, SIGBUS);
|
2002-08-16 10:23:17 -04:00
|
|
|
if (pthread_sigmask(SIG_BLOCK, &ss, NULL) != 0) {
|
|
|
|
#ifdef THREAD_DEBUG
|
2001-09-09 22:24:39 -04:00
|
|
|
LOG_ERROR("Pthread_sigmask() failed for blocking signals");
|
2002-08-16 10:23:17 -04:00
|
|
|
#endif
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Let the calling thread catch all the relevant signals
|
|
|
|
*
|
|
|
|
* This function only valid for non-Win32
|
|
|
|
*/
|
|
|
|
static void _catch_signals(void)
|
|
|
|
{
|
2016-03-18 04:51:27 -04:00
|
|
|
#if !defined(_WIN32) && !defined(__ANDROID__)
|
2001-09-09 22:24:39 -04:00
|
|
|
sigset_t ss;
|
|
|
|
|
|
|
|
sigemptyset(&ss);
|
|
|
|
|
|
|
|
/* These ones should only be accepted by the signal handling thread (main thread) */
|
|
|
|
sigaddset(&ss, SIGHUP);
|
|
|
|
sigaddset(&ss, SIGCHLD);
|
|
|
|
sigaddset(&ss, SIGINT);
|
|
|
|
sigaddset(&ss, SIGPIPE);
|
2004-01-26 21:16:25 -05:00
|
|
|
sigaddset(&ss, SIGTERM);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-08-16 10:23:17 -04:00
|
|
|
if (pthread_sigmask(SIG_UNBLOCK, &ss, NULL) != 0) {
|
|
|
|
#ifdef THREAD_DEBUG
|
2001-09-09 22:24:39 -04:00
|
|
|
LOG_ERROR("pthread_sigmask() failed for catching signals!");
|
2002-08-16 10:23:17 -04:00
|
|
|
#endif
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-20 12:12:05 -04:00
|
|
|
thread_type *thread_create_c(const char *name, void *(*start_routine)(void *),
|
|
|
|
void *arg, int detached, int line, const char *file)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-07-27 14:19:31 -04:00
|
|
|
thread_type *thread = NULL;
|
|
|
|
thread_start_t *start = NULL;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
2016-03-16 06:11:18 -04:00
|
|
|
thread = (thread_type *)calloc(1, sizeof(thread_type));
|
2003-07-27 14:19:31 -04:00
|
|
|
do {
|
|
|
|
if (thread == NULL)
|
|
|
|
break;
|
|
|
|
start = (thread_start_t *)calloc(1, sizeof(thread_start_t));
|
|
|
|
if (start == NULL)
|
|
|
|
break;
|
|
|
|
if (pthread_attr_init (&attr) < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
thread->line = line;
|
|
|
|
thread->file = strdup(file);
|
|
|
|
|
2016-03-16 06:11:18 -04:00
|
|
|
_mutex_lock (&_threadtree_mutex);
|
2003-07-27 14:19:31 -04:00
|
|
|
thread->thread_id = _next_thread_id++;
|
|
|
|
_mutex_unlock (&_threadtree_mutex);
|
|
|
|
|
|
|
|
thread->name = strdup(name);
|
|
|
|
thread->create_time = time(NULL);
|
|
|
|
|
|
|
|
start->start_routine = start_routine;
|
|
|
|
start->arg = arg;
|
|
|
|
start->thread = thread;
|
|
|
|
|
2005-09-15 18:33:13 -04:00
|
|
|
pthread_attr_setstacksize (&attr, 512*1024);
|
2016-03-18 04:51:27 -04:00
|
|
|
|
|
|
|
#ifndef __ANDROID__
|
2003-07-27 14:19:31 -04:00
|
|
|
pthread_attr_setinheritsched (&attr, PTHREAD_INHERIT_SCHED);
|
2016-03-18 04:51:27 -04:00
|
|
|
#endif
|
|
|
|
|
2003-07-27 14:19:31 -04:00
|
|
|
if (detached)
|
|
|
|
{
|
|
|
|
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
thread->detached = 1;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-07-27 14:19:31 -04:00
|
|
|
if (pthread_create (&thread->sys_thread, &attr, _start_routine, start) == 0)
|
|
|
|
{
|
|
|
|
pthread_attr_destroy (&attr);
|
2022-03-20 12:12:21 -04:00
|
|
|
#ifdef HAVE_PTHREAD_SETNAME_NP
|
|
|
|
if (pthread_setname_np(thread->sys_thread, name) != 0) {
|
|
|
|
/* If the call above did not work it was likely due to name limit restrictions.
|
|
|
|
* In that case we retry with a fixed name so we do not inherit our parent's name.
|
|
|
|
*/
|
|
|
|
pthread_setname_np(thread->sys_thread, "Worker");
|
|
|
|
}
|
|
|
|
#endif
|
2003-07-27 14:19:31 -04:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pthread_attr_destroy (&attr);
|
|
|
|
}
|
|
|
|
while (0);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-08-16 10:23:17 -04:00
|
|
|
#ifdef THREAD_DEBUG
|
2003-07-27 14:19:31 -04:00
|
|
|
LOG_ERROR("Could not create new thread %s", name);
|
2002-08-16 10:23:17 -04:00
|
|
|
#endif
|
2003-07-27 14:19:31 -04:00
|
|
|
if (start) free (start);
|
|
|
|
if (thread) free (thread);
|
|
|
|
return NULL;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* _mutex_create
|
2016-03-16 06:11:18 -04:00
|
|
|
**
|
2001-09-09 22:24:39 -04:00
|
|
|
** creates a mutex
|
|
|
|
*/
|
|
|
|
static void _mutex_create(mutex_t *mutex)
|
|
|
|
{
|
2003-03-04 10:31:34 -05:00
|
|
|
#ifdef DEBUG_MUTEXES
|
2003-03-14 21:10:19 -05:00
|
|
|
mutex->thread_id = MUTEX_STATE_NEVERLOCKED;
|
|
|
|
mutex->line = -1;
|
2003-03-04 10:31:34 -05:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_init(&mutex->sys_mutex, NULL);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_mutex_create_c(mutex_t *mutex, int line, char *file)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_create(mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_MUTEXES
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(&_mutextree_mutex);
|
|
|
|
mutex->mutex_id = _next_mutex_id++;
|
|
|
|
avl_insert(_mutextree, (void *)mutex);
|
|
|
|
_mutex_unlock(&_mutextree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_mutex_destroy (mutex_t *mutex)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_destroy(&mutex->sys_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_MUTEXES
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(&_mutextree_mutex);
|
|
|
|
avl_delete(_mutextree, mutex, _free_mutex);
|
|
|
|
_mutex_unlock(&_mutextree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_mutex_lock_c(mutex_t *mutex, int line, char *file)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MUTEXES
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_type *th = thread_self();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (!th) LOG_WARN("No mt record for %u in lock [%s:%d]", thread_self(), file, line);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
LOG_DEBUG5("Locking %p (%s) on line %d in file %s by thread %d", mutex, mutex->name, line, file, th ? th->thread_id : -1);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
# ifdef CHECK_MUTEXES
|
2003-03-14 21:10:19 -05:00
|
|
|
/* Just a little sanity checking to make sure that we're locking
|
|
|
|
** mutexes correctly
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (th) {
|
|
|
|
int locks = 0;
|
|
|
|
avl_node *node;
|
|
|
|
mutex_t *tmutex;
|
|
|
|
|
|
|
|
_mutex_lock(&_mutextree_mutex);
|
|
|
|
|
|
|
|
node = avl_get_first (_mutextree);
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
while (node) {
|
|
|
|
tmutex = (mutex_t *)node->key;
|
|
|
|
|
|
|
|
if (tmutex->mutex_id == mutex->mutex_id) {
|
2016-03-16 06:11:18 -04:00
|
|
|
if (tmutex->thread_id == th->thread_id) {
|
2003-03-14 21:10:19 -05:00
|
|
|
/* Deadlock, same thread can't lock the same mutex twice */
|
2016-03-16 06:11:18 -04:00
|
|
|
LOG_ERROR7("DEADLOCK AVOIDED (%d == %d) on mutex [%s] in file %s line %d by thread %d [%s]",
|
2003-03-14 21:10:19 -05:00
|
|
|
tmutex->thread_id, th->thread_id, mutex->name ? mutex->name : "undefined", file, line, th->thread_id, th->name);
|
|
|
|
|
|
|
|
_mutex_unlock(&_mutextree_mutex);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-16 06:11:18 -04:00
|
|
|
} else if (tmutex->thread_id == th->thread_id) {
|
2003-03-14 21:10:19 -05:00
|
|
|
/* Mutex locked by this thread (not this mutex) */
|
|
|
|
locks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = avl_get_next(node);
|
|
|
|
}
|
|
|
|
|
2016-03-16 06:11:18 -04:00
|
|
|
if (locks > 0) {
|
2003-03-14 21:10:19 -05:00
|
|
|
/* Has already got a mutex locked */
|
|
|
|
if (_multi_mutex.thread_id != th->thread_id) {
|
|
|
|
/* Tries to lock two mutexes, but has not got the double mutex, norty boy! */
|
|
|
|
LOG_WARN("(%d != %d) Thread %d [%s] tries to lock a second mutex [%s] in file %s line %d, without locking double mutex!",
|
|
|
|
_multi_mutex.thread_id, th->thread_id, th->thread_id, th->name, mutex->name ? mutex->name : "undefined", file, line);
|
|
|
|
}
|
|
|
|
}
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_unlock(&_mutextree_mutex);
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
# endif /* CHECK_MUTEXES */
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(mutex);
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(&_mutextree_mutex);
|
|
|
|
|
|
|
|
LOG_DEBUG2("Locked %p by thread %d", mutex, th ? th->thread_id : -1);
|
|
|
|
mutex->line = line;
|
|
|
|
if (th) {
|
|
|
|
mutex->thread_id = th->thread_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
_mutex_unlock(&_mutextree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif /* DEBUG_MUTEXES */
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_mutex_unlock_c(mutex_t *mutex, int line, char *file)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MUTEXES
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_type *th = thread_self();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (!th) {
|
|
|
|
LOG_ERROR3("No record for %u in unlock [%s:%d]", thread_self(), file, line);
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
LOG_DEBUG5("Unlocking %p (%s) on line %d in file %s by thread %d", mutex, mutex->name, line, file, th ? th->thread_id : -1);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
mutex->line = line;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
# ifdef CHECK_MUTEXES
|
2003-03-14 21:10:19 -05:00
|
|
|
if (th) {
|
|
|
|
int locks = 0;
|
|
|
|
avl_node *node;
|
|
|
|
mutex_t *tmutex;
|
|
|
|
|
|
|
|
_mutex_lock(&_mutextree_mutex);
|
|
|
|
|
|
|
|
while (node) {
|
|
|
|
tmutex = (mutex_t *)node->key;
|
|
|
|
|
|
|
|
if (tmutex->mutex_id == mutex->mutex_id) {
|
|
|
|
if (tmutex->thread_id != th->thread_id) {
|
2016-03-16 06:11:18 -04:00
|
|
|
LOG_ERROR7("ILLEGAL UNLOCK (%d != %d) on mutex [%s] in file %s line %d by thread %d [%s]", tmutex->thread_id, th->thread_id,
|
2003-03-14 21:10:19 -05:00
|
|
|
mutex->name ? mutex->name : "undefined", file, line, th->thread_id, th->name);
|
|
|
|
_mutex_unlock(&_mutextree_mutex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (tmutex->thread_id == th->thread_id) {
|
|
|
|
locks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = avl_get_next (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((locks > 0) && (_multi_mutex.thread_id != th->thread_id)) {
|
|
|
|
/* Don't have double mutex, has more than this mutex left */
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
LOG_WARN("(%d != %d) Thread %d [%s] tries to unlock a mutex [%s] in file %s line %d, without owning double mutex!",
|
|
|
|
_multi_mutex.thread_id, th->thread_id, th->thread_id, th->name, mutex->name ? mutex->name : "undefined", file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
_mutex_unlock(&_mutextree_mutex);
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
# endif /* CHECK_MUTEXES */
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_unlock(mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(&_mutextree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
LOG_DEBUG2("Unlocked %p by thread %d", mutex, th ? th->thread_id : -1);
|
|
|
|
mutex->line = -1;
|
|
|
|
if (mutex->thread_id == th->thread_id) {
|
|
|
|
mutex->thread_id = MUTEX_STATE_NOTLOCKED;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_unlock(&_mutextree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_unlock(mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif /* DEBUG_MUTEXES */
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_cond_create_c(cond_t *cond, int line, char *file)
|
|
|
|
{
|
2024-02-09 05:08:53 -05:00
|
|
|
pthread_condattr_t attr;
|
|
|
|
pthread_condattr_init(&attr);
|
|
|
|
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
|
|
|
|
pthread_cond_init(&cond->sys_cond, &attr);
|
|
|
|
pthread_condattr_destroy(&attr);
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_init(&cond->cond_mutex, NULL);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_cond_destroy(cond_t *cond)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_destroy(&cond->cond_mutex);
|
|
|
|
pthread_cond_destroy(&cond->sys_cond);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_cond_signal_c(cond_t *cond, int line, char *file)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_cond_signal(&cond->sys_cond);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_cond_broadcast_c(cond_t *cond, int line, char *file)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_cond_broadcast(&cond->sys_cond);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2002-08-05 10:48:04 -04:00
|
|
|
void thread_cond_timedwait_c(cond_t *cond, int millis, int line, char *file)
|
|
|
|
{
|
|
|
|
struct timespec time;
|
|
|
|
|
2024-02-09 05:08:53 -05:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &time);
|
|
|
|
|
|
|
|
time.tv_nsec += millis*1000000;
|
|
|
|
|
|
|
|
if (time.tv_nsec >= 1000000000) {
|
|
|
|
unsigned long int extra = time.tv_nsec / 1000000000;
|
|
|
|
time.tv_nsec -= extra * 1000000000;
|
|
|
|
time.tv_sec += extra;
|
|
|
|
}
|
2002-08-05 10:48:04 -04:00
|
|
|
|
|
|
|
pthread_mutex_lock(&cond->cond_mutex);
|
|
|
|
pthread_cond_timedwait(&cond->sys_cond, &cond->cond_mutex, &time);
|
|
|
|
pthread_mutex_unlock(&cond->cond_mutex);
|
|
|
|
}
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
void thread_cond_wait_c(cond_t *cond, int line, char *file)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_lock(&cond->cond_mutex);
|
|
|
|
pthread_cond_wait(&cond->sys_cond, &cond->cond_mutex);
|
|
|
|
pthread_mutex_unlock(&cond->cond_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_rwlock_create_c(rwlock_t *rwlock, int line, char *file)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_rwlock_init(&rwlock->sys_rwlock, NULL);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_rwlock_destroy(rwlock_t *rwlock)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_rwlock_destroy(&rwlock->sys_rwlock);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_rwlock_rlock_c(rwlock_t *rwlock, int line, char *file)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_rwlock_rdlock(&rwlock->sys_rwlock);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_rwlock_wlock_c(rwlock_t *rwlock, int line, char *file)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_rwlock_wrlock(&rwlock->sys_rwlock);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_rwlock_unlock_c(rwlock_t *rwlock, int line, char *file)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_rwlock_unlock(&rwlock->sys_rwlock);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2003-07-27 14:19:31 -04:00
|
|
|
void thread_exit_c(long val, int line, char *file)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_type *th = thread_self();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#if defined(DEBUG_MUTEXES) && defined(CHECK_MUTEXES)
|
2003-03-14 21:10:19 -05:00
|
|
|
if (th) {
|
|
|
|
avl_node *node;
|
|
|
|
mutex_t *tmutex;
|
|
|
|
char name[40];
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(&_mutextree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
while (node) {
|
|
|
|
tmutex = (mutex_t *)node->key;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (tmutex->thread_id == th->thread_id) {
|
2016-03-16 06:11:18 -04:00
|
|
|
LOG_WARN("Thread %d [%s] exiting in file %s line %d, without unlocking mutex [%s]",
|
2003-03-14 21:10:19 -05:00
|
|
|
th->thread_id, th->name, file, line, mutex_to_string(tmutex, name));
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
node = avl_get_next (node);
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_unlock(&_mutextree_mutex);
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-07-27 14:19:31 -04:00
|
|
|
if (th && th->detached)
|
|
|
|
{
|
2002-08-16 10:23:17 -04:00
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-14 21:10:19 -05:00
|
|
|
LOG_INFO4("Removing thread %d [%s] started at [%s:%d], reason: 'Thread Exited'", th->thread_id, th->name, th->file, th->line);
|
2002-08-16 10:23:17 -04:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(&_threadtree_mutex);
|
2003-07-27 14:19:31 -04:00
|
|
|
avl_delete(_threadtree, th, _free_thread);
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_unlock(&_threadtree_mutex);
|
|
|
|
}
|
2009-01-08 22:18:03 -05:00
|
|
|
|
2003-07-27 14:19:31 -04:00
|
|
|
pthread_exit ((void*)val);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sleep for a number of microseconds */
|
|
|
|
void thread_sleep(unsigned long len)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2003-03-14 21:10:19 -05:00
|
|
|
Sleep(len / 1000);
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
|
|
|
# ifdef HAVE_NANOSLEEP
|
2003-03-14 21:10:19 -05:00
|
|
|
struct timespec time_sleep;
|
|
|
|
struct timespec time_remaining;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
time_sleep.tv_sec = len / 1000000;
|
|
|
|
time_sleep.tv_nsec = (len % 1000000) * 1000;
|
|
|
|
|
|
|
|
ret = nanosleep(&time_sleep, &time_remaining);
|
|
|
|
while (ret != 0 && errno == EINTR) {
|
|
|
|
time_sleep.tv_sec = time_remaining.tv_sec;
|
|
|
|
time_sleep.tv_nsec = time_remaining.tv_nsec;
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
ret = nanosleep(&time_sleep, &time_remaining);
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
# else
|
2003-03-14 21:10:19 -05:00
|
|
|
struct timeval tv;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
tv.tv_sec = len / 1000000;
|
|
|
|
tv.tv_usec = (len % 1000000);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
select(0, NULL, NULL, NULL, &tv);
|
2001-09-09 22:24:39 -04:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *_start_routine(void *arg)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_start_t *start = (thread_start_t *)arg;
|
|
|
|
void *(*start_routine)(void *) = start->start_routine;
|
|
|
|
void *real_arg = start->arg;
|
|
|
|
thread_type *thread = start->thread;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_block_signals();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* insert thread into thread tree here */
|
|
|
|
_mutex_lock(&_threadtree_mutex);
|
|
|
|
thread->sys_thread = pthread_self();
|
|
|
|
avl_insert(_threadtree, (void *)thread);
|
|
|
|
_mutex_unlock(&_threadtree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-08-16 10:23:17 -04:00
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-14 21:10:19 -05:00
|
|
|
LOG_INFO4("Added thread %d [%s] started at [%s:%d]", thread->thread_id, thread->name, thread->file, thread->line);
|
2002-08-16 10:23:17 -04:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2016-03-18 04:51:27 -04:00
|
|
|
#ifndef __ANDROID__
|
2003-07-27 14:19:31 -04:00
|
|
|
pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
|
2016-03-18 04:51:27 -04:00
|
|
|
#endif
|
|
|
|
|
2003-07-27 14:19:31 -04:00
|
|
|
free (start);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
(start_routine)(real_arg);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-07-27 14:19:31 -04:00
|
|
|
if (thread->detached)
|
|
|
|
{
|
|
|
|
_mutex_lock (&_threadtree_mutex);
|
|
|
|
avl_delete (_threadtree, thread, _free_thread);
|
|
|
|
_mutex_unlock (&_threadtree_mutex);
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return NULL;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2002-12-29 04:55:50 -05:00
|
|
|
thread_type *thread_self(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_node *node;
|
|
|
|
thread_type *th;
|
|
|
|
pthread_t sys_thread = pthread_self();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(&_threadtree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (_threadtree == NULL) {
|
2002-08-16 10:23:17 -04:00
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-14 21:10:19 -05:00
|
|
|
LOG_WARN("Thread tree is empty, this must be wrong!");
|
2002-08-16 10:23:17 -04:00
|
|
|
#endif
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_unlock(&_threadtree_mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
node = avl_get_first(_threadtree);
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
while (node) {
|
|
|
|
th = (thread_type *)node->key;
|
|
|
|
|
|
|
|
if (th && pthread_equal(sys_thread, th->sys_thread)) {
|
|
|
|
_mutex_unlock(&_threadtree_mutex);
|
|
|
|
return th;
|
|
|
|
}
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
node = avl_get_next(node);
|
|
|
|
}
|
|
|
|
_mutex_unlock(&_threadtree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
|
2002-08-16 10:23:17 -04:00
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-14 21:10:19 -05:00
|
|
|
LOG_ERROR("Nonexistant thread alive...");
|
2002-08-16 10:23:17 -04:00
|
|
|
#endif
|
2016-03-16 06:11:18 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return NULL;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_rename(const char *name)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_type *th;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
th = thread_self();
|
|
|
|
if (th->name) free(th->name);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
th->name = strdup(name);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 06:11:18 -04:00
|
|
|
static void _mutex_lock(mutex_t *mutex)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_lock(&mutex->sys_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _mutex_unlock(mutex_t *mutex)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_unlock(&mutex->sys_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void thread_library_lock(void)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_lock(&_library_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_library_unlock(void)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
_mutex_unlock(&_library_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2002-12-29 04:55:50 -05:00
|
|
|
void thread_join(thread_type *thread)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
void *ret;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-06-20 02:37:10 -04:00
|
|
|
pthread_join(thread->sys_thread, &ret);
|
2002-11-22 08:00:44 -05:00
|
|
|
_mutex_lock(&_threadtree_mutex);
|
|
|
|
avl_delete(_threadtree, thread, _free_thread);
|
|
|
|
_mutex_unlock(&_threadtree_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* AVL tree functions */
|
|
|
|
|
2003-03-04 10:31:34 -05:00
|
|
|
#ifdef DEBUG_MUTEXES
|
2001-09-09 22:24:39 -04:00
|
|
|
static int _compare_mutexes(void *compare_arg, void *a, void *b)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
mutex_t *m1, *m2;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
m1 = (mutex_t *)a;
|
|
|
|
m2 = (mutex_t *)b;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (m1->mutex_id > m2->mutex_id)
|
|
|
|
return 1;
|
|
|
|
if (m1->mutex_id < m2->mutex_id)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
2003-03-04 10:31:34 -05:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
static int _compare_threads(void *compare_arg, void *a, void *b)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_type *t1, *t2;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
t1 = (thread_type *)a;
|
|
|
|
t2 = (thread_type *)b;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (t1->thread_id > t2->thread_id)
|
|
|
|
return 1;
|
|
|
|
if (t1->thread_id < t2->thread_id)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2003-03-04 10:31:34 -05:00
|
|
|
#ifdef DEBUG_MUTEXES
|
2001-09-09 22:24:39 -04:00
|
|
|
static int _free_mutex(void *key)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
mutex_t *m;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
m = (mutex_t *)key;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (m && m->file) {
|
|
|
|
free(m->file);
|
|
|
|
m->file = NULL;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* all mutexes are static. don't need to free them */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return 1;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
2003-03-04 10:31:34 -05:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
static int _free_thread(void *key)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
thread_type *t;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
t = (thread_type *)key;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (t->file)
|
|
|
|
free(t->file);
|
|
|
|
if (t->name)
|
|
|
|
free(t->name);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
free(t);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return 1;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-08 22:18:03 -05:00
|
|
|
#ifdef HAVE_PTHREAD_SPIN_LOCK
|
|
|
|
void thread_spin_create (spin_t *spin)
|
|
|
|
{
|
|
|
|
int x = pthread_spin_init (&spin->lock, PTHREAD_PROCESS_PRIVATE);
|
|
|
|
if (x)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_spin_destroy (spin_t *spin)
|
|
|
|
{
|
|
|
|
pthread_spin_destroy (&spin->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_spin_lock (spin_t *spin)
|
|
|
|
{
|
|
|
|
int x = pthread_spin_lock (&spin->lock);
|
|
|
|
if (x != 0)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_spin_unlock (spin_t *spin)
|
|
|
|
{
|
|
|
|
pthread_spin_unlock (&spin->lock);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|