static void su_source_decref(su_port_t *self, int blocking, char const *who);
static struct _GSource *su_source_gsource(su_port_t *port);
-static int su_source_send(su_port_t *self, su_msg_r rmsg);
-
static int su_source_register(su_port_t *self,
su_root_t *root,
su_wait_t *wait,
static int su_source_remove_prepoll(su_port_t *port,
su_root_t *root);
static int su_source_multishot(su_port_t *self, int multishot);
+static int su_source_wakeup(su_port_t *self);
+static int su_source_is_running(su_port_t *self);
static char const *su_source_name(su_port_t const *self);
su_source_gsource,
- su_source_send,
+ su_base_port_send,
su_source_register,
su_source_unregister,
su_source_deregister,
su_base_port_start_shared,
su_base_port_wait,
NULL,
+ su_base_port_deferrable,
+ su_base_port_max_defer,
+ su_source_wakeup,
+ su_source_is_running,
}};
static char const *su_source_name(su_port_t const *self)
su_source_port_deinit(ss->ss_port);
}
-void su_source_port_lock(su_port_t *self, char const *who)
-{
- PORT_LOCK_DEBUG(("%p at %s locking(%p)...",
- (void *)g_thread_self(), who, self));
-
- g_static_mutex_lock(self->sup_mutex);
-
- PORT_LOCK_DEBUG((" ...%p at %s locked(%p)...",
- (void *)g_thread_self(), who, self));
-}
-
-void su_source_port_unlock(su_port_t *self, char const *who)
-{
- g_static_mutex_unlock(self->sup_mutex);
-
- PORT_LOCK_DEBUG((" ...%p at %s unlocked(%p)\n",
- (void *)g_thread_self(), who, self));
-}
-
/** @internal Send a message to the port. */
-int su_source_send(su_port_t *self, su_msg_r rmsg)
+int su_source_wakeup(su_port_t *self)
{
- int wakeup = su_base_port_send(self, rmsg);
- GMainContext *gmc;
-
- if (wakeup < 0)
- return -1;
- if (wakeup == 0)
- return 0;
-
- gmc = g_source_get_context(self->sup_source);
+ GMainContext *gmc = g_source_get_context(self->sup_source);
if (gmc)
g_main_context_wakeup(gmc);
* @return Number of wait objects removed.
*/
int su_source_unregister_all(su_port_t *self,
- su_root_t *root)
+ su_root_t *root)
{
unsigned i, j;
unsigned n_waits;
/**Set mask for a registered event. @internal
*
- * The function su_source_eventmask() sets the mask describing events that can
- * signal the registered callback.
+ * Sets the mask describing events that can signal the registered callback.
*
* @param port pointer to port object
* @param index registration index
* @retval 0 when successful,
* @retval -1 upon an error.
*/
+static
int su_source_eventmask(su_port_t *self, int index, int socket, int events)
{
unsigned n;
}
-/** @internal Main loop.
- *
- * The function @c su_source_run() runs the main loop
+/** @internal Run the main loop.
*
- * The function @c su_source_run() runs until @c su_source_break() is called
- * from a callback.
+ * The main loop runs until su_source_break() is called from a callback.
*
- * @param self pointer to root object
+ * @param self pointer to port object
* */
+static
void su_source_run(su_port_t *self)
{
GMainContext *gmc;
}
}
+static int su_source_is_running(su_port_t *self)
+{
+ return self->sup_main_loop && g_main_loop_is_running(self->sup_main_loop);
+}
+
/** @internal
* The function @c su_source_break() is used to terminate execution of @c
* su_source_run(). It can be called from a callback function.
* @param self pointer to port
*
*/
+static
void su_source_break(su_port_t *self)
{
enter;
{
su_port_prefer(su_source_port_create, NULL);
}
-
struct su_timer_s {
su_task_r sut_task; /**< Task reference */
- size_t sut_heap_index; /**< Timer is set (inserted in heap) */
+ size_t sut_set; /**< Timer is set (inserted in heap) */
su_time_t sut_when; /**< When timer should be waken up next time */
su_duration_t sut_duration; /**< Timer duration */
su_timer_f sut_wakeup; /**< Function to call when waken up */
su_timer_arg_t *sut_arg; /**< Pointer to argument data */
- su_time_t sut_run; /**< When this timer was last waken up */
unsigned sut_woken; /**< Timer has waken up this many times */
unsigned sut_running:2;/**< Timer is running */
run_for_ever = 2 /**< Do not compensate */
};
-#define SU_TIMER_IS_SET(sut) ((sut)->sut_heap_index != 0)
+#define SU_TIMER_IS_SET(sut) ((sut)->sut_set != 0)
HEAP_DECLARE(su_inline, su_timer_queue_t, timers_, su_timer_t *);
su_inline void timers_set(su_timer_t **array, size_t index, su_timer_t *t)
{
- array[t->sut_heap_index = index] = t;
+ array[t->sut_set = index] = t;
}
su_inline int timers_less(su_timer_t *a, su_timer_t *b)
return -1;
if (SU_TIMER_IS_SET(t))
- timers_remove(timers[0], t->sut_heap_index);
+ timers_remove(timers[0], t->sut_set);
t->sut_wakeup = wakeup;
t->sut_arg = arg;
}
}
+/** Check if the timer has been set.
+ *
+ * @param t pointer to a timer object
+ *
+ * @return Nonzero if set, zero if reset.
+ *
+ * @NEW_1_12_11
+ */
+int su_timer_is_set(su_timer_t const *t)
+{
+ return t && t->sut_set != 0;
+}
+
+/**Return when the timer has been last expired.
+ *
+ * @param t pointer to a timer object
+ *
+ * @return Timestamp (as returned by su_time()).
+ *
+ * @note If the timer is running (set with su_timer_run()), the returned
+ * timestamp not the actual time but it is rather calculated from the
+ * initial timestamp.
+ *
+ * @NEW_1_12_11
+ */
+su_time_t su_timer_latest(su_timer_t const *t)
+{
+ su_time_t tv = { 0, 0 };
+
+ return t ? t->sut_when : tv;
+}
/** Set the timer for the given @a interval.
*
su_timer_arg_t *arg)
{
su_timer_queue_t *timers = su_timer_queue(t, 1, "su_timer_run");
- su_time_t now;
if (timers == NULL)
return -1;
t->sut_running = run_at_intervals;
- t->sut_run = now = su_now();
t->sut_woken = 0;
- return su_timer_set0(timers, t, wakeup, arg, now, t->sut_duration);
+ return su_timer_set0(timers, t, wakeup, arg, su_now(), t->sut_duration);
}
/**Set the timer for regular intervals.
su_timer_arg_t *arg)
{
su_timer_queue_t *timers = su_timer_queue(t, 1, "su_timer_set_for_ever");
- su_time_t now;
if (timers == NULL)
return -1;
t->sut_running = run_for_ever;
- t->sut_run = now = su_now();
t->sut_woken = 0;
- return su_timer_set0(timers, t, wakeup, arg, now, t->sut_duration);
+ return su_timer_set0(timers, t, wakeup, arg, su_now(), t->sut_duration);
}
/**Reset the timer.
return -1;
if (SU_TIMER_IS_SET(t))
- timers_remove(timers[0], t->sut_heap_index);
+ timers_remove(timers[0], t->sut_set);
t->sut_wakeup = NULL;
t->sut_arg = NULL;
t->sut_running = reset;
- memset(&t->sut_run, 0, sizeof(t->sut_run));
-
return 0;
}
if (t->sut_running == run_at_intervals) {
while (t->sut_running == run_at_intervals &&
+ t->sut_set == 0 &&
t->sut_duration > 0) {
if (su_time_diff(t->sut_when, now) > 0) {
- su_timer_set0(timers, t, f, t->sut_arg, t->sut_run, 0);
+ su_timer_set0(timers, t, f, t->sut_arg, t->sut_when, 0);
break;
}
- t->sut_when = t->sut_run = su_time_add(t->sut_run, t->sut_duration);
+ t->sut_when = su_time_add(t->sut_when, t->sut_duration);
t->sut_woken++;
f(su_root_magic(su_task_root(t->sut_task)), t, t->sut_arg), n++;
}
t->sut_woken++;
t->sut_when = now;
f(su_root_magic(su_task_root(t->sut_task)), t, t->sut_arg), n++;
- if (t->sut_running == run_for_ever)
+ if (t->sut_running == run_for_ever && t->sut_set == 0)
su_timer_set0(timers, t, f, t->sut_arg, now, t->sut_duration);
}
else {