summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/timeWrapper.cpp
blob: adf87681cb4cb835ec74fcaa61818c8f43e4f7f9 (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
/*
  wrapps calls to usleep, gettimeofday,...
  Copyright (C) 2000  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */



#include "timeWrapper.h"

#if defined WIN32
// usec goes from 0 -> 1000000 (one Million) under Unix
int gettimeofday(struct timeval *tv, struct timezone *tz) {
  if(tv) {
    struct _timeb tb;                         
    _ftime(&tb);                              
    tv->tv_sec=tb.time;                       
    tv->tv_usec=1000*tb.millitm;   
  }
  return(0);
}

void abs_usleep(const timeval* tm) {
  _sleep((tm->tv_usec / 1000) + (tm->tv_sec * 1000));
}
#endif

#ifndef WIN32

void abs_usleep(struct timeval* tm) {
  select(0,NULL,NULL,NULL,tm);
}

#endif

TimeWrapper::TimeWrapper() {
}


TimeWrapper::~TimeWrapper() {
}

void TimeWrapper::sleep(int sec) {
  timeval_t time;
  time.tv_sec=sec;
  time.tv_usec=0;
  TimeWrapper::usleep(&time); 
}

void TimeWrapper::usleep(unsigned long usec) {
  timeval_t time;
  time.tv_sec=0;
  time.tv_usec=usec;
  TimeWrapper::usleep(&time);
}

void TimeWrapper::usleep(timeval_t* time) {
  struct timeval waitTime;
  waitTime.tv_sec=time->tv_sec;
  waitTime.tv_usec=time->tv_usec;
  /*Threads and usleep does not work, select is very portable*/
  ::abs_usleep(&waitTime);
}


void TimeWrapper::gettimeofday(timeval_t* time) {
  struct timeval waitTime;
  ::gettimeofday(&waitTime,NULL);
  time->tv_sec=waitTime.tv_sec;
  time->tv_usec=waitTime.tv_usec;
}