📄 thread.c,v
字号:
Sleep(len / 1000);d598 14a611 14 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; ret = nanosleep(&time_sleep, &time_remaining); }d613 1a613 1 struct timeval tv;d615 2a616 2 tv.tv_sec = len / 1000000; tv.tv_usec = (len % 1000000);d618 1a618 1 select(0, NULL, NULL, NULL, &tv);d625 4a628 4 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;d631 1a631 1 _block_signals();d633 1a633 1 free(start);d635 5a639 5 /* insert thread into thread tree here */ _mutex_lock(&_threadtree_mutex); thread->sys_thread = pthread_self(); avl_insert(_threadtree, (void *)thread); _mutex_unlock(&_threadtree_mutex);d642 1a642 1 LOG_INFO4("Added thread %d [%s] started at [%s:%d]", thread->thread_id, thread->name, thread->file, thread->line);d645 2a646 2 if (detach) { pthread_detach(thread->sys_thread);d648 2a649 2 } pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);d651 4a654 4 /* call the real start_routine and start the thread ** this should never exit! */ (start_routine)(real_arg);d657 1a657 1 LOG_WARN("Thread x should never exit from here!!!");d660 1a660 1 return NULL;d665 3a667 3 avl_node *node; thread_type *th; pthread_t sys_thread = pthread_self();d669 1a669 1 _mutex_lock(&_threadtree_mutex);d671 1a671 1 if (_threadtree == NULL) {d673 1a673 1 LOG_WARN("Thread tree is empty, this must be wrong!");d675 17a691 17 _mutex_unlock(&_threadtree_mutex); return NULL; } node = avl_get_first(_threadtree); while (node) { th = (thread_type *)node->key; if (th && pthread_equal(sys_thread, th->sys_thread)) { _mutex_unlock(&_threadtree_mutex); return th; } node = avl_get_next(node); } _mutex_unlock(&_threadtree_mutex);d695 1a695 1 LOG_ERROR("Nonexistant thread alive...");d697 2a698 2 return NULL;d703 1a703 1 thread_type *th;d705 2a706 2 th = thread_self(); if (th->name) free(th->name);d708 1a708 1 th->name = strdup(name);d713 1a713 1 pthread_mutex_lock(&mutex->sys_mutex);d718 1a718 1 pthread_mutex_unlock(&mutex->sys_mutex);d724 1a724 1 _mutex_lock(&_library_mutex);d729 1a729 1 _mutex_unlock(&_library_mutex);d734 2a735 2 void *ret; int i;d737 1a737 1 i = pthread_join(thread->sys_thread, &ret);d748 1a748 1 mutex_t *m1, *m2;d750 2a751 2 m1 = (mutex_t *)a; m2 = (mutex_t *)b;d753 5a757 5 if (m1->mutex_id > m2->mutex_id) return 1; if (m1->mutex_id < m2->mutex_id) return -1; return 0;d763 1a763 1 thread_type *t1, *t2;d765 2a766 2 t1 = (thread_type *)a; t2 = (thread_type *)b;d768 5a772 5 if (t1->thread_id > t2->thread_id) return 1; if (t1->thread_id < t2->thread_id) return -1; return 0;d778 1a778 1 mutex_t *m;d780 1a780 1 m = (mutex_t *)key;d782 4a785 4 if (m && m->file) { free(m->file); m->file = NULL; }d787 1a787 1 /* all mutexes are static. don't need to free them */d789 1a789 1 return 1;d795 1a795 1 thread_type *t;d797 1a797 1 t = (thread_type *)key;d799 4a802 4 if (t->file) free(t->file); if (t->name) free(t->name);d804 1a804 1 free(t);d806 1a806 1 return 1;@1.22log@reduce include file namespace clutter for libshout and the associatedsmaller libs.@text@a740 1 _free_thread(thread);@1.21log@include the automake config.h file if the application defines one@text@d45 2a46 2#include "thread.h"#include "avl.h"d48 1a48 1#include "log.h"@1.20log@Make various thread structures omit the bits only used in debug mode.Some of these are pretty heavily used, so saving 10-20 bytes each can bequite significant.No functional differences.@text@d21 4@1.19log@Fix some warnings, fix cflags.@text@a86 1static int _logid = -1;d90 2d94 4d99 3d103 1d107 3d112 3d119 1d121 3a124 1static int _free_mutex(void *key);d152 1a161 1#ifdef DEBUG_MUTEXESd194 1d198 1d313 1d316 1a521 2static int rwlocknum = 0;d742 1d756 1d772 1d788 1@1.18log@Rename thread_t to avoid problems on OS X@text@d91 2a92 1static mutex_t _threadtree_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1 };d96 4a99 2static mutex_t _mutextree_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1 };static mutex_t _library_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1 };@1.17log@Lots of bugfixes contributed by Karl Heyes.@text@d83 1a83 1 thread_t *thread;d121 1a121 1 thread_t *thread;d152 1a152 1 thread = (thread_t *)malloc(sizeof(thread_t));d241 2a242 1thread_t *thread_create_c(char *name, void *(*start_routine)(void *), void *arg, int detached, int line, char *file)d245 1a245 1 thread_t *thread;d248 1a248 1 thread = (thread_t *)malloc(sizeof(thread_t)); d322 1a322 1 thread_t *th = thread_self();d394 1a394 1 thread_t *th = thread_self();d526 1a526 1 thread_t *th = thread_self();d601 1a601 1 thread_t *thread = start->thread;d636 1a636 1thread_t *thread_self(void)d639 1a639 1 thread_t *th;d655 1a655 1 th = (thread_t *)node->key;d676 1a676 1 thread_t *th;d705 1a705 1void thread_join(thread_t *thread)d735 1a735 1 thread_t *t1, *t2;d737 2a738 2 t1 = (thread_t *)a; t2 = (thread_t *)b;d765 1a765 1 thread_t *t;d767 1a767 1 t = (thread_t *)key;d781 1a781 1 thread_t *t = key;@1.16log@Bugfix: thread_join is often called after a thread has already exited, which itdoes using thread_exit(). thread_exit() was freeing the thread structure, sothread_join was using freed memory. Rearrange things so that if the threadis detached, the freeing happens in thread_join instead.@text@d710 3@1.15log@Liberally sprinkle #ifdef THREAD_DEBUG around so libshout doesn't need to linkwith it.@text@d105 1d258 1d556 1a556 1 avl_delete(_threadtree, th, _free_thread);d619 1d710 1d775 7@1.14log@Timing fixes@text@a40 1#include "log.h"d43 3d51 1d69 1d124 1a125 2#ifdef THREAD_DEBUGd180 1a182 1 log_shutdown();d205 2a206 1 if (pthread_sigmask(SIG_BLOCK, &ss, NULL) != 0)d209 2d231 2a232 1 if (pthread_sigmask(SIG_UNBLOCK, &ss, NULL) != 0)d235 2d266 1d269 1d272 1d274 1d549 1d551 1d611 1d613 1d625 1d627 1d641 1d643 1d663 1d665 1@1.13log@Various cleanups@text@d571 1a571 1 tv.tv_usec = (len % 1000000) / 1000;@1.12log@oddsock's xslt stats support, slightly cleaned up@text@d231 1a231 1long thread_create_c(char *name, void *(*start_routine)(void *), void *arg, int detached, int line, char *file)d262 1a262 1 return -1;d265 1a265 2// return thread->thread_id; return thread->sys_thread;d678 1a678 1void thread_join(long thread)d683 1a683 1 i = pthread_join(thread, &ret);@1.11log@Cleaned up version of Ciaran Anscomb's relaying patch.@text@a117 5 /* this must be called to init pthreads-win32 */#ifdef _WIN32 ptw32_processInitialize();#endifd127 1a127 1 /* create all the interal mutexes, and initialize the mutex tree */@1.10log@Lots of patches committable now that my sound card works properly again.logging API changed slightly (I got sick of gcc warnings about deprecatedfeatures).resampling (for live input, not yet for reencoding) is in there.several patches from Karl Heyes have been incorporated.@text@d468 12d486 2@1.9log@Don't use start after freeing it in thread startup code.@text@d50 16a65 16#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)#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)#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)#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)a257 1@1.8log@win32 patches from Ed@text@d577 1d591 1a591 1 if (start->detached) {@1.7log@More win32 fixes.@text@d28 2d36 1a38 2#include <pthread.h>d46 1a46 1#define __FUNCTION__ __FILE__ ":" __LINE__@1.6log@minor build fixes for win32 courtesy of Oddsock@text@d117 5@1.5log@Revert the stacksize work. It's stupid.The original patch from Ben Laurie some years ago was needed becauseFreeBSD's default stack size was < 8k and this wasn't acceptable.Both Linux and Solaris had reasonable defaults for stacksize, or grew thestack as needed to a reasonable size.Testing today and consulting documentation shows that the default stacksizes on FreeBSD, Linux, and Solaris are all acceptable. Linux can growto 2MB, 32bit Solaris defaults to 1MB, 64bit Solaris defaults to 2MB, andFreeBSD defaults to 64k.In my opinion FreeBSD needs to get with the program and provide areasonable default. 64k is enough for us, but might not be for others.@text@d31 3d43 4@1.4log@Stack size per thread needs to be configurable. Setting it on a globalbases is not enough. ices and icecast need this to be different, andif one is interested in tuning memory usage, one will want to alter thisper thread.@text@d223 1a223 1long thread_create_c(char *name, void *(*start_routine)(void *), void *arg, int stacksize, int detached, int line, char *file)a224 1 pthread_attr_t attr;a245 2 pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, stacksize);d248 1a248 1 if (pthread_create(&thread->sys_thread, &attr, _start_routine, start) == 0)a251 2 pthread_attr_destroy(&attr); @1.3log@Win32 fixes. Specifically a header change and not using the gcc extensionsfor vararg macros. It's not as pretty, but it works.@text@a58 3/* INTERNAL DATA */#define STACKSIZE 8192d223 1a223 1long thread_create_c(char *name, void *(*start_routine)(void *), void *arg, int detached, int line, char *file)d248 1a248 1 pthread_attr_setstacksize(&attr, STACKSIZE);@1.2log@Oddsock found this bug when working with icecast2 on freebsd. Nanosecondswere off by a few orders of magnitude.@text@a26 1#include <sys/time.h>d30 1d42 16a57 4#define LOG_ERROR(y, z...) log_write(_logid, 1, CATMODULE "/" __FUNCTION__, y, ##z)#define LOG_WARN(y, z...) log_write(_logid, 2, CATMODULE "/" __FUNCTION__, y, ##z)#define LOG_INFO(y, z...) log_write(_logid, 3, CATMODULE "/" __FUNCTION__, y, ##z)#define LOG_DEBUG(y, z...) log_write(_logid, 4, CATMODULE "/" __FUNCTION__, y, ##z)d312 1a312 1 LOG_DEBUG("Locking %p (%s) on line %d in file %s by thread %d", mutex, mutex->name, line, file, th ? th->thread_id : -1);d334 1a334 1 LOG_ERROR("DEADLOCK AVOIDED (%d == %d) on mutex [%s] in file %s line %d by thread %d [%s]", d365 1a365 1 LOG_DEBUG("Locked %p by thread %d", mutex, th ? th->thread_id : -1);d383 1a383 1 LOG_ERROR("No record for %u in unlock [%s:%d]", thread_self(), file, line);d386 1a386 1 LOG_DEBUG("Unlocking %p (%s) on line %d in file %s by thread %d", mutex, mutex->name, line, file, th ? th->thread_id : -1);d403 1a403 1 LOG_ERROR("ILLEGAL UNLOCK (%d != %d) on mutex [%s] in file %s line %d by thread %d [%s]", tmutex->thread_id, th->thread_id, d430 1a430 1 LOG_DEBUG("Unlocked %p by thread %d", mutex, th ? th->thread_id : -1);d524 1a524 1 LOG_INFO("Removing thread %d [%s] started at [%s:%d], reason: 'Thread Exited'", th->thread_id, th->name, th->file, th->line);d583 1a583 1 LOG_INFO("Added thread %d [%s] started at [%s:%d]", thread->thread_id, thread->name, thread->file, thread->line);@1.1log@Initial revision@text@d534 1a534 1 time_sleep.tv_nsec = len % 1000000;@1.1.1.1log@move to cvs@text@@
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -