d2bfd86b08c45b3ed7758c300b25788c0184fdd3
[sliver-openvswitch.git] / lib / timer.h
1 /*
2  * Copyright (c) 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef TIMER_H
18 #define TIMER_H 1
19
20 #include <stdbool.h>
21
22 #include "timeval.h"
23
24 struct timer {
25     long long int t;
26 };
27
28 long long int timer_msecs_until_expired(const struct timer *);
29 void timer_wait(const struct timer *);
30
31 /* Causes 'timer' to expire when 'duration' milliseconds have passed.
32  *
33  * May be used to initialize 'timer'. */
34 static inline void
35 timer_set_duration(struct timer *timer, long long int duration)
36 {
37     timer->t = time_msec() + duration;
38 }
39
40 /* Causes 'timer' never to expire.
41  *
42  * May be used to initialize 'timer'. */
43 static inline void
44 timer_set_infinite(struct timer *timer)
45 {
46     timer->t = LLONG_MAX;
47 }
48
49 /* Causes 'timer' to expire immediately.
50  *
51  * May be used to initialize 'timer'. */
52 static inline void
53 timer_set_expired(struct timer *timer)
54 {
55     timer->t = LLONG_MIN;
56 }
57
58 /* True if 'timer' had (or will have) expired at 'time'. */
59 static inline bool
60 timer_expired_at(const struct timer *timer, long long int time)
61 {
62     return time >= timer->t;
63 }
64
65 /* True if 'timer' has expired. */
66 static inline bool
67 timer_expired(const struct timer *timer)
68 {
69     return timer_expired_at(timer, time_msec());
70 }
71
72 /* Returns ture if 'timer' will never expire. */
73 static inline bool
74 timer_is_infinite(const struct timer *timer)
75 {
76     return timer->t == LLONG_MAX;
77 }
78
79 #endif /* timer.h */