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