summaryrefslogtreecommitdiff
path: root/util/concurrency/spin_lock.cpp
diff options
context:
space:
mode:
authorAntonin Kral <a.kral@bobek.cz>2011-03-17 00:05:43 +0100
committerAntonin Kral <a.kral@bobek.cz>2011-03-17 00:05:43 +0100
commit582fc32574a3b158c81e49cb00e6ae59205e66ba (patch)
treeac64a3243e0d2121709f685695247052858115c8 /util/concurrency/spin_lock.cpp
parent2761bffa96595ac1698d86bbc2e95ebb0d4d6e93 (diff)
downloadmongodb-582fc32574a3b158c81e49cb00e6ae59205e66ba.tar.gz
Imported Upstream version 1.8.0
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
}