event: add function BlockingCall()
Replaces io_thread_call(). This approach is more generic and easier to use due to std::function.
This commit is contained in:
@@ -115,53 +115,3 @@ io_thread_inside(void)
|
||||
{
|
||||
return io.thread != NULL && g_thread_self() == io.thread;
|
||||
}
|
||||
|
||||
struct call_data {
|
||||
GThreadFunc function;
|
||||
gpointer data;
|
||||
bool done;
|
||||
gpointer result;
|
||||
};
|
||||
|
||||
static gboolean
|
||||
io_thread_call_func(gpointer _data)
|
||||
{
|
||||
struct call_data *data = (struct call_data *)_data;
|
||||
|
||||
gpointer result = data->function(data->data);
|
||||
|
||||
io.mutex.lock();
|
||||
data->done = true;
|
||||
data->result = result;
|
||||
io.cond.broadcast();
|
||||
io.mutex.unlock();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
gpointer
|
||||
io_thread_call(GThreadFunc function, gpointer _data)
|
||||
{
|
||||
assert(io.thread != NULL);
|
||||
|
||||
if (io_thread_inside())
|
||||
/* we're already in the I/O thread - no
|
||||
synchronization needed */
|
||||
return function(_data);
|
||||
|
||||
struct call_data data = {
|
||||
function,
|
||||
_data,
|
||||
false,
|
||||
nullptr,
|
||||
};
|
||||
|
||||
io.loop->AddIdle(io_thread_call_func, &data);
|
||||
|
||||
io.mutex.lock();
|
||||
while (!data.done)
|
||||
io.cond.wait(io.mutex);
|
||||
io.mutex.unlock();
|
||||
|
||||
return data.result;
|
||||
}
|
||||
|
Reference in New Issue
Block a user