Permalink
Browse files

Convert triggers from mainloop to qbloop

  • Loading branch information...
beekhof committed Jan 18, 2012
1 parent 8de7f5d commit de4b7b4798c43efbb4cec2a5577ed32b7ec9d932
Showing with 37 additions and 86 deletions.
  1. +0 −1 include/crm/common/mainloop.h
  2. +37 −85 lib/common/mainloop.c
@@ -21,7 +21,6 @@
# include <glib.h>
typedef struct trigger_s {
- GSource source;
gboolean trigger;
void *user_data;
guint id;
View
@@ -29,110 +29,62 @@
#include <crm/crm.h>
#include <crm/common/mainloop.h>
-static gboolean
-crm_trigger_prepare(GSource * source, gint * timeout)
+crm_trigger_t *
+mainloop_add_trigger(int priority, gboolean(*dispatch) (gpointer user_data), gpointer userdata)
{
- crm_trigger_t *trig = (crm_trigger_t *) source;
-
- /* cluster-glue's FD and IPC related sources make use of
- * g_source_add_poll() but do not set a timeout in their prepare
- * functions
- *
- * This means mainloop's poll() will block until an event for one
- * of these sources occurs - any /other/ type of source, such as
- * this one or g_idle_*, that doesn't use g_source_add_poll() is
- * S-O-L and wont be processed until there is something fd-based
- * happens.
- *
- * Luckily the timeout we can set here affects all sources and
- * puts an upper limit on how long poll() can take.
- *
- * So unconditionally set a small-ish timeout, not too small that
- * we're in constant motion, which will act as an upper bound on
- * how long the signal handling might be delayed for.
- */
- *timeout = 500; /* Timeout in ms */
-
- return trig->trigger;
-}
+ crm_trigger_t *source = (crm_trigger_t *) userdata;
-static gboolean
-crm_trigger_check(GSource * source)
-{
- crm_trigger_t *trig = (crm_trigger_t *) source;
+ source->trigger = FALSE;
+ source->user_data = userdata;
+ source->dispatch = dispatch;
+ source->priority = priority;
- return trig->trigger;
+ return source;
}
-static gboolean
-crm_trigger_dispatch(GSource * source, GSourceFunc callback, gpointer userdata)
+int mainloop_trigger_dispatch(void *userdata)
{
- crm_trigger_t *trig = (crm_trigger_t *) source;
-
- trig->trigger = FALSE;
-
- if (callback) {
- return callback(trig->user_data);
+ crm_trigger_t *source = (crm_trigger_t *) userdata;
+
+ if(source->trigger && source->delete == FALSE) {
+ source->trigger = FALSE;
+ if(source->dispatch) {
+ if(source->dispatch(userdata) == FALSE) {
+ source->delete = TRUE;
+ }
+ }
}
- return TRUE;
-}
-
-static GSourceFuncs crm_trigger_funcs = {
- crm_trigger_prepare,
- crm_trigger_check,
- crm_trigger_dispatch,
- NULL
-};
-
-static crm_trigger_t *
-mainloop_setup_trigger(GSource * source, int priority, gboolean(*dispatch) (gpointer user_data),
- gpointer userdata)
-{
- crm_trigger_t *trigger = NULL;
- trigger = (crm_trigger_t *) source;
-
- trigger->id = 0;
- trigger->trigger = FALSE;
- trigger->user_data = userdata;
-
- if (dispatch) {
- g_source_set_callback(source, dispatch, trigger, NULL);
+ if(source->delete) {
+ crm_free(source);
}
-
- g_source_set_priority(source, priority);
- g_source_set_can_recurse(source, FALSE);
-
- trigger->id = g_source_attach(source, NULL);
- return trigger;
-}
-
-crm_trigger_t *
-mainloop_add_trigger(int priority, gboolean(*dispatch) (gpointer user_data), gpointer userdata)
-{
- GSource *source = NULL;
-
- CRM_ASSERT(sizeof(crm_trigger_t) > sizeof(GSource));
- source = g_source_new(&crm_trigger_funcs, sizeof(crm_trigger_t));
- CRM_ASSERT(source != NULL);
-
- return mainloop_setup_trigger(source, priority, dispatch, userdata);
+ return 0;
}
void
mainloop_set_trigger(crm_trigger_t * source)
{
- source->trigger = TRUE;
+ int rc = 0;
+ if(source->trigger == FALSE) {
+ source->trigger = TRUE;
+ rc = qb_loop_job_add(qb_loop_t *l,
+ source->priority,
+ source->user_data,
+ mainloop_trigger_dispatch);
+ if(rc != 0) {
+ crm_err("Couldn't trigger execution of source %p", source);
+ }
+ }
}
gboolean
mainloop_destroy_trigger(crm_trigger_t * source)
{
- source->trigger = FALSE;
- if (source->id > 0) {
- g_source_remove(source->id);
+ source->delete = TRUE;
+ if(source->trigger == FALSE) {
+ mainloop_trigger_dispatch(source->user_data);
}
- return TRUE;
+ return source->trigger;
}
typedef struct signal_s {
@@ -224,7 +176,7 @@ mainloop_add_signal(int sig, void (*dispatch) (int sig))
CRM_ASSERT(sizeof(crm_signal_t) > sizeof(GSource));
source = g_source_new(&crm_signal_funcs, sizeof(crm_signal_t));
- crm_signals[sig] = (crm_signal_t *) mainloop_setup_trigger(source, priority, NULL, NULL);
+ crm_signals[sig] = (crm_signal_t *) mainloop_add_trigger(source, priority, NULL, NULL);
CRM_ASSERT(crm_signals[sig] != NULL);
crm_signals[sig]->handler = dispatch;

0 comments on commit de4b7b4

Please sign in to comment.