Browse Source

linuxthreads.old: Implement pthread_tryjoin_np(), pthread_timedjoin_np()

Some applications needs it.

Signed-off-by: Leonid Lisovskiy <lly.dev@gmail.com>
Leonid Lisovskiy 8 years ago
parent
commit
c1b5c59ece

+ 138 - 0
libpthread/linuxthreads.old/join.c

@@ -192,6 +192,144 @@ int pthread_join(pthread_t thread_id, void ** thread_return)
   return 0;
 }
 
+int pthread_tryjoin_np(pthread_t thread_id, void ** thread_return)
+{
+  volatile pthread_descr self = thread_self();
+  struct pthread_request request;
+  pthread_handle handle = thread_handle(thread_id);
+  pthread_descr th;
+  int result = 0;
+
+  /* Make sure the descriptor is valid.  */
+  __pthread_lock(&handle->h_lock, self);
+  if (invalid_handle(handle, thread_id)) {
+    result = ESRCH;
+    goto err;
+  }
+  th = handle->h_descr;
+  /* Is the thread joinable?.  */
+  if (th->p_detached || th->p_joining != NULL) {
+    result = EINVAL;
+    goto err;
+  }
+  if (th == self) {
+    result = EDEADLK;
+    goto err;
+  }
+  /* Return right away if the thread hasn't terminated yet.  */
+  if (! th->p_terminated) {
+    result = EBUSY;
+    goto err;
+  }
+
+  /* Get return value */
+  if (thread_return != NULL) *thread_return = th->p_retval;
+  __pthread_unlock(&handle->h_lock);
+  /* Send notification to thread manager */
+  if (__pthread_manager_request >= 0) {
+    request.req_thread = self;
+    request.req_kind = REQ_FREE;
+    request.req_args.free.thread_id = thread_id;
+    TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
+		(char *) &request, sizeof(request)));
+  }
+  return 0;
+
+err:
+  __pthread_unlock(&handle->h_lock);
+  return result;
+}
+
+int pthread_timedjoin_np(pthread_t thread_id, void ** thread_return,
+			const struct timespec *abstime)
+{
+  volatile pthread_descr self = thread_self();
+  struct pthread_request request;
+  pthread_handle handle = thread_handle(thread_id);
+  pthread_descr th;
+  pthread_extricate_if extr;
+  int already_canceled = 0;
+  int result = 0;
+  PDEBUG("\n");
+
+  /* Set up extrication interface */
+  extr.pu_object = handle;
+  extr.pu_extricate_func = join_extricate_func;
+
+  __pthread_lock(&handle->h_lock, self);
+  if (invalid_handle(handle, thread_id)) {
+    result = ESRCH;
+    goto err;
+  }
+  th = handle->h_descr;
+  if (th == self) {
+    result = EDEADLK;
+    goto err;
+  }
+  /* If detached or already joined, error */
+  if (th->p_detached || th->p_joining != NULL) {
+    result = EINVAL;
+    goto err;
+  }
+  /* If not terminated yet, suspend ourselves. */
+  if (! th->p_terminated) {
+    /* Register extrication interface */
+    __pthread_set_own_extricate_if(self, &extr);
+    if (!(THREAD_GETMEM(self, p_canceled)
+	&& THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
+      th->p_joining = self;
+    else
+      already_canceled = 1;
+    __pthread_unlock(&handle->h_lock);
+
+    if (already_canceled) {
+      __pthread_set_own_extricate_if(self, 0);
+      __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
+    }
+
+  PDEBUG("before suspend\n");
+    result = (timedsuspend(self, abstime) == 0) ? ETIMEDOUT : 0;
+  PDEBUG("after suspend\n");
+    /* Deregister extrication interface */
+    __pthread_set_own_extricate_if(self, 0);
+
+    /* This is a cancellation point */
+    if (result == 0
+        && THREAD_GETMEM(self, p_woken_by_cancel)
+	&& THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
+      THREAD_SETMEM(self, p_woken_by_cancel, 0);
+      __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
+    }
+    __pthread_lock(&handle->h_lock, self);
+  }
+
+  /* We might have timed out. */
+  if (result == 0) {
+    /* Get return value */
+    if (thread_return != NULL) *thread_return = th->p_retval;
+  }
+  else
+    th->p_joining = NULL;
+
+  __pthread_unlock(&handle->h_lock);
+
+  if (result == 0) {
+    /* Send notification to thread manager */
+    if (__pthread_manager_request >= 0) {
+      request.req_thread = self;
+      request.req_kind = REQ_FREE;
+      request.req_args.free.thread_id = thread_id;
+      TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
+		(char *) &request, sizeof(request)));
+    }
+  }
+  return result;
+
+err:
+  __pthread_unlock(&handle->h_lock);
+  return result;
+}
+
 int pthread_detach(pthread_t thread_id)
 {
   int terminated;

+ 15 - 0
libpthread/linuxthreads.old/sysdeps/pthread/pthread.h

@@ -179,6 +179,21 @@ extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__));
    is not NULL.  */
 extern int pthread_join (pthread_t __th, void **__thread_return);
 
+#ifdef __USE_GNU
+/* Check whether thread TH has terminated.  If yes return the status of
+   the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL.  */
+extern int pthread_tryjoin_np (pthread_t __th, void **__thread_return) __THROW;
+
+/* Make calling thread wait for termination of the thread TH, but only
+   until TIMEOUT.  The exit status of the thread is stored in
+   *THREAD_RETURN, if THREAD_RETURN is not NULL.
+
+   This function is a cancellation point and therefore not marked with
+   __THROW.  */
+extern int pthread_timedjoin_np (pthread_t __th, void **__thread_return,
+				 __const struct timespec *__abstime);
+#endif
+
 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
    The resources of TH will therefore be freed immediately when it
    terminates, instead of waiting for another thread to perform PTHREAD_JOIN

+ 104 - 0
test/pthread/tst-join2.c

@@ -0,0 +1,104 @@
+/* Copyright (C) 2002 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+
+static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+
+
+static void *
+tf (void *arg)
+{
+  if (pthread_mutex_lock (&lock) != 0)
+    {
+      puts ("child: mutex_lock failed");
+      return NULL;
+    }
+
+  return (void *) 42l;
+}
+
+
+static int
+do_test (void)
+{
+  pthread_t th;
+
+  if (pthread_mutex_lock (&lock) != 0)
+    {
+      puts ("mutex_lock failed");
+      exit (1);
+    }
+
+  if (pthread_create (&th, NULL, tf, NULL) != 0)
+    {
+      puts ("mutex_create failed");
+      exit (1);
+    }
+
+  void *status;
+  int val = pthread_tryjoin_np (th, &status);
+  if (val == 0)
+    {
+      puts ("1st tryjoin succeeded");
+      exit (1);
+    }
+  else if (val != EBUSY)
+    {
+      puts ("1st tryjoin didn't return EBUSY");
+      exit (1);
+    }
+
+  if (pthread_mutex_unlock (&lock) != 0)
+    {
+      puts ("mutex_unlock failed");
+      exit (1);
+    }
+
+  while ((val = pthread_tryjoin_np (th, &status)) != 0)
+    {
+      if (val != EBUSY)
+	{
+	  printf ("tryjoin returned %s (%d), expected only 0 or EBUSY\n",
+		  strerror (val), val);
+	  exit (1);
+	}
+
+      /* Delay minimally.  */
+      struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
+      nanosleep (&ts, NULL);
+    }
+
+  if (status != (void *) 42l)
+    {
+      printf ("return value %p, expected %p\n", status, (void *) 42l);
+      exit (1);
+    }
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"

+ 123 - 0
test/pthread/tst-join3.c

@@ -0,0 +1,123 @@
+/* Copyright (C) 2002 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+
+
+static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+
+
+static void *
+tf (void *arg)
+{
+  if (pthread_mutex_lock (&lock) != 0)
+    {
+      puts ("child: mutex_lock failed");
+      return NULL;
+    }
+
+  return (void *) 42l;
+}
+
+
+static int
+do_test (void)
+{
+  pthread_t th;
+
+  if (pthread_mutex_lock (&lock) != 0)
+    {
+      puts ("mutex_lock failed");
+      exit (1);
+    }
+
+  if (pthread_create (&th, NULL, tf, NULL) != 0)
+    {
+      puts ("mutex_create failed");
+      exit (1);
+    }
+
+  void *status;
+  struct timespec ts;
+  struct timeval tv;
+  (void) gettimeofday (&tv, NULL);
+  TIMEVAL_TO_TIMESPEC (&tv, &ts);
+  ts.tv_nsec += 200000000;
+  if (ts.tv_nsec >= 1000000000)
+    {
+      ts.tv_nsec -= 1000000000;
+      ++ts.tv_sec;
+    }
+  int val = pthread_timedjoin_np (th, &status, &ts);
+  if (val == 0)
+    {
+      puts ("1st timedjoin succeeded");
+      exit (1);
+    }
+  else if (val != ETIMEDOUT)
+    {
+      puts ("1st timedjoin didn't return ETIMEDOUT");
+      exit (1);
+    }
+
+  if (pthread_mutex_unlock (&lock) != 0)
+    {
+      puts ("mutex_unlock failed");
+      exit (1);
+    }
+
+  while (1)
+    {
+      (void) gettimeofday (&tv, NULL);
+      TIMEVAL_TO_TIMESPEC (&tv, &ts);
+      ts.tv_nsec += 200000000;
+      if (ts.tv_nsec >= 1000000000)
+	{
+	  ts.tv_nsec -= 1000000000;
+	  ++ts.tv_sec;
+	}
+
+      val = pthread_timedjoin_np (th, &status, &ts);
+      if (val == 0)
+	break;
+
+      if (val != ETIMEDOUT)
+	{
+	  printf ("timedjoin returned %s (%d), expected only 0 or ETIMEDOUT\n",
+		  strerror (val), val);
+	  exit (1);
+	}
+    }
+
+  if (status != (void *) 42l)
+    {
+      printf ("return value %p, expected %p\n", status, (void *) 42l);
+      exit (1);
+    }
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"