Changeset 165952 in webkit
- Timestamp:
- Mar 20, 2014 12:56:38 AM (10 years ago)
- Location:
- trunk/Source/WTF
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r165946 r165952 1 2014-03-18 Carlos Garcia Campos <cgarcia@igalia.com> 2 3 [GLIB] Add GMainLoopSource class to wrap idle and timeout sources 4 https://bugs.webkit.org/show_bug.cgi?id=130027 5 6 Reviewed by Martin Robinson. 7 8 GLib main loop sources like idle and timeouts are sometimes 9 unconvenient to use and it's very common to forget canceling the 10 source when the object is destroyed or reset the source ID in the 11 callback when called. GMainLoopSource is a wrapper class to make it 12 easier to handle sources and also to avoid those typical mistakes. 13 It forces to create sources with a name and the user doesn't have 14 to deal with the source ID anymore. The source is cancelled when 15 the object is deleted or when a new source is scheduled. It uses 16 std::function for callbacks so that we no longer need to use the 17 "proxy" static callbacks either. We can use std::bind to use a 18 function pointer or a member or even lambda functions. It also handles 19 repeating sources automatically depending on whether the given 20 function returns a bool or not. 21 22 * GNUmakefile.list.am: 23 * wtf/PlatformEfl.cmake: 24 * wtf/PlatformGTK.cmake: 25 * wtf/RunLoop.h: 26 * wtf/gobject/GMainLoopSource.cpp: Added. 27 (WTF::GMainLoopSource::createAndDeleteOnDestroy): 28 (WTF::GMainLoopSource::GMainLoopSource): 29 (WTF::GMainLoopSource::~GMainLoopSource): 30 (WTF::GMainLoopSource::cancel): 31 (WTF::GMainLoopSource::reset): 32 (WTF::GMainLoopSource::scheduleIdleSource): 33 (WTF::GMainLoopSource::schedule): 34 (WTF::GMainLoopSource::scheduleTimeoutSource): 35 (WTF::GMainLoopSource::scheduleAfterDelay): 36 (WTF::GMainLoopSource::voidCallback): 37 (WTF::GMainLoopSource::boolCallback): 38 (WTF::GMainLoopSource::socketCallback): 39 (WTF::GMainLoopSource::destroy): 40 (WTF::GMainLoopSource::voidSourceCallback): 41 (WTF::GMainLoopSource::boolSourceCallback): 42 (WTF::GMainLoopSource::socketSourceCallback): 43 * wtf/gobject/GMainLoopSource.h: Added. 44 (WTF::GMainLoopSource::isScheduled): 45 * wtf/gtk/MainThreadGtk.cpp: 46 (WTF::scheduleDispatchFunctionsOnMainThread): 47 * wtf/gtk/RunLoopGtk.cpp: 48 (WTF::RunLoop::wakeUp): 49 (WTF::RunLoop::TimerBase::TimerBase): 50 (WTF::RunLoop::TimerBase::start): 51 (WTF::RunLoop::TimerBase::stop): 52 (WTF::RunLoop::TimerBase::isActive): 53 1 54 2014-03-19 Gavin Barraclough <barraclough@apple.com> 2 55 -
trunk/Source/WTF/GNUmakefile.list.am
r164430 r165952 213 213 Source/WTF/wtf/dtoa/strtod.h \ 214 214 Source/WTF/wtf/dtoa/utils.h \ 215 Source/WTF/wtf/gobject/GMainLoopSource.cpp \ 216 Source/WTF/wtf/gobject/GMainLoopSource.h \ 215 217 Source/WTF/wtf/gobject/GMutexLocker.h \ 216 218 Source/WTF/wtf/gobject/GRefPtr.cpp \ -
trunk/Source/WTF/wtf/PlatformEfl.cmake
r163797 r165952 5 5 efl/RunLoopEfl.cpp 6 6 7 gobject/GMainLoopSource.cpp 7 8 gobject/GRefPtr.cpp 8 9 ) -
trunk/Source/WTF/wtf/PlatformGTK.cmake
r163797 r165952 1 1 list(APPEND WTF_SOURCES 2 gobject/GMainLoopSource.cpp 2 3 gobject/GRefPtr.cpp 3 4 gobject/GlibUtilities.cpp -
trunk/Source/WTF/wtf/RunLoop.h
r165746 r165952 38 38 39 39 #if USE(GLIB) 40 #include <wtf/gobject/G RefPtr.h>40 #include <wtf/gobject/GMainLoopSource.h> 41 41 #endif 42 42 … … 100 100 bool m_isRepeating; 101 101 #elif USE(GLIB) 102 static gboolean timerFiredCallback(RunLoop::TimerBase*); 103 gboolean isRepeating() const { return m_isRepeating; } 104 void clearTimerSource(); 105 GRefPtr<GSource> m_timerSource; 106 gboolean m_isRepeating; 102 GMainLoopSource m_timerSource; 107 103 #endif 108 104 }; -
trunk/Source/WTF/wtf/gtk/MainThreadGtk.cpp
r158103 r165952 31 31 #include "MainThread.h" 32 32 33 #include < glib.h>33 #include <wtf/gobject/GMainLoopSource.h> 34 34 35 35 namespace WTF { … … 39 39 } 40 40 41 static gboolean timeoutFired(gpointer)42 {43 dispatchFunctionsFromMainThread();44 return FALSE;45 }46 47 41 void scheduleDispatchFunctionsOnMainThread() 48 42 { 49 g_idle_add_full(G_PRIORITY_DEFAULT, timeoutFired, 0, 0);43 GMainLoopSource::createAndDeleteOnDestroy().schedule("[WebKit] dispatchFunctionsFromMainThread", dispatchFunctionsFromMainThread); 50 44 } 51 45 -
trunk/Source/WTF/wtf/gtk/RunLoopGtk.cpp
r165746 r165952 98 98 } 99 99 100 gboolean RunLoop::queueWork(RunLoop* runLoop)101 {102 runLoop->performWork();103 return FALSE;104 }105 106 100 void RunLoop::wakeUp() 107 101 { 108 GRefPtr<GSource> source = adoptGRef(g_idle_source_new()); 109 g_source_set_priority(source.get(), G_PRIORITY_DEFAULT); 110 g_source_set_callback(source.get(), reinterpret_cast<GSourceFunc>(&RunLoop::queueWork), this, 0); 111 g_source_attach(source.get(), m_runLoopContext.get()); 112 102 ref(); 103 GMainLoopSource::createAndDeleteOnDestroy().schedule("[WebKit] RunLoop work", std::bind(&RunLoop::performWork, this), 104 G_PRIORITY_DEFAULT, [this] { deref(); }); 113 105 g_main_context_wakeup(m_runLoopContext.get()); 114 106 } … … 116 108 RunLoop::TimerBase::TimerBase(RunLoop& runLoop) 117 109 : m_runLoop(runLoop) 118 , m_timerSource(0)119 110 { 120 111 } … … 125 116 } 126 117 127 void RunLoop::TimerBase::clearTimerSource()128 {129 m_timerSource = 0;130 }131 132 gboolean RunLoop::TimerBase::timerFiredCallback(RunLoop::TimerBase* timer)133 {134 GSource* currentTimerSource = timer->m_timerSource.get();135 bool isRepeating = timer->isRepeating();136 // This can change the timerSource by starting a new timer within the callback.137 if (!isRepeating && currentTimerSource == timer->m_timerSource.get())138 timer->clearTimerSource();139 140 timer->fired();141 return isRepeating;142 }143 144 118 void RunLoop::TimerBase::start(double fireInterval, bool repeat) 145 119 { 146 if (m_timerSource) 147 stop(); 148 149 m_timerSource = adoptGRef(g_timeout_source_new(static_cast<guint>(fireInterval * 1000))); 150 m_isRepeating = repeat; 151 g_source_set_callback(m_timerSource.get(), reinterpret_cast<GSourceFunc>(&RunLoop::TimerBase::timerFiredCallback), this, 0); 152 g_source_attach(m_timerSource.get(), m_runLoop.m_runLoopContext.get()); 120 m_timerSource.scheduleAfterDelay("[WebKit] RunLoop::Timer", std::function<bool ()>([this, repeat] { fired(); return repeat; }), 121 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::duration<double>(fireInterval))); 153 122 } 154 123 155 124 void RunLoop::TimerBase::stop() 156 125 { 157 if (!m_timerSource) 158 return; 159 160 g_source_destroy(m_timerSource.get()); 161 clearTimerSource(); 126 m_timerSource.cancel(); 162 127 } 163 128 164 129 bool RunLoop::TimerBase::isActive() const 165 130 { 166 return m_timerSource ;131 return m_timerSource.isScheduled(); 167 132 } 168 133
Note: See TracChangeset
for help on using the changeset viewer.