lsquic_alarmset.c revision 229fce07
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_alarmset.c -- A set of alarms
4 */
5
6#include <assert.h>
7#include <string.h>
8
9#include "lsquic_types.h"
10#include "lsquic_alarmset.h"
11
12#define LSQUIC_LOGGER_MODULE LSQLM_ALARMSET
13#define LSQUIC_LOG_CONN_ID alset->as_cid
14#include "lsquic_logger.h"
15
16
17void
18lsquic_alarmset_init (lsquic_alarmset_t *alset, lsquic_cid_t cid)
19{
20    alset->as_cid       = cid;
21    alset->as_armed_set = 0;
22}
23
24
25void
26lsquic_alarmset_init_alarm (lsquic_alarmset_t *alset, enum alarm_id al_id,
27                            lsquic_alarm_cb_f callback, void *cb_ctx)
28{
29    alset->as_alarms[ al_id ].callback = callback;
30    alset->as_alarms[ al_id ].cb_ctx   = cb_ctx;
31}
32
33
34void
35lsquic_alarmset_ring_expired (lsquic_alarmset_t *alset, lsquic_time_t now)
36{
37    enum alarm_id_bit armed_set;
38    enum alarm_id al_id;
39
40    for (al_id = 0, armed_set = alset->as_armed_set;
41            al_id < MAX_LSQUIC_ALARMS && armed_set;
42                armed_set &= ~(1 << al_id), ++al_id)
43        if (armed_set & (1 << al_id))
44        {
45            if (alset->as_expiry[al_id] < now)
46            {
47                alset->as_armed_set &= ~(1 << al_id);
48                LSQ_INFO("ring expired alarm %d", al_id);
49                alset->as_alarms[al_id].callback(
50                                alset->as_alarms[al_id].cb_ctx,
51                                alset->as_expiry[al_id], now);
52            }
53        }
54}
55
56
57lsquic_time_t
58lsquic_alarmset_mintime (const lsquic_alarmset_t *alset)
59{
60    lsquic_time_t expiry;
61    enum alarm_id al_id;
62
63    if (alset->as_armed_set)
64    {
65        expiry = UINT64_MAX;
66        for (al_id = 0; al_id < MAX_LSQUIC_ALARMS; ++al_id)
67            if ((alset->as_armed_set & (1 << al_id))
68                                && alset->as_expiry[al_id] < expiry)
69            {
70                expiry = alset->as_expiry[al_id];
71            }
72        return expiry;
73    }
74    else
75        return 0;
76}
77