dmime/tests: Test output tool note requeueing.
[wine.git] / libs / fluidsynth / glib.c
blobcd938a4fb6bb4659db626de3fa36f1cb956efd03
1 /*
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
19 #include <glib.h>
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;
25 else buffer[0] = 0;
26 return ret;
29 int g_snprintf( char *buffer, size_t size, const char *format, ... )
31 va_list args;
32 int ret;
34 va_start( args, format );
35 ret = g_vsnprintf( buffer, size, format, args );
36 va_end( args );
38 return ret;
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 );
62 return (UINT_PTR)ret;
65 GThread *g_thread_try_new( const char *name, GThreadFunc func, gpointer data, GError **err )
67 GThread *thread;
69 if (!(thread = calloc( 1, sizeof(*thread) ))) return NULL;
70 thread->ref = 2;
71 thread->func = func;
72 thread->data = data;
74 if (!(thread->handle = CreateThread( NULL, 0, g_thread_wrapper, thread, 0, NULL )))
76 free( thread );
77 return NULL;
80 return thread;
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 )
97 *error = NULL;
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;
105 return 0;