]> mj.ucw.cz Git - libucw.git/commitdiff
Use gettid() on Linux.
authorMartin Mares <mj@ucw.cz>
Mon, 18 Dec 2006 11:29:30 +0000 (12:29 +0100)
committerMartin Mares <mj@ucw.cz>
Mon, 18 Dec 2006 11:29:30 +0000 (12:29 +0100)
This is a hack for two reasons:

  o  Current glibc releases (up to 2.5) have no wrapper for gettid(),
     so we have to use _syscall0 to access it.
  o  There is no guarantee that pthreads will correspond to kernel
     threads. However, in the two existing implementations of pthreads
     on Linux, they correspond either to kernel processes or kernel
     threads, so assumptions that the (pid, tid) pair is unique are
     correct.

On the other hand, using real thread ID's has many advantages,
so I am keeping this patch at least in the dev-sorter branch to see
if it's safe or not.

lib/threads.c

index cd1db74df31922d76777019963f9dd31cf1cc088..ce55c0e6d2ec8702ba9a2309f943dbe0c91fbcd7 100644 (file)
 
 #include <pthread.h>
 
+#ifdef CONFIG_LINUX
+#include <sys/types.h>
+#include <linux/unistd.h>
+#ifdef __NR_gettid
+static _syscall0(pid_t, gettid)
+#define CONFIG_USE_GETTID
+#endif
+#endif
+
 static pthread_key_t ucwlib_context_key;
 static pthread_mutex_t ucwlib_master_mutex;
 
@@ -34,10 +43,18 @@ ucwlib_threads_init(void)
 static int
 ucwlib_tid(void)
 {
-  static tid_counter;
+  static int tid_counter;
+  int tid;
+
+#ifdef CONFIG_USE_GETTID
+  tid = gettid();
+  if (tid > 0)
+    return tid;
+  /* The syscall might be unimplemented */
+#endif
 
   ucwlib_lock();
-  int tid = ++tid_counter;
+  tid = ++tid_counter;
   ucwlib_unlock();
   return tid;
 }