Dart event loop, timer callback executing in main thread

176 views
Skip to first unread message

Kyle Gretchev

unread,
Jul 16, 2013, 9:52:31 PM7/16/13
to mi...@dartlang.org
Hi there,

Lately I've been taking a stab at writing a wrapper library for GLFW, where some of the library functions can ONLY be run from the original thread that spawned the GL window context. This works fine if I spawn the window and redraw the screen from within a blocking while loop within my main function, but what I'm looking to do is asynchronously redraw the window with a timer.

The callback function of a timer does not appear to be executed in the same thread that spawned it. My demo program has all the window initialization in my main function, and then creates a timer with a closure passed as an argument to handle all the window drawing. Once the main function finishes executing, I'm not sure what happens to that thread.

Am I missing something fundamental here, or is this 'working as intended' under the covers with how the Dart event loop is constructed?

Another example of an event loop structure I'm familar with is libuv, where all asynchronous operations eventually return to the main application thread (https://github.com/joyent/libuv)

Thanks.

Florian Loitsch

unread,
Jul 17, 2013, 6:54:54 AM7/17/13
to General Dart Discussion, vm-...@dartlang.org, e...@dartlang.org
[+vm-dev, eng]


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
 
 



--
Give a man a fire and he's warm for the whole day,
but set fire to him and he's warm for the rest of his life. - Terry Pratchett

Seth Ladd

unread,
Jul 17, 2013, 1:11:11 PM7/17/13
to General Dart Discussion, vm-...@dartlang.org, e...@dartlang.org, Kathy Walrath
+ Kathy

Vijay Menon

unread,
Jul 17, 2013, 1:27:03 PM7/17/13
to General Dart Discussion, vm-...@dartlang.org, e...@dartlang.org, Kathy Walrath
Kyle,

I'm not sure I have enough contextual info, but you might take a look at the openglui embedder in the main dart repo (under runtime/embedders/openglui).  It's doing something similar, but wrapping the Skia C++ library.

One thing we had to be careful with was to interact with the main Dart isolate from the same C++ thread.  E.g., we couldn't start up the isolate on one OS thread and invoke functions from another.

Cheers,

Vijay

Kyle Gretchev

unread,
Sep 4, 2013, 6:41:08 PM9/4/13
to mi...@dartlang.org, vm-...@dartlang.org, e...@dartlang.org, Kathy Walrath
I've solved my problem, and I thought I'd share my (messy) solution with a helping of code snippets.

I pulled out the existing Dart_RunLoop() function and modified it behave as an event-loop system that can processes messages from other threads, or more accurately execute functions passed to it from other threads.

while (!data.done) {
ml.Wait();

if (signalFunc != nullptr) {
function<void(void)> func = *signalFunc;
func();
signalFunc = nullptr;
tmpMl.Notify();
}
}


Above is the modified while loop from the default Dart_RunLoop() function.The monitor locker was only signaled from the RunLoopDone() callback function, but now an additional signaling function which can be called from native extensions can have their code executed within this context.

DART_EXPORT void SignalMain(function<void(void)> func) {
mutex.Lock();
if (locker == nullptr) {
func();
} else {
signalFunc = &func;
locker->Notify();
tmpMl.Wait();
}

mutex.Unlock();
}

And now from a native extension that exposes the GLFW library to dart, I can do the following:

signalMain([]() {
glfwPollEvents();
});

glfwPollEvents() and other functions must only be run within the application's main thread. This little solution provides functionality to do this in a synchronous way.

Reply all
Reply to author
Forward
0 new messages