aboutsummaryrefslogtreecommitdiff
path: root/src/system_wrappers/source/rw_lock_generic.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/system_wrappers/source/rw_lock_generic.cc')
-rw-r--r--src/system_wrappers/source/rw_lock_generic.cc106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/system_wrappers/source/rw_lock_generic.cc b/src/system_wrappers/source/rw_lock_generic.cc
new file mode 100644
index 0000000000..a468ef3b16
--- /dev/null
+++ b/src/system_wrappers/source/rw_lock_generic.cc
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "rw_lock_generic.h"
+
+#include "condition_variable_wrapper.h"
+#include "critical_section_wrapper.h"
+
+namespace webrtc {
+RWLockWrapperGeneric::RWLockWrapperGeneric()
+ : _readersActive(0),
+ _writerActive(false),
+ _readersWaiting(0),
+ _writersWaiting(0)
+{
+ _critSectPtr = CriticalSectionWrapper::CreateCriticalSection();
+ _readCondPtr = ConditionVariableWrapper::CreateConditionVariable();
+ _writeCondPtr = ConditionVariableWrapper::CreateConditionVariable();
+}
+
+RWLockWrapperGeneric::~RWLockWrapperGeneric()
+{
+ delete _writeCondPtr;
+ delete _readCondPtr;
+ delete _critSectPtr;
+}
+
+int RWLockWrapperGeneric::Init()
+{
+ return 0;
+}
+
+void RWLockWrapperGeneric::AcquireLockExclusive()
+{
+ _critSectPtr->Enter();
+
+ if (_writerActive || _readersActive > 0)
+ {
+ ++_writersWaiting;
+
+ while (_writerActive || _readersActive > 0)
+ {
+ _writeCondPtr->SleepCS(*_critSectPtr);
+ }
+
+ --_writersWaiting;
+ }
+ _writerActive = true;
+ _critSectPtr->Leave();
+}
+
+void RWLockWrapperGeneric::ReleaseLockExclusive()
+{
+ _critSectPtr->Enter();
+
+ _writerActive = false;
+
+ if (_writersWaiting > 0)
+ {
+ _writeCondPtr->Wake();
+
+ }else if (_readersWaiting > 0)
+ {
+ _readCondPtr->WakeAll();
+ }
+ _critSectPtr->Leave();
+}
+
+void RWLockWrapperGeneric::AcquireLockShared()
+{
+ _critSectPtr->Enter();
+
+ if (_writerActive || _writersWaiting > 0)
+ {
+ ++_readersWaiting;
+
+ while (_writerActive || _writersWaiting > 0)
+ {
+ _readCondPtr->SleepCS(*_critSectPtr);
+ }
+ --_readersWaiting;
+ }
+ ++_readersActive;
+ _critSectPtr->Leave();
+}
+
+void RWLockWrapperGeneric::ReleaseLockShared()
+{
+ _critSectPtr->Enter();
+
+ --_readersActive;
+
+ if (_readersActive == 0 && _writersWaiting > 0)
+ {
+ _writeCondPtr->Wake();
+ }
+ _critSectPtr->Leave();
+}
+} // namespace webrtc