Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/fluidsynth/glib.c
8670 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
LARGE_INTEGER counter, frequency;
44
45
QueryPerformanceFrequency( &frequency );
46
QueryPerformanceCounter( &counter );
47
48
return counter.QuadPart * 1000000.0 / frequency.QuadPart; /* time in micros */
49
}
50
51
void g_usleep( unsigned int micros )
52
{
53
Sleep( (micros + 999) / 1000 );
54
}
55
56
static DWORD CALLBACK g_thread_wrapper( void *args )
57
{
58
GThread *thread = args;
59
thread->result = thread->func( thread->data );
60
g_thread_unref( thread );
61
return 0;
62
}
63
64
GThread *g_thread_try_new( const char *name, GThreadFunc func, gpointer data, GError **err )
65
{
66
GThread *thread;
67
68
if (!(thread = calloc( 1, sizeof(*thread) ))) return NULL;
69
thread->ref = 2;
70
thread->func = func;
71
thread->data = data;
72
73
if (!(thread->handle = CreateThread( NULL, 0, g_thread_wrapper, thread, 0, NULL )))
74
{
75
free( thread );
76
return NULL;
77
}
78
79
return thread;
80
}
81
82
void g_thread_unref( GThread *thread )
83
{
84
if (!InterlockedDecrement( &thread->ref ))
85
{
86
CloseHandle( thread->handle );
87
free( thread );
88
}
89
}
90
91
gpointer g_thread_join( GThread *thread )
92
{
93
gpointer result;
94
95
WaitForSingleObject( thread->handle, INFINITE );
96
result = thread->result;
97
g_thread_unref( thread );
98
return result;
99
}
100
101
void g_clear_error( GError **error )
102
{
103
*error = NULL;
104
}
105
106
int g_file_test( const char *path, int test )
107
{
108
DWORD attrs = GetFileAttributesA( path );
109
if (attrs != INVALID_FILE_ATTRIBUTES)
110
{
111
if (test & G_FILE_TEST_EXISTS) return 1;
112
if ((test & G_FILE_TEST_IS_REGULAR) && !(attrs & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE))) return 1;
113
}
114
return 0;
115
}
116
117