From 715d32a47773ed1ed2c157e6ff24111a2160842e Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Sun, 7 Sep 2008 12:35:05 +0000 Subject: [PATCH] Add mutex lock support for _WIN32. --- errlog.c | 10 +++++++--- jcc.c | 57 +++++++++++++++++++++++++++--------------------------- jcc.h | 15 +++++++++++++- miscutil.c | 29 +++++++++++---------------- 4 files changed, 61 insertions(+), 50 deletions(-) diff --git a/errlog.c b/errlog.c index d9f72ed0..34bf3489 100644 --- 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 $ @@ -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 $ + * 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 @@ -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 -#ifdef FEATURE_PTHREAD +#ifdef MUTEX_LOCKS_AVAILABLE 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); } -#else /* ! FEATURE_PTHREAD */ +#else /* ! MUTEX_LOCKS_AVAILABLE */ /* * FIXME we need a cross-platform locking mechanism. * The locking/unlocking functions below should be diff --git a/jcc.c b/jcc.c index d56c815f..2d96ac9f 100644 --- 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 $ @@ -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 $ + * 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. * @@ -1170,7 +1174,7 @@ static int32 server_thread(void *data); #define sleep(N) DosSleep(((N) * 100)) #endif -#ifdef FEATURE_PTHREAD +#ifdef MUTEX_LOCKS_AVAILABLE /* * 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 */ -#endif /* FEATURE_PTHREAD */ +#endif /* def MUTEX_LOCKS_AVAILABLE */ #if defined(unix) const char *basedir = NULL; @@ -2958,21 +2962,22 @@ static void usage(const char *myname) #endif /* #if !defined(_WIN32) || defined(_WIN_CONSOLE) */ -#ifdef FEATURE_PTHREAD +#ifdef MUTEX_LOCKS_AVAILABLE /********************************************************************* * * Function : privoxy_mutex_lock * - * Description : Locks a mutex using pthread_mutex_lock. + * Description : Locks a mutex. * * 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) { +#ifdef FEATURE_PTHREAD int err = pthread_mutex_lock(mutex); if (err) { @@ -2983,6 +2988,9 @@ void privoxy_mutex_lock(privoxy_mutex_t *mutex) } 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 * - * Description : Unlocks a mutex using pthread_mutex_unlock. + * Description : Unlocks a mutex. * * 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) { +#ifdef FEATURE_PTHREAD int err = pthread_mutex_unlock(mutex); if (err) { @@ -3010,6 +3019,9 @@ void privoxy_mutex_unlock(privoxy_mutex_t *mutex) } 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 * - * Description : Prepares a mutex using pthread_mutex_init. + * Description : Prepares a mutex. * * 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) { +#ifdef FEATURE_PTHREAD 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); } -} +#else + InitializeCriticalSection(mutex); #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) { -#ifdef FEATURE_PTHREAD +#ifdef MUTEX_LOCKS_AVAILABLE /* * 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 */ -#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); -#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 */ diff --git a/jcc.h b/jcc.h index 03482e89..86e7334d 100644 --- a/jcc.h +++ b/jcc.h @@ -1,6 +1,6 @@ #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 $ @@ -35,6 +35,10 @@ * * 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 @@ -186,11 +190,20 @@ extern int no_daemon; extern int g_terminate; #endif +#if defined(FEATURE_PTHREAD) || defined(_WIN32) +#define MUTEX_LOCKS_AVAILABLE + #ifdef FEATURE_PTHREAD #include 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); diff --git a/miscutil.c b/miscutil.c index 4e5e3330..614e09d5 100644 --- a/miscutil.c +++ b/miscutil.c @@ -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 $ @@ -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 $ + * 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. @@ -972,33 +976,22 @@ long int pick_from_range(long int range) #ifdef HAVE_RANDOM number = random() % range + 1; -#elif defined(FEATURE_PTHREAD) +#elif defined(MUTEX_LOCKS_AVAILABLE) 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); -#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? */ - 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); -#endif /* def _WIN32 */ #endif /* (def HAVE_RANDOM) */ -- 2.39.2