Index: libraries/nacl-mounts/base/PthreadHelpers.h |
=================================================================== |
--- libraries/nacl-mounts/base/PthreadHelpers.h (revision 0) |
+++ libraries/nacl-mounts/base/PthreadHelpers.h (revision 0) |
@@ -0,0 +1,134 @@ |
+/* |
+ * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+#ifndef PTHREAD_HELPERS_H |
+#define PTHREAD_HELPERS_H |
+ |
+#include <assert.h> |
+#include <pthread.h> |
+#include <stdint.h> |
+ |
+// A macro to disallow the evil copy constructor and operator= functions |
+// This should be used in the private: declarations for a class |
+#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
+ TypeName(const TypeName&); \ |
+ void operator=(const TypeName&) |
+ |
+// A macro to disallow all the implicit constructors, namely the |
+// default constructor, copy constructor and operator= functions. |
+// |
+// This should be used in the private: declarations for a class |
+// that wants to prevent anyone from instantiating it. This is |
+// especially useful for classes containing only static methods. |
+#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
+ TypeName(); \ |
+ DISALLOW_COPY_AND_ASSIGN(TypeName) |
+ |
+class Mutex { |
Dmitry Polukhin
2012/05/31 14:06:45
SimpleAutoLock is analog used in nacl-mounts. I th
Evgeniy Stepanov
2012/06/01 07:59:23
I second that. Let's start with replacing Mutex::L
vissi
2012/06/01 09:12:57
Can we leave Cond?
vissi
2012/06/01 09:12:57
Mutex::Lock has been replaced.
|
+ public: |
+ Mutex() { |
+ pthread_mutexattr_t attrs; |
+ int result = pthread_mutexattr_init(&attrs); |
+ assert(result == 0); |
+ result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE); |
+ assert(result == 0); |
+ result = pthread_mutex_init(&mutex_, &attrs); |
+ assert(result == 0); |
+ pthread_mutexattr_destroy(&attrs); |
+ } |
+ |
+ ~Mutex() { |
+ pthread_mutex_destroy(&mutex_); |
+ } |
+ |
+ pthread_mutex_t* get() { |
+ return &mutex_; |
+ } |
+ |
+ class Lock { |
+ public: |
+ explicit Lock(Mutex& mutex) : mutex_(mutex) { |
+ pthread_mutex_lock(mutex_.get()); |
+ } |
+ |
+ ~Lock() { |
+ pthread_mutex_unlock(mutex_.get()); |
+ } |
+ |
+ private: |
+ Mutex& mutex_; |
+ DISALLOW_COPY_AND_ASSIGN(Lock); |
+ }; |
+ |
+ private: |
+ pthread_mutex_t mutex_; |
+ DISALLOW_COPY_AND_ASSIGN(Mutex); |
+}; |
+ |
+class Cond { |
+ public: |
+ Cond() { |
+ pthread_cond_init(&cond_, NULL); |
+ } |
+ |
+ ~Cond() { |
+ pthread_cond_destroy(&cond_); |
+ } |
+ |
+ pthread_cond_t* get() { |
+ return &cond_; |
+ } |
+ |
+ void broadcast() { |
+ pthread_cond_broadcast(&cond_); |
+ } |
+ |
+ void signal() { |
+ pthread_cond_signal(&cond_); |
+ } |
+ |
+ int wait(Mutex& mutex) { |
+ return pthread_cond_wait(&cond_, mutex.get()); |
+ } |
+ |
+ int timedwait(Mutex& mutex, const timespec* abstime) { |
+ return pthread_cond_timedwait(&cond_, mutex.get(), abstime); |
+ } |
+ |
+ private: |
+ mutable pthread_cond_t cond_; |
+ DISALLOW_COPY_AND_ASSIGN(Cond); |
+}; |
+ |
+class ThreadSafeRefCount { |
+ public: |
+ ThreadSafeRefCount() |
+ : ref_(0) { |
+ } |
+ |
+ int32_t AddRef() { |
+ __sync_fetch_and_add(&ref_, 1); |
+ return ref_; |
+ } |
+ |
+ int32_t Release() { |
+ __sync_fetch_and_sub(&ref_, 1); |
+ return ref_; |
+ } |
+ |
+ private: |
+ int32_t ref_; |
+ DISALLOW_COPY_AND_ASSIGN(ThreadSafeRefCount); |
+}; |
+ |
+#ifndef NDEBUG |
+#define LOG(format, args...) \ |
+ dbgprintf(format , ## args) |
+#else |
+#define LOG(format, args...) |
+#endif |
+ |
+#endif // PTHREAD_HELPERS_H |
+ |