| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_SYNC_UTIL_PTHREAD_HELPERS_H_ | |
| 6 #define CHROME_BROWSER_SYNC_UTIL_PTHREAD_HELPERS_H_ | |
| 7 | |
| 8 #include <pthread.h> | |
| 9 #include "base/logging.h" | |
| 10 #include "build/build_config.h" | |
| 11 | |
| 12 #ifdef OS_WIN | |
| 13 typedef void* thread_handle; | |
| 14 #else | |
| 15 typedef pthread_t thread_handle; | |
| 16 #endif | |
| 17 | |
| 18 // Creates a pthread, detaches from it, and returns a Win32 HANDLE for it that | |
| 19 // the caller must CloseHandle(). | |
| 20 thread_handle CreatePThread(void* (*start)(void* payload), void* parameter); | |
| 21 | |
| 22 template <typename LockType> | |
| 23 class PThreadScopedLock { | |
| 24 public: | |
| 25 explicit inline PThreadScopedLock(LockType* lock) : lock_(lock) { | |
| 26 if (lock_) | |
| 27 lock->Lock(); | |
| 28 } | |
| 29 inline ~PThreadScopedLock() { | |
| 30 Unlock(); | |
| 31 } | |
| 32 inline void Unlock() { | |
| 33 if (lock_) { | |
| 34 lock_->Unlock(); | |
| 35 lock_ = NULL; | |
| 36 } | |
| 37 } | |
| 38 LockType* lock_; | |
| 39 | |
| 40 private: | |
| 41 DISALLOW_COPY_AND_ASSIGN(PThreadScopedLock); | |
| 42 }; | |
| 43 | |
| 44 class PThreadNoLock { | |
| 45 public: | |
| 46 inline void Lock() { } | |
| 47 inline void Unlock() { } | |
| 48 }; | |
| 49 | |
| 50 // On win32, the pthread mutex implementation is about as efficient a critical | |
| 51 // section. It uses atomic operations and only needs kernel calls on | |
| 52 // contention. | |
| 53 class PThreadMutex { | |
| 54 public: | |
| 55 inline PThreadMutex() { | |
| 56 pthread_mutexattr_t* attributes = NULL; | |
| 57 #ifndef NDEBUG | |
| 58 private_attributes_in_use_ = true; | |
| 59 pthread_mutexattr_init(&mutex_attributes_); | |
| 60 pthread_mutexattr_settype(&mutex_attributes_, PTHREAD_MUTEX_ERRORCHECK); | |
| 61 attributes = &mutex_attributes_; | |
| 62 #endif | |
| 63 int result = pthread_mutex_init(&mutex_, attributes); | |
| 64 DCHECK_EQ(0, result); | |
| 65 } | |
| 66 inline explicit PThreadMutex(const pthread_mutexattr_t* attr) { | |
| 67 #ifndef NDEBUG | |
| 68 private_attributes_in_use_ = false; | |
| 69 #endif | |
| 70 int result = pthread_mutex_init(&mutex_, attr); | |
| 71 DCHECK_EQ(0, result); | |
| 72 } | |
| 73 inline ~PThreadMutex() { | |
| 74 int result = pthread_mutex_destroy(&mutex_); | |
| 75 DCHECK_EQ(0, result); | |
| 76 #ifndef NDEBUG | |
| 77 if (private_attributes_in_use_) { | |
| 78 pthread_mutexattr_destroy(&mutex_attributes_); | |
| 79 } | |
| 80 #endif | |
| 81 } | |
| 82 inline void Lock() { | |
| 83 int result = pthread_mutex_lock(&mutex_); | |
| 84 DCHECK_EQ(0, result); | |
| 85 } | |
| 86 inline void Unlock() { | |
| 87 int result = pthread_mutex_unlock(&mutex_); | |
| 88 DCHECK_EQ(0, result); | |
| 89 } | |
| 90 pthread_mutex_t mutex_; | |
| 91 | |
| 92 #ifndef NDEBUG | |
| 93 pthread_mutexattr_t mutex_attributes_; | |
| 94 bool private_attributes_in_use_; | |
| 95 #endif | |
| 96 | |
| 97 private: | |
| 98 DISALLOW_COPY_AND_ASSIGN(PThreadMutex); | |
| 99 }; | |
| 100 | |
| 101 class PThreadCondVar { | |
| 102 public: | |
| 103 inline PThreadCondVar() { | |
| 104 int result = pthread_cond_init(&condvar_, 0); | |
| 105 DCHECK_EQ(0, result); | |
| 106 } | |
| 107 ~PThreadCondVar() { | |
| 108 int result = pthread_cond_destroy(&condvar_); | |
| 109 DCHECK_EQ(0, result); | |
| 110 } | |
| 111 inline void Signal() { | |
| 112 int result = pthread_cond_signal(&condvar_); | |
| 113 DCHECK_EQ(0, result); | |
| 114 } | |
| 115 inline void Broadcast() { | |
| 116 int result = pthread_cond_broadcast(&condvar_); | |
| 117 DCHECK_EQ(0, result); | |
| 118 } | |
| 119 pthread_cond_t condvar_; | |
| 120 | |
| 121 private: | |
| 122 DISALLOW_COPY_AND_ASSIGN(PThreadCondVar); | |
| 123 }; | |
| 124 | |
| 125 // Returns the absolute time ms milliseconds from now. Useful for passing | |
| 126 // result to pthread_cond_timedwait(). | |
| 127 struct timespec GetPThreadAbsoluteTime(uint32 ms_from_now); | |
| 128 | |
| 129 // Assign a descriptive label to the current thread. This is useful to see | |
| 130 // in a GUI debugger, but may not be implementable on all platforms. | |
| 131 void NameCurrentThreadForDebugging(const char* name); | |
| 132 | |
| 133 #endif // CHROME_BROWSER_SYNC_UTIL_PTHREAD_HELPERS_H_ | |
| OLD | NEW |