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

Re: NPTL: pthread_condtimedwait hang or mutex_lock hang



The following program is a simpler illustration of the problem posted by me earlier. It fails and succeeds in the same environments as the first program.

------------------------- Beginning of source code ------------------

#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>

static pthread_mutex_t gbl_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  gbl_condv = PTHREAD_COND_INITIALIZER;

void waitThread_cleanup(void *arg)
{
  int rc;
  rc = pthread_mutex_unlock(&gbl_mutex);
  assert(rc == 0);
  return;
}

void * waitThread(void *arg)
{
  int rc;
  pthread_cleanup_push(waitThread_cleanup, NULL);
  rc = pthread_mutex_lock(&gbl_mutex);
  assert(rc == 0);
  /* wait until this thread is canceled */
  while (1 == 1) {
    rc = pthread_cond_wait(&gbl_condv, &gbl_mutex);
    assert(rc == 0);
  }
  /* this routine never reaches this point */
  rc = pthread_mutex_unlock(&gbl_mutex);
  assert(rc == 0);
  pthread_cleanup_pop(0);
  return NULL;
}

main (int argc, char *argv[])
{
  int i, rc;
  pthread_t wait_tid;
  for (i = 0; i < 1000000; i++)
    {
      fprintf(stderr, "loop %d\n", i);
      rc = pthread_create(&wait_tid, NULL, waitThread, NULL);
      assert(rc == 0);
      rc = pthread_cancel(wait_tid);
      assert(rc == 0);
      rc = pthread_join(wait_tid, NULL);
      assert(rc == 0);
    }
  return;
}

----------------------------- End of source code -------------

The initial thread simply loops doing the following: create another thread, cancel the other thread, and join with the other thread. The created thread simply loops on calls to pthread_cond_wait() until it is canceled. The thread cancelation cleanup routine simply unlocks the mutex associated with the
condition variable.


When the program fails, the second time a thread is created, the created
thread hangs forever attempting to lock the mutex.

Here are what the threads look like in gdb when the hang occurs:

(gdb) where
#0  0xb75ebc32 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1  0xb75ccc1d in pthread_join () from /lib/tls/libpthread.so.0
#2  0x0804875b in main (argc=1, argv=0xbfffe2e4) at mtcond4.c:62

(gdb) thread 2
[Switching to thread 2 (Thread -1219953744 (LWP 8736))]#0  0xb75ebc32 in
_dl_sysinfo_int80 () from /lib/ld-linux.so.2

(gdb) where
#0  0xb75ebc32 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1  0xb75d067b in __lll_mutex_lock_wait () from /lib/tls/libpthread.so.0
#2  0x00002220 in ?? ()
#3  0xb75d4b3c in __JCR_LIST__ () from /lib/tls/libpthread.so.0
#4  0x080498e0 in p.0 ()
#5  0xb75cd787 in _L_mutex_lock_28 () from /lib/tls/libpthread.so.0
#6  0xb75d4b3c in __JCR_LIST__ () from /lib/tls/libpthread.so.0
#7  0xb748fbb0 in ?? ()
#8  0xb748fa98 in ?? ()
#9  0x08048658 in waitThread (arg=0x80498e0) at mtcond4.c:27






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