diff options
author | Hoa V. Dinh <dinh.viet.hoa@gmail.com> | 2014-11-11 16:39:57 -0800 |
---|---|---|
committer | Hoa V. Dinh <dinh.viet.hoa@gmail.com> | 2014-11-11 16:39:57 -0800 |
commit | db59b2dae2cb60c0f5f0c2181228b59140e52c90 (patch) | |
tree | 52236bc459f2e8377b710e33ce6a8ce3dd21f1cb /src/core | |
parent | 9800b912589a57a3e622753a30d72857334b1c29 (diff) |
Implemented MCMainThread for GTK
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/basetypes/MCMainThreadGTK.cpp | 93 | ||||
-rw-r--r-- | src/core/basetypes/MCMainThreadMac.mm | 2 |
2 files changed, 93 insertions, 2 deletions
diff --git a/src/core/basetypes/MCMainThreadGTK.cpp b/src/core/basetypes/MCMainThreadGTK.cpp new file mode 100644 index 00000000..fdcca867 --- /dev/null +++ b/src/core/basetypes/MCMainThreadGTK.cpp @@ -0,0 +1,93 @@ +// +// MCMainThreadGTK.cpp +// mailcore2 +// +// Created by Hoa Dinh on 11/11/14. +// Copyright (c) 2014 MailCore. All rights reserved. +// + +#include "MCMainThread.h" + +#include <glib.h> +#include <stdlib.h> +#include <libetpan/libetpan.h> + +struct main_thread_call_data { + void (* function)(void *); + void * context; + struct mailsem * sem; +}; + +static gboolean main_thread_wrapper(struct main_thread_call_data * data) +{ + data->function(data->context); + free(data); + return G_SOURCE_REMOVE; +} + +void mailcore::callOnMainThread(void (* function)(void *), void * context) +{ + struct main_thread_call_data * data = (struct main_thread_call_data *) malloc(sizeof(* data)); + data->function = function; + data->context = context; + data->sem = NULL; + g_idle_add((GSourceFunc) main_thread_wrapper, (gpointer) data); +} + +static gboolean main_thread_wait_wrapper(struct main_thread_call_data * data) +{ + data->function(data->context); + mailsem_up(data->sem); + return G_SOURCE_REMOVE; +} + +void mailcore::callOnMainThreadAndWait(void (* function)(void *), void * context) +{ + struct main_thread_call_data * data = (struct main_thread_call_data *) malloc(sizeof(* data)); + data->function = function; + data->context = context; + data->sem = mailsem_new(); + g_idle_add((GSourceFunc) main_thread_wait_wrapper, (gpointer) data); + + // Wait. + mailsem_down(data->sem); + + mailsem_free(data->sem); + free(data); +} + +struct call_after_delay_data { + void (* function)(void *); + void * context; + guint source_id; +}; + +static gboolean call_after_delay_wrapper(struct main_thread_call_data * data) +{ + data->function(data->context); + free(data); + return G_SOURCE_REMOVE; +} + +static void call_after_delay_destroy_notify(struct main_thread_call_data * data) +{ + free(data); +} + +void * mailcore::callAfterDelay(void (* function)(void *), void * context, double time) +{ + struct call_after_delay_data * data = (struct call_after_delay_data *) malloc(sizeof(* data)); + data->function = function; + data->context = context; + data->source_id = g_timeout_add_full(G_PRIORITY_DEFAULT, (guint) (time * 1000), + (GSourceFunc) call_after_delay_wrapper, data, + (GDestroyNotify) call_after_delay_destroy_notify); + return data; +} + +void mailcore::cancelDelayedCall(void * delayedCall) +{ + struct call_after_delay_data * data = (struct call_after_delay_data *) delayedCall; + g_source_remove(data->source_id); + free(data); +} diff --git a/src/core/basetypes/MCMainThreadMac.mm b/src/core/basetypes/MCMainThreadMac.mm index 8ecb7cae..5f6c5104 100644 --- a/src/core/basetypes/MCMainThreadMac.mm +++ b/src/core/basetypes/MCMainThreadMac.mm @@ -68,7 +68,6 @@ void mailcore::callOnMainThread(void (* function)(void *), void * context) [caller setFunction:function]; [caller setContext:context]; [caller performSelectorOnMainThread:@selector(call) withObject:nil waitUntilDone:NO]; - //[caller release]; } void mailcore::callOnMainThreadAndWait(void (* function)(void *), void * context) @@ -78,7 +77,6 @@ void mailcore::callOnMainThreadAndWait(void (* function)(void *), void * context [caller setFunction:function]; [caller setContext:context]; [caller performSelectorOnMainThread:@selector(call) withObject:nil waitUntilDone:YES]; - //[caller release]; } void * mailcore::callAfterDelay(void (* function)(void *), void * context, double time) |