| 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() : MutexBase(false) {} |
| 102 bool tryLock(); | 104 bool tryLock(); |
| 103 }; | 105 }; |
| 104 | 106 |
| 105 class WTF_EXPORT RecursiveMutex : public MutexBase { | 107 class WTF_EXPORT RecursiveMutex : public MutexBase { |
| 106 public: | 108 public: |
| 107 RecursiveMutex() : MutexBase(true) { } | 109 RecursiveMutex() : MutexBase(true) {} |
| 108 bool tryLock(); | 110 bool tryLock(); |
| 109 }; | 111 }; |
| 110 | 112 |
| 111 typedef Locker<MutexBase> MutexLocker; | 113 typedef Locker<MutexBase> MutexLocker; |
| 112 | 114 |
| 113 class MutexTryLocker final { | 115 class MutexTryLocker final { |
| 114 STACK_ALLOCATED(); | 116 STACK_ALLOCATED(); |
| 115 WTF_MAKE_NONCOPYABLE(MutexTryLocker); | 117 WTF_MAKE_NONCOPYABLE(MutexTryLocker); |
| 116 public: | |
| 117 MutexTryLocker(Mutex& mutex) : m_mutex(mutex), m_locked(mutex.tryLock()) { } | |
| 118 ~MutexTryLocker() | |
| 119 { | |
| 120 if (m_locked) | |
| 121 m_mutex.unlock(); | |
| 122 } | |
| 123 | 118 |
| 124 bool locked() const { return m_locked; } | 119 public: |
| 120 MutexTryLocker(Mutex& mutex) : m_mutex(mutex), m_locked(mutex.tryLock()) {} |
| 121 ~MutexTryLocker() { |
| 122 if (m_locked) |
| 123 m_mutex.unlock(); |
| 124 } |
| 125 | 125 |
| 126 private: | 126 bool locked() const { return m_locked; } |
| 127 Mutex& m_mutex; | 127 |
| 128 bool m_locked; | 128 private: |
| 129 Mutex& m_mutex; |
| 130 bool m_locked; |
| 129 }; | 131 }; |
| 130 | 132 |
| 131 class WTF_EXPORT ThreadCondition final { | 133 class WTF_EXPORT ThreadCondition final { |
| 132 USING_FAST_MALLOC(ThreadCondition); // Only HeapTest.cpp requires. | 134 USING_FAST_MALLOC(ThreadCondition); // Only HeapTest.cpp requires. |
| 133 WTF_MAKE_NONCOPYABLE(ThreadCondition); | 135 WTF_MAKE_NONCOPYABLE(ThreadCondition); |
| 134 public: | |
| 135 ThreadCondition(); | |
| 136 ~ThreadCondition(); | |
| 137 | 136 |
| 138 void wait(MutexBase&); | 137 public: |
| 139 // Returns true if the condition was signaled before absoluteTime, false if
the absoluteTime was reached or is in the past. | 138 ThreadCondition(); |
| 140 // The absoluteTime is in seconds, starting on January 1, 1970. The time is
assumed to use the same time zone as WTF::currentTime(). | 139 ~ThreadCondition(); |
| 141 bool timedWait(MutexBase&, double absoluteTime); | |
| 142 void signal(); | |
| 143 void broadcast(); | |
| 144 | 140 |
| 145 private: | 141 void wait(MutexBase&); |
| 146 PlatformCondition m_condition; | 142 // Returns true if the condition was signaled before absoluteTime, false if th
e absoluteTime was reached or is in the past. |
| 143 // The absoluteTime is in seconds, starting on January 1, 1970. The time is as
sumed to use the same time zone as WTF::currentTime(). |
| 144 bool timedWait(MutexBase&, double absoluteTime); |
| 145 void signal(); |
| 146 void broadcast(); |
| 147 |
| 148 private: |
| 149 PlatformCondition m_condition; |
| 147 }; | 150 }; |
| 148 | 151 |
| 149 #if OS(WIN) | 152 #if OS(WIN) |
| 150 // The absoluteTime is in seconds, starting on January 1, 1970. The time is assu
med to use the same time zone as WTF::currentTime(). | 153 // The absoluteTime is in seconds, starting on January 1, 1970. The time is assu
med to use the same time zone as WTF::currentTime(). |
| 151 // Returns an interval in milliseconds suitable for passing to one of the Win32
wait functions (e.g., ::WaitForSingleObject). | 154 // Returns an interval in milliseconds suitable for passing to one of the Win32
wait functions (e.g., ::WaitForSingleObject). |
| 152 DWORD absoluteTimeToWaitTimeoutInterval(double absoluteTime); | 155 DWORD absoluteTimeToWaitTimeoutInterval(double absoluteTime); |
| 153 #endif | 156 #endif |
| 154 | 157 |
| 155 } // namespace WTF | 158 } // namespace WTF |
| 156 | 159 |
| 157 using WTF::MutexBase; | 160 using WTF::MutexBase; |
| 158 using WTF::Mutex; | 161 using WTF::Mutex; |
| 159 using WTF::RecursiveMutex; | 162 using WTF::RecursiveMutex; |
| 160 using WTF::MutexLocker; | 163 using WTF::MutexLocker; |
| 161 using WTF::MutexTryLocker; | 164 using WTF::MutexTryLocker; |
| 162 using WTF::ThreadCondition; | 165 using WTF::ThreadCondition; |
| 163 | 166 |
| 164 #if OS(WIN) | 167 #if OS(WIN) |
| 165 using WTF::absoluteTimeToWaitTimeoutInterval; | 168 using WTF::absoluteTimeToWaitTimeoutInterval; |
| 166 #endif | 169 #endif |
| 167 | 170 |
| 168 #endif // ThreadingPrimitives_h | 171 #endif // ThreadingPrimitives_h |
| OLD | NEW |