rabbitmq-c  0.8.0
C AMQP Client library for RabbitMQ
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
amqp_time.h
1 /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
2 /*
3  * Portions created by Alan Antonuk are Copyright (c) 2013-2014 Alan Antonuk.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef AMQP_TIMER_H
25 #define AMQP_TIMER_H
26 
27 #include <stdint.h>
28 
29 #ifdef _WIN32
30 # ifndef WINVER
31 # define WINVER 0x0502
32 # endif
33 # ifndef WIN32_LEAN_AND_MEAN
34 # define WIN32_LEAN_AND_MEAN
35 # endif
36 # include <Winsock2.h>
37 #else
38 # include <sys/time.h>
39 #endif
40 
41 #define AMQP_MS_PER_S 1000
42 #define AMQP_US_PER_MS 1000
43 #define AMQP_NS_PER_S 1000000000
44 #define AMQP_NS_PER_MS 1000000
45 #define AMQP_NS_PER_US 1000
46 
47 /* This represents a point in time in reference to a monotonic clock.
48  *
49  * The internal representation is ns, relative to the monotonic clock.
50  *
51  * There are two 'special' values:
52  * - 0: means 'this instant', its meant for polls with a 0-timeout, or
53  * non-blocking option
54  * - UINT64_MAX: means 'at infinity', its mean for polls with an infinite
55  * timeout
56  */
57 typedef struct amqp_time_t_ {
58  uint64_t time_point_ns;
59 } amqp_time_t;
60 
61 /* Gets a monotonic timestamp. This will return 0 if the underlying call to the
62  * system fails.
63  */
64 uint64_t amqp_get_monotonic_timestamp(void);
65 
66 /* Get a amqp_time_t that is timeout from now.
67  * If timeout is NULL, an amqp_time_infinite() is created.
68  * If timeout = {0, 0}, an amqp_time_immediate() is created.
69  *
70  * Returns AMQP_STATUS_OK on success.
71  * AMQP_STATUS_INVALID_PARAMETER if timeout is invalid
72  * AMQP_STATUS_TIMER_FAILURE if the underlying call to get the current timestamp
73  * fails.
74  */
75 int amqp_time_from_now(amqp_time_t *time, struct timeval *timeout);
76 
77 /* Get a amqp_time_t that is seconds from now.
78  * If seconds <= 0, then amqp_time_infinite() is created.
79  *
80  * Returns AMQP_STATUS_OK on success.
81  * AMQP_STATUS_TIMER_FAILURE if the underlying call to get the current timestamp
82  * fails.
83  */
84 int amqp_time_s_from_now(amqp_time_t *time, int seconds);
85 
86 /* Create an immediate amqp_time_t */
87 amqp_time_t amqp_time_immediate(void);
88 
89 /* Create an infinite amqp_time_t */
90 amqp_time_t amqp_time_infinite(void);
91 
92 /* Gets the number of ms until the amqp_time_t, suitable for the timeout
93  * parameter in poll().
94  *
95  * -1 will be returned for amqp_time_infinite values.
96  * 0 will be returned for amqp_time_immediate values.
97  * AMQP_STATUS_TIMEOUT will be returned if time was in the past.
98  * AMQP_STATUS_TIMER_FAILURE will be returned if the underlying call to get the
99  * current timestamp fails.
100  */
101 int amqp_time_ms_until(amqp_time_t time);
102 
103 /* Gets a timeval filled in with the time until amqp_time_t. Suitable for the
104  * parameter in select().
105  *
106  * The in parameter specifies a storage location for *out.
107  * If time is an inf timeout, then *out = NULL.
108  * If time is a 0-timeout or the timer has expired, then *out = {0, 0}
109  * Otherwise *out is set to the time left on the time.
110  *
111  * AMQP_STATUS_OK will be returned if successfully filled.
112  * AMQP_STATUS_TIMER_FAILURE is returned when the underlying call to get the
113  * current timestamp fails.
114  */
115 int amqp_time_tv_until(amqp_time_t time, struct timeval *in,
116  struct timeval **out);
117 
118 /* Test whether current time is past the provided time.
119  *
120  * TODO: this isn't a great interface to use. Fix this.
121  *
122  * Return AMQP_STATUS_OK if time has not past
123  * Return AMQP_STATUS_TIMEOUT if time has past
124  * Return AMQP_STATUS_TIMER_FAILURE if the underlying call to get the current
125  * timestamp fails.
126  */
127 int amqp_time_has_past(amqp_time_t time);
128 
129 /* Return the time value that happens first */
130 amqp_time_t amqp_time_first(amqp_time_t l, amqp_time_t r);
131 
132 int amqp_time_equal(amqp_time_t l, amqp_time_t r);
133 #endif /* AMQP_TIMER_H */
Definition: amqp_time.h:57