]> granicus.if.org Git - libevent/commitdiff
Construct Windows locks using InitializeCriticalSectionAndSpinCount
authorNick Mathewson <nickm@torproject.org>
Tue, 16 Feb 2010 00:54:15 +0000 (19:54 -0500)
committerNick Mathewson <nickm@torproject.org>
Thu, 18 Feb 2010 05:27:13 +0000 (00:27 -0500)
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
evthread_win32.c

index 0dd4e90474860146884014c4799768ba685017c4..87ac85dd397dc477382696e23a0ef7129b7ca514 100644 (file)
@@ -193,7 +193,7 @@ event_iocp_port_launch(void)
                ++port->n_live_threads;
        }
 
-       InitializeCriticalSection(&port->lock);
+       InitializeCriticalSectionAndSpinCount(&port->lock, 1000);
 
        return port;
 err:
index 995e0e151fa15f3c5c46f74da10b81ef4472516e..655cff837400bb56a8656fe063f0d52aace6c4c1 100644 (file)
@@ -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;
 }