aboutsummaryrefslogtreecommitdiff
path: root/cpp/src/sfntly/port
diff options
context:
space:
mode:
authorarthurhsu <arthurhsu@google.com>2011-09-12 20:07:59 +0000
committerarthurhsu <arthurhsu@google.com>2011-09-12 20:07:59 +0000
commit309eb234315620dcfbb88fa46a4cb0e2a8ab677d (patch)
tree6006779a1c0ca4bb199b6d315b21daf3fcd7311b /cpp/src/sfntly/port
parent28258feb374913d40c347932937500210ba23d7c (diff)
downloadsfntly-309eb234315620dcfbb88fa46a4cb0e2a8ab677d.tar.gz
Add mutex
Diffstat (limited to 'cpp/src/sfntly/port')
-rw-r--r--cpp/src/sfntly/port/lock.cc72
-rw-r--r--cpp/src/sfntly/port/lock.h76
-rw-r--r--cpp/src/sfntly/port/type.h6
3 files changed, 154 insertions, 0 deletions
diff --git a/cpp/src/sfntly/port/lock.cc b/cpp/src/sfntly/port/lock.cc
new file mode 100644
index 0000000..6c0c309
--- /dev/null
+++ b/cpp/src/sfntly/port/lock.cc
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2011 Google Inc. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "sfntly/port/lock.h"
+
+namespace sfntly {
+
+#if defined (WIN32)
+
+Lock::Lock() {
+ // The second parameter is the spin count, for short-held locks it avoid the
+ // contending thread from going to sleep which helps performance greatly.
+ ::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
+}
+
+Lock::~Lock() {
+ ::DeleteCriticalSection(&os_lock_);
+}
+
+bool Lock::Try() {
+ if (::TryEnterCriticalSection(&os_lock_) != FALSE) {
+ return true;
+ }
+ return false;
+}
+
+void Lock::Acquire() {
+ ::EnterCriticalSection(&os_lock_);
+}
+
+void Lock::Unlock() {
+ ::LeaveCriticalSection(&os_lock_);
+}
+
+#else // We assume it's pthread
+
+Lock::Lock() {
+ pthread_mutex_init(&os_lock_, NULL);
+}
+
+Lock::~Lock() {
+ pthread_mutex_destroy(&os_lock_);
+}
+
+bool Lock::Try() {
+ return (pthread_mutex_trylock(&os_lock_) == 0);
+}
+
+void Lock::Acquire() {
+ pthread_mutex_lock(&os_lock_);
+}
+
+void Lock::Unlock() {
+ pthread_mutex_unlock(&os_lock_);
+}
+
+#endif
+
+} // namespace sfntly
diff --git a/cpp/src/sfntly/port/lock.h b/cpp/src/sfntly/port/lock.h
new file mode 100644
index 0000000..b2e29bf
--- /dev/null
+++ b/cpp/src/sfntly/port/lock.h
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2011 Google Inc. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SFNTLY_CPP_SRC_SFNTLY_PORT_LOCK_H_
+#define SFNTLY_CPP_SRC_SFNTLY_PORT_LOCK_H_
+
+#if defined (WIN32)
+#include <windows.h>
+#else // Assume pthread.
+#include <pthread.h>
+#include <errno.h>
+#endif
+
+#include "sfntly/port/type.h"
+
+namespace sfntly {
+
+#if defined (WIN32)
+ typedef CRITICAL_SECTION OSLockType;
+#else // Assume pthread.
+ typedef pthread_mutex_t OSLockType;
+#endif
+
+class Lock {
+ public:
+ Lock();
+ ~Lock();
+
+ // If the lock is not held, take it and return true. If the lock is already
+ // held by something else, immediately return false.
+ bool Try();
+
+ // Take the lock, blocking until it is available if necessary.
+ void Acquire();
+
+ // Release the lock. This must only be called by the lock's holder: after
+ // a successful call to Try, or a call to Lock.
+ void Unlock();
+
+ private:
+ OSLockType os_lock_;
+ NO_COPY_AND_ASSIGN(Lock);
+};
+
+// A helper class that acquires the given Lock while the AutoLock is in scope.
+class AutoLock {
+ public:
+ explicit AutoLock(Lock& lock) : lock_(lock) {
+ lock_.Acquire();
+ }
+
+ ~AutoLock() {
+ lock_.Unlock();
+ }
+
+ private:
+ Lock& lock_;
+ NO_COPY_AND_ASSIGN(AutoLock);
+};
+
+} // namespace sfntly
+
+#endif // SFNTLY_CPP_SRC_SFNTLY_PORT_LOCK_H_
diff --git a/cpp/src/sfntly/port/type.h b/cpp/src/sfntly/port/type.h
index 458a364..20a5ba8 100644
--- a/cpp/src/sfntly/port/type.h
+++ b/cpp/src/sfntly/port/type.h
@@ -56,6 +56,12 @@ typedef std::vector<byte_t> ByteVector;
typedef std::vector<int32_t> IntegerList;
typedef std::set<int32_t> IntegerSet;
+// A macro to disallow the copy constructor and operator= functions.
+// This should be used in the private: declarations for a class.
+#define NO_COPY_AND_ASSIGN(TypeName) \
+ TypeName(const TypeName&); \
+ void operator=(const TypeName&)
+
} // namespace sfntly
// Make google3 happy since it prohibits RTTI.