summaryrefslogtreecommitdiff
path: root/util/concurrency/spin_lock.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'util/concurrency/spin_lock.cpp')
-rw-r--r--util/concurrency/spin_lock.cpp34
1 files changed, 26 insertions, 8 deletions
diff --git a/util/concurrency/spin_lock.cpp b/util/concurrency/spin_lock.cpp
index b3e689a..0f33609 100644
--- a/util/concurrency/spin_lock.cpp
+++ b/util/concurrency/spin_lock.cpp
@@ -16,18 +16,29 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include "pch.h"
#include <time.h>
#include "spin_lock.h"
namespace mongo {
- SpinLock::SpinLock() : _locked( false ){}
-
- SpinLock::~SpinLock(){}
+ SpinLock::~SpinLock() {
+#if defined(_WIN32)
+ DeleteCriticalSection(&_cs);
+#endif
+ }
- void SpinLock::lock(){
+ SpinLock::SpinLock()
#if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
+ : _locked( false ) { }
+#elif defined(_WIN32)
+ { InitializeCriticalSectionAndSpinCount(&_cs, 4000); }
+#else
+ : _mutex( "SpinLock" ) { }
+#endif
+ void SpinLock::lock() {
+#if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
// fast path
if (!_locked && !__sync_lock_test_and_set(&_locked, true)) {
return;
@@ -44,21 +55,28 @@ namespace mongo {
while (__sync_lock_test_and_set(&_locked, true)) {
nanosleep(&t, NULL);
}
+#elif defined(_WIN32)
+ EnterCriticalSection(&_cs);
#else
-
- // WARNING "TODO Missing spin lock in this platform."
+ // WARNING Missing spin lock in this platform. This can potentially
+ // be slow.
+ _mutex.lock();
#endif
}
- void SpinLock::unlock(){
+ void SpinLock::unlock() {
#if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
__sync_lock_release(&_locked);
+#elif defined(WIN32)
+
+ LeaveCriticalSection(&_cs);
+
#else
- // WARNING "TODO Missing spin lock in this platform."
+ _mutex.unlock();
#endif
}