| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) | 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 #endif | 42 #endif |
| 43 | 43 |
| 44 #if OS(POSIX) | 44 #if OS(POSIX) |
| 45 #include <pthread.h> | 45 #include <pthread.h> |
| 46 #endif | 46 #endif |
| 47 | 47 |
| 48 namespace WTF { | 48 namespace WTF { |
| 49 | 49 |
| 50 #if OS(POSIX) | 50 #if OS(POSIX) |
| 51 struct PlatformMutex { | 51 struct PlatformMutex { |
| 52 pthread_mutex_t m_internalMutex; | 52 pthread_mutex_t m_internalMutex; |
| 53 #if ENABLE(ASSERT) | 53 #if ENABLE(ASSERT) |
| 54 size_t m_recursionCount; | 54 size_t m_recursionCount; |
| 55 #endif | 55 #endif |
| 56 }; | 56 }; |
| 57 typedef pthread_cond_t PlatformCondition; | 57 typedef pthread_cond_t PlatformCondition; |
| 58 #elif OS(WIN) | 58 #elif OS(WIN) |
| 59 struct PlatformMutex { | 59 struct PlatformMutex { |
| 60 CRITICAL_SECTION m_internalMutex; | 60 CRITICAL_SECTION m_internalMutex; |
| 61 size_t m_recursionCount; | 61 size_t m_recursionCount; |
| 62 }; | 62 }; |
| 63 struct PlatformCondition { | 63 struct PlatformCondition { |
| 64 size_t m_waitersGone; | 64 size_t m_waitersGone; |
| 65 size_t m_waitersBlocked; | 65 size_t m_waitersBlocked; |
| 66 size_t m_waitersToUnblock; | 66 size_t m_waitersToUnblock; |
| 67 HANDLE m_blockLock; | 67 HANDLE m_blockLock; |
| 68 HANDLE m_blockQueue; | 68 HANDLE m_blockQueue; |
| 69 HANDLE m_unblockLock; | 69 HANDLE m_unblockLock; |
| 70 | 70 |
| 71 bool timedWait(PlatformMutex&, DWORD durationMilliseconds); | 71 bool timedWait(PlatformMutex&, DWORD durationMilliseconds); |
| 72 void signal(bool unblockAll); | 72 void signal(bool unblockAll); |
| 73 }; | 73 }; |
| 74 #else | 74 #else |
| 75 typedef void* PlatformMutex; | 75 typedef void* PlatformMutex; |
| 76 typedef void* PlatformCondition; | 76 typedef void* PlatformCondition; |
| 77 #endif | 77 #endif |
| 78 | 78 |
| 79 class WTF_EXPORT MutexBase { | 79 class WTF_EXPORT MutexBase { |
| 80 WTF_MAKE_NONCOPYABLE(MutexBase); USING_FAST_MALLOC(MutexBase); | 80 WTF_MAKE_NONCOPYABLE(MutexBase); |
| 81 public: | 81 USING_FAST_MALLOC(MutexBase); |
| 82 ~MutexBase(); | |
| 83 | 82 |
| 84 void lock(); | 83 public: |
| 85 void unlock(); | 84 ~MutexBase(); |
| 85 |
| 86 void lock(); |
| 87 void unlock(); |
| 86 #if ENABLE(ASSERT) | 88 #if ENABLE(ASSERT) |
| 87 bool locked() { return m_mutex.m_recursionCount > 0; } | 89 bool locked() { return m_mutex.m_recursionCount > 0; } |
| 88 #endif | 90 #endif |
| 89 | 91 |
| 90 public: | 92 public: |
| 91 PlatformMutex& impl() { return m_mutex; } | 93 PlatformMutex& impl() { return m_mutex; } |
| 92 | 94 |
| 93 protected: | 95 protected: |
| 94 MutexBase(bool recursive); | 96 MutexBase(bool recursive); |
| 95 | 97 |
| 96 PlatformMutex m_mutex; | 98 PlatformMutex m_mutex; |
| 97 }; | 99 }; |
| 98 | 100 |
| 99 class WTF_EXPORT Mutex : public MutexBase { | 101 class WTF_EXPORT Mutex : public MutexBase { |
| 100 public: | 102 public: |
| 101 Mutex() : MutexBase(false) { } | 103 Mutex() |
| 102 bool tryLock(); | 104 : MutexBase(false) {} |
| 105 bool tryLock(); |
| 103 }; | 106 }; |
| 104 | 107 |
| 105 class WTF_EXPORT RecursiveMutex : public MutexBase { | 108 class WTF_EXPORT RecursiveMutex : public MutexBase { |
| 106 public: | 109 public: |
| 107 RecursiveMutex() : MutexBase(true) { } | 110 RecursiveMutex() |
| 108 bool tryLock(); | 111 : MutexBase(true) {} |
| 112 bool tryLock(); |
| 109 }; | 113 }; |
| 110 | 114 |
| 111 typedef Locker<MutexBase> MutexLocker; | 115 typedef Locker<MutexBase> MutexLocker; |
| 112 | 116 |
| 113 class MutexTryLocker { | 117 class MutexTryLocker { |
| 114 WTF_MAKE_NONCOPYABLE(MutexTryLocker); | 118 WTF_MAKE_NONCOPYABLE(MutexTryLocker); |
| 115 public: | |
| 116 MutexTryLocker(Mutex& mutex) : m_mutex(mutex), m_locked(mutex.tryLock()) { } | |
| 117 ~MutexTryLocker() | |
| 118 { | |
| 119 if (m_locked) | |
| 120 m_mutex.unlock(); | |
| 121 } | |
| 122 | 119 |
| 123 bool locked() const { return m_locked; } | 120 public: |
| 121 MutexTryLocker(Mutex& mutex) |
| 122 : m_mutex(mutex), m_locked(mutex.tryLock()) {} |
| 123 ~MutexTryLocker() { |
| 124 if (m_locked) |
| 125 m_mutex.unlock(); |
| 126 } |
| 124 | 127 |
| 125 private: | 128 bool locked() const { return m_locked; } |
| 126 Mutex& m_mutex; | 129 |
| 127 bool m_locked; | 130 private: |
| 131 Mutex& m_mutex; |
| 132 bool m_locked; |
| 128 }; | 133 }; |
| 129 | 134 |
| 130 class WTF_EXPORT ThreadCondition { | 135 class WTF_EXPORT ThreadCondition { |
| 131 WTF_MAKE_NONCOPYABLE(ThreadCondition); | 136 WTF_MAKE_NONCOPYABLE(ThreadCondition); |
| 132 public: | |
| 133 ThreadCondition(); | |
| 134 ~ThreadCondition(); | |
| 135 | 137 |
| 136 void wait(MutexBase&); | 138 public: |
| 137 // Returns true if the condition was signaled before absoluteTime, false if
the absoluteTime was reached or is in the past. | 139 ThreadCondition(); |
| 138 // The absoluteTime is in seconds, starting on January 1, 1970. The time is
assumed to use the same time zone as WTF::currentTime(). | 140 ~ThreadCondition(); |
| 139 bool timedWait(MutexBase&, double absoluteTime); | |
| 140 void signal(); | |
| 141 void broadcast(); | |
| 142 | 141 |
| 143 private: | 142 void wait(MutexBase&); |
| 144 PlatformCondition m_condition; | 143 // Returns true if the condition was signaled before absoluteTime, false if th
e absoluteTime was reached or is in the past. |
| 144 // The absoluteTime is in seconds, starting on January 1, 1970. The time is as
sumed to use the same time zone as WTF::currentTime(). |
| 145 bool timedWait(MutexBase&, double absoluteTime); |
| 146 void signal(); |
| 147 void broadcast(); |
| 148 |
| 149 private: |
| 150 PlatformCondition m_condition; |
| 145 }; | 151 }; |
| 146 | 152 |
| 147 #if OS(WIN) | 153 #if OS(WIN) |
| 148 // The absoluteTime is in seconds, starting on January 1, 1970. The time is assu
med to use the same time zone as WTF::currentTime(). | 154 // The absoluteTime is in seconds, starting on January 1, 1970. The time is assu
med to use the same time zone as WTF::currentTime(). |
| 149 // Returns an interval in milliseconds suitable for passing to one of the Win32
wait functions (e.g., ::WaitForSingleObject). | 155 // Returns an interval in milliseconds suitable for passing to one of the Win32
wait functions (e.g., ::WaitForSingleObject). |
| 150 DWORD absoluteTimeToWaitTimeoutInterval(double absoluteTime); | 156 DWORD absoluteTimeToWaitTimeoutInterval(double absoluteTime); |
| 151 #endif | 157 #endif |
| 152 | 158 |
| 153 } // namespace WTF | 159 } // namespace WTF |
| 154 | 160 |
| 155 using WTF::MutexBase; | 161 using WTF::MutexBase; |
| 156 using WTF::Mutex; | 162 using WTF::Mutex; |
| 157 using WTF::RecursiveMutex; | 163 using WTF::RecursiveMutex; |
| 158 using WTF::MutexLocker; | 164 using WTF::MutexLocker; |
| 159 using WTF::MutexTryLocker; | 165 using WTF::MutexTryLocker; |
| 160 using WTF::ThreadCondition; | 166 using WTF::ThreadCondition; |
| 161 | 167 |
| 162 #if OS(WIN) | 168 #if OS(WIN) |
| 163 using WTF::absoluteTimeToWaitTimeoutInterval; | 169 using WTF::absoluteTimeToWaitTimeoutInterval; |
| 164 #endif | 170 #endif |
| 165 | 171 |
| 166 #endif // ThreadingPrimitives_h | 172 #endif // ThreadingPrimitives_h |
| OLD | NEW |