Add mutex lock support for _WIN32.
authorFabian Keil <fk@fabiankeil.de>
Sun, 7 Sep 2008 12:35:05 +0000 (12:35 +0000)
committerFabian Keil <fk@fabiankeil.de>
Sun, 7 Sep 2008 12:35:05 +0000 (12:35 +0000)
errlog.c
jcc.c
jcc.h
miscutil.c

index d9f72ed..34bf348 100644 (file)
--- a/errlog.c
+++ b/errlog.c
@@ -1,4 +1,4 @@
-const char errlog_rcs[] = "$Id: errlog.c,v 1.74 2008/08/06 18:33:36 fabiankeil Exp $";
+const char errlog_rcs[] = "$Id: errlog.c,v 1.75 2008/09/04 08:13:58 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/errlog.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/errlog.c,v $
@@ -33,6 +33,10 @@ const char errlog_rcs[] = "$Id: errlog.c,v 1.74 2008/08/06 18:33:36 fabiankeil E
  *
  * Revisions   :
  *    $Log: errlog.c,v $
  *
  * Revisions   :
  *    $Log: errlog.c,v $
+ *    Revision 1.75  2008/09/04 08:13:58  fabiankeil
+ *    Prepare for critical sections on Windows by adding a
+ *    layer of indirection before the pthread mutex functions.
+ *
  *    Revision 1.74  2008/08/06 18:33:36  fabiankeil
  *    If the "close fd first" workaround doesn't work,
  *    the fatal error message will be lost, so we better
  *    Revision 1.74  2008/08/06 18:33:36  fabiankeil
  *    If the "close fd first" workaround doesn't work,
  *    the fatal error message will be lost, so we better
@@ -436,7 +440,7 @@ static char *w32_socket_strerr(int errcode, char *tmp_buf);
 static char *os2_socket_strerr(int errcode, char *tmp_buf);
 #endif
 
 static char *os2_socket_strerr(int errcode, char *tmp_buf);
 #endif
 
-#ifdef FEATURE_PTHREAD
+#ifdef MUTEX_LOCKS_AVAILABLE
 static inline void lock_logfile(void)
 {
    privoxy_mutex_lock(&log_mutex);
 static inline void lock_logfile(void)
 {
    privoxy_mutex_lock(&log_mutex);
@@ -453,7 +457,7 @@ static inline void unlock_loginit(void)
 {
    privoxy_mutex_unlock(&log_init_mutex);
 }
 {
    privoxy_mutex_unlock(&log_init_mutex);
 }
-#else /* ! FEATURE_PTHREAD */
+#else /* ! MUTEX_LOCKS_AVAILABLE */
 /*
  * FIXME we need a cross-platform locking mechanism.
  * The locking/unlocking functions below should be 
 /*
  * FIXME we need a cross-platform locking mechanism.
  * The locking/unlocking functions below should be 
diff --git a/jcc.c b/jcc.c
index d56c815..2d96ac9 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.185 2008/08/30 12:03:07 fabiankeil Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.186 2008/09/04 08:13:58 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -33,6 +33,10 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.185 2008/08/30 12:03:07 fabiankeil Exp $"
  *
  * Revisions   :
  *    $Log: jcc.c,v $
  *
  * Revisions   :
  *    $Log: jcc.c,v $
+ *    Revision 1.186  2008/09/04 08:13:58  fabiankeil
+ *    Prepare for critical sections on Windows by adding a
+ *    layer of indirection before the pthread mutex functions.
+ *
  *    Revision 1.185  2008/08/30 12:03:07  fabiankeil
  *    Remove FEATURE_COOKIE_JAR.
  *
  *    Revision 1.185  2008/08/30 12:03:07  fabiankeil
  *    Remove FEATURE_COOKIE_JAR.
  *
@@ -1170,7 +1174,7 @@ static int32 server_thread(void *data);
 #define sleep(N)  DosSleep(((N) * 100))
 #endif
 
 #define sleep(N)  DosSleep(((N) * 100))
 #endif
 
-#ifdef FEATURE_PTHREAD
+#ifdef MUTEX_LOCKS_AVAILABLE
 /*
  * XXX: Does the locking stuff really belong in this file?
  */
 /*
  * XXX: Does the locking stuff really belong in this file?
  */
@@ -1193,7 +1197,7 @@ privoxy_mutex_t localtime_mutex;
 privoxy_mutex_t rand_mutex;
 #endif /* ndef HAVE_RANDOM */
 
 privoxy_mutex_t rand_mutex;
 #endif /* ndef HAVE_RANDOM */
 
-#endif /* FEATURE_PTHREAD */
+#endif /* def MUTEX_LOCKS_AVAILABLE */
 
 #if defined(unix)
 const char *basedir = NULL;
 
 #if defined(unix)
 const char *basedir = NULL;
@@ -2958,21 +2962,22 @@ static void usage(const char *myname)
 #endif /* #if !defined(_WIN32) || defined(_WIN_CONSOLE) */
 
 
 #endif /* #if !defined(_WIN32) || defined(_WIN_CONSOLE) */
 
 
-#ifdef FEATURE_PTHREAD
+#ifdef MUTEX_LOCKS_AVAILABLE
 /*********************************************************************
  *
  * Function    :  privoxy_mutex_lock
  *
 /*********************************************************************
  *
  * Function    :  privoxy_mutex_lock
  *
- * Description :  Locks a mutex using pthread_mutex_lock.
+ * Description :  Locks a mutex.
  *
  * Parameters  :
  *          1  :  mutex = The mutex to lock.
  *
  *
  * Parameters  :
  *          1  :  mutex = The mutex to lock.
  *
- * Returns     :  Void, exits in case of errors.
+ * Returns     :  Void. May exit in case of errors.
  *
  *********************************************************************/
 void privoxy_mutex_lock(privoxy_mutex_t *mutex)
 {
  *
  *********************************************************************/
 void privoxy_mutex_lock(privoxy_mutex_t *mutex)
 {
+#ifdef FEATURE_PTHREAD
    int err = pthread_mutex_lock(mutex);
    if (err)
    {
    int err = pthread_mutex_lock(mutex);
    if (err)
    {
@@ -2983,6 +2988,9 @@ void privoxy_mutex_lock(privoxy_mutex_t *mutex)
       }
       exit(1);
    }
       }
       exit(1);
    }
+#else
+   EnterCriticalSection(mutex);
+#endif /* def FEATURE_PTHREAD */
 }
 
 
 }
 
 
@@ -2990,16 +2998,17 @@ void privoxy_mutex_lock(privoxy_mutex_t *mutex)
  *
  * Function    :  privoxy_mutex_unlock
  *
  *
  * Function    :  privoxy_mutex_unlock
  *
- * Description :  Unlocks a mutex using pthread_mutex_unlock.
+ * Description :  Unlocks a mutex.
  *
  * Parameters  :
  *          1  :  mutex = The mutex to unlock.
  *
  *
  * Parameters  :
  *          1  :  mutex = The mutex to unlock.
  *
- * Returns     :  Void, exits in case of errors.
+ * Returns     :  Void. May exit in case of errors.
  *
  *********************************************************************/
 void privoxy_mutex_unlock(privoxy_mutex_t *mutex)
 {
  *
  *********************************************************************/
 void privoxy_mutex_unlock(privoxy_mutex_t *mutex)
 {
+#ifdef FEATURE_PTHREAD
    int err = pthread_mutex_unlock(mutex);
    if (err)
    {
    int err = pthread_mutex_unlock(mutex);
    if (err)
    {
@@ -3010,6 +3019,9 @@ void privoxy_mutex_unlock(privoxy_mutex_t *mutex)
       }
       exit(1);
    }
       }
       exit(1);
    }
+#else
+   LeaveCriticalSection(mutex);
+#endif /* def FEATURE_PTHREAD */
 }
 
 
 }
 
 
@@ -3017,16 +3029,17 @@ void privoxy_mutex_unlock(privoxy_mutex_t *mutex)
  *
  * Function    :  privoxy_mutex_init
  *
  *
  * Function    :  privoxy_mutex_init
  *
- * Description :  Prepares a mutex using pthread_mutex_init.
+ * Description :  Prepares a mutex.
  *
  * Parameters  :
  *          1  :  mutex = The mutex to initialize.
  *
  *
  * Parameters  :
  *          1  :  mutex = The mutex to initialize.
  *
- * Returns     :  Void, exits in case of errors.
+ * Returns     :  Void. May exit in case of errors.
  *
  *********************************************************************/
 static void privoxy_mutex_init(privoxy_mutex_t *mutex)
 {
  *
  *********************************************************************/
 static void privoxy_mutex_init(privoxy_mutex_t *mutex)
 {
+#ifdef FEATURE_PTHREAD
    int err = pthread_mutex_init(mutex, 0);
    if (err)
    {
    int err = pthread_mutex_init(mutex, 0);
    if (err)
    {
@@ -3034,9 +3047,11 @@ static void privoxy_mutex_init(privoxy_mutex_t *mutex)
          strerror(err));
       exit(1);
    }
          strerror(err));
       exit(1);
    }
-}
+#else
+   InitializeCriticalSection(mutex);
 #endif /* def FEATURE_PTHREAD */
 #endif /* def FEATURE_PTHREAD */
-
+}
+#endif /* def MUTEX_LOCKS_AVAILABLE */
 
 /*********************************************************************
  *
 
 /*********************************************************************
  *
@@ -3051,7 +3066,7 @@ static void privoxy_mutex_init(privoxy_mutex_t *mutex)
  *********************************************************************/
 static void initialize_mutexes(void)
 {
  *********************************************************************/
 static void initialize_mutexes(void)
 {
-#ifdef FEATURE_PTHREAD
+#ifdef MUTEX_LOCKS_AVAILABLE
    /*
     * Prepare global mutex semaphores
     */
    /*
     * Prepare global mutex semaphores
     */
@@ -3085,12 +3100,7 @@ static void initialize_mutexes(void)
 #ifndef HAVE_RANDOM
    privoxy_mutex_init(&rand_mutex);
 #endif /* ndef HAVE_RANDOM */
 #ifndef HAVE_RANDOM
    privoxy_mutex_init(&rand_mutex);
 #endif /* ndef HAVE_RANDOM */
-#endif /* FEATURE_PTHREAD */
-
-   /*
-    * TODO: mutex support for mingw32 would be swell.
-    */
-
+#endif /* def MUTEX_LOCKS_AVAILABLE */
 }
 
 
 }
 
 
@@ -3306,15 +3316,6 @@ int main(int argc, const char *argv[])
    random_seed = (unsigned int)time(NULL);
 #ifdef HAVE_RANDOM
    srandom(random_seed);
    random_seed = (unsigned int)time(NULL);
 #ifdef HAVE_RANDOM
    srandom(random_seed);
-#elif defined (_WIN32)
-   /*
-    * See pick_from_range() in miscutil.c for details.
-    */
-   log_error(LOG_LEVEL_INFO,
-      "No thread-safe PRNG implemented for your platform. "
-      "Using weak \'randomization\' factor which will "
-      "limit the already questionable usefulness of "
-      "header-time-randomizing actions (disabled by default).");
 #else
    srand(random_seed);
 #endif /* ifdef HAVE_RANDOM */
 #else
    srand(random_seed);
 #endif /* ifdef HAVE_RANDOM */
diff --git a/jcc.h b/jcc.h
index 03482e8..86e7334 100644 (file)
--- a/jcc.h
+++ b/jcc.h
@@ -1,6 +1,6 @@
 #ifndef JCC_H_INCLUDED
 #define JCC_H_INCLUDED
 #ifndef JCC_H_INCLUDED
 #define JCC_H_INCLUDED
-#define JCC_H_VERSION "$Id: jcc.h,v 1.22 2007/06/01 18:16:36 fabiankeil Exp $"
+#define JCC_H_VERSION "$Id: jcc.h,v 1.23 2008/09/04 08:13:58 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.h,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.h,v $
  *
  * Revisions   :
  *    $Log: jcc.h,v $
  *
  * Revisions   :
  *    $Log: jcc.h,v $
+ *    Revision 1.23  2008/09/04 08:13:58  fabiankeil
+ *    Prepare for critical sections on Windows by adding a
+ *    layer of indirection before the pthread mutex functions.
+ *
  *    Revision 1.22  2007/06/01 18:16:36  fabiankeil
  *    Use the same mutex for gethostbyname() and gethostbyaddr() to prevent
  *    deadlocks and crashes on OpenBSD and possibly other OS with neither
  *    Revision 1.22  2007/06/01 18:16:36  fabiankeil
  *    Use the same mutex for gethostbyname() and gethostbyaddr() to prevent
  *    deadlocks and crashes on OpenBSD and possibly other OS with neither
@@ -186,11 +190,20 @@ extern int no_daemon;
 extern int g_terminate;
 #endif
 
 extern int g_terminate;
 #endif
 
+#if defined(FEATURE_PTHREAD) || defined(_WIN32)
+#define MUTEX_LOCKS_AVAILABLE
+
 #ifdef FEATURE_PTHREAD
 #include <pthread.h>
 
 typedef pthread_mutex_t privoxy_mutex_t;
 
 #ifdef FEATURE_PTHREAD
 #include <pthread.h>
 
 typedef pthread_mutex_t privoxy_mutex_t;
 
+#else
+
+typedef CRITICAL_SECTION privoxy_mutex_t;
+
+#endif
+
 extern void privoxy_mutex_lock(privoxy_mutex_t *mutex);
 extern void privoxy_mutex_unlock(privoxy_mutex_t *mutex);
 
 extern void privoxy_mutex_lock(privoxy_mutex_t *mutex);
 extern void privoxy_mutex_unlock(privoxy_mutex_t *mutex);
 
index 4e5e333..614e09d 100644 (file)
@@ -1,4 +1,4 @@
-const char miscutil_rcs[] = "$Id: miscutil.c,v 1.58 2008/04/17 14:53:30 fabiankeil Exp $";
+const char miscutil_rcs[] = "$Id: miscutil.c,v 1.59 2008/09/04 08:13:58 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/miscutil.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/miscutil.c,v $
@@ -44,6 +44,10 @@ const char miscutil_rcs[] = "$Id: miscutil.c,v 1.58 2008/04/17 14:53:30 fabianke
  *
  * Revisions   :
  *    $Log: miscutil.c,v $
  *
  * Revisions   :
  *    $Log: miscutil.c,v $
+ *    Revision 1.59  2008/09/04 08:13:58  fabiankeil
+ *    Prepare for critical sections on Windows by adding a
+ *    layer of indirection before the pthread mutex functions.
+ *
  *    Revision 1.58  2008/04/17 14:53:30  fabiankeil
  *    Move simplematch() into urlmatch.c as it's only
  *    used to match (old-school) domain patterns.
  *    Revision 1.58  2008/04/17 14:53:30  fabiankeil
  *    Move simplematch() into urlmatch.c as it's only
  *    used to match (old-school) domain patterns.
@@ -972,33 +976,22 @@ long int pick_from_range(long int range)
 
 #ifdef HAVE_RANDOM
    number = random() % range + 1; 
 
 #ifdef HAVE_RANDOM
    number = random() % range + 1; 
-#elif defined(FEATURE_PTHREAD)
+#elif defined(MUTEX_LOCKS_AVAILABLE)
    privoxy_mutex_lock(&rand_mutex);
    privoxy_mutex_lock(&rand_mutex);
+#ifdef _WIN32
+   srand((unsigned)(GetCurrentThreadId()+GetTickCount()));
+#endif /* def _WIN32 */
    number = rand() % (long int)(range + 1);
    privoxy_mutex_unlock(&rand_mutex);
    number = rand() % (long int)(range + 1);
    privoxy_mutex_unlock(&rand_mutex);
-#else
-#ifdef _WIN32
-   /*
-    * On Windows and mingw32 srand() has to be called in every
-    * rand()-using thread, but can cause crashes if it's not
-    * mutex protected.
-    *
-    * Currently we don't have mutexes for mingw32, and for
-    * our purpose this cludge is probably preferable to crashes.
-    *
-    * The warning is shown once on startup from jcc.c.
-    */
-   number = (range + GetCurrentThreadId() % range) / 2;
 #else
    /*
     * XXX: Which platforms reach this and are there
     * better options than just using rand() and hoping
     * that it's safe?
     */
 #else
    /*
     * XXX: Which platforms reach this and are there
     * better options than just using rand() and hoping
     * that it's safe?
     */
-   log_error(LOG_LEVEL_INFO, "No thread-safe PRNG available? Header time randomization might cause "
-      "crashes, predictable results or even combine these fine options.");
+   log_error(LOG_LEVEL_INFO, "No thread-safe PRNG available? Header time randomization "
+      "might cause crashes, predictable results or even combine these fine options.");
    number = rand() % (long int)(range + 1);
    number = rand() % (long int)(range + 1);
-#endif /* def _WIN32 */ 
 
 #endif /* (def HAVE_RANDOM) */
 
 
 #endif /* (def HAVE_RANDOM) */