]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
D�but du portage BeOS. Beaucoup de fuchiers ont �t� modifi� car il a fallu
[vlc] / src / misc / mtime.c
1 /*****************************************************************************
2  * mtime.c: high rezolution time management functions
3  * Functions are prototyped in mtime.h.
4  *****************************************************************************
5  * Copyright (C) 1998, 1999, 2000 VideoLAN
6  *
7  * Authors:
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public
20  * License along with this program; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  *****************************************************************************/
24
25 /*
26  * TODO:
27  *  see if using Linux real-time extensions is possible and profitable
28  */
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <stdio.h>                                              /* sprintf() */
34 #include <unistd.h>                                              /* select() */
35 #include <sys/time.h>
36
37 #ifdef SYS_BEOS
38 #include <kernel/OS.h>
39 #endif
40
41 #include "common.h"
42 #include "mtime.h"
43
44 /*****************************************************************************
45  * mstrtime: return a date in a readable format
46  *****************************************************************************
47  * This functions is provided for any interface function which need to print a
48  * date. psz_buffer should be a buffer long enough to store the formatted
49  * date.
50  *****************************************************************************/
51 char *mstrtime( char *psz_buffer, mtime_t date )
52 {
53     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
54              (int) (date / (1000LL * 1000LL * 60LL * 60LL) % 24LL),
55              (int) (date / (1000LL * 1000LL * 60LL) % 60LL),
56              (int) (date / (1000LL * 1000LL) % 60LL),
57              (int) (date / 1000LL % 1000LL),
58              (int) (date % 1000LL) );
59     return( psz_buffer );
60 }
61
62 /*****************************************************************************
63  * mdate: return high precision date (inline function)
64  *****************************************************************************
65  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
66  * ftime() function (1 kHz resolution).
67  *****************************************************************************/
68 mtime_t mdate( void )
69 {
70 #ifdef SYS_BEOS
71     return( real_time_clock_usecs() );
72 #else
73     struct timeval tv_date;
74
75     /* gettimeofday() could return an error, and should be tested. However, the
76      * only possible error, according to 'man', is EFAULT, which can not happen
77      * here, since tv is a local variable. */
78     gettimeofday( &tv_date, NULL );
79     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
80 #endif
81 }
82
83 /*****************************************************************************
84  * mwait: wait for a date (inline function)
85  *****************************************************************************
86  * This function uses select() and an system date function to wake up at a
87  * precise date. It should be used for process synchronization. If current date
88  * is posterior to wished date, the function returns immediately.
89  *****************************************************************************/
90 void mwait( mtime_t date )
91 {
92 #ifdef SYS_BEOS
93     mtime_t delay;
94     
95     delay = date - real_time_clock_usecs();
96     if( delay <= 0 )
97     {
98         return;
99     }
100     snooze( delay );
101 #else /* SYS_BEOS */
102
103     struct timeval tv_date, tv_delay;
104     mtime_t        delay;          /* delay in msec, signed to detect errors */
105
106     /* see mdate() about gettimeofday() possible errors */
107     gettimeofday( &tv_date, NULL );
108
109     /* calculate delay and check if current date is before wished date */
110     delay = date - (mtime_t) tv_date.tv_sec * 1000000 - (mtime_t) tv_date.tv_usec;
111     if( delay <= 0 )                 /* wished date is now or already passed */
112     {
113         return;
114     }
115 #ifndef usleep
116     tv_delay.tv_sec = delay / 1000000;
117     tv_delay.tv_usec = delay % 1000000;
118
119     /* see msleep() about select() errors */
120     select( 0, NULL, NULL, NULL, &tv_delay );
121 #else
122     usleep( delay );
123 #endif
124
125 #endif /* SYS_BEOS */
126 }
127
128 /*****************************************************************************
129  * msleep: more precise sleep() (inline function)                        (ok ?)
130  *****************************************************************************
131  * Portable usleep() function.
132  *****************************************************************************/
133 void msleep( mtime_t delay )
134 {
135 #ifdef SYS_BEOS
136     snooze( delay );
137 #else /* SYS_BEOS */
138
139 #ifndef usleep
140     struct timeval tv_delay;
141
142     tv_delay.tv_sec = delay / 1000000;
143     tv_delay.tv_usec = delay % 1000000;
144     /* select() return value should be tested, since several possible errors
145      * can occur. However, they should only happen in very particular occasions
146      * (i.e. when a signal is sent to the thread, or when memory is full), and
147      * can be ingnored. */
148     select( 0, NULL, NULL, NULL, &tv_delay );
149 #else
150     usleep( delay );
151 #endif
152
153 #endif /* SYS_BEOS */
154 }