2001-09-09 22:24:39 -04:00
|
|
|
/* thread.h
|
|
|
|
* - Thread Abstraction Function Headers
|
|
|
|
*
|
2018-10-12 06:28:09 -04:00
|
|
|
* Copyright (C) 2014 Michael Smith <msmith@icecast.org>,
|
|
|
|
* Brendan Cully <brendan@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>,
|
|
|
|
* Jack Moffitt <jack@icecast.org>,
|
|
|
|
* Copyright (C) 2012-2018 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
|
2001-09-09 22:24:39 -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.
|
2001-09-09 22:24:39 -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
|
|
|
*/
|
|
|
|
|
2018-10-12 06:16:06 -04:00
|
|
|
#ifndef _LIBIGLOO__THREAD_H_
|
|
|
|
#define _LIBIGLOO__THREAD_H_
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-11-02 07:29:00 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-11-02 07:20:04 -04:00
|
|
|
#include <igloo/config.h>
|
2018-11-01 09:23:50 -04:00
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2002-12-29 04:55:50 -05:00
|
|
|
/* renamed from thread_t due to conflict on OS X */
|
|
|
|
|
2003-03-05 14:52:10 -05:00
|
|
|
typedef struct {
|
2003-03-14 21:10:19 -05:00
|
|
|
/* the local id for the thread, and it's name */
|
|
|
|
long thread_id;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
/* the time the thread was created */
|
|
|
|
time_t create_time;
|
|
|
|
|
|
|
|
/* the file and line which created this thread */
|
|
|
|
char *file;
|
|
|
|
int line;
|
|
|
|
|
|
|
|
/* is the thread running detached? */
|
|
|
|
int detached;
|
|
|
|
|
|
|
|
/* the system specific thread */
|
|
|
|
pthread_t sys_thread;
|
2018-10-12 07:27:17 -04:00
|
|
|
} igloo_thread_type;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-05 14:52:10 -05:00
|
|
|
typedef struct {
|
2003-03-04 10:31:34 -05:00
|
|
|
#ifdef DEBUG_MUTEXES
|
2003-03-14 21:10:19 -05:00
|
|
|
/* the local id and name of the mutex */
|
|
|
|
long mutex_id;
|
|
|
|
char *name;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* the thread which is currently locking this mutex */
|
|
|
|
long thread_id;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* the file and line where the mutex was locked */
|
|
|
|
char *file;
|
|
|
|
int line;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-04 10:31:34 -05:00
|
|
|
#endif
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* the system specific mutex */
|
|
|
|
pthread_mutex_t sys_mutex;
|
2018-10-12 07:27:17 -04:00
|
|
|
} igloo_mutex_t;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-05 14:52:10 -05:00
|
|
|
typedef struct {
|
2003-03-04 10:31:34 -05:00
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-14 21:10:19 -05:00
|
|
|
long cond_id;
|
|
|
|
char *name;
|
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_t cond_mutex;
|
|
|
|
pthread_cond_t sys_cond;
|
2018-10-12 07:27:17 -04:00
|
|
|
} igloo_cond_t;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-05 14:52:10 -05:00
|
|
|
typedef struct {
|
2003-03-04 10:31:34 -05:00
|
|
|
#ifdef THREAD_DEBUG
|
2003-03-14 21:10:19 -05:00
|
|
|
long rwlock_id;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
/* information on which thread and where in the code
|
|
|
|
** this rwlock was write locked
|
|
|
|
*/
|
|
|
|
long thread_id;
|
|
|
|
char *file;
|
|
|
|
int line;
|
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_rwlock_t sys_rwlock;
|
2018-10-12 07:27:17 -04:00
|
|
|
} igloo_rwlock_t;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-11-01 09:23:50 -04:00
|
|
|
#ifdef IGLOO_CTC_HAVE_PTHREAD_SPIN_LOCK
|
2009-01-08 22:18:03 -05:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
pthread_spinlock_t lock;
|
2018-10-12 07:27:17 -04:00
|
|
|
} igloo_spin_t;
|
2009-01-08 22:18:03 -05:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
void igloo_thread_spin_create (igloo_spin_t *spin);
|
|
|
|
void igloo_thread_spin_destroy (igloo_spin_t *spin);
|
|
|
|
void igloo_thread_spin_lock (igloo_spin_t *spin);
|
|
|
|
void igloo_thread_spin_unlock (igloo_spin_t *spin);
|
2009-01-08 22:18:03 -05:00
|
|
|
#else
|
2018-10-12 07:27:17 -04:00
|
|
|
typedef igloo_mutex_t igloo_spin_t;
|
2018-10-12 06:55:24 -04:00
|
|
|
#define igloo_thread_spin_create(x) thread_mutex_create(x)
|
|
|
|
#define igloo_thread_spin_destroy(x) igloo_thread_mutex_destroy(x)
|
|
|
|
#define igloo_thread_spin_lock(x) thread_mutex_lock(x)
|
|
|
|
#define igloo_thread_spin_unlock(x) thread_mutex_unlock(x)
|
2009-01-08 22:18:03 -05:00
|
|
|
#endif
|
|
|
|
|
2018-10-31 12:30:23 -04:00
|
|
|
#define igloo_thread_create(n,x,y,z) igloo_thread_create_c(n,x,y,z,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_mutex_create(x) igloo_thread_mutex_create_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_mutex_lock(x) igloo_thread_mutex_lock_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_mutex_unlock(x) igloo_thread_mutex_unlock_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_cond_create(x) igloo_thread_cond_create_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_cond_signal(x) igloo_thread_cond_signal_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_cond_broadcast(x) igloo_thread_cond_broadcast_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_cond_wait(x) igloo_thread_cond_wait_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_cond_timedwait(x,t) igloo_thread_cond_wait_c(x,t,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_rwlock_create(x) igloo_thread_rwlock_create_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_rwlock_rlock(x) igloo_thread_rwlock_rlock_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_rwlock_wlock(x) igloo_thread_rwlock_wlock_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_rwlock_unlock(x) igloo_thread_rwlock_unlock_c(x,__LINE__,__FILE__)
|
|
|
|
#define igloo_thread_exit(x) igloo_thread_exit_c(x,__LINE__,__FILE__)
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
#define igloo_MUTEX_STATE_NOTLOCKED -1
|
|
|
|
#define igloo_MUTEX_STATE_NEVERLOCKED -2
|
|
|
|
#define igloo_MUTEX_STATE_UNINIT -3
|
|
|
|
#define igloo_THREAD_DETACHED 1
|
|
|
|
#define igloo_THREAD_ATTACHED 0
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* creation, destruction, locking, unlocking, signalling and waiting */
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_thread_type *igloo_thread_create_c(char *name, void *(*start_routine)(void *),
|
2002-12-29 04:55:50 -05:00
|
|
|
void *arg, int detached, int line, char *file);
|
2018-10-12 07:27:17 -04:00
|
|
|
void igloo_thread_mutex_create_c(igloo_mutex_t *mutex, int line, char *file);
|
|
|
|
void igloo_thread_mutex_lock_c(igloo_mutex_t *mutex, int line, char *file);
|
|
|
|
void igloo_thread_mutex_unlock_c(igloo_mutex_t *mutex, int line, char *file);
|
|
|
|
void igloo_thread_mutex_destroy(igloo_mutex_t *mutex);
|
|
|
|
void igloo_thread_cond_create_c(igloo_cond_t *cond, int line, char *file);
|
|
|
|
void igloo_thread_cond_signal_c(igloo_cond_t *cond, int line, char *file);
|
|
|
|
void igloo_thread_cond_broadcast_c(igloo_cond_t *cond, int line, char *file);
|
|
|
|
void igloo_thread_cond_wait_c(igloo_cond_t *cond, int line, char *file);
|
|
|
|
void igloo_thread_cond_timedwait_c(igloo_cond_t *cond, int millis, int line, char *file);
|
|
|
|
void igloo_thread_cond_destroy(igloo_cond_t *cond);
|
|
|
|
void igloo_thread_rwlock_create_c(igloo_rwlock_t *rwlock, int line, char *file);
|
|
|
|
void igloo_thread_rwlock_rlock_c(igloo_rwlock_t *rwlock, int line, char *file);
|
|
|
|
void igloo_thread_rwlock_wlock_c(igloo_rwlock_t *rwlock, int line, char *file);
|
|
|
|
void igloo_thread_rwlock_unlock_c(igloo_rwlock_t *rwlock, int line, char *file);
|
|
|
|
void igloo_thread_rwlock_destroy(igloo_rwlock_t *rwlock);
|
2018-10-12 06:55:24 -04:00
|
|
|
void igloo_thread_exit_c(long val, int line, char *file);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* sleeping */
|
2018-10-12 06:55:24 -04:00
|
|
|
void igloo_thread_sleep(unsigned long len);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* for using library functions which aren't threadsafe */
|
2018-10-12 06:55:24 -04:00
|
|
|
void igloo_thread_library_lock(void);
|
|
|
|
void igloo_thread_library_unlock(void);
|
2018-10-12 07:34:55 -04:00
|
|
|
#define IGLOO_PROTECT_CODE(code) { igloo_thread_library_lock(); code; igloo_thread_library_unlock(); }
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* thread information functions */
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_thread_type *igloo_thread_self(void);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* renames current thread */
|
2018-10-12 06:55:24 -04:00
|
|
|
void igloo_thread_rename(const char *name);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* waits until thread_exit is called for another thread */
|
2018-10-12 07:27:17 -04:00
|
|
|
void igloo_thread_join(igloo_thread_type *thread);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-11-02 07:29:00 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif /* __THREAD_H__ */
|