1 | /* |
2 | * librdkafka - Apache Kafka C library |
3 | * |
4 | * Copyright (c) 2012-2013, Magnus Edenhill |
5 | * All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions are met: |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
13 | * this list of conditions and the following disclaimer in the documentation |
14 | * and/or other materials provided with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #ifndef _RDKAFKA_TIMER_H_ |
30 | #define _RDKAFKA_TIMER_H_ |
31 | |
32 | #include "rd.h" |
33 | |
34 | /* A timer engine. */ |
35 | typedef struct rd_kafka_timers_s { |
36 | |
37 | TAILQ_HEAD(, rd_kafka_timer_s) rkts_timers; |
38 | |
39 | struct rd_kafka_s *rkts_rk; |
40 | |
41 | mtx_t rkts_lock; |
42 | cnd_t rkts_cond; |
43 | |
44 | int rkts_enabled; |
45 | } rd_kafka_timers_t; |
46 | |
47 | |
48 | typedef struct rd_kafka_timer_s { |
49 | TAILQ_ENTRY(rd_kafka_timer_s) rtmr_link; |
50 | |
51 | rd_ts_t rtmr_next; |
52 | rd_ts_t rtmr_interval; /* interval in microseconds */ |
53 | rd_bool_t rtmr_oneshot; /**< Only fire once. */ |
54 | |
55 | void (*rtmr_callback) (rd_kafka_timers_t *rkts, void *arg); |
56 | void *rtmr_arg; |
57 | } rd_kafka_timer_t; |
58 | |
59 | |
60 | |
61 | int rd_kafka_timer_stop (rd_kafka_timers_t *rkts, |
62 | rd_kafka_timer_t *rtmr, int lock); |
63 | void rd_kafka_timer_start0 (rd_kafka_timers_t *rkts, |
64 | rd_kafka_timer_t *rtmr, rd_ts_t interval, |
65 | rd_bool_t oneshot, |
66 | void (*callback) (rd_kafka_timers_t *rkts, |
67 | void *arg), |
68 | void *arg); |
69 | #define rd_kafka_timer_start(rkts,rtmr,interval,callback,arg) \ |
70 | rd_kafka_timer_start0(rkts,rtmr,interval,rd_false,callback,arg) |
71 | #define rd_kafka_timer_start_oneshot(rkts,rtmr,interval,callback,arg) \ |
72 | rd_kafka_timer_start0(rkts,rtmr,interval,rd_true,callback,arg) |
73 | |
74 | void rd_kafka_timer_backoff (rd_kafka_timers_t *rkts, |
75 | rd_kafka_timer_t *rtmr, int backoff_us); |
76 | rd_ts_t rd_kafka_timer_next (rd_kafka_timers_t *rkts, rd_kafka_timer_t *rtmr, |
77 | int do_lock); |
78 | |
79 | void rd_kafka_timers_interrupt (rd_kafka_timers_t *rkts); |
80 | rd_ts_t rd_kafka_timers_next (rd_kafka_timers_t *rkts, int timeout_ms, |
81 | int do_lock); |
82 | void rd_kafka_timers_run (rd_kafka_timers_t *rkts, int timeout_us); |
83 | void rd_kafka_timers_destroy (rd_kafka_timers_t *rkts); |
84 | void rd_kafka_timers_init (rd_kafka_timers_t *rkte, rd_kafka_t *rk); |
85 | |
86 | #endif /* _RDKAFKA_TIMER_H_ */ |
87 | |