[xiph-commits] r6887 - icecast/branches/thread-icecast-kh

karl at dactyl.lonelymoon.com karl
Sat Jun 26 09:44:02 PDT 2004


Author: karl
Date: Sat Jun 26 09:44:02 2004
New Revision: 6887

Modified:
icecast/branches/thread-icecast-kh/thread.c
icecast/branches/thread-icecast-kh/thread.h
Log:
resync work, debug log updates


Modified: icecast/branches/thread-icecast-kh/thread.c
===================================================================
--- icecast/branches/thread-icecast-kh/thread.c	2004-06-26 16:37:24 UTC (rev 6886)
+++ icecast/branches/thread-icecast-kh/thread.c	2004-06-26 16:44:01 UTC (rev 6887)
@@ -54,6 +54,7 @@
#ifdef THREAD_DEBUG
#define CATMODULE "thread"
#define LOG_ERROR(y) log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y)
+#define LOG_ERROR1(y, z1) log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y, z1)
#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)

@@ -68,6 +69,8 @@

#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_DEBUG3(y, z1, z2, z3) log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y, z1, z2, z3)
+#define LOG_DEBUG4(y, z1, z2, z3, z4) log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4)
#define LOG_DEBUG5(y, z1, z2, z3, z4, z5) log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
#endif

@@ -89,55 +92,45 @@
static avl_tree *_threadtree = NULL;


-#ifdef DEBUG_MUTEXES
+#ifdef THREAD_DEBUG

+/* this is x86 specific, but gets a very precise and low overhead
+ * timer, other platforms may have similar mechanisms
+ */
#define rdtscll(val) \
__asm__ __volatile__("rdtsc" : "=A" (val))

-static inline unsigned long long get_cycles (void)
+static inline unsigned long long get_count (void)
{
-        unsigned long long ret;
+    unsigned long long ret;

-        rdtscll(ret);
-        return ret;
+    rdtscll(ret);
+    return ret;
}
-#endif

-#if 1
-#ifdef DEBUG_MUTEXES
-static mutex_t _threadtree_mutex = { -1, "unset", MUTEX_STATE_UNINIT, (unsigned long long)0, NULL, -1,
-    PTHREAD_MUTEX_INITIALIZER};
-#else
-static mutex_t _threadtree_mutex = { PTHREAD_MUTEX_INITIALIZER };
-#endif
-#endif
-
-
-#ifdef DEBUG_MUTEXES
static int _logid = -1;
static long _next_mutex_id = 0;
-#endif
-
-#if 0
static avl_tree *_mutextree = NULL;
-static mutex_t _mutextree_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
-    PTHREAD_MUTEX_INITIALIZER};

-#ifdef DEBUG_MUTEXES
-static mutex_t _library_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
-    PTHREAD_MUTEX_INITIALIZER};
+static mutex_t _threadtree_mutex = { -1, "unset", MUTEX_STATE_UNINIT,
+    (unsigned long long)0, NULL, -1, PTHREAD_MUTEX_INITIALIZER};
+static mutex_t _mutextree_mutex = { -1, "unset", MUTEX_STATE_UNINIT,
+    (unsigned long long)0, NULL, -1, PTHREAD_MUTEX_INITIALIZER};
+static mutex_t _library_mutex = { -1, NULL, MUTEX_STATE_UNINIT,
+    (unsigned long long)0, NULL, -1, PTHREAD_MUTEX_INITIALIZER};
+
+static int _compare_mutexes(void *compare_arg, void *a, void *b);
+static int _free_mutex(void *key);
+
#else
+
+static mutex_t _threadtree_mutex = { PTHREAD_MUTEX_INITIALIZER };
static mutex_t _library_mutex = { PTHREAD_MUTEX_INITIALIZER };
+
#endif
-#endif
+
/* INTERNAL FUNCTIONS */

-/* avl tree functions */
-#ifdef DEBUG_MUTEXES
-static int _compare_mutexes(void *compare_arg, void *a, void *b);
-static int _free_mutex(void *key);
-#endif
-
static int _compare_threads(void *compare_arg, void *a, void *b);
static int _free_thread(void *key);

@@ -160,27 +153,25 @@
/* set up logging */

#ifdef THREAD_DEBUG
-    log_initialize();
-    _logid = log_open("thread.log");
-    log_set_level(_logid, THREAD_DEBUG);
-#endif
-
-#ifdef DEBUG_MUTEXES
/* create all the internal mutexes, and initialize the mutex tree */

-    // _mutextree = avl_tree_new(_compare_mutexes, NULL);
+    _mutextree = avl_tree_new(_compare_mutexes, NULL);

/* we have to create this one by hand, because there's no
** mutextree_mutex to lock yet!
*/
-    // _mutex_create(&_mutextree_mutex);
+    _mutex_create(&_mutextree_mutex);

-    // _mutextree_mutex.mutex_id = _next_mutex_id++;
-    // avl_insert(_mutextree, (void *)&_mutextree_mutex);
+    _mutextree_mutex.mutex_id = _next_mutex_id++;
+    avl_insert(_mutextree, (void *)&_mutextree_mutex);
+
+    log_initialize();
+    _logid = log_open("thread.log");
+    log_set_level(_logid, 4);
#endif

-    // thread_mutex_create(&_threadtree_mutex);
-    // thread_mutex_create(&_library_mutex);
+    thread_mutex_create("threadtree", &_threadtree_mutex);
+    thread_mutex_create("thread lib", &_library_mutex);

/* initialize the thread tree and insert the main thread */

@@ -205,7 +196,7 @@
void thread_shutdown(void)
{
if (_initialized == 1) {
-        // thread_mutex_destroy(&_library_mutex);
+        thread_mutex_destroy(&_library_mutex);
thread_mutex_destroy(&_threadtree_mutex);
#ifdef THREAD_DEBUG
thread_mutex_destroy(&_mutextree_mutex);
@@ -317,6 +308,9 @@
thread->detached = 1;
}

+#ifdef __OpenBSD__
+        thread->running = 1;
+#endif
if (pthread_create (&thread->sys_thread, &attr, _start_routine, start) == 0)
{
pthread_attr_destroy (&attr);
@@ -328,7 +322,7 @@
while (0);

#ifdef THREAD_DEBUG
-    LOG_ERROR("Could not create new thread %s", name);
+    LOG_ERROR1("Could not create new thread %s", name);
#endif
if (start) free (start);
if (thread) free (thread);
@@ -341,7 +335,7 @@
*/
static void _mutex_create(mutex_t *mutex)
{
-#ifdef DEBUG_MUTEXES
+#ifdef THREAD_DEBUG
mutex->thread_id = MUTEX_STATE_NEVERLOCKED;
mutex->line = -1;
#endif
@@ -349,16 +343,18 @@
pthread_mutex_init(&mutex->sys_mutex, NULL);
}

-void thread_mutex_create_c(mutex_t *mutex, int line, char *name)
+void thread_mutex_create_c(const char *name, mutex_t *mutex, int line, const char *file)
{
_mutex_create(mutex);

-#ifdef DEBUG_MUTEXES
+#ifdef THREAD_DEBUG
mutex->name = strdup (name);
-    // _mutex_lock(&_mutextree_mutex);
+    _mutex_lock(&_mutextree_mutex);
mutex->mutex_id = _next_mutex_id++;
-    // avl_insert(_mutextree, (void *)mutex);
-    // _mutex_unlock(&_mutextree_mutex);
+    avl_insert(_mutextree, (void *)mutex);
+    _mutex_unlock(&_mutextree_mutex);
+
+    LOG_DEBUG3 ("mutex %s created (%s:%d)", mutex->name, file, line);
#endif
}

@@ -366,21 +362,20 @@
{
pthread_mutex_destroy(&mutex->sys_mutex);

-#ifdef DEBUG_MUTEXES
+#ifdef THREAD_DEBUG
free (mutex->file);
-    // _mutex_lock(&_mutextree_mutex);
-    // avl_delete(_mutextree, mutex, _free_mutex);
-    // _mutex_unlock(&_mutextree_mutex);
+    _mutex_lock(&_mutextree_mutex);
+    avl_delete(_mutextree, mutex, _free_mutex);
+    _mutex_unlock(&_mutextree_mutex);
#endif
}

void thread_mutex_lock_c(mutex_t *mutex, int line, char *file)
{
-#ifdef DEBUG_MUTEXES
-    mutex->lock_start = get_cycles();
+#ifdef THREAD_DEBUG
+    mutex->lock_start = get_count();
mutex->file = strdup (file);
mutex->line = line;
-#endif
#if 0
thread_type *th = thread_self();

@@ -446,7 +441,7 @@
}

_mutex_unlock(&_mutextree_mutex);
-#else
+#endif
_mutex_lock(mutex);
#endif /* DEBUG_MUTEXES */
}
@@ -511,11 +506,11 @@
}

_mutex_unlock(&_mutextree_mutex);
-#else
+#endif /* DEBUG_MUTEXES */
_mutex_unlock(mutex);
-#endif /* DEBUG_MUTEXES */
-#ifdef DEBUG_MUTEXES
-    printf ("lock %s, at %s:%d lasted %llu\n", mutex->name, mutex->file, mutex->line, get_cycles() - mutex->lock_start);
+#ifdef THREAD_DEBUG
+    LOG_DEBUG4 ("lock %s, at %s:%d lasted %llu", mutex->name, mutex->file,
+            mutex->line, get_count() - mutex->lock_start);
free (mutex->file);
mutex->file = NULL;
#endif
@@ -613,18 +608,22 @@
_mutex_unlock(&_mutextree_mutex);
}
#endif
-
+#ifdef __OpenBSD__
+    thread->running = 0;
+#endif
+
if (th && th->detached)
{
#ifdef THREAD_DEBUG
-        LOG_INFO4("Removing thread %d [%s] started at [%s:%d], reason: 'Thread Exited'", th->thread_id, th->name, th->file, th->line);
-#endif
+        LOG_DEBUG4("Removing thread %d [%s] started at [%s:%d]", th->thread_id,
+                th->name, th->file, th->line);

_mutex_lock(&_threadtree_mutex);
avl_delete(_threadtree, th, _free_thread);
_mutex_unlock(&_threadtree_mutex);
+#endif
}
-
+
pthread_exit ((void*)val);
}

@@ -676,7 +675,8 @@
_mutex_unlock(&_threadtree_mutex);

#ifdef THREAD_DEBUG
-    LOG_INFO4("Added thread %d [%s] started at [%s:%d]", thread->thread_id, thread->name, thread->file, thread->line);
+    LOG_DEBUG4("Added thread %d [%s] started at [%s:%d]", thread->thread_id,
+            thread->name, thread->file, thread->line);
#endif

pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
@@ -684,6 +684,10 @@

(start_routine)(real_arg);

+#ifdef __OpenBSD__
+    thread->running = 0;
+#endif
+
if (thread->detached)
{
_mutex_lock (&_threadtree_mutex);
@@ -755,12 +759,12 @@

void thread_library_lock(void)
{
-    // _mutex_lock(&_library_mutex);
+    _mutex_lock(&_library_mutex);
}

void thread_library_unlock(void)
{
-    // _mutex_unlock(&_library_mutex);
+    _mutex_unlock(&_library_mutex);
}

void thread_join(thread_type *thread)
@@ -768,6 +772,11 @@
void *ret;
int i;

+#ifdef __OpenBSD__
+    /* openbsd masks signals while waiting */
+    while (thread->running)
+        thread_sleep (200000);
+#endif
i = pthread_join(thread->sys_thread, &ret);
_mutex_lock(&_threadtree_mutex);
avl_delete(_threadtree, thread, _free_thread);
@@ -776,7 +785,7 @@

/* AVL tree functions */

-#ifdef DEBUG_MUTEXES
+#ifdef THREAD_DEBUG
static int _compare_mutexes(void *compare_arg, void *a, void *b)
{
mutex_t *m1, *m2;
@@ -806,7 +815,7 @@
return 0;
}

-#ifdef DEBUG_MUTEXES
+#ifdef THREAD_DEBUG
static int _free_mutex(void *key)
{
mutex_t *m;

Modified: icecast/branches/thread-icecast-kh/thread.h
===================================================================
--- icecast/branches/thread-icecast-kh/thread.h	2004-06-26 16:37:24 UTC (rev 6886)
+++ icecast/branches/thread-icecast-kh/thread.h	2004-06-26 16:44:01 UTC (rev 6887)
@@ -40,12 +40,16 @@
/* is the thread running detached? */
int detached;

+#ifdef __OpenBSD__
+    int running;
+#endif
+
/* the system specific thread */
pthread_t sys_thread;
} thread_type;

typedef struct {
-#ifdef DEBUG_MUTEXES
+#ifdef THREAD_DEBUG
/* the local id and name of the mutex */
long mutex_id;
char *name;
@@ -93,7 +97,7 @@
} rwlock_t;

#define thread_create(n,x,y,z) thread_create_c(n,x,y,z,__LINE__,__FILE__)
-#define thread_mutex_create(name,x) thread_mutex_create_c(x,__LINE__,name)
+#define thread_mutex_create(name,x) thread_mutex_create_c(name,x,__LINE__,__FILE__)
#define thread_mutex_lock(x) thread_mutex_lock_c(x,__LINE__,__FILE__)
#define thread_mutex_unlock(x) thread_mutex_unlock_c(x,__LINE__,__FILE__)
#define thread_cond_create(x) thread_cond_create_c(x,__LINE__,__FILE__)
@@ -150,7 +154,7 @@
/* creation, destruction, locking, unlocking, signalling and waiting */
thread_type *thread_create_c(char *name, void *(*start_routine)(void *),
void *arg, int detached, int line, char *file);
-void thread_mutex_create_c(mutex_t *mutex, int line, char *file);
+void thread_mutex_create_c(const char *name, mutex_t *mutex, int line, const char *file);
void thread_mutex_lock_c(mutex_t *mutex, int line, char *file);
void thread_mutex_unlock_c(mutex_t *mutex, int line, char *file);
void thread_mutex_destroy(mutex_t *mutex);



More information about the commits mailing list