cfm: Fix broken fault logic.
[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 long long int timer_enabled_at(const struct timer *, long long int duration);
30 void timer_wait(const struct timer *);
31
32 /* Causes 'timer' to expire when 'duration' milliseconds have passed.
33  *
34  * May be used to initialize 'timer'. */
35 static inline void
36 timer_set_duration(struct timer *timer, long long int duration)
37 {
38     timer->t = time_msec() + duration;
39 }
40
41 /* Causes 'timer' never to expire.
42  *
43  * May be used to initialize 'timer'. */
44 static inline void
45 timer_set_infinite(struct timer *timer)
46 {
47     timer->t = LLONG_MAX;
48 }
49
50 /* Causes 'timer' to expire immediately.
51  *
52  * May be used to initialize 'timer'. */
53 static inline void
54 timer_set_expired(struct timer *timer)
55 {
56     timer->t = LLONG_MIN;
57 }
58
59 /* True if 'timer' had (or will have) expired at 'time'. */
60 static inline bool
61 timer_expired_at(const struct timer *timer, long long int time)
62 {
63     return time >= timer->t;
64 }
65
66 /* True if 'timer' has expired. */
67 static inline bool
68 timer_expired(const struct timer *timer)
69 {
70     return timer_expired_at(timer, time_msec());
71 }
72
73 /* Returns ture if 'timer' will never expire. */
74 static inline bool
75 timer_is_infinite(const struct timer *timer)
76 {
77     return timer->t == LLONG_MAX;
78 }
79
80 #endif /* timer.h */