~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to libvmtools/monotonicTimer.c

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-03-31 14:20:05 UTC
  • mfrom: (1.4.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110331142005-3n9red91p7ogkweo
Tags: 2011.03.28-387002-0ubuntu1
* Merge latest upstream git tag.  This has the unlocked_ioctl change
  needed to fix dkms build failures (LP: #727342)
* Changes in debian/rules:
  - work around a bug in toolbox/Makefile, where install-exec-hook is
    not happening.  This needs to get fixed the right way.
  - don't install 'vmware-user' which seems to no longer exist
  - move /etc/xdg into open-vm-toolbox (which should be done using .install)
* debian/open-vm-tools.init: add 'modprobe [-r] vmblock'. (LP: #332323)
* debian/rules and debian/open-vm-toolbox.lintian-overrides:
  - Make vmware-user-suid-wrapper suid-root (LP: #332323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************
 
2
 * Copyright (C) 2008 VMware, Inc. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License as published
 
6
 * by the Free Software Foundation version 2.1 and no later version.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
10
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 
11
 * License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 
15
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 
16
 *
 
17
 *********************************************************/
 
18
 
 
19
/**
 
20
 * @file monotonicTimer.c
 
21
 *
 
22
 * A GSource that implements a timer backed by a monotonic time source.
 
23
 */
 
24
 
 
25
#include <limits.h>
 
26
#include "vmware.h"
 
27
#include "system.h"
 
28
#include "vmware/tools/utils.h"
 
29
 
 
30
typedef struct MTimerSource {
 
31
   GSource     src;
 
32
   gint        timeout;
 
33
   uint64      last;
 
34
} MTimerSource;
 
35
 
 
36
 
 
37
/*
 
38
 *******************************************************************************
 
39
 * MTimerSourcePrepare --                                                 */ /**
 
40
 *
 
41
 * Callback for the "prepare()" event source function. Sets the timeout to
 
42
 * the number of milliseconds this timer expects to sleep for. If the timeout
 
43
 * has already expired, update the internal state tracking the last time the
 
44
 * timer was fired.
 
45
 *
 
46
 * @param[in]  src         The source.
 
47
 * @param[out] timeout     Where to store the timeout.
 
48
 *
 
49
 * @return TRUE if timeout has already expired.
 
50
 *
 
51
 *******************************************************************************
 
52
 */
 
53
 
 
54
static gboolean
 
55
MTimerSourcePrepare(GSource *src,
 
56
                    gint *timeout)
 
57
{
 
58
   MTimerSource *timer = (MTimerSource *) src;
 
59
 
 
60
   if (timer->timeout == 0) {
 
61
      *timeout = 0;
 
62
      return TRUE;
 
63
   } else {
 
64
         uint64 now = System_GetTimeMonotonic() * 10;
 
65
         uint64 diff;
 
66
 
 
67
         ASSERT(now >= timer->last);
 
68
 
 
69
         diff = now - timer->last;
 
70
         if (diff >= timer->timeout) {
 
71
            timer->last = now;
 
72
            *timeout = 0;
 
73
            return TRUE;
 
74
         }
 
75
 
 
76
      *timeout = MIN(INT_MAX, timer->timeout - diff);
 
77
      return FALSE;
 
78
   }
 
79
}
 
80
 
 
81
 
 
82
/*
 
83
 *******************************************************************************
 
84
 * MTimerSourceCheck --                                                   */ /**
 
85
 *
 
86
 * Checks whether the timeout has expired.
 
87
 *
 
88
 * @param[in]  src     The source.
 
89
 *
 
90
 * @return Whether the timeout has expired.
 
91
 *
 
92
 *******************************************************************************
 
93
 */
 
94
 
 
95
static gboolean
 
96
MTimerSourceCheck(GSource *src)
 
97
{
 
98
   gint unused;
 
99
   return MTimerSourcePrepare(src, &unused);
 
100
}
 
101
 
 
102
 
 
103
/*
 
104
 *******************************************************************************
 
105
 * MTimerSourceDispatch --                                                */ /**
 
106
 *
 
107
 * Calls the callback associated with the timer, if any.
 
108
 *
 
109
 * @param[in]  src         Unused.
 
110
 * @param[in]  callback    The callback to be called.
 
111
 * @param[in]  data        User-supplied data.
 
112
 *
 
113
 * @return The return value of the callback, or FALSE if the callback is NULL.
 
114
 *
 
115
 *******************************************************************************
 
116
 */
 
117
 
 
118
static gboolean
 
119
MTimerSourceDispatch(GSource *src,
 
120
                     GSourceFunc callback,
 
121
                     gpointer data)
 
122
{
 
123
   return (callback != NULL) ? callback(data) : FALSE;
 
124
}
 
125
 
 
126
 
 
127
/*
 
128
 *******************************************************************************
 
129
 * MTimerSourceFinalize --                                                */ /**
 
130
 *
 
131
 * Does nothing. The main glib code already does all the cleanup needed.
 
132
 *
 
133
 * @param[in]  src     The source.
 
134
 *
 
135
 *******************************************************************************
 
136
 */
 
137
 
 
138
static void
 
139
MTimerSourceFinalize(GSource *src)
 
140
{
 
141
}
 
142
 
 
143
 
 
144
/**
 
145
 *
 
146
 * @addtogroup vmtools_utils
 
147
 * @{
 
148
 */
 
149
 
 
150
/*
 
151
 *******************************************************************************
 
152
 * VMTools_CreateTimer --                                                 */ /**
 
153
 *
 
154
 * @brief Create a timer based on a monotonic clock source.
 
155
 *
 
156
 * This timer differs from the glib timeout source, which uses the system time.
 
157
 * It is recommended for code that needs more reliable time tracking, using a
 
158
 * clock that is not affected by changes in the system time (which can happen
 
159
 * when using NTP or the Tools time synchronization feature).
 
160
 *
 
161
 * @param[in] timeout   The timeout for the timer, must be >= 0.
 
162
 *
 
163
 * @return The new source.
 
164
 *
 
165
 *******************************************************************************
 
166
 */
 
167
 
 
168
GSource *
 
169
VMTools_CreateTimer(gint timeout)
 
170
{
 
171
   static GSourceFuncs srcFuncs = {
 
172
      MTimerSourcePrepare,
 
173
      MTimerSourceCheck,
 
174
      MTimerSourceDispatch,
 
175
      MTimerSourceFinalize,
 
176
      NULL,
 
177
      NULL
 
178
   };
 
179
   MTimerSource *ret;
 
180
 
 
181
   ASSERT(timeout >= 0);
 
182
 
 
183
   ret = (MTimerSource *) g_source_new(&srcFuncs, sizeof *ret);
 
184
   ret->last = System_GetTimeMonotonic() * 10;
 
185
   ret->timeout = timeout;
 
186
 
 
187
   return &ret->src;
 
188
}
 
189
 
 
190
/** @}  */
 
191