char *thread_name(pthread_t tid) {char s[100]; thread_name_t *n; static int tid_count = 1000; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static thread_name_t *thread_names = NULL; if (pthread_equal(NULL_TID, tid)) tid = pthread_self(); pthread_mutex_lock(&lock); for (n = thread_names; n != NULL; n=n->next) {if (pthread_equal(n->tid, tid)) goto L; } n = (thread_name_t *) malloc(sizeof(thread_name_t)); n->tid = tid; #if defined(__alpha) && defined(__osf__) sprintf(s, "T@%d", pthread_getsequence_np(tid)); #else sprintf(s, "T@%d", tid_count); tid_count++; #endif n->name = (char *) malloc(strlen(s)+1); strcpy(n->name, s); n->next = thread_names; thread_names = n; L:pthread_mutex_unlock(&lock); return(n->name); }
static void pid_callback(int *const pid, int *const tid) { #if !_BT_LOG_MESSAGE_FORMAT_CONTAINS(PID, BT_LOG_MESSAGE_CTX_FORMAT) VAR_UNUSED(pid); #else #if defined(_WIN32) || defined(_WIN64) *pid = GetCurrentProcessId(); #else *pid = getpid(); #endif #endif #if !_BT_LOG_MESSAGE_FORMAT_CONTAINS(TID, BT_LOG_MESSAGE_CTX_FORMAT) VAR_UNUSED(tid); #else #if defined(_WIN32) || defined(_WIN64) *tid = GetCurrentThreadId(); #elif defined(__CYGWIN__) pthread_t thr = pthread_self(); *tid = (int)pthread_getsequence_np(&thr); #elif defined(__sun__) *tid = (int)pthread_self(); #elif defined(__ANDROID__) *tid = gettid(); #elif defined(__linux__) *tid = syscall(SYS_gettid); #elif defined(__MACH__) *tid = (int)pthread_mach_thread_np(pthread_self()); #else #define Platform not supported #endif #endif }
/* include pr_thread_id */ long pr_thread_id(pthread_t * ptr) { #if defined(sun) return ((ptr == NULL) ? pthread_self() : *ptr); /* Solaris */ #elif defined(__osf__) && defined(__alpha) pthread_t tid; tid = (ptr == NULL) ? pthread_self() : *ptr; /* Digital Unix */ return (pthread_getsequence_np(tid)); #else /* 4everything else */ return ((ptr == NULL) ? pthread_self() : *ptr); #endif }
/* returns thread index */ static unsigned int GetThreadIndex(void) { #if HAVE_PTHREAD_GETSEQUENCE_NP return pthread_getsequence_np(pthread_self()); #else unsigned int index; /* first, we init the keys if this is the first time */ pthread_once(&once_key, init_keys); index = (unsigned long)pthread_getspecific(thread_key); if (index == 0) { index = next_index++; pthread_setspecific(thread_key, (void *)(unsigned long)index); } return index; #endif }