From 32c6f1bacdb40f0d1b57aa6e8b2ce4eaf3979d08 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 15 Feb 2010 19:54:15 -0500 Subject: [PATCH] Construct Windows locks using InitializeCriticalSectionAndSpinCount Previously we were using InitializeCriticalSection, which creates a lock that blocks immediately on contention and waits to be rescheduled. This is inefficient; it's better to wait for a little while before telling the US to reschedule us, in case the lock becomes available again really soon (since most locks mostly do). Good pthreads implementations do this automatically. On Windows, though, we need to call this magic function, and we need to pick the spin count ourselves. --- event_iocp.c | 2 +- evthread_win32.c | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/event_iocp.c b/event_iocp.c index 0dd4e904..87ac85dd 100644 --- a/event_iocp.c +++ b/event_iocp.c @@ -193,7 +193,7 @@ event_iocp_port_launch(void) ++port->n_live_threads; } - InitializeCriticalSection(&port->lock); + InitializeCriticalSectionAndSpinCount(&port->lock, 1000); return port; err: diff --git a/evthread_win32.c b/evthread_win32.c index 995e0e15..655cff83 100644 --- a/evthread_win32.c +++ b/evthread_win32.c @@ -38,13 +38,18 @@ struct event_base; #include "mm-internal.h" +#define SPIN_COUNT 2000 + static void * evthread_win32_lock_create(unsigned locktype) { CRITICAL_SECTION *lock = mm_malloc(sizeof(CRITICAL_SECTION)); if (!lock) return NULL; - InitializeCriticalSection(lock); + if (InitializeCriticalSectionAndSpinCount(lock, SPIN_COUNT) == 0) { + mm_free(lock); + return NULL; + } return lock; } -- 2.50.1