[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r20077: {torvm} Add initial thread support back into build and wrap non-reen (torvm/trunk/build/win32/src/torvm-w32)



Author: coderman
Date: 2009-07-18 02:07:13 -0400 (Sat, 18 Jul 2009)
New Revision: 20077

Modified:
   torvm/trunk/build/win32/src/torvm-w32/Makefile
   torvm/trunk/build/win32/src/torvm-w32/thr.c
   torvm/trunk/build/win32/src/torvm-w32/thr.h
   torvm/trunk/build/win32/src/torvm-w32/torvm.c
Log:
Add initial thread support back into build and wrap non-reentrant logging functions with a critical section.

Modified: torvm/trunk/build/win32/src/torvm-w32/Makefile
===================================================================
--- torvm/trunk/build/win32/src/torvm-w32/Makefile	2009-07-17 19:13:55 UTC (rev 20076)
+++ torvm/trunk/build/win32/src/torvm-w32/Makefile	2009-07-18 06:07:13 UTC (rev 20077)
@@ -5,7 +5,6 @@
 BINDIR=C:\Tor_VM
 CPPFLAGS += -I. -I/usr/include -I/usr/local/include -MMD -MP
 CPPFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
-#LDFLAGS += -L/usr/lib -L/usr/local/lib
 LIBS += -lws2_32 -liphlpapi
 
 all: torvm$(EXESUF)

Modified: torvm/trunk/build/win32/src/torvm-w32/thr.c
===================================================================
--- torvm/trunk/build/win32/src/torvm-w32/thr.c	2009-07-17 19:13:55 UTC (rev 20076)
+++ torvm/trunk/build/win32/src/torvm-w32/thr.c	2009-07-18 06:07:13 UTC (rev 20077)
@@ -1,6 +1,5 @@
 #include "torvm.h"
-/*XXX ignore threading until bundle merge completed so buildbot is happy now.*/
-#if 0
+
 /* Some statics to keep track of things...
  * XXX: note that this is inherently unaware of a thread handle
  * allocated by an external process with privs in the Tor VM process.
@@ -12,26 +11,39 @@
 
 BOOL  createcs (LPCRITICAL_SECTION cs)
 {
-   /* The high bit is set to pre-allocate any necessary resources so that
-    * a low memory condition does introduce an exception leading to ugly
-    * failure recovery...
-    */
-   if (!InitializeCriticalSectionAndSpinCount(&CriticalSection, 0x80000400) ) return FALSE;
-   return TRUE;
+  /* The high bit is set to pre-allocate any necessary resources so that
+   * a low memory condition does introduce an exception leading to ugly
+   * failure recovery...
+   */
+  if (!InitializeCriticalSectionAndSpinCount(cs, 0x80000400))
+    return FALSE;
+  return TRUE;
 }
 
 BOOL  destroycs (LPCRITICAL_SECTION cs)
 {
+  if (!cs) {
+    return FALSE;
+  }
+  DeleteCriticalSection(cs);
   return TRUE;
 }
 
 BOOL  entercs (LPCRITICAL_SECTION cs)
 {
+  if (!cs) {
+    return FALSE;
+  }
+  EnterCriticalSection(cs);
   return TRUE;
 }
 
 BOOL  leavecs (LPCRITICAL_SECTION cs)
 {
+  if (!cs) {
+    return FALSE;
+  }
+  LeaveCriticalSection(cs);
   return TRUE;
 }
 
@@ -69,14 +81,12 @@
                  BOOL     startsignaled)
 {
   DWORD icount = 0;
-  if (limit > MAX_SEM_COUNT) limit = MAX_SEM_COUNT;
+  if (limit > MAXSEMCOUNT) limit = MAXSEMCOUNT;
   if (startsignaled == TRUE) icount = limit;
-  *semptr = CreateSemaphore( 
-              0,              // default security attributes
-              icount,         // initial count
-              limit,          // maximum count
-              0               // unnamed semaphore
-            );
+  *semptr = CreateSemaphore(0,
+                            icount,
+                            limit,
+                            0);
   return TRUE;
 }
 
@@ -102,6 +112,7 @@
 }
 
 BOOL  createthr (PFnThreadMain  thrmain,
+                 LPVOID         arg,
                  LPDWORD        thrid,
                  BOOL           suspended)
 {
@@ -110,13 +121,12 @@
   DWORD cflags = 0;
   HANDLE newthr;
   if (suspended) cflags |= CREATE_SUSPENDED;
-  newthr = CreateThread(
-             NULL,    // default security attributes
-             0,       // use default stack size
-             f,
-             arg,
-             cflags,
-             &tid);
+  newthr = CreateThread(NULL,
+                        0,
+                        f,
+                        arg,
+                        cflags,
+                        &tid);
   return TRUE;
 }
 
@@ -137,7 +147,7 @@
 
 VOID  exitthr (DWORD exitcode)
 {
-  return TRUE;
+  return;
 }
  
 BOOL  checkthr (HANDLE thr,
@@ -188,4 +198,3 @@
 {
   return;
 }
-#endif /* XXX end if 0 */

Modified: torvm/trunk/build/win32/src/torvm-w32/thr.h
===================================================================
--- torvm/trunk/build/win32/src/torvm-w32/thr.h	2009-07-17 19:13:55 UTC (rev 20076)
+++ torvm/trunk/build/win32/src/torvm-w32/thr.h	2009-07-18 06:07:13 UTC (rev 20077)
@@ -5,10 +5,9 @@
 #define __thr_h__
 
 #include "torvm.h"
-/*XXX ignore threading until bundle merge completed so buildbot is happy now.*/
-#if 0
+
 /* XXX: these should probably be macros or inline but for now the
- * strack frames are useful for debugging.
+ * stack frames are useful for debugging.
  */
 /* Critical section primitives. */
 BOOL  createcs (LPCRITICAL_SECTION cs);
@@ -25,6 +24,9 @@
 BOOL  unlock (HANDLE lock);
 
 /* Semaphore signalling primitives. */
+#ifndef MAXSEMCOUNT
+#define MAXSEMCOUNT 32
+#endif
 BOOL  createsem (LPHANDLE semptr,
                  LONG     limit,
                  BOOL     startsignaled);
@@ -37,6 +39,7 @@
 /* Thread primitives. */
 typedef DWORD (__stdcall *PFnThreadMain)(LPVOID param);
 BOOL  createthr (PFnThreadMain  thrmain,
+                 LPVOID         arg,
                  LPDWORD        thrid,
                  BOOL           suspended);
 BOOL  destroythr (HANDLE thr);
@@ -90,5 +93,4 @@
  */
 BOOL  enumthrhnds (LPHANDLE *hndlist);
 VOID  destroythrhnds (LPHANDLE hndlist);
-#endif /* XXX end if 0 */
 #endif /* thr_h */

Modified: torvm/trunk/build/win32/src/torvm-w32/torvm.c
===================================================================
--- torvm/trunk/build/win32/src/torvm-w32/torvm.c	2009-07-17 19:13:55 UTC (rev 20076)
+++ torvm/trunk/build/win32/src/torvm-w32/torvm.c	2009-07-18 06:07:13 UTC (rev 20077)
@@ -44,12 +44,21 @@
  *   ldebug to debug file
  *   fatal logs error and then exits process
  */
+static LPCRITICAL_SECTION  s_logcs = NULL;
 static HANDLE  s_logh = INVALID_HANDLE_VALUE;
 static HANDLE  s_dbgh = INVALID_HANDLE_VALUE;
 
+void loginit (void) {
+  if (!s_logcs) {
+    s_logcs = malloc(sizeof(CRITICAL_SECTION));
+    createcs(s_logcs);
+  }
+}
 
 void logto (LPTSTR  path)
 {
+  loginit();
+  entercs(s_logcs);
   if (s_logh != INVALID_HANDLE_VALUE) {
     CloseHandle (s_logh);
   }
@@ -79,14 +88,24 @@
   SYSTEMTIME        now;
   va_list           ap;
 
+  /* XXX: This will block all other threads trying to log while waiting for
+   * file I/O. To prevent badness when writes block a log writer thread
+   * dedicated to disk I/O should be used and all other logging appends to
+   * queue and unblocks.
+   *   (For example, write to stalled SMB/USB/etc file system.)
+   */
+  loginit();
+  entercs(s_logcs);
+
   if ( (fda == INVALID_HANDLE_VALUE) &&
        (fdb == INVALID_HANDLE_VALUE) &&
        (fdc == INVALID_HANDLE_VALUE)   )
-    return;
+    goto finished;
 
   if (msgbuf == NULL) {
     msgbuf = malloc (msgmax);
-    if (!msgbuf) return;
+    if (!msgbuf) 
+      goto finished;
   }
   GetSystemTime (&now);
   coff = msgbuf;
@@ -135,6 +154,9 @@
     WriteFile (fdc, newline, strlen(newline), &written, NULL);
     FlushFileBuffers (fdc);
   }
+
+ finished:
+  leavecs(s_logcs);
   return;
 }
 
@@ -185,6 +207,9 @@
 
 void debugto (LPTSTR  path)
 { 
+  loginit();
+  entercs(s_logcs);
+
   if (s_dbgh != INVALID_HANDLE_VALUE) {
     CloseHandle (s_dbgh);
   }
@@ -195,6 +220,7 @@
                        CREATE_ALWAYS,   
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
+  leavecs(s_logcs);
 }