DO NOT UPDATE!  COMPILES BUT DOESN'T WORK.

Major overhaul to the way thread startup and exit is done.  Removes some
ugly gunk in the scheduler, and adds support for thread detaching and
cancellation.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@326 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/vg_include.h b/coregrind/vg_include.h
index 9431c8e..e81fe77 100644
--- a/coregrind/vg_include.h
+++ b/coregrind/vg_include.h
@@ -146,6 +146,12 @@
    beyond it. */
 #define VG_PTHREAD_STACK_SIZE 65536
 
+/* Number of entries in the semaphore-remapping table. */
+#define VG_N_SEMAPHORES 50
+
+/* Number of entries in the rwlock-remapping table. */
+#define VG_N_RWLOCKS 50
+
 
 /* ---------------------------------------------------------------------
    Basic types
@@ -416,27 +422,54 @@
 #define VG_USERREQ__MEMALIGN            0x2009
 
 
-#define VG_USERREQ__PTHREAD_CREATE          0x3001
-#define VG_USERREQ__PTHREAD_JOIN            0x3002
-#define VG_USERREQ__PTHREAD_GET_THREADID    0x3003
-#define VG_USERREQ__PTHREAD_MUTEX_LOCK      0x3004
-#define VG_USERREQ__PTHREAD_MUTEX_TRYLOCK   0x3005
-#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK    0x3006
-#define VG_USERREQ__PTHREAD_CANCEL          0x3007
-#define VG_USERREQ__PTHREAD_EXIT            0x3008
-#define VG_USERREQ__PTHREAD_COND_WAIT       0x3009
-#define VG_USERREQ__PTHREAD_COND_TIMEDWAIT  0x300A
-#define VG_USERREQ__PTHREAD_COND_SIGNAL     0x300B
-#define VG_USERREQ__PTHREAD_COND_BROADCAST  0x300C
-#define VG_USERREQ__PTHREAD_KEY_CREATE      0x300D
-#define VG_USERREQ__PTHREAD_KEY_DELETE      0x300E
-#define VG_USERREQ__PTHREAD_SETSPECIFIC     0x300F
-#define VG_USERREQ__PTHREAD_GETSPECIFIC     0x3010
-#define VG_USERREQ__READ_MILLISECOND_TIMER  0x3011
-#define VG_USERREQ__PTHREAD_SIGMASK         0x3012
-#define VG_USERREQ__SIGWAIT                 0x3013
-#define VG_USERREQ__PTHREAD_KILL            0x3014
-#define VG_USERREQ__PTHREAD_YIELD           0x3015
+/* (Fn, Arg): Create a new thread and run Fn applied to Arg in it.  Fn
+   MUST NOT return -- ever.  Eventually it will do either __QUIT or
+   __WAIT_JOINER.  */
+#define VG_USERREQ__APPLY_IN_NEW_THREAD     0x3001
+
+/* ( no-args ): calling thread disappears from the system forever.
+   Reclaim resources. */
+#define VG_USERREQ__QUIT                    0x3002
+
+/* ( void* ): calling thread waits for joiner and returns the void* to
+   it. */
+#define VG_USERREQ__WAIT_JOINER             0x3003
+
+/* ( ThreadId, void** ): wait to join a thread. */
+#define VG_USERREQ__PTHREAD_JOIN            0x3004
+
+/* Set cancellation state and type for this thread. */
+#define VG_USERREQ__SET_CANCELSTATE         0x3005
+#define VG_USERREQ__SET_CANCELTYPE          0x3006
+
+/* ( no-args ): Test if we are at a cancellation point. */
+#define VG_USERREQ__TESTCANCEL              0x3007
+
+/* ( ThreadId, &thread_exit_wrapper is the only allowable arg ): call
+   with this arg to indicate that a cancel is now pending for the
+   specified thread. */
+#define VG_USERREQ__SET_CANCELPEND          0x3008
+
+/* Set/get detach state for this thread. */
+#define VG_USERREQ__SET_OR_GET_DETACH       0x3009
+
+#define VG_USERREQ__PTHREAD_GET_THREADID    0x300B
+#define VG_USERREQ__PTHREAD_MUTEX_LOCK      0x300C
+#define VG_USERREQ__PTHREAD_MUTEX_TRYLOCK   0x300D
+#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK    0x300E
+#define VG_USERREQ__PTHREAD_COND_WAIT       0x300F
+#define VG_USERREQ__PTHREAD_COND_TIMEDWAIT  0x3010
+#define VG_USERREQ__PTHREAD_COND_SIGNAL     0x3011
+#define VG_USERREQ__PTHREAD_COND_BROADCAST  0x3012
+#define VG_USERREQ__PTHREAD_KEY_CREATE      0x3013
+#define VG_USERREQ__PTHREAD_KEY_DELETE      0x3014
+#define VG_USERREQ__PTHREAD_SETSPECIFIC     0x3015
+#define VG_USERREQ__PTHREAD_GETSPECIFIC     0x3016
+#define VG_USERREQ__READ_MILLISECOND_TIMER  0x3017
+#define VG_USERREQ__PTHREAD_SIGMASK         0x3018
+#define VG_USERREQ__SIGWAIT                 0x3019
+#define VG_USERREQ__PTHREAD_KILL            0x301A
+#define VG_USERREQ__PTHREAD_YIELD           0x301B
 
 /* Cosmetic ... */
 #define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
@@ -444,7 +477,6 @@
 /* 
 In vg_constants.h:
 #define VG_USERREQ__SIGNAL_RETURNS          0x4001
-#define VG_USERREQ__PTHREAD_RETURNS         0x4002
 */
 
 
@@ -506,10 +538,6 @@
          the mutex finally gets unblocked. */
       ThreadStatus status;
 
-      /* Identity of joiner (thread who called join on me), or
-         VG_INVALID_THREADID if no one asked to join yet. */
-      ThreadId joiner;
-
       /* When .status == WaitMX, points to the mutex I am waiting for.
          When .status == WaitCV, points to the mutex associated with
          the condition variable indicated by the .associated_cv field.
@@ -529,8 +557,26 @@
          pthread_cond_wait. */
       UInt awaken_at;
 
-      /* return value */
-      void* retval;
+      /* If VgTs_WaitJoiner, return value, as generated by joinees. */
+      void* joinee_retval;
+
+      /* If VgTs_WaitJoinee, place to copy the return value to, and
+         the identity of the thread we're waiting for. */
+      void**   joiner_thread_return;
+      ThreadId joiner_jee_tid;      
+
+      /* Cancelability state and type. */
+      Bool cancel_st; /* False==PTH_CANCEL_DISABLE; True==.._ENABLE */
+      Bool cancel_ty; /* False==PTH_CANC_ASYNCH; True==..._DEFERRED */
+     
+      /* Pointer to fn to call to do cancellation.  Indicates whether
+         or not cancellation is pending.  If NULL, not pending.  Else
+         should be &thread_exit_wrapper(), indicating that
+         cancallation is pending. */
+      void (*cancel_pend)(void*);
+
+      /* Whether or not detached. */
+      Bool detached;
 
       /* thread-specific data */
       void* specifics[VG_N_THREAD_KEYS];
@@ -1694,9 +1740,9 @@
 extern void VG_(helper_value_check1_fail);
 extern void VG_(helper_value_check0_fail);
 
-/* NOT FUNCTIONS; these are bogus RETURN ADDRESS. */
+/* NOT A FUNCTION; this is a bogus RETURN ADDRESS. */
 extern void VG_(signalreturn_bogusRA)( void );
-extern void VG_(pthreadreturn_bogusRA)( void );
+
 
 /* ---------------------------------------------------------------------
    Exports of vg_cachesim.c