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 30 matching lines...) Expand all Loading... |
41 #include <windows.h> | 41 #include <windows.h> |
42 #endif | 42 #endif |
43 | 43 |
44 #if USE(PTHREADS) | 44 #if USE(PTHREADS) |
45 #include <pthread.h> | 45 #include <pthread.h> |
46 #endif | 46 #endif |
47 | 47 |
48 namespace WTF { | 48 namespace WTF { |
49 | 49 |
50 #if USE(PTHREADS) | 50 #if USE(PTHREADS) |
51 typedef pthread_mutex_t PlatformMutex; | 51 struct PlatformMutex { |
| 52 pthread_mutex_t m_internalMutex; |
| 53 #if ENABLE(ASSERT) |
| 54 size_t m_recursionCount; |
| 55 #endif |
| 56 }; |
52 typedef pthread_cond_t PlatformCondition; | 57 typedef pthread_cond_t PlatformCondition; |
53 #elif OS(WIN) | 58 #elif OS(WIN) |
54 struct PlatformMutex { | 59 struct PlatformMutex { |
55 CRITICAL_SECTION m_internalMutex; | 60 CRITICAL_SECTION m_internalMutex; |
56 size_t m_recursionCount; | 61 size_t m_recursionCount; |
57 }; | 62 }; |
58 struct PlatformCondition { | 63 struct PlatformCondition { |
59 size_t m_waitersGone; | 64 size_t m_waitersGone; |
60 size_t m_waitersBlocked; | 65 size_t m_waitersBlocked; |
61 size_t m_waitersToUnblock; | 66 size_t m_waitersToUnblock; |
62 HANDLE m_blockLock; | 67 HANDLE m_blockLock; |
63 HANDLE m_blockQueue; | 68 HANDLE m_blockQueue; |
64 HANDLE m_unblockLock; | 69 HANDLE m_unblockLock; |
65 | 70 |
66 bool timedWait(PlatformMutex&, DWORD durationMilliseconds); | 71 bool timedWait(PlatformMutex&, DWORD durationMilliseconds); |
67 void signal(bool unblockAll); | 72 void signal(bool unblockAll); |
68 }; | 73 }; |
69 #else | 74 #else |
70 typedef void* PlatformMutex; | 75 typedef void* PlatformMutex; |
71 typedef void* PlatformCondition; | 76 typedef void* PlatformCondition; |
72 #endif | 77 #endif |
73 | 78 |
74 class WTF_EXPORT Mutex { | 79 class WTF_EXPORT MutexBase { |
75 WTF_MAKE_NONCOPYABLE(Mutex); WTF_MAKE_FAST_ALLOCATED; | 80 WTF_MAKE_NONCOPYABLE(MutexBase); WTF_MAKE_FAST_ALLOCATED; |
76 public: | 81 public: |
77 Mutex(); | 82 ~MutexBase(); |
78 ~Mutex(); | |
79 | 83 |
80 void lock(); | 84 void lock(); |
81 bool tryLock(); | |
82 void unlock(); | 85 void unlock(); |
| 86 #if ENABLE(ASSERT) |
| 87 bool locked() { return m_mutex.m_recursionCount > 0; } |
| 88 #endif |
83 | 89 |
84 public: | 90 public: |
85 PlatformMutex& impl() { return m_mutex; } | 91 PlatformMutex& impl() { return m_mutex; } |
86 private: | 92 |
| 93 protected: |
| 94 MutexBase(bool recursive); |
| 95 |
87 PlatformMutex m_mutex; | 96 PlatformMutex m_mutex; |
88 }; | 97 }; |
89 | 98 |
90 typedef Locker<Mutex> MutexLocker; | 99 class WTF_EXPORT Mutex : public MutexBase { |
| 100 public: |
| 101 Mutex() : MutexBase(false) { } |
| 102 bool tryLock(); |
| 103 }; |
| 104 |
| 105 class WTF_EXPORT RecursiveMutex : public MutexBase { |
| 106 public: |
| 107 RecursiveMutex() : MutexBase(true) { } |
| 108 bool tryLock(); |
| 109 }; |
| 110 |
| 111 typedef Locker<MutexBase> MutexLocker; |
91 | 112 |
92 class MutexTryLocker { | 113 class MutexTryLocker { |
93 WTF_MAKE_NONCOPYABLE(MutexTryLocker); | 114 WTF_MAKE_NONCOPYABLE(MutexTryLocker); |
94 public: | 115 public: |
95 MutexTryLocker(Mutex& mutex) : m_mutex(mutex), m_locked(mutex.tryLock()) { } | 116 MutexTryLocker(Mutex& mutex) : m_mutex(mutex), m_locked(mutex.tryLock()) { } |
96 ~MutexTryLocker() | 117 ~MutexTryLocker() |
97 { | 118 { |
98 if (m_locked) | 119 if (m_locked) |
99 m_mutex.unlock(); | 120 m_mutex.unlock(); |
100 } | 121 } |
101 | 122 |
102 bool locked() const { return m_locked; } | 123 bool locked() const { return m_locked; } |
103 | 124 |
104 private: | 125 private: |
105 Mutex& m_mutex; | 126 Mutex& m_mutex; |
106 bool m_locked; | 127 bool m_locked; |
107 }; | 128 }; |
108 | 129 |
109 class WTF_EXPORT ThreadCondition { | 130 class WTF_EXPORT ThreadCondition { |
110 WTF_MAKE_NONCOPYABLE(ThreadCondition); | 131 WTF_MAKE_NONCOPYABLE(ThreadCondition); |
111 public: | 132 public: |
112 ThreadCondition(); | 133 ThreadCondition(); |
113 ~ThreadCondition(); | 134 ~ThreadCondition(); |
114 | 135 |
115 void wait(Mutex&); | 136 void wait(MutexBase&); |
116 // Returns true if the condition was signaled before absoluteTime, false if
the absoluteTime was reached or is in the past. | 137 // Returns true if the condition was signaled before absoluteTime, false if
the absoluteTime was reached or is in the past. |
117 // The absoluteTime is in seconds, starting on January 1, 1970. The time is
assumed to use the same time zone as WTF::currentTime(). | 138 // The absoluteTime is in seconds, starting on January 1, 1970. The time is
assumed to use the same time zone as WTF::currentTime(). |
118 bool timedWait(Mutex&, double absoluteTime); | 139 bool timedWait(MutexBase&, double absoluteTime); |
119 void signal(); | 140 void signal(); |
120 void broadcast(); | 141 void broadcast(); |
121 | 142 |
122 private: | 143 private: |
123 PlatformCondition m_condition; | 144 PlatformCondition m_condition; |
124 }; | 145 }; |
125 | 146 |
126 #if OS(WIN) | 147 #if OS(WIN) |
127 // The absoluteTime is in seconds, starting on January 1, 1970. The time is assu
med to use the same time zone as WTF::currentTime(). | 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(). |
128 // Returns an interval in milliseconds suitable for passing to one of the Win32
wait functions (e.g., ::WaitForSingleObject). | 149 // Returns an interval in milliseconds suitable for passing to one of the Win32
wait functions (e.g., ::WaitForSingleObject). |
129 DWORD absoluteTimeToWaitTimeoutInterval(double absoluteTime); | 150 DWORD absoluteTimeToWaitTimeoutInterval(double absoluteTime); |
130 #endif | 151 #endif |
131 | 152 |
132 } // namespace WTF | 153 } // namespace WTF |
133 | 154 |
| 155 using WTF::MutexBase; |
134 using WTF::Mutex; | 156 using WTF::Mutex; |
| 157 using WTF::RecursiveMutex; |
135 using WTF::MutexLocker; | 158 using WTF::MutexLocker; |
136 using WTF::MutexTryLocker; | 159 using WTF::MutexTryLocker; |
137 using WTF::ThreadCondition; | 160 using WTF::ThreadCondition; |
138 | 161 |
139 #if OS(WIN) | 162 #if OS(WIN) |
140 using WTF::absoluteTimeToWaitTimeoutInterval; | 163 using WTF::absoluteTimeToWaitTimeoutInterval; |
141 #endif | 164 #endif |
142 | 165 |
143 #endif // ThreadingPrimitives_h | 166 #endif // ThreadingPrimitives_h |
OLD | NEW |