summaryrefslogtreecommitdiff
path: root/open-vm-tools/libvmtools/monotonicTimer.c
blob: eb2fb3480f8995d107f00db19d34532bcd0eed20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*********************************************************
 * Copyright (C) 2008 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/**
 * @file monotonicTimer.c
 *
 * A GSource that implements a timer backed by a monotonic time source.
 */

#include <limits.h>
#include "vmware.h"
#include "system.h"
#include "vmware/tools/utils.h"

typedef struct MTimerSource {
   GSource     src;
   gint        timeout;
   uint64      last;
} MTimerSource;


/*
 *******************************************************************************
 * MTimerSourcePrepare --                                                 */ /**
 *
 * Callback for the "prepare()" event source function. Sets the timeout to
 * the number of milliseconds this timer expects to sleep for. If the timeout
 * has already expired, update the internal state tracking the last time the
 * timer was fired.
 *
 * @param[in]  src         The source.
 * @param[out] timeout     Where to store the timeout.
 *
 * @return TRUE if timeout has already expired.
 *
 *******************************************************************************
 */

static gboolean
MTimerSourcePrepare(GSource *src,
                    gint *timeout)
{
   MTimerSource *timer = (MTimerSource *) src;

   if (timer->timeout == 0) {
      *timeout = 0;
      return TRUE;
   } else {
         uint64 now = System_GetTimeMonotonic() * 10;
         uint64 diff;

         ASSERT(now >= timer->last);

         diff = now - timer->last;
         if (diff >= timer->timeout) {
            timer->last = now;
            *timeout = 0;
            return TRUE;
         }

      *timeout = MIN(INT_MAX, timer->timeout - diff);
      return FALSE;
   }
}


/*
 *******************************************************************************
 * MTimerSourceCheck --                                                   */ /**
 *
 * Checks whether the timeout has expired.
 *
 * @param[in]  src     The source.
 *
 * @return Whether the timeout has expired.
 *
 *******************************************************************************
 */

static gboolean
MTimerSourceCheck(GSource *src)
{
   gint unused;
   return MTimerSourcePrepare(src, &unused);
}


/*
 *******************************************************************************
 * MTimerSourceDispatch --                                                */ /**
 *
 * Calls the callback associated with the timer, if any.
 *
 * @param[in]  src         Unused.
 * @param[in]  callback    The callback to be called.
 * @param[in]  data        User-supplied data.
 *
 * @return The return value of the callback, or FALSE if the callback is NULL.
 *
 *******************************************************************************
 */

static gboolean
MTimerSourceDispatch(GSource *src,
                     GSourceFunc callback,
                     gpointer data)
{
   return (callback != NULL) ? callback(data) : FALSE;
}


/*
 *******************************************************************************
 * MTimerSourceFinalize --                                                */ /**
 *
 * Does nothing. The main glib code already does all the cleanup needed.
 *
 * @param[in]  src     The source.
 *
 *******************************************************************************
 */

static void
MTimerSourceFinalize(GSource *src)
{
}


/**
 *
 * @addtogroup vmtools_utils
 * @{
 */

/*
 *******************************************************************************
 * VMTools_CreateTimer --                                                 */ /**
 *
 * @brief Create a timer based on a monotonic clock source.
 *
 * This timer differs from the glib timeout source, which uses the system time.
 * It is recommended for code that needs more reliable time tracking, using a
 * clock that is not affected by changes in the system time (which can happen
 * when using NTP or the Tools time synchronization feature).
 *
 * @param[in] timeout   The timeout for the timer, must be >= 0.
 *
 * @return The new source.
 *
 *******************************************************************************
 */

GSource *
VMTools_CreateTimer(gint timeout)
{
   static GSourceFuncs srcFuncs = {
      MTimerSourcePrepare,
      MTimerSourceCheck,
      MTimerSourceDispatch,
      MTimerSourceFinalize,
      NULL,
      NULL
   };
   MTimerSource *ret;

   ASSERT(timeout >= 0);

   ret = (MTimerSource *) g_source_new(&srcFuncs, sizeof *ret);
   ret->last = System_GetTimeMonotonic() * 10;
   ret->timeout = timeout;

   return &ret->src;
}

/** @}  */