From 99a8fab5a63b69fe283ba8819240f5b5d4e023d0 Mon Sep 17 00:00:00 2001
From: Christopher Faylor <me@cgf.cx>
Date: Fri, 29 Aug 2003 02:59:06 +0000
Subject: [PATCH] * sigproc.h: Make some functions regparm. * sigproc.cc
 (checkstate): Make regparm. (getevent): Change parameters in declaration,
 rename from getsem, make regparm. (sig_send): Recognize that nosync is now an
 event.  Remove some old cruft from previous interrupt anywhere signal
 handler. (getevent): Change parameters in definition, rename from getsem. 
 Allocate event rather than semaphore. (wait_sig): Treat sigcatch_nosync as an
 event.

---
 winsup/cygwin/sigproc.cc | 61 ++++++++++++++++++----------------------
 winsup/cygwin/sigproc.h  |  8 +++---
 2 files changed, 31 insertions(+), 38 deletions(-)

diff --git a/winsup/cygwin/sigproc.cc b/winsup/cygwin/sigproc.cc
index 60c26072d..fd4f295cc 100644
--- a/winsup/cygwin/sigproc.cc
+++ b/winsup/cygwin/sigproc.cc
@@ -127,9 +127,9 @@ static bool NO_COPY pending_signals = false;	// true if signals pending
 
 /* Functions
  */
-static int __stdcall checkstate (waitq *);
+static int __stdcall checkstate (waitq *) __attribute__ ((regparm (1)));
 static __inline__ BOOL get_proc_lock (DWORD, DWORD);
-static HANDLE __stdcall getsem (_pinfo *, const char *, int, int);
+static HANDLE __stdcall getevent (_pinfo *, const char *) __attribute__ ((regparm (2)));
 static void __stdcall remove_zombie (int);
 static DWORD WINAPI wait_sig (VOID *arg);
 static int __stdcall stopped_or_terminated (waitq *, _pinfo *);
@@ -679,18 +679,21 @@ sig_send (_pinfo *p, int sig, DWORD ebp, bool exception)
   sigproc_printf ("pid %d, signal %d, its_me %d", p->pid, sig, its_me);
 
   LONG *todo;
+  bool issem;
   if (its_me)
     {
       if (!wait_for_completion)
 	{
 	  thiscatch = sigcatch_nosync;
 	  todo = myself->getsigtodo (sig);
+	  issem = false;
 	}
       else if (tid != mainthread.id)
 	{
 	  thiscatch = sigcatch_nonmain;
 	  thiscomplete = sigcomplete_nonmain;
 	  todo = getlocal_sigtodo (sig);
+	  issem = true;
 	}
       else
 	{
@@ -698,12 +701,16 @@ sig_send (_pinfo *p, int sig, DWORD ebp, bool exception)
 	  thiscomplete = sigcomplete_main;
 	  thisframe.set (mainthread, ebp, exception);
 	  todo = getlocal_sigtodo (sig);
+	  issem = true;
 	}
     }
-  else if ((thiscatch = getsem (p, "sigcatch", 0, 0)))
-    todo = p->getsigtodo (sig);
+  else if ((thiscatch = getevent (p, "sigcatch")))
+    {
+      todo = p->getsigtodo (sig);
+      issem = false;
+    }
   else
-    goto out;		  // Couldn't get the semaphore.  getsem issued
+    goto out;		  // Couldn't get the semaphore.  getevent issued
 			  //  an error, if appropriate.
 
 #if WHEN_MULTI_THREAD_SIGNALS_WORK
@@ -719,15 +726,7 @@ sig_send (_pinfo *p, int sig, DWORD ebp, bool exception)
 
   /* Notify the process that a signal has arrived.
    */
-  SetLastError (0);
-
-#if 0
-  int prio;
-  prio = GetThreadPriority (GetCurrentThread ());
-  (void) SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL);
-#endif
-
-  if (!ReleaseSemaphore (thiscatch, 1, NULL) && (int) GetLastError () > 0)
+  if (issem ? !ReleaseSemaphore (thiscatch, 1, NULL) : !SetEvent (thiscatch))
     {
       /* Couldn't signal the semaphore.  This probably means that the
        * process is exiting.
@@ -764,24 +763,9 @@ sig_send (_pinfo *p, int sig, DWORD ebp, bool exception)
   else
     {
       sigproc_printf ("Waiting for thiscomplete %p", thiscomplete);
-
-      SetLastError (0);
       rc = WaitForSingleObject (thiscomplete, WSSC);
-#if 0 // STILL NEEDED?
-      /* Check for strangeness due to this thread being redirected by the
-	 signal handler.  Sometimes a WAIT_TIMEOUT will occur when the
-	 thread hasn't really timed out.  So, check again.
-	 FIXME: This isn't foolproof. */
-      if (rc != WAIT_OBJECT_0 &&
-	  WaitForSingleObject (thiscomplete, 0) == WAIT_OBJECT_0)
-	rc = WAIT_OBJECT_0;
-#endif
     }
 
-#if 0
-  SetThreadPriority (GetCurrentThread (), prio);
-#endif
-
   if (rc == WAIT_OBJECT_0)
     rc = 0;		// Successful exit
   else
@@ -892,7 +876,7 @@ out:
 /* Get or create a process specific semaphore used in message passing.
  */
 static HANDLE __stdcall
-getsem (_pinfo *p, const char *str, int init, int max)
+getevent (_pinfo *p, const char *str)
 {
   HANDLE h;
   char sem_name[MAX_PATH];
@@ -912,14 +896,18 @@ getsem (_pinfo *p, const char *str, int init, int max)
 	low_priority_sleep (1);
     }
 
-  SetLastError (0);
   if (p == NULL)
     {
       char sa_buf[1024];
 
       DWORD winpid = GetCurrentProcessId ();
+#if 0
       h = CreateSemaphore (sec_user_nih (sa_buf), init, max,
 			   str = shared_name (sem_name, str, winpid));
+#else
+      h = CreateEvent (sec_user_nih (sa_buf), FALSE, FALSE,
+		       str = shared_name (sem_name, str, winpid));
+#endif
       p = myself;
       if (!h)
 	{
@@ -929,8 +917,13 @@ getsem (_pinfo *p, const char *str, int init, int max)
     }
   else
     {
+#if 0
       h = OpenSemaphore (SEMAPHORE_ALL_ACCESS, FALSE,
 			 shared_name (sem_name, str, p->dwProcessId));
+#else
+      h = OpenEvent (EVENT_ALL_ACCESS, FALSE,
+		     shared_name (sem_name, str, p->dwProcessId));
+#endif
 
       if (!h)
 	{
@@ -1075,7 +1068,7 @@ wait_sig (VOID *self)
    * sigcomplete_nonmain   - semaphore signaled for non-main thread on signal
    *			     completion
    */
-  sigcatch_nosync = getsem (NULL, "sigcatch", 0, MAXLONG);
+  sigcatch_nosync = getevent (NULL, "sigcatch");
   sigcatch_nonmain = CreateSemaphore (&sec_none_nih, 0, MAXLONG, NULL);
   sigcatch_main = CreateSemaphore (&sec_none_nih, 0, MAXLONG, NULL);
   sigcomplete_nonmain = CreateSemaphore (&sec_none_nih, 0, MAXLONG, NULL);
@@ -1198,7 +1191,7 @@ wait_sig (VOID *self)
 			  if (rc == RC_MAIN)
 			    {
 			      flush = true;
-			      ReleaseSemaphore (sigcatch_nosync, 1, NULL);
+			      SetEvent (sigcatch_nosync);
 			      goto out1;
 			    }
 			  break;
@@ -1257,7 +1250,7 @@ wait_sig (VOID *self)
     out1:
       if (saw_failed_interrupt)
 	{
-	  ReleaseSemaphore (sigcatch_nosync, 1, NULL);
+	  SetEvent (sigcatch_nosync);
 	  low_priority_sleep (0);	/* Hopefully, other thread will be waking up soon. */
 	}
       sigproc_printf ("looping");
diff --git a/winsup/cygwin/sigproc.h b/winsup/cygwin/sigproc.h
index d3061bc50..5cedaeae3 100644
--- a/winsup/cygwin/sigproc.h
+++ b/winsup/cygwin/sigproc.h
@@ -101,12 +101,12 @@ BOOL __stdcall my_parent_is_alive ();
 extern "C" int __stdcall sig_dispatch_pending ();
 extern "C" void __stdcall set_process_mask (sigset_t newmask);
 extern "C" void __stdcall reset_signal_arrived ();
-int __stdcall sig_handle (int);
-void __stdcall sig_clear (int);
-void __stdcall sig_set_pending (int);
+int __stdcall sig_handle (int) __attribute__ ((regparm (1)));
+void __stdcall sig_clear (int) __attribute__ ((regparm (1)));
+void __stdcall sig_set_pending (int) __attribute__ ((regparm (1)));
 int __stdcall handle_sigsuspend (sigset_t);
 
-int __stdcall proc_subproc (DWORD, DWORD);
+int __stdcall proc_subproc (DWORD, DWORD) __attribute__ ((regparm (2)));
 
 class _pinfo;
 void __stdcall proc_terminate ();