| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions | |
| 7 * are met: | |
| 8 * | |
| 9 * 1. Redistributions of source code must retain the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer. | |
| 11 * 2. Redistributions in binary form must reproduce the above copyright | |
| 12 * notice, this list of conditions and the following disclaimer in the | |
| 13 * documentation and/or other materials provided with the distribution. | |
| 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
| 15 * its contributors may be used to endorse or promote products derived | |
| 16 * from this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
| 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
| 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 * | |
| 29 */ | |
| 30 | |
| 31 #ifndef ThreadingPrimitives_h | |
| 32 #define ThreadingPrimitives_h | |
| 33 | |
| 34 #include <wtf/Platform.h> | |
| 35 | |
| 36 #include <wtf/Assertions.h> | |
| 37 #include <wtf/FastAllocBase.h> | |
| 38 #include <wtf/Locker.h> | |
| 39 #include <wtf/Noncopyable.h> | |
| 40 | |
| 41 #if OS(WINDOWS) | |
| 42 #include <windows.h> | |
| 43 #endif | |
| 44 | |
| 45 #if USE(PTHREADS) | |
| 46 #include <pthread.h> | |
| 47 #endif | |
| 48 | |
| 49 namespace WTF { | |
| 50 | |
| 51 #if USE(PTHREADS) | |
| 52 typedef pthread_mutex_t PlatformMutex; | |
| 53 typedef pthread_cond_t PlatformCondition; | |
| 54 #elif OS(WINDOWS) | |
| 55 struct PlatformMutex { | |
| 56 CRITICAL_SECTION m_internalMutex; | |
| 57 size_t m_recursionCount; | |
| 58 }; | |
| 59 struct PlatformCondition { | |
| 60 size_t m_waitersGone; | |
| 61 size_t m_waitersBlocked; | |
| 62 size_t m_waitersToUnblock; | |
| 63 HANDLE m_blockLock; | |
| 64 HANDLE m_blockQueue; | |
| 65 HANDLE m_unblockLock; | |
| 66 | |
| 67 bool timedWait(PlatformMutex&, DWORD durationMilliseconds); | |
| 68 void signal(bool unblockAll); | |
| 69 }; | |
| 70 #else | |
| 71 typedef void* PlatformMutex; | |
| 72 typedef void* PlatformCondition; | |
| 73 #endif | |
| 74 | |
| 75 class Mutex { | |
| 76 WTF_MAKE_NONCOPYABLE(Mutex); WTF_MAKE_FAST_ALLOCATED; | |
| 77 public: | |
| 78 WTF_EXPORT_PRIVATE Mutex(); | |
| 79 WTF_EXPORT_PRIVATE ~Mutex(); | |
| 80 | |
| 81 WTF_EXPORT_PRIVATE void lock(); | |
| 82 WTF_EXPORT_PRIVATE bool tryLock(); | |
| 83 WTF_EXPORT_PRIVATE void unlock(); | |
| 84 | |
| 85 public: | |
| 86 PlatformMutex& impl() { return m_mutex; } | |
| 87 private: | |
| 88 PlatformMutex m_mutex; | |
| 89 }; | |
| 90 | |
| 91 typedef Locker<Mutex> MutexLocker; | |
| 92 | |
| 93 class MutexTryLocker { | |
| 94 WTF_MAKE_NONCOPYABLE(MutexTryLocker); | |
| 95 public: | |
| 96 MutexTryLocker(Mutex& mutex) : m_mutex(mutex), m_locked(mutex.tryLock()) { } | |
| 97 ~MutexTryLocker() | |
| 98 { | |
| 99 if (m_locked) | |
| 100 m_mutex.unlock(); | |
| 101 } | |
| 102 | |
| 103 bool locked() const { return m_locked; } | |
| 104 | |
| 105 private: | |
| 106 Mutex& m_mutex; | |
| 107 bool m_locked; | |
| 108 }; | |
| 109 | |
| 110 class ThreadCondition { | |
| 111 WTF_MAKE_NONCOPYABLE(ThreadCondition); | |
| 112 public: | |
| 113 WTF_EXPORT_PRIVATE ThreadCondition(); | |
| 114 WTF_EXPORT_PRIVATE ~ThreadCondition(); | |
| 115 | |
| 116 WTF_EXPORT_PRIVATE void wait(Mutex& mutex); | |
| 117 // Returns true if the condition was signaled before absoluteTime, false if
the absoluteTime was reached or is in the past. | |
| 118 // The absoluteTime is in seconds, starting on January 1, 1970. The time is
assumed to use the same time zone as WTF::currentTime(). | |
| 119 WTF_EXPORT_PRIVATE bool timedWait(Mutex&, double absoluteTime); | |
| 120 WTF_EXPORT_PRIVATE void signal(); | |
| 121 WTF_EXPORT_PRIVATE void broadcast(); | |
| 122 | |
| 123 private: | |
| 124 PlatformCondition m_condition; | |
| 125 }; | |
| 126 | |
| 127 #if OS(WINDOWS) | |
| 128 // The absoluteTime is in seconds, starting on January 1, 1970. The time is assu
med to use the same time zone as WTF::currentTime(). | |
| 129 // Returns an interval in milliseconds suitable for passing to one of the Win32
wait functions (e.g., ::WaitForSingleObject). | |
| 130 WTF_EXPORT_PRIVATE DWORD absoluteTimeToWaitTimeoutInterval(double absoluteTime); | |
| 131 #endif | |
| 132 | |
| 133 } // namespace WTF | |
| 134 | |
| 135 using WTF::Mutex; | |
| 136 using WTF::MutexLocker; | |
| 137 using WTF::MutexTryLocker; | |
| 138 using WTF::ThreadCondition; | |
| 139 | |
| 140 #if OS(WINDOWS) | |
| 141 using WTF::absoluteTimeToWaitTimeoutInterval; | |
| 142 #endif | |
| 143 | |
| 144 #endif // ThreadingPrimitives_h | |
| OLD | NEW |