1 /*****************************************************************************
2  * osdep.c: platform-specific code
3  *****************************************************************************
4  * Copyright (C) 2003-2021 x264 project
5  *
6  * Authors: Steven Walters <kemuri9@gmail.com>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Henrik Gramner <henrik@gramner.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27 
28 #include "osdep.h"
29 
30 #if SYS_WINDOWS
31 #include <sys/types.h>
32 #include <sys/timeb.h>
33 #else
34 #include <sys/time.h>
35 #endif
36 #include <time.h>
37 
38 #if PTW32_STATIC_LIB
39 /* this is a global in pthread-win32 to indicate if it has been initialized or not */
40 extern int ptw32_processInitialized;
41 #endif
42 
x264_mdate(void)43 int64_t x264_mdate( void )
44 {
45 #if SYS_WINDOWS
46     struct timeb tb;
47     ftime( &tb );
48     return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
49 #elif HAVE_CLOCK_GETTIME
50     struct timespec ts;
51     clock_gettime( CLOCK_MONOTONIC, &ts );
52     return (int64_t)ts.tv_sec * 1000000 + (int64_t)ts.tv_nsec / 1000;
53 #else
54     struct timeval tv_date;
55     gettimeofday( &tv_date, NULL );
56     return (int64_t)tv_date.tv_sec * 1000000 + (int64_t)tv_date.tv_usec;
57 #endif
58 }
59 
60 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
61 /* state of the threading library being initialized */
62 static volatile LONG threading_is_init = 0;
63 
threading_destroy(void)64 static void threading_destroy( void )
65 {
66 #if PTW32_STATIC_LIB
67     pthread_win32_thread_detach_np();
68     pthread_win32_process_detach_np();
69 #else
70     x264_win32_threading_destroy();
71 #endif
72 }
73 
threading_init(void)74 static int threading_init( void )
75 {
76 #if PTW32_STATIC_LIB
77     /* if static pthread-win32 is already initialized, then do nothing */
78     if( ptw32_processInitialized )
79         return 0;
80     if( !pthread_win32_process_attach_np() )
81         return -1;
82 #else
83     if( x264_win32_threading_init() )
84         return -1;
85 #endif
86     /* register cleanup to run at process termination */
87     atexit( threading_destroy );
88     return 0;
89 }
90 
x264_threading_init(void)91 int x264_threading_init( void )
92 {
93     LONG state;
94     while( (state = InterlockedCompareExchange( &threading_is_init, -1, 0 )) != 0 )
95     {
96         /* if already init, then do nothing */
97         if( state > 0 )
98             return 0;
99     }
100     if( threading_init() < 0 )
101     {
102         InterlockedExchange( &threading_is_init, 0 );
103         return -1;
104     }
105     InterlockedExchange( &threading_is_init, 1 );
106     return 0;
107 }
108 #endif
109