pthreads, part 8

Alex Waugh alex at alexwaugh.com
Tue Apr 1 14:35:06 PST 2003


This removes initialisation that is no longer necessary for many
functions, which allows mutexes to work before pthread_create is
called. Also, the ticker is now stopped when there is only one thread
left running, and __pthread_start_ticker will not enable it until more
than one thread is running.

Alex


Index: unixlib/source/clib/pthread.h
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/clib/pthread.h,v
retrieving revision 1.4
diff -u -r1.4 pthread.h
--- unixlib/source/clib/pthread.h	5 Jan 2003 12:36:35 -0000	1.4
+++ unixlib/source/clib/pthread.h	1 Apr 2003 22:06:35 -0000
@@ -544,8 +545,6 @@
 
 /* zero if the context switcher is allowed to switch threads */
 extern int __pthread_worksemaphore;
-
-#define PTHREAD_INIT if (!__pthread_system_running) __pthread_init ();
 
 #endif /* __UNIXLIB_INTERNALS */
 
Index: unixlib/source/pthread/_context.s
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/_context.s,v
retrieving revision 1.2
diff -u -r1.2 _context.s
--- unixlib/source/pthread/_context.s	5 Jan 2003 12:36:35 -0000	1.2
+++ unixlib/source/pthread/_context.s	1 Apr 2003 22:25:48 -0000
@@ -65,6 +63,11 @@
 	LDR	a1, [a1]
 	CMP	a1, #1
 	stackreturn	NE, "pc"
+	; Don't start if there's only one thread running
+	LDR	a1, =|__pthread_running_threads|
+	LDR	a1, [a1]
+	CMP	a1, #1
+	stackreturn	LE, "pc"
 	; Don't start if the ticker is already running
 	LDR	a1, =|ticker_started|
 	LDR	a1, [a1]
Index: unixlib/source/pthread/atfork.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/atfork.c,v
retrieving revision 1.1
diff -u -r1.1 atfork.c
--- unixlib/source/pthread/atfork.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/atfork.c	1 Apr 2003 22:06:36 -0000
@@ -34,8 +34,6 @@
 {
   struct fork_handlers *newhandlers;
 
-  PTHREAD_INIT
-
   newhandlers = malloc (sizeof (struct fork_handlers));
   if (newhandlers == NULL)
     return ENOMEM;
Index: unixlib/source/pthread/cleanup.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/cleanup.c,v
retrieving revision 1.1
diff -u -r1.1 cleanup.c
--- unixlib/source/pthread/cleanup.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/cleanup.c	1 Apr 2003 22:06:36 -0000
@@ -21,8 +21,6 @@
 void
 __pthread_cleanup_push (struct __pthread_cleanup *cleanup)
 {
-  PTHREAD_INIT
-
   __pthread_disable_ints ();
 
   cleanup->next = __pthread_running_thread->cleanupfns;
@@ -34,8 +32,6 @@
 void
 __pthread_cleanup_pop (void)
 {
-  PTHREAD_INIT
-
   __pthread_disable_ints ();
 
   __pthread_running_thread->cleanupfns = __pthread_running_thread->cleanupfns->next;
Index: unixlib/source/pthread/cond.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/cond.c,v
retrieving revision 1.1
diff -u -r1.1 cond.c
--- unixlib/source/pthread/cond.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/cond.c	1 Apr 2003 22:06:36 -0000
@@ -59,8 +59,6 @@
 {
   pthread_t thread;
 
-  PTHREAD_INIT
-
   if (cond == NULL || mutex == NULL)
     return EINVAL;
 
Index: unixlib/source/pthread/context.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/context.c,v
retrieving revision 1.2
diff -u -r1.2 context.c
--- unixlib/source/pthread/context.c	5 Jan 2003 12:36:35 -0000	1.2
+++ unixlib/source/pthread/context.c	1 Apr 2003 22:06:36 -0000
@@ -27,7 +29,7 @@
 pthread_t __pthread_thread_list = NULL; /* Linked list of all threads */
 pthread_t __pthread_running_thread = NULL; /* Currently running thread */
 
-int __pthread_running_threads = 0; /* Number of threads */
+int __pthread_running_threads = 1; /* Number of threads */
 
 
 /* De-allocates an idle node from the list */
Index: unixlib/source/pthread/create.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/create.c,v
retrieving revision 1.1
diff -u -r1.1 create.c
--- unixlib/source/pthread/create.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/create.c	1 Apr 2003 22:06:36 -0000
@@ -53,7 +53,7 @@
 {
   pthread_t thread;
 
-  PTHREAD_INIT
+  if (!__pthread_system_running) __pthread_init ();
 
   if (threadin == NULL)
     return EINVAL;
@@ -98,7 +98,6 @@
   if (attr != NULL)
     thread->detachstate = attr->detachstate;
 
-  __pthread_running_threads++;
 
   __pthread_init_save_area (thread->saved_context);
 
@@ -122,7 +121,12 @@
 
   *threadin = thread;
 
+  __pthread_running_threads++;
+
+  __pthread_start_ticker ();
+
   __pthread_enable_ints ();
+
 
   return 0;
 }
Index: unixlib/source/pthread/exit.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/exit.c,v
retrieving revision 1.1
diff -u -r1.1 exit.c
--- unixlib/source/pthread/exit.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/exit.c	1 Apr 2003 22:06:37 -0000
@@ -74,6 +74,16 @@
 #endif
 
   __pthread_running_threads--;
+
+  if (__pthread_running_threads <= 1)
+    {
+#ifdef PTHREAD_DEBUG
+      __os_print ("-- pthread_exit: Last or penultimate thread exited, stopping interrupts\r\n");
+#endif
+      /* There is no need for the ticker if there is only one thread left */
+      __pthread_stop_ticker ();
+    }
+
   if (__pthread_running_threads > 0)
     {
       /* Not the last thread */
@@ -87,12 +97,9 @@
         }
     }
 
-
-  /* Last thread running has called pthread_exit so do some special cleanup */
 #ifdef PTHREAD_DEBUG
-  __os_print ("-- pthread_exit: Last thread exited, stopping interrupts\r\n");
+  __os_print ("-- pthread_exit: Last thread exited, calling exit()\r\n");
 #endif
-  __pthread_stop_ticker (); /* Turn off the scheduler for the last time */
 
   exit ((int)status);
 }
Index: unixlib/source/pthread/init.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/init.c,v
retrieving revision 1.1
diff -u -r1.1 init.c
--- unixlib/source/pthread/init.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/init.c	1 Apr 2003 22:06:37 -0000
@@ -74,6 +74,5 @@
   __os_print ("-- __pthread_init: Starting the interupts\r\n");
 #endif
   __pthread_system_running = 1;
-  __pthread_start_ticker ();
 }
 
Index: unixlib/source/pthread/join.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/join.c,v
retrieving revision 1.1
diff -u -r1.1 join.c
--- unixlib/source/pthread/join.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/join.c	1 Apr 2003 22:06:37 -0000
@@ -25,8 +25,6 @@
 {
   pthread_t thread;
 
-  PTHREAD_INIT
-
   if (tojoin == __pthread_running_thread)
     {
 #ifdef PTHREAD_DEBUG
Index: unixlib/source/pthread/lock.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/lock.c,v
retrieving revision 1.1
diff -u -r1.1 lock.c
--- unixlib/source/pthread/lock.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/lock.c	1 Apr 2003 22:06:37 -0000
@@ -125,8 +125,6 @@
 int
 __pthread_lock_lock (pthread_mutex_t *mutex, const enum __pthread_locktype type, const int trylock)
 {
-  PTHREAD_INIT
-
   __pthread_disable_ints ();
 
 #ifdef PTHREAD_DEBUG
@@ -217,8 +215,6 @@
 {
   pthread_t write = NULL;
 
-  PTHREAD_INIT
-
   if (mutex == NULL)
     return EINVAL;
 
Index: unixlib/source/pthread/mutex.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/mutex.c,v
retrieving revision 1.1
diff -u -r1.1 mutex.c
--- unixlib/source/pthread/mutex.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/mutex.c	1 Apr 2003 22:06:37 -0000
@@ -69,8 +69,6 @@
 int
 pthread_mutex_trylock (pthread_mutex_t *mutex)
 {
-  PTHREAD_INIT
-
   if (mutex == NULL || mutex->type != LOCK_MUTEX)
     return EINVAL;
 
@@ -81,8 +79,6 @@
 int
 pthread_mutex_lock (pthread_mutex_t *mutex)
 {
-  PTHREAD_INIT
-
   if (mutex == NULL || mutex->type != LOCK_MUTEX)
     return EINVAL;
 
Index: unixlib/source/pthread/rwlock.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/rwlock.c,v
retrieving revision 1.1
diff -u -r1.1 rwlock.c
--- unixlib/source/pthread/rwlock.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/rwlock.c	1 Apr 2003 22:06:38 -0000
@@ -71,8 +71,6 @@
 int
 pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
 {
-  PTHREAD_INIT
-
   if (rwlock == NULL || (rwlock->type != LOCK_WRITE && rwlock->type != LOCK_READ))
     return EINVAL;
 
@@ -83,8 +81,6 @@
 int
 pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
 {
-  PTHREAD_INIT
-
   if (rwlock == NULL || (rwlock->type != LOCK_WRITE && rwlock->type != LOCK_READ))
     return EINVAL;
 
@@ -95,8 +91,6 @@
 int
 pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
 {
-  PTHREAD_INIT
-
   if (rwlock == NULL || (rwlock->type != LOCK_WRITE && rwlock->type != LOCK_READ))
     return EINVAL;
 
@@ -107,8 +101,6 @@
 int
 pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
 {
-  PTHREAD_INIT
-
   if (rwlock == NULL || (rwlock->type != LOCK_WRITE && rwlock->type != LOCK_READ))
     return EINVAL;
 
Index: unixlib/source/pthread/self.c
===================================================================
RCS file: /usr/local/cvsroot/gccsdk/unixlib/source/pthread/self.c,v
retrieving revision 1.1
diff -u -r1.1 self.c
--- unixlib/source/pthread/self.c	15 Dec 2002 13:16:55 -0000	1.1
+++ unixlib/source/pthread/self.c	1 Apr 2003 22:06:38 -0000
@@ -17,11 +17,8 @@
 #include <pthread.h>
 
 /* Return the thread id of the currently running thread */
-/* Will start the thread system if it's not running */
 pthread_t
 pthread_self (void)
 {
-  PTHREAD_INIT
-
   return __pthread_running_thread;
 }


-- 
Alex Waugh                                           alex at alexwaugh.com

PHP, Roots, Subversion, WebJames and more from http://www.alexwaugh.com/




More information about the gcc mailing list