| 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 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 #include "wtf/Threading.h" | 38 #include "wtf/Threading.h" |
| 39 | 39 |
| 40 namespace WTF { | 40 namespace WTF { |
| 41 | 41 |
| 42 // 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. |
| 43 // 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 |
| 44 // thread gets captured when setShared(true) is called. | 44 // thread gets captured when setShared(true) is called. |
| 45 // The mode may be changed by calling useMutexMode (or turnOffVerification). | 45 // The mode may be changed by calling useMutexMode (or turnOffVerification). |
| 46 class ThreadRestrictionVerifier { | 46 class ThreadRestrictionVerifier { |
| 47 public: | 47 public: |
| 48 ThreadRestrictionVerifier() | 48 ThreadRestrictionVerifier() |
| 49 : m_shared(false) | 49 : m_shared(false), m_owningThread(0) { |
| 50 , m_owningThread(0) | 50 } |
| 51 { | |
| 52 } | |
| 53 | 51 |
| 54 // Indicates that the object may (or may not) be owned by more than one plac
e. | 52 // Indicates that the object may (or may not) be owned by more than one place. |
| 55 void setShared(bool shared) | 53 void setShared(bool shared) { |
| 56 { | 54 bool previouslyShared = m_shared; |
| 57 bool previouslyShared = m_shared; | 55 m_shared = shared; |
| 58 m_shared = shared; | |
| 59 | 56 |
| 60 if (!m_shared) | 57 if (!m_shared) |
| 61 return; | 58 return; |
| 62 | 59 |
| 63 ASSERT_UNUSED(previouslyShared, shared != previouslyShared); | 60 ASSERT_UNUSED(previouslyShared, shared != previouslyShared); |
| 64 // Capture the current thread to verify that subsequent ref/deref happen
on this thread. | 61 // Capture the current thread to verify that subsequent ref/deref happen on
this thread. |
| 65 m_owningThread = currentThread(); | 62 m_owningThread = currentThread(); |
| 66 } | 63 } |
| 67 | 64 |
| 68 // Is it OK to use the object at this moment on the current thread? | 65 // Is it OK to use the object at this moment on the current thread? |
| 69 bool isSafeToUse() const | 66 bool isSafeToUse() const { |
| 70 { | 67 if (!m_shared) |
| 71 if (!m_shared) | 68 return true; |
| 72 return true; | |
| 73 | 69 |
| 74 return m_owningThread == currentThread(); | 70 return m_owningThread == currentThread(); |
| 75 } | 71 } |
| 76 | 72 |
| 77 private: | 73 private: |
| 78 bool m_shared; | 74 bool m_shared; |
| 79 | 75 |
| 80 // Used by SingleThreadVerificationMode | 76 // Used by SingleThreadVerificationMode |
| 81 ThreadIdentifier m_owningThread; | 77 ThreadIdentifier m_owningThread; |
| 82 }; | 78 }; |
| 83 | |
| 84 } | 79 } |
| 85 | 80 |
| 86 #endif // ENABLE(ASSERT) | 81 #endif // ENABLE(ASSERT) |
| 87 #endif // ThreadRestrictionVerifier_h | 82 #endif // ThreadRestrictionVerifier_h |
| OLD | NEW |