NAMD
wkfutils.c
Go to the documentation of this file.
1 /***************************************************************************
2  *cr
3  *cr (C) Copyright 1995-2009 John E. Stone
4  *cr
5  ***************************************************************************/
6 /***************************************************************************
7  * RCS INFORMATION:
8  *
9  * $RCSfile: wkfutils.c,v $
10  * $Author: dhardy $ $Locker: $ $State: Exp $
11  * $Revision: 1.1 $ $Date: 2011/08/14 13:49:14 $
12  *
13  ***************************************************************************/
14 /*
15  * Copyright (c) 1994-2009 John E. Stone
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  * notice, this list of conditions and the following disclaimer in the
25  * documentation and/or other materials provided with the distribution.
26  * 3. The name of the author may not be used to endorse or promote products
27  * derived from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
30  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
33  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41 
42 #include "wkfutils.h"
43 
44 #include <string.h>
45 #include <ctype.h>
46 #include <math.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 
50 #if defined(_MSC_VER)
51 #include <windows.h>
52 #include <conio.h>
53 #else
54 #include <unistd.h>
55 #include <sys/time.h>
56 #include <errno.h>
57 
58 #if defined(ARCH_AIX4)
59 #include <strings.h>
60 #endif
61 
62 #if defined(__irix)
63 #include <bstring.h>
64 #endif
65 
66 #if defined(__hpux)
67 #include <time.h>
68 #endif /* HPUX */
69 #endif /* _MSC_VER */
70 
71 
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 
77 #if defined(_MSC_VER)
78 typedef struct {
79  DWORD starttime;
80  DWORD endtime;
81 } wkf_timer;
82 
84  wkf_timer * t = (wkf_timer *) v;
85  t->starttime = GetTickCount();
86 }
87 
89  wkf_timer * t = (wkf_timer *) v;
90  t->endtime = GetTickCount();
91 }
92 
94  wkf_timer * t = (wkf_timer *) v;
95  double ttime;
96 
97  ttime = ((double) (t->endtime - t->starttime)) / 1000.0;
98 
99  return ttime;
100 }
101 
103  wkf_timer * t = (wkf_timer *) v;
104  double ttime;
105  ttime = ((double) (t->starttime)) / 1000.0;
106  return ttime;
107 }
108 
110  wkf_timer * t = (wkf_timer *) v;
111  double ttime;
112  ttime = ((double) (t->endtime)) / 1000.0;
113  return ttime;
114 }
115 
116 #else
117 
118 /* Unix with gettimeofday() */
119 typedef struct {
120  struct timeval starttime, endtime;
121  struct timezone tz;
122 } wkf_timer;
123 
125  wkf_timer * t = (wkf_timer *) v;
126  gettimeofday(&t->starttime, &t->tz);
127 }
128 
130  wkf_timer * t = (wkf_timer *) v;
131  gettimeofday(&t->endtime, &t->tz);
132 }
133 
135  wkf_timer * t = (wkf_timer *) v;
136  double ttime;
137  ttime = ((double) (t->endtime.tv_sec - t->starttime.tv_sec)) +
138  ((double) (t->endtime.tv_usec - t->starttime.tv_usec)) / 1000000.0;
139  return ttime;
140 }
141 
143  wkf_timer * t = (wkf_timer *) v;
144  double ttime;
145  ttime = ((double) t->starttime.tv_sec) +
146  ((double) t->starttime.tv_usec) / 1000000.0;
147  return ttime;
148 }
149 
151  wkf_timer * t = (wkf_timer *) v;
152  double ttime;
153  ttime = ((double) t->endtime.tv_sec) +
154  ((double) t->endtime.tv_usec) / 1000000.0;
155  return ttime;
156 }
157 
158 #endif
159 
160 /* system independent routines to create and destroy timers */
162  wkf_timer * t;
163  t = (wkf_timer *) malloc(sizeof(wkf_timer));
164  memset(t, 0, sizeof(wkf_timer));
165  return t;
166 }
167 
169  free(v);
170 }
171 
173  wkf_timer_stop(v);
174  return wkf_timer_time(v);
175 }
176 
178 wkfmsgtimer * wkf_msg_timer_create(double updatetime) {
179  wkfmsgtimer *mt;
180  mt = (wkfmsgtimer *) malloc(sizeof(wkfmsgtimer));
181  if (mt != NULL) {
182  mt->timer = wkf_timer_create();
183  mt->updatetime = updatetime;
184  wkf_timer_start(mt->timer);
185  }
186  return mt;
187 }
188 
191  double elapsed = wkf_timer_timenow(mt->timer);
192  if (elapsed > mt->updatetime) {
193  /* reset the clock and return true that our timer expired */
194  wkf_timer_start(mt->timer);
195  return 1;
196  } else if (elapsed < 0) {
197  /* time went backwards, best reset our clock! */
198  wkf_timer_start(mt->timer);
199  }
200  return 0;
201 }
202 
206  free(mt);
207 }
208 
209 #ifdef __cplusplus
210 }
211 #endif
212 
double wkf_timer_time(wkf_timerhandle v)
Definition: wkfutils.c:134
void * wkf_timerhandle
Definition: wkfutils.h:49
void wkf_timer_destroy(wkf_timerhandle v)
Definition: wkfutils.c:168
double wkf_timer_start_time(wkf_timerhandle v)
Definition: wkfutils.c:142
wkfmsgtimer * wkf_msg_timer_create(double updatetime)
Definition: wkfutils.c:178
wkf_timerhandle wkf_timer_create(void)
Definition: wkfutils.c:161
void wkf_msg_timer_destroy(wkfmsgtimer *mt)
Definition: wkfutils.c:204
void wkf_timer_stop(wkf_timerhandle v)
Definition: wkfutils.c:129
wkf_timerhandle timer
Definition: wkfutils.h:60
struct timeval starttime endtime
Definition: wkfutils.c:120
struct timezone tz
Definition: wkfutils.c:121
double updatetime
Definition: wkfutils.h:61
void wkf_timer_start(wkf_timerhandle v)
Definition: wkfutils.c:124
double wkf_timer_timenow(wkf_timerhandle v)
Definition: wkfutils.c:172
int wkf_msg_timer_timeout(wkfmsgtimer *mt)
Definition: wkfutils.c:190
double wkf_timer_stop_time(wkf_timerhandle v)
Definition: wkfutils.c:150