margo-timer.h 2.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * (C) 2016 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#ifndef __MARGO_TIMER
#define __MARGO_TIMER

#ifdef __cplusplus
extern "C" {
#endif

typedef void (*margo_timer_cb_fn)(void *);

Shane Snyder's avatar
Shane Snyder committed
16
typedef struct margo_timed_element
17 18 19 20
{
    margo_timer_cb_fn cb_fn;
    void *cb_dat;
    double expiration;
Shane Snyder's avatar
Shane Snyder committed
21 22 23
    struct margo_timed_element *next;
    struct margo_timed_element *prev;
} margo_timer_t;
24

25
/**
Shane Snyder's avatar
Shane Snyder committed
26
 * Initializes the margo timer interface
27 28
 * @param [in] mid Margo instance
 * @returns 0 on success, -1 on failure
29
 */
30 31
int margo_timer_instance_init(
    margo_instance_id mid);
32

33
/**
Shane Snyder's avatar
Shane Snyder committed
34
 * Shuts down the margo timer interface
35
 * @param [in] mid Margo instance
36
 */
37 38
void margo_timer_instance_finalize(
    margo_instance_id mid);
39

40
/**
Shane Snyder's avatar
Shane Snyder committed
41 42
 * Initializes a margo timer object which will perform some action
 * after a specified time duration
43
 * @param [in] mid Margo instance
Shane Snyder's avatar
Shane Snyder committed
44
 * @param [in] timer pointer to margo timer object to be initialized
45 46 47 48
 * @param [in] cb_fn callback function for timeout action
 * @param [in] cb_dat callback data passed to the callback function
 * @param [in] timeout_ms timeout duration in milliseconds
 */
Shane Snyder's avatar
Shane Snyder committed
49
void margo_timer_init(
50
    margo_instance_id mid,
Shane Snyder's avatar
Shane Snyder committed
51
    margo_timer_t *timer,
52 53
    margo_timer_cb_fn cb_fn,
    void *cb_dat,
Shane Snyder's avatar
Shane Snyder committed
54
    double timeout_ms);
55 56

/**
Shane Snyder's avatar
Shane Snyder committed
57
 * Destroys a margo timer object which was previously initialized
58
 * @param [in] mid Margo instance
Shane Snyder's avatar
Shane Snyder committed
59
 * @param [in] timer pointer to margo timer object to be destroyed
60
 */
Shane Snyder's avatar
Shane Snyder committed
61
void margo_timer_destroy(
62
    margo_instance_id mid,
Shane Snyder's avatar
Shane Snyder committed
63
    margo_timer_t *timer);
64

65 66
/**
 * Checks for expired timers and performs specified timeout action
67
 * @param [in] mid Margo instance
68
 */
69
void margo_check_timers(
70
    margo_instance_id mid);
71

72 73 74 75 76 77 78 79 80 81 82
/**
 * Determines the amount of time (in seconds) until the next timer
 * is set to expire
 * @param [in] mid Margo instance
 * @param [out] time until next timer expiration
 * @returns 0 when there is a queued timer which will expire, -1 otherwise
 */
int margo_timer_get_next_expiration(
    margo_instance_id mid,
    double *next_timer_exp);

83 84 85 86 87
#ifdef __cplusplus
}
#endif

#endif /* __MARGO_TIMER */