Libav
time.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "config.h"
20 
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <time.h>
24 #if HAVE_CLOCK_GETTIME
25 #include <time.h>
26 #endif
27 #if HAVE_GETTIMEOFDAY
28 #include <sys/time.h>
29 #endif
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #if HAVE_WINDOWS_H
34 #include <windows.h>
35 #endif
36 
37 #include "time.h"
38 #include "error.h"
39 
40 int64_t av_gettime(void)
41 {
42 #if HAVE_GETTIMEOFDAY
43  struct timeval tv;
44  gettimeofday(&tv, NULL);
45  return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
46 #elif HAVE_GETSYSTEMTIMEASFILETIME
47  FILETIME ft;
48  int64_t t;
49  GetSystemTimeAsFileTime(&ft);
50  t = (int64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime;
51  return t / 10 - 11644473600000000; /* Jan 1, 1601 */
52 #else
53  return -1;
54 #endif
55 }
56 
57 int64_t av_gettime_relative(void)
58 {
59 #if HAVE_CLOCK_GETTIME
60  struct timespec ts;
61  clock_gettime(CLOCK_MONOTONIC, &ts);
62  return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
63 #else
64  return av_gettime() + 42 * 60 * 60 * INT64_C(1000000);
65 #endif
66 }
67 
68 int av_usleep(unsigned usec)
69 {
70 #if HAVE_NANOSLEEP
71  struct timespec ts = { usec / 1000000, usec % 1000000 * 1000 };
72  while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
73  return 0;
74 #elif HAVE_USLEEP
75  return usleep(usec);
76 #elif HAVE_SLEEP
77  Sleep(usec / 1000);
78  return 0;
79 #else
80  return AVERROR(ENOSYS);
81 #endif
82 }
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:68
error code definitions
#define AVERROR(e)
Definition: error.h:43
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:40
NULL
Definition: eval.c:55
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:57