Fix exclusion logic at thread creation and allow main thread to exit.
authorMarcus Brinkmann <marcus.brinkmann@ruhr-uni-bochum.de>
Fri, 6 Jan 2012 13:03:13 +0000 (14:03 +0100)
committerMarcus Brinkmann <marcus.brinkmann@ruhr-uni-bochum.de>
Fri, 6 Jan 2012 13:06:04 +0000 (14:06 +0100)
* libnpth.vers (npth_create, npth_exit): New.
* npth.def (npth_create, npth_exit): Likewise.
* npth.h (npth_create): Change from macro to declaration.
(npth_exit): New declaration.
* npth.c (startup_s): New struct.
(thread_startup, npth_create, npth_exit): New functions.

src/libnpth.vers
src/npth.c
src/npth.def
src/npth.h

index 1e52687..dea00dc 100644 (file)
@@ -23,7 +23,9 @@
 NPTH_1.0 {
   global:
     npth_init;
+    npth_create;
     npth_join;
+    npth_exit;
     npth_mutex_lock;
     npth_mutex_timedlock;
     npth_rwlock_rdlock;
index 0880d31..c7b805b 100644 (file)
@@ -109,6 +109,58 @@ npth_init (void)
 }
 
 \f
+struct startup_s
+{
+  void *(*start_routine) (void *);
+  void *arg;
+};
+
+
+static void *
+thread_start (void *startup_arg)
+{
+  struct startup_s *startup = startup_arg;
+  void *(*start_routine) (void *);
+  void *arg;
+  void *result;
+
+  start_routine = startup->start_routine;
+  arg = startup->arg;
+  free (startup);
+
+  LEAVE();
+  result = (*start_routine) (arg);
+  /* Note: instead of returning here, we might end up in
+     npth_exit() instead.  */
+  ENTER();
+
+  return result;
+}
+
+
+int
+npth_create (npth_t *thread, const npth_attr_t *attr,
+            void *(*start_routine) (void *), void *arg)
+{
+  int err;
+  struct startup_s *startup;
+
+  startup = malloc (sizeof (*startup));
+  if (!startup)
+    return errno;
+
+  err = pthread_create (thread, attr, thread_start, arg);
+  if (err)
+    {
+      free (startup);
+      return err;
+    }
+
+  /* Memory is released in thread_start.  */
+  return 0;
+}
+
+
 int
 npth_join (npth_t thread, void **retval)
 {
@@ -127,6 +179,15 @@ npth_join (npth_t thread, void **retval)
 }
 
 
+void
+npth_exit (void *retval)
+{
+  ENTER();
+  pthread_exit (retval);
+  /* Never reached.  But just in case pthread_exit does return... */
+  LEAVE();
+}
+
 \f
 int
 npth_mutex_lock (npth_mutex_t *mutex)
index c2df7b8..785feca 100644 (file)
 
 EXPORTS
     npth_init                          @1
-    npth_join                          @2
-    npth_mutex_lock                    @3
-    npth_mutex_timedlock               @4
-    npth_rwlock_rdlock                 @5
-    npth_rwlock_timedrdlock            @6
-    npth_rwlock_wrlock                 @7
-    npth_rwlock_timedwrlock            @8
-    npth_cond_wait                     @9
-    npth_cond_timedwait                        @10
-    npth_usleep                                @11
-    npth_sleep                         @12
-    npth_waitpid                       @13
-    npth_system                                @14
-    npth_sigmask                       @15
-    npth_sigwait                       @16
-    npth_connect                       @17
-    npth_accept                                @18
-    npth_select                                @19
-    npth_pselect                       @20
-    npth_read                          @21
-    npth_write                         @22
-    npth_recvmsg                       @23
-    npth_sendmsg                       @24
+    npth_create                                @2
+    npth_join                          @3
+    npth_exit                          @4
+    npth_mutex_lock                    @5
+    npth_mutex_timedlock               @6
+    npth_rwlock_rdlock                 @7
+    npth_rwlock_timedrdlock            @8
+    npth_rwlock_wrlock                 @9
+    npth_rwlock_timedwrlock            @10
+    npth_cond_wait                     @11
+    npth_cond_timedwait                        @12
+    npth_usleep                                @13
+    npth_sleep                         @14
+    npth_waitpid                       @15
+    npth_system                                @16
+    npth_sigmask                       @17
+    npth_sigwait                       @18
+    npth_connect                       @19
+    npth_accept                                @20
+    npth_select                                @21
+    npth_pselect                       @22
+    npth_read                          @23
+    npth_write                         @24
+    npth_recvmsg                       @25
+    npth_sendmsg                       @26
 
-    npth_sigev_init                    @25
-    npth_sigev_add                     @26
-    npth_sigev_fini                    @27
-    npth_sigev_sigmask                 @28
-    npth_sigev_get_pending             @29
+    npth_sigev_init                    @27
+    npth_sigev_add                     @28
+    npth_sigev_fini                    @29
+    npth_sigev_sigmask                 @30
+    npth_sigev_get_pending             @31
 
 ; END
 
index 4888576..6920da6 100644 (file)
@@ -98,7 +98,9 @@ int npth_init(void);
 
 #define npth_t pthread_t
 
-#define npth_create pthread_create
+int npth_create(npth_t *thread, const npth_attr_t *attr,
+               void *(*start_routine) (void *), void *arg);
+
 
 /* The Pth version of pth_once supports passing an argument, the
    pthread version does not.  We would have to reimplement the whole
@@ -120,7 +122,7 @@ int npth_init(void);
 int npth_join(npth_t thread, void **retval);
 #define npth_detach pthread_detach
 
-/* pth_exit */
+void npth_exit(void *retval);
 
 \f
 /* Utilities */