Mailing List archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vdr] Re: Threading issues on RH9



Hi,

I tried to unify the different patches (this time against
1.2.0pre1). The attached thing compiles and doesn't causes any
harm on a non-NPTL system.

-- 
Stefan Huelswitt
huels@iname.com  | http://home.pages.de/~nathan
diff -urN vdr-1.2.0pre1-orig/ringbuffer.c vdr-1.2.0pre1-pthread/ringbuffer.c
--- vdr-1.2.0pre1-orig/ringbuffer.c	2003-05-12 19:38:11.000000000 +0200
+++ vdr-1.2.0pre1-pthread/ringbuffer.c	2003-06-01 08:55:17.000000000 +0200
@@ -75,7 +75,7 @@
 {
   margin = Margin;
   buffer = NULL;
-  getThreadPid = -1;
+  getThreadTid = 0;
   if (Size > 1) { // 'Size - 1' must not be 0!
      buffer = MALLOC(uchar, Size);
      if (!buffer)
@@ -125,7 +125,7 @@
         int percent = maxFill * 100 / (Size() - 1) / 5 * 5;
         if (abs(lastPercent - percent) >= 5) {
            if (percent > 75)
-              dsyslog("buffer usage: %d%% (pid=%d)", percent, getThreadPid);
+              dsyslog("buffer usage: %d%% (pid=%ld)", percent, getThreadTid);
            lastPercent = percent;
            }
         }
@@ -159,8 +159,8 @@
 {
   uchar *p = NULL;
   Lock();
-  if (getThreadPid < 0)
-     getThreadPid = getpid();
+  if (getThreadTid <= 0)
+     getThreadTid = pthread_self();
   int rest = Size() - tail;
   if (rest < margin && head < tail) {
      int t = margin - rest;
diff -urN vdr-1.2.0pre1-orig/ringbuffer.h vdr-1.2.0pre1-pthread/ringbuffer.h
--- vdr-1.2.0pre1-orig/ringbuffer.h	2003-05-12 19:35:10.000000000 +0200
+++ vdr-1.2.0pre1-pthread/ringbuffer.h	2003-06-01 08:55:17.000000000 +0200
@@ -46,7 +46,7 @@
   int margin, head, tail;
   int lastGet;
   uchar *buffer;
-  pid_t getThreadPid;
+  pthread_t getThreadTid;
 public:
   cRingBufferLinear(int Size, int Margin = 0, bool Statistics = false);
     ///< Creates a linear ring buffer.
diff -urN vdr-1.2.0pre1-orig/thread.c vdr-1.2.0pre1-pthread/thread.c
--- vdr-1.2.0pre1-orig/thread.c	2003-05-18 14:45:13.000000000 +0200
+++ vdr-1.2.0pre1-pthread/thread.c	2003-06-01 09:34:34.000000000 +0200
@@ -30,7 +30,7 @@
 
 void cCondVar::Wait(cMutex &Mutex)
 {
-  if (Mutex.locked && Mutex.lockingPid == getpid()) {
+  if (Mutex.locked && pthread_equal(Mutex.lockingThread,pthread_self())) {
      int locked = Mutex.locked;
      Mutex.locked = 0; // have to clear the locked count here, as pthread_cond_wait
                        // does an implizit unlock of the mutex
@@ -43,7 +43,7 @@
 {
   bool r = true; // true = condition signaled false = timeout
 
-  if (Mutex.locked && Mutex.lockingPid == getpid()) {
+  if (Mutex.locked && pthread_equal(Mutex.lockingThread,pthread_self())) {
      struct timeval now;                   // unfortunately timedwait needs the absolute time, not the delta :-(
      if (gettimeofday(&now, NULL) == 0) {  // get current time
         now.tv_usec += TimeoutMs * 1000;   // add the timeout
@@ -61,7 +61,7 @@
         if (pthread_cond_timedwait(&cond, &Mutex.mutex, &abstime) == ETIMEDOUT)
            r = false;
         Mutex.locked = locked;
-        Mutex.lockingPid = getpid();
+        Mutex.lockingThread = pthread_self();
         }
      }
   return r;
@@ -83,7 +83,7 @@
 
 cMutex::cMutex(void)
 {
-  lockingPid = 0;
+  lockingThread = 0;
   locked = 0;
   pthread_mutex_init(&mutex, NULL);
 }
@@ -95,9 +95,9 @@
 
 void cMutex::Lock(void)
 {
-  if (getpid() != lockingPid || !locked) {
+  if (!pthread_equal(lockingThread,pthread_self()) || !locked) {
      pthread_mutex_lock(&mutex);
-     lockingPid = getpid();
+     lockingThread = pthread_self();
      }
   locked++;
 }
@@ -105,7 +105,7 @@
 void cMutex::Unlock(void)
 {
  if (!--locked) {
-    lockingPid = 0;
+    lockingThread = 0;
     pthread_mutex_unlock(&mutex);
     }
 }
@@ -125,7 +125,7 @@
      signalHandlerInstalled = true;
      }
   running = false;
-  parentPid = threadPid = 0;
+  parentThread = thread = 0;
 }
 
 cThread::~cThread()
@@ -139,8 +139,9 @@
 
 void *cThread::StartThread(cThread *Thread)
 {
-  Thread->threadPid = getpid();
+  Thread->thread = pthread_self();
   Thread->Action();
+  Thread->thread = 0;
   return NULL;
 }
 
@@ -148,7 +149,7 @@
 {
   if (!running) {
      running = true;
-     parentPid = getpid();
+     parentThread = pthread_self();
      pthread_create(&thread, NULL, (void *(*) (void *))&StartThread, (void *)this);
      pthread_setschedparam(thread, SCHED_RR, 0);
      usleep(10000); // otherwise calling Active() immediately after Start() causes a "pure virtual method called" error
@@ -158,12 +159,21 @@
 
 bool cThread::Active(void)
 {
-  if (threadPid) {
-     if (kill(threadPid, SIGIO) < 0) { // couldn't find another way of checking whether the thread is still running - any ideas?
-        if (errno == ESRCH)
-           threadPid = 0;
-        else
+  if (thread) {
+     /*
+      * Single UNIX Spec v2 says:
+      *
+      * The pthread_kill() function is used to request
+      * that a signal be delivered to the specified thread.
+      *
+      * As in kill(), if sig is zero, error checking is
+      * performed but no signal is actually sent.
+      */
+     int err;
+     if ((err = pthread_kill(thread, 0)) != 0) {
+        if (err != ESRCH)
            LOG_ERROR;
+        thread = 0;
         }
      else
         return true;
@@ -180,14 +190,14 @@
             return;
          usleep(10000);
          }
-     esyslog("ERROR: thread %d won't end (waited %d seconds) - cancelling it...", threadPid, WaitSeconds);
+     esyslog("ERROR: thread %ld won't end (waited %d seconds) - cancelling it...", thread, WaitSeconds);
      }
   pthread_cancel(thread);
 }
 
 void cThread::WakeUp(void)
 {
-  kill(parentPid, SIGIO); // makes any waiting 'select()' call return immediately
+  pthread_kill(parentThread, SIGIO); // makes any waiting 'select()' call return immediately
 }
 
 bool cThread::EmergencyExit(bool Request)
diff -urN vdr-1.2.0pre1-orig/thread.h vdr-1.2.0pre1-pthread/thread.h
--- vdr-1.2.0pre1-orig/thread.h	2003-05-03 16:03:36.000000000 +0200
+++ vdr-1.2.0pre1-pthread/thread.h	2003-06-01 09:16:07.000000000 +0200
@@ -32,7 +32,7 @@
   friend class cCondVar;
 private:
   pthread_mutex_t mutex;
-  pid_t lockingPid;
+  pthread_t lockingThread;
   int locked;
 public:
   cMutex(void);
@@ -44,9 +44,8 @@
 class cThread {
   friend class cThreadLock;
 private:
-  pthread_t thread;
+  pthread_t thread, parentThread;
   cMutex mutex;
-  pid_t parentPid, threadPid;
   bool running;
   static bool emergencyExitRequested;
   static bool signalHandlerInstalled;

Home | Main Index | Thread Index