Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/fluidsynth/glib.c
4389 views
1
/*
2
* Copyright 2023 Rémi Bernon for CodeWeavers
3
*
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.
8
*
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.
13
*
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
17
*/
18
19
#include <glib.h>
20
21
int g_vsnprintf( char *buffer, size_t size, const char *format, va_list args )
22
{
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;
27
}
28
29
int g_snprintf( char *buffer, size_t size, const char *format, ... )
30
{
31
va_list args;
32
int ret;
33
34
va_start( args, format );
35
ret = g_vsnprintf( buffer, size, format, args );
36
va_end( args );
37
38
return ret;
39
}
40
41
double g_get_monotonic_time(void)
42
{
43
static LARGE_INTEGER frequency = {0};
44
LARGE_INTEGER counter;
45
46
if (!frequency.QuadPart) QueryPerformanceFrequency( &frequency );
47
QueryPerformanceCounter( &counter );
48
49
return counter.QuadPart * 1000000.0 / frequency.QuadPart; /* time in micros */
50
}
51
52
void g_usleep( unsigned int micros )
53
{
54
Sleep( micros / 1000 );
55
}
56
57
static DWORD CALLBACK g_thread_wrapper( void *args )
58
{
59
GThread *thread = args;
60
gpointer ret = thread->func( thread->data );
61
if (!InterlockedDecrement( &thread->ref )) free( thread );
62
return (UINT_PTR)ret;
63
}
64
65
GThread *g_thread_try_new( const char *name, GThreadFunc func, gpointer data, GError **err )
66
{
67
GThread *thread;
68
69
if (!(thread = calloc( 1, sizeof(*thread) ))) return NULL;
70
thread->ref = 2;
71
thread->func = func;
72
thread->data = data;
73
74
if (!(thread->handle = CreateThread( NULL, 0, g_thread_wrapper, thread, 0, NULL )))
75
{
76
free( thread );
77
return NULL;
78
}
79
80
return thread;
81
}
82
83
void g_thread_unref( GThread *thread )
84
{
85
CloseHandle( thread->handle );
86
if (!InterlockedDecrement( &thread->ref )) free( thread );
87
}
88
89
void g_thread_join( GThread *thread )
90
{
91
WaitForSingleObject( thread->handle, INFINITE );
92
g_thread_unref( thread );
93
}
94
95
void g_clear_error( GError **error )
96
{
97
*error = NULL;
98
}
99
100
int g_file_test( const char *path, int test )
101
{
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;
106
}
107
108