margo-timer.h 2.2 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
/**
26 27
 * Creates a margo_timer_list.
 * @returns a new margo_timer_list, or NULL if failed 
28
 */
29
struct margo_timer_list* margo_timer_list_create();
30

31
/**
32 33
 * Frees the timer list
 * @param [in] timer_lst timer list to free
34
 */
35
void margo_timer_list_free(struct margo_timer_list* timer_lst);
36

37
/**
Shane Snyder's avatar
Shane Snyder committed
38 39
 * Initializes a margo timer object which will perform some action
 * after a specified time duration
40
 * @param [in] mid Margo instance
Shane Snyder's avatar
Shane Snyder committed
41
 * @param [in] timer pointer to margo timer object to be initialized
42 43 44 45
 * @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
46
void margo_timer_init(
47
    margo_instance_id mid,
Shane Snyder's avatar
Shane Snyder committed
48
    margo_timer_t *timer,
49 50
    margo_timer_cb_fn cb_fn,
    void *cb_dat,
Shane Snyder's avatar
Shane Snyder committed
51
    double timeout_ms);
52 53

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

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

69 70 71 72 73 74 75 76 77 78 79
/**
 * 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);

80 81 82 83 84 85
/**
 * Gets the margo_timer_list from the margo instance.
 * This function is defined in margo.c.
 */
struct margo_timer_list *margo_get_timer_list(margo_instance_id mid);

86 87 88 89 90
#ifdef __cplusplus
}
#endif

#endif /* __MARGO_TIMER */