| 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 | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 // A helper class to safely dereference the callback objects held by | 37 // A helper class to safely dereference the callback objects held by |
| 38 // SQLStatement and SQLTransaction on the proper thread. The 'wrapped' | 38 // SQLStatement and SQLTransaction on the proper thread. The 'wrapped' |
| 39 // callback is dereferenced: | 39 // callback is dereferenced: |
| 40 // - by destructing the enclosing wrapper - on any thread | 40 // - by destructing the enclosing wrapper - on any thread |
| 41 // - by calling clear() - on any thread | 41 // - by calling clear() - on any thread |
| 42 // - by unwrapping and then dereferencing normally - on context thread only | 42 // - by unwrapping and then dereferencing normally - on context thread only |
| 43 // Oilpan: ~T must be thread-independent. | 43 // Oilpan: ~T must be thread-independent. |
| 44 template<typename T> class SQLCallbackWrapper { | 44 template<typename T> class SQLCallbackWrapper { |
| 45 DISALLOW_ALLOCATION(); | 45 DISALLOW_ALLOCATION(); |
| 46 public: | 46 public: |
| 47 SQLCallbackWrapper(PassOwnPtrWillBeRawPtr<T> callback, ExecutionContext* exe
cutionContext) | 47 SQLCallbackWrapper(T* callback, ExecutionContext* executionContext) |
| 48 : m_callback(callback) | 48 : m_callback(callback) |
| 49 , m_executionContext(m_callback ? executionContext : 0) | 49 , m_executionContext(m_callback ? executionContext : 0) |
| 50 { | 50 { |
| 51 ASSERT(!m_callback || (m_executionContext.get() && m_executionContext->i
sContextThread())); | 51 ASSERT(!m_callback || (m_executionContext.get() && m_executionContext->i
sContextThread())); |
| 52 } | 52 } |
| 53 | 53 |
| 54 ~SQLCallbackWrapper() | 54 ~SQLCallbackWrapper() |
| 55 { | 55 { |
| 56 #if !ENABLE(OILPAN) | 56 #if !ENABLE(OILPAN) |
| 57 clear(); | 57 clear(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 68 { | 68 { |
| 69 #if ENABLE(OILPAN) | 69 #if ENABLE(OILPAN) |
| 70 // It's safe to call ~T in non-context-thread. | 70 // It's safe to call ~T in non-context-thread. |
| 71 // Implementation classes of ExecutionContext are on-heap. Their | 71 // Implementation classes of ExecutionContext are on-heap. Their |
| 72 // destructors are called in their owner threads. | 72 // destructors are called in their owner threads. |
| 73 MutexLocker locker(m_mutex); | 73 MutexLocker locker(m_mutex); |
| 74 m_callback.clear(); | 74 m_callback.clear(); |
| 75 m_executionContext.clear(); | 75 m_executionContext.clear(); |
| 76 #else | 76 #else |
| 77 ExecutionContext* context; | 77 ExecutionContext* context; |
| 78 OwnPtr<T> callback; | |
| 79 { | 78 { |
| 80 MutexLocker locker(m_mutex); | 79 MutexLocker locker(m_mutex); |
| 81 if (!m_callback) { | 80 if (!m_callback) { |
| 82 ASSERT(!m_executionContext); | 81 ASSERT(!m_executionContext); |
| 83 return; | 82 return; |
| 84 } | 83 } |
| 85 if (m_executionContext->isContextThread()) { | 84 if (m_executionContext->isContextThread()) { |
| 86 m_callback.clear(); | 85 m_callback.clear(); |
| 87 m_executionContext.clear(); | 86 m_executionContext.clear(); |
| 88 return; | 87 return; |
| 89 } | 88 } |
| 90 context = m_executionContext.release().leakRef(); | 89 context = m_executionContext.release().leakRef(); |
| 91 callback = m_callback.release(); | |
| 92 } | 90 } |
| 93 context->postTask(SafeReleaseTask::create(callback.release())); | 91 context->postTask(SafeReleaseTask::create(m_callback)); |
| 94 #endif | 92 #endif |
| 95 } | 93 } |
| 96 | 94 |
| 97 PassOwnPtrWillBeRawPtr<T> unwrap() | 95 T* unwrap() |
| 98 { | 96 { |
| 99 MutexLocker locker(m_mutex); | 97 MutexLocker locker(m_mutex); |
| 100 ASSERT(!m_callback || m_executionContext->isContextThread()); | 98 ASSERT(!m_callback || m_executionContext->isContextThread()); |
| 101 m_executionContext.clear(); | 99 m_executionContext.clear(); |
| 102 return m_callback.release(); | 100 return m_callback.release(); |
| 103 } | 101 } |
| 104 | 102 |
| 105 // Useful for optimizations only, please test the return value of unwrap to
be sure. | 103 // Useful for optimizations only, please test the return value of unwrap to
be sure. |
| 106 bool hasCallback() const { return m_callback; } | 104 bool hasCallback() const { return m_callback; } |
| 107 | 105 |
| 108 private: | 106 private: |
| 109 #if !ENABLE(OILPAN) | 107 #if !ENABLE(OILPAN) |
| 110 class SafeReleaseTask : public ExecutionContextTask { | 108 class SafeReleaseTask : public ExecutionContextTask { |
| 111 public: | 109 public: |
| 112 static PassOwnPtr<SafeReleaseTask> create(PassOwnPtr<T> callbackToReleas
e) | 110 static PassOwnPtr<SafeReleaseTask> create(T* callbackToRelease) |
| 113 { | 111 { |
| 114 return adoptPtr(new SafeReleaseTask(callbackToRelease)); | 112 return adoptPtr(new SafeReleaseTask(callbackToRelease)); |
| 115 } | 113 } |
| 116 | 114 |
| 117 virtual void performTask(ExecutionContext* context) | 115 virtual void performTask(ExecutionContext* context) |
| 118 { | 116 { |
| 119 ASSERT(m_callbackToRelease && context && context->isContextThread())
; | 117 ASSERT(m_callbackToRelease && context && context->isContextThread())
; |
| 120 m_callbackToRelease.clear(); | 118 m_callbackToRelease.clear(); |
| 121 context->deref(); | 119 context->deref(); |
| 122 } | 120 } |
| 123 | 121 |
| 124 virtual bool isCleanupTask() const { return true; } | 122 virtual bool isCleanupTask() const { return true; } |
| 125 | 123 |
| 126 private: | 124 private: |
| 127 explicit SafeReleaseTask(PassOwnPtr<T> callbackToRelease) | 125 explicit SafeReleaseTask(T* callbackToRelease) |
| 128 : m_callbackToRelease(callbackToRelease) | 126 : m_callbackToRelease(callbackToRelease) |
| 129 { | 127 { |
| 130 } | 128 } |
| 131 | 129 |
| 132 OwnPtr<T> m_callbackToRelease; | 130 CrossThreadPersistent<T> m_callbackToRelease; |
| 133 }; | 131 }; |
| 134 #endif | 132 #endif |
| 135 | 133 |
| 136 Mutex m_mutex; | 134 Mutex m_mutex; |
| 137 OwnPtrWillBeMember<T> m_callback; | 135 CrossThreadPersistentWillBeMember<T> m_callback; |
| 138 RefPtrWillBeMember<ExecutionContext> m_executionContext; | 136 RefPtrWillBeMember<ExecutionContext> m_executionContext; |
| 139 }; | 137 }; |
| 140 | 138 |
| 141 } // namespace blink | 139 } // namespace blink |
| 142 | 140 |
| 143 #endif // SQLCallbackWrapper_h | 141 #endif // SQLCallbackWrapper_h |
| OLD | NEW |