-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 $
*
* 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
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);
{
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
-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 $
*
* 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.
*
#define sleep(N) DosSleep(((N) * 100))
#endif
-#ifdef FEATURE_PTHREAD
+#ifdef MUTEX_LOCKS_AVAILABLE
/*
* XXX: Does the locking stuff really belong in this file?
*/
privoxy_mutex_t rand_mutex;
#endif /* ndef HAVE_RANDOM */
-#endif /* FEATURE_PTHREAD */
+#endif /* def MUTEX_LOCKS_AVAILABLE */
#if defined(unix)
const char *basedir = NULL;
#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)
{
}
exit(1);
}
+#else
+ EnterCriticalSection(mutex);
+#endif /* def FEATURE_PTHREAD */
}
*
* 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)
{
}
exit(1);
}
+#else
+ LeaveCriticalSection(mutex);
+#endif /* def FEATURE_PTHREAD */
}
*
* 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)
{
strerror(err));
exit(1);
}
-}
+#else
+ InitializeCriticalSection(mutex);
#endif /* def FEATURE_PTHREAD */
-
+}
+#endif /* def MUTEX_LOCKS_AVAILABLE */
/*********************************************************************
*
*********************************************************************/
static void initialize_mutexes(void)
{
-#ifdef FEATURE_PTHREAD
+#ifdef MUTEX_LOCKS_AVAILABLE
/*
* Prepare global mutex semaphores
*/
#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 */
}
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 */
#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 $
*
* 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
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;
+#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);
-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 $
*
* 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.
#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) */