diff options
Diffstat (limited to 'JavaScriptCore/wtf/TCSpinLock.h')
| -rw-r--r-- | JavaScriptCore/wtf/TCSpinLock.h | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/JavaScriptCore/wtf/TCSpinLock.h b/JavaScriptCore/wtf/TCSpinLock.h index 8a73e13..81b7d0c 100644 --- a/JavaScriptCore/wtf/TCSpinLock.h +++ b/JavaScriptCore/wtf/TCSpinLock.h @@ -1,4 +1,5 @@ // Copyright (c) 2005, 2006, Google Inc. +// Copyright (c) 2010, Patrick Gansterer <paroga@paroga.com> // All rights reserved. // // Redistribution and use in source and binary forms, with or without @@ -37,8 +38,6 @@ #include <time.h> /* For nanosleep() */ -#include <sched.h> /* For sched_yield() */ - #if HAVE(STDINT_H) #include <stdint.h> #elif HAVE(INTTYPES_H) @@ -52,6 +51,8 @@ #define WIN32_LEAN_AND_MEAN #endif #include <windows.h> +#else +#include <sched.h> /* For sched_yield() */ #endif static void TCMalloc_SlowLock(volatile unsigned int* lockword); @@ -134,7 +135,12 @@ struct TCMalloc_SpinLock { #define SPINLOCK_INITIALIZER { 0 } static void TCMalloc_SlowLock(volatile unsigned int* lockword) { - sched_yield(); // Yield immediately since fast path failed +// Yield immediately since fast path failed +#if OS(WINDOWS) + Sleep(0); +#else + sched_yield(); +#endif while (true) { int r; #if COMPILER(GCC) @@ -189,6 +195,44 @@ static void TCMalloc_SlowLock(volatile unsigned int* lockword) { } } +#elif OS(WINDOWS) + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include <windows.h> + +static void TCMalloc_SlowLock(LPLONG lockword); + +// The following is a struct so that it can be initialized at compile time +struct TCMalloc_SpinLock { + + inline void Lock() { + if (InterlockedExchange(&m_lockword, 1)) + TCMalloc_SlowLock(&m_lockword); + } + + inline void Unlock() { + InterlockedExchange(&m_lockword, 0); + } + + inline bool IsHeld() const { + return m_lockword != 0; + } + + inline void Init() { m_lockword = 0; } + + LONG m_lockword; +}; + +#define SPINLOCK_INITIALIZER { 0 } + +static void TCMalloc_SlowLock(LPLONG lockword) { + Sleep(0); // Yield immediately since fast path failed + while (InterlockedExchange(lockword, 1)) + Sleep(2); +} + #else #include <pthread.h> |
