BRL-CAD
timer.c
Go to the documentation of this file.
1 /* T I M E R . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  * Copyright (c) Tim Riker
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this file; see the file named COPYING for more
19  * information.
20  */
21 
22 #include "common.h"
23 
24 #include <time.h>
25 #include "bio.h"
26 
27 #ifdef HAVE_SYS_TIME_H
28 # include <sys/time.h>
29 #endif
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33 #ifdef HAVE_SCHED_H
34 # include <sched.h>
35 #endif
36 
37 #include "bu/log.h"
38 #include "bu/time.h"
39 
40 
41 int64_t
43 {
44 #ifdef HAVE_SYS_TIME_H
45 
46  struct timeval nowTime;
47 
48  gettimeofday(&nowTime, NULL);
49  return ((int64_t)nowTime.tv_sec * (int64_t)1000000
50  + (int64_t)nowTime.tv_usec);
51 
52 #else /* HAVE_SYS_TIME_H */
53 # ifdef HAVE_WINDOWS_H
54 
55  LARGE_INTEGER count;
56  static LARGE_INTEGER freq = {0};
57 
58  if (freq.QuadPart == 0)
59  if (QueryPerformanceFrequency(&freq) == 0) {
60  bu_log("QueryPerformanceFrequency failed\n");
61  return -1;
62  }
63 
64  if (QueryPerformanceCounter(&count) == 0) {
65  bu_log("QueryPerformanceCounter failed\n");
66  return -1;
67  }
68 
69  return 1e6*count.QuadPart/freq.QuadPart;
70 
71 # else /* HAVE_WINDOWS_H */
72 # warning "bu_gettime() implementation missing for this machine type"
73  bu_log("timer.c: WARNING, no gettime implementation for this machine type.\n");
74  return -1;
75 
76 # endif /* HAVE_WINDOWS_H */
77 #endif /* HAVE_SYS_TIME_H */
78 
79 }
80 
81 /*
82  * Local Variables:
83  * mode: C
84  * tab-width: 8
85  * indent-tabs-mode: t
86  * c-file-style: "stroustrup"
87  * End:
88  * ex: shiftwidth=4 tabstop=8
89  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Header file for the BRL-CAD common definitions.
int64_t bu_gettime(void)
Definition: timer.c:42