| 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 22 matching lines...) Expand all Loading... |
| 33 #include "wtf/ThreadingPrimitives.h" | 33 #include "wtf/ThreadingPrimitives.h" |
| 34 | 34 |
| 35 namespace WebCore { | 35 namespace WebCore { |
| 36 | 36 |
| 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 template<typename T> class SQLCallbackWrapper { | 44 template<typename T> class SQLCallbackWrapper { |
| 45 DISALLOW_ALLOCATION(); |
| 44 public: | 46 public: |
| 45 SQLCallbackWrapper(PassOwnPtr<T> callback, ExecutionContext* executionContex
t) | 47 SQLCallbackWrapper(PassOwnPtr<T> callback, ExecutionContext* executionContex
t) |
| 46 : m_callback(callback) | 48 : m_callback(callback) |
| 47 , m_executionContext(m_callback ? executionContext : 0) | 49 , m_executionContext(m_callback ? executionContext : 0) |
| 48 { | 50 { |
| 49 ASSERT(!m_callback || (m_executionContext.get() && m_executionContext->i
sContextThread())); | 51 ASSERT(!m_callback || (m_executionContext.get() && m_executionContext->i
sContextThread())); |
| 50 } | 52 } |
| 51 | 53 |
| 52 ~SQLCallbackWrapper() | 54 ~SQLCallbackWrapper() |
| 53 { | 55 { |
| 54 clear(); | 56 clear(); |
| 55 } | 57 } |
| 56 | 58 |
| 59 // FIXME: Oilpan: Trace m_executionContext. |
| 60 void trace(Visitor* visitor) { } |
| 61 |
| 57 void clear() | 62 void clear() |
| 58 { | 63 { |
| 64 #if ENABLE(OILPAN) |
| 65 // It's safe to call ~T in non-context-thread. |
| 66 // Implementation classes of ExecutionContext are on-heap. Their |
| 67 // destructors are called in their owner threads. |
| 68 MutexLocker locker(m_mutex); |
| 69 m_callback.clear(); |
| 70 m_executionContext.clear(); |
| 71 #else |
| 59 ExecutionContext* context; | 72 ExecutionContext* context; |
| 60 OwnPtr<T> callback; | 73 OwnPtr<T> callback; |
| 61 { | 74 { |
| 62 MutexLocker locker(m_mutex); | 75 MutexLocker locker(m_mutex); |
| 63 if (!m_callback) { | 76 if (!m_callback) { |
| 64 ASSERT(!m_executionContext); | 77 ASSERT(!m_executionContext); |
| 65 return; | 78 return; |
| 66 } | 79 } |
| 67 if (m_executionContext->isContextThread()) { | 80 if (m_executionContext->isContextThread()) { |
| 68 m_callback.clear(); | 81 m_callback.clear(); |
| 69 m_executionContext.clear(); | 82 m_executionContext.clear(); |
| 70 return; | 83 return; |
| 71 } | 84 } |
| 72 context = m_executionContext.release().leakRef(); | 85 context = m_executionContext.release().leakRef(); |
| 73 callback = m_callback.release(); | 86 callback = m_callback.release(); |
| 74 } | 87 } |
| 75 context->postTask(SafeReleaseTask::create(callback.release())); | 88 context->postTask(SafeReleaseTask::create(callback.release())); |
| 89 #endif |
| 76 } | 90 } |
| 77 | 91 |
| 78 PassOwnPtr<T> unwrap() | 92 PassOwnPtr<T> unwrap() |
| 79 { | 93 { |
| 80 MutexLocker locker(m_mutex); | 94 MutexLocker locker(m_mutex); |
| 81 ASSERT(!m_callback || m_executionContext->isContextThread()); | 95 ASSERT(!m_callback || m_executionContext->isContextThread()); |
| 82 m_executionContext.clear(); | 96 m_executionContext.clear(); |
| 83 return m_callback.release(); | 97 return m_callback.release(); |
| 84 } | 98 } |
| 85 | 99 |
| 86 // Useful for optimizations only, please test the return value of unwrap to
be sure. | 100 // Useful for optimizations only, please test the return value of unwrap to
be sure. |
| 87 bool hasCallback() const { return m_callback; } | 101 bool hasCallback() const { return m_callback; } |
| 88 | 102 |
| 89 private: | 103 private: |
| 104 #if !ENABLE(OILPAN) |
| 90 class SafeReleaseTask : public ExecutionContextTask { | 105 class SafeReleaseTask : public ExecutionContextTask { |
| 91 public: | 106 public: |
| 92 static PassOwnPtr<SafeReleaseTask> create(PassOwnPtr<T> callbackToReleas
e) | 107 static PassOwnPtr<SafeReleaseTask> create(PassOwnPtr<T> callbackToReleas
e) |
| 93 { | 108 { |
| 94 return adoptPtr(new SafeReleaseTask(callbackToRelease)); | 109 return adoptPtr(new SafeReleaseTask(callbackToRelease)); |
| 95 } | 110 } |
| 96 | 111 |
| 97 virtual void performTask(ExecutionContext* context) | 112 virtual void performTask(ExecutionContext* context) |
| 98 { | 113 { |
| 99 ASSERT(m_callbackToRelease && context && context->isContextThread())
; | 114 ASSERT(m_callbackToRelease && context && context->isContextThread())
; |
| 100 m_callbackToRelease.clear(); | 115 m_callbackToRelease.clear(); |
| 101 context->deref(); | 116 context->deref(); |
| 102 } | 117 } |
| 103 | 118 |
| 104 virtual bool isCleanupTask() const { return true; } | 119 virtual bool isCleanupTask() const { return true; } |
| 105 | 120 |
| 106 private: | 121 private: |
| 107 explicit SafeReleaseTask(PassOwnPtr<T> callbackToRelease) | 122 explicit SafeReleaseTask(PassOwnPtr<T> callbackToRelease) |
| 108 : m_callbackToRelease(callbackToRelease) | 123 : m_callbackToRelease(callbackToRelease) |
| 109 { | 124 { |
| 110 } | 125 } |
| 111 | 126 |
| 112 OwnPtr<T> m_callbackToRelease; | 127 OwnPtr<T> m_callbackToRelease; |
| 113 }; | 128 }; |
| 129 #endif |
| 114 | 130 |
| 115 Mutex m_mutex; | 131 Mutex m_mutex; |
| 116 OwnPtr<T> m_callback; | 132 OwnPtr<T> m_callback; |
| 117 RefPtr<ExecutionContext> m_executionContext; | 133 RefPtr<ExecutionContext> m_executionContext; |
| 118 }; | 134 }; |
| 119 | 135 |
| 120 } // namespace WebCore | 136 } // namespace WebCore |
| 121 | 137 |
| 122 #endif // SQLCallbackWrapper_h | 138 #endif // SQLCallbackWrapper_h |
| OLD | NEW |