2 * Copyright 2023 RĂ©mi Bernon for CodeWeavers
4 * This library 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.
9 * This library 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 int g_vsnprintf( char *buffer
, size_t size
, const char *format
, va_list args
)
23 int ret
= _vsnprintf( buffer
, size
- 1, format
, args
);
24 if (ret
>= 0 && ret
< size
) buffer
[ret
] = 0;
29 int g_snprintf( char *buffer
, size_t size
, const char *format
, ... )
34 va_start( args
, format
);
35 ret
= g_vsnprintf( buffer
, size
, format
, args
);
41 double g_get_monotonic_time(void)
43 static LARGE_INTEGER frequency
= {0};
44 LARGE_INTEGER counter
;
46 if (!frequency
.QuadPart
) QueryPerformanceFrequency( &frequency
);
47 QueryPerformanceCounter( &counter
);
49 return counter
.QuadPart
* 1000000.0 / frequency
.QuadPart
; /* time in micros */
52 void g_usleep( unsigned int micros
)
54 Sleep( micros
/ 1000 );
57 static DWORD CALLBACK
g_thread_wrapper( void *args
)
59 GThread
*thread
= args
;
60 gpointer ret
= thread
->func( thread
->data
);
61 if (!InterlockedDecrement( &thread
->ref
)) free( thread
);
65 GThread
*g_thread_try_new( const char *name
, GThreadFunc func
, gpointer data
, GError
**err
)
69 if (!(thread
= calloc( 1, sizeof(*thread
) ))) return NULL
;
74 if (!(thread
->handle
= CreateThread( NULL
, 0, g_thread_wrapper
, thread
, 0, NULL
)))
83 void g_thread_unref( GThread
*thread
)
85 CloseHandle( thread
->handle
);
86 if (!InterlockedDecrement( &thread
->ref
)) free( thread
);
89 void g_thread_join( GThread
*thread
)
91 WaitForSingleObject( thread
->handle
, INFINITE
);
92 g_thread_unref( thread
);
95 void g_clear_error( GError
**error
)
100 int g_file_test( const char *path
, int test
)
102 DWORD attrs
= GetFileAttributesA( path
);
103 if (test
== G_FILE_TEST_EXISTS
) return attrs
!= INVALID_FILE_ATTRIBUTES
;
104 if (test
== G_FILE_TEST_IS_REGULAR
) return attrs
== FILE_ATTRIBUTE_NORMAL
;