| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef ThreadRestrictionVerifier_h | 31 #ifndef ThreadRestrictionVerifier_h |
| 32 #define ThreadRestrictionVerifier_h | 32 #define ThreadRestrictionVerifier_h |
| 33 | 33 |
| 34 #ifndef NDEBUG | 34 #include "wtf/Assertions.h" |
| 35 | 35 |
| 36 #include "wtf/Assertions.h" | 36 #if ENABLE(ASSERT) |
| 37 |
| 37 #include "wtf/Threading.h" | 38 #include "wtf/Threading.h" |
| 38 | 39 |
| 39 namespace WTF { | 40 namespace WTF { |
| 40 | 41 |
| 41 // Verifies that a class is used in a way that respects its lack of thread-safet
y. | 42 // Verifies that a class is used in a way that respects its lack of thread-safet
y. |
| 42 // The default mode is to verify that the object will only be used on a single t
hread. The | 43 // The default mode is to verify that the object will only be used on a single t
hread. The |
| 43 // thread gets captured when setShared(true) is called. | 44 // thread gets captured when setShared(true) is called. |
| 44 // The mode may be changed by calling useMutexMode (or turnOffVerification). | 45 // The mode may be changed by calling useMutexMode (or turnOffVerification). |
| 45 class ThreadRestrictionVerifier { | 46 class ThreadRestrictionVerifier { |
| 46 public: | 47 public: |
| 47 ThreadRestrictionVerifier() | 48 ThreadRestrictionVerifier() |
| 48 : m_shared(false) | 49 : m_shared(false) |
| 49 , m_owningThread(0) | 50 , m_owningThread(0) |
| 50 { | 51 { |
| 51 } | 52 } |
| 52 | 53 |
| 53 // Indicates that the object may (or may not) be owned by more than one plac
e. | 54 // Indicates that the object may (or may not) be owned by more than one plac
e. |
| 54 void setShared(bool shared) | 55 void setShared(bool shared) |
| 55 { | 56 { |
| 56 #if ENABLE(ASSERT) | |
| 57 bool previouslyShared = m_shared; | 57 bool previouslyShared = m_shared; |
| 58 #endif | |
| 59 m_shared = shared; | 58 m_shared = shared; |
| 60 | 59 |
| 61 if (!m_shared) | 60 if (!m_shared) |
| 62 return; | 61 return; |
| 63 | 62 |
| 64 ASSERT(shared != previouslyShared); | 63 ASSERT(shared != previouslyShared); |
| 65 // Capture the current thread to verify that subsequent ref/deref happen
on this thread. | 64 // Capture the current thread to verify that subsequent ref/deref happen
on this thread. |
| 66 m_owningThread = currentThread(); | 65 m_owningThread = currentThread(); |
| 67 } | 66 } |
| 68 | 67 |
| 69 // Is it OK to use the object at this moment on the current thread? | 68 // Is it OK to use the object at this moment on the current thread? |
| 70 bool isSafeToUse() const | 69 bool isSafeToUse() const |
| 71 { | 70 { |
| 72 if (!m_shared) | 71 if (!m_shared) |
| 73 return true; | 72 return true; |
| 74 | 73 |
| 75 return m_owningThread == currentThread(); | 74 return m_owningThread == currentThread(); |
| 76 } | 75 } |
| 77 | 76 |
| 78 private: | 77 private: |
| 79 bool m_shared; | 78 bool m_shared; |
| 80 | 79 |
| 81 // Used by SingleThreadVerificationMode | 80 // Used by SingleThreadVerificationMode |
| 82 ThreadIdentifier m_owningThread; | 81 ThreadIdentifier m_owningThread; |
| 83 }; | 82 }; |
| 84 | 83 |
| 85 } | 84 } |
| 86 | 85 |
| 87 #endif // !NDEBUG | 86 #endif // ENABLE(ASSERT) |
| 88 #endif // ThreadRestrictionVerifier_h | 87 #endif // ThreadRestrictionVerifier_h |
| OLD | NEW |