1
//{$ifndef __G_THREAD_H__}
2
//{$define __G_THREAD_H__}
4
//{$include gerror.inc}
5
// {$include gtypes.inc}
7
{* GLib Thread support *}
10
function g_thread_error_quark : TGQuark; cdecl; external gthreadlib name 'g_thread_error_quark';
13
function G_THREAD_ERROR: TGQuark;
19
PGThreadError = ^TGThreadError;
20
TGThreadError = ( G_THREAD_ERROR_AGAIN { Resource temporarily unavailable });
22
TGThreadFunc = function (data:gpointer):gpointer;cdecl;
24
PGThreadPriority = ^TGThreadPriority;
25
TGThreadPriority = (G_THREAD_PRIORITY_LOW,
26
G_THREAD_PRIORITY_NORMAL,
27
G_THREAD_PRIORITY_HIGH,
28
G_THREAD_PRIORITY_URGENT);
35
priority : TGThreadPriority;
39
PGMutex = pointer; // GMutex;
40
PGCond = pointer; // GCond;
41
PGPrivate = pointer; // GPrivate;
43
// typedef struct _GMutex* GStaticMutex;
44
PGStaticMutex = ^TGStaticMutex;
45
TGStaticMutex = PGMutex;
47
PGThreadFunctions = ^TGThreadFunctions;
48
TGThreadFunctions = record
49
mutex_new : function :PGMutex; cdecl;
50
mutex_lock : procedure (mutex:PGMutex); cdecl;
51
mutex_trylock : function (mutex:PGMutex):gboolean; cdecl;
52
mutex_unlock : procedure (mutex:PGMutex); cdecl;
53
mutex_free : procedure (mutex:PGMutex); cdecl;
54
cond_new : function :PGCond; cdecl;
55
cond_signal : procedure (cond:PGCond); cdecl;
56
cond_broadcast : procedure (cond:PGCond); cdecl;
57
cond_wait : procedure (cond:PGCond; mutex:PGMutex); cdecl;
58
cond_timed_wait : function (cond:PGCond; mutex:PGMutex; end_time:PGTimeVal):gboolean; cdecl;
59
cond_free : procedure (cond:PGCond); cdecl;
60
private_new : function (dest:TGDestroyNotify):PGPrivate; cdecl;
61
private_get : function (private_key:PGPrivate):gpointer; cdecl;
62
private_set : procedure (private_key:PGPrivate; data:gpointer); cdecl;
63
thread_create : procedure (func:TGThreadFunc; data:gpointer; stack_size:gulong; joinable:gboolean; bound:gboolean;
64
priority:TGThreadPriority; thread:gpointer; error:PPGError); cdecl;
65
thread_yield : procedure ; cdecl;
66
thread_join : procedure (thread:gpointer); cdecl;
67
thread_exit : procedure ; cdecl;
68
thread_set_priority : procedure (thread:gpointer; priority:TGThreadPriority); cdecl;
69
thread_self : procedure (thread:gpointer); cdecl;
70
thread_equal : function (thread1:gpointer; thread2:gpointer):gboolean; cdecl;
77
g_thread_functions_for_glib_use : TGThreadFunctions; external gliblib name 'g_thread_functions_for_glib_use';
78
g_thread_use_default_impl : gboolean; external gliblib name 'g_thread_use_default_impl';
79
g_threads_got_initialized : gboolean; external gliblib name 'g_threads_got_initialized';
81
g_thread_functions_for_glib_use : TGThreadFunctions;cvar;external; // ?????
82
g_thread_use_default_impl : gboolean;cvar;external;
83
g_threads_got_initialized : gboolean;cvar;external;
88
{ initializes the mutex/cond/private implementation for glib, might
89
only be called once, and must not be called directly or indirectly
90
from another glib-function, e.g. as a callback.
93
procedure g_thread_init(vtable:PGThreadFunctions);cdecl;external gthreadlib name 'g_thread_init';
95
{ Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all
96
mutexes will check for re-locking and re-unlocking }
98
{ Initialize thread system with errorcheck mutexes. vtable must be
99
NULL. Do not call directly. Use #define G_ERRORCHECK_MUTEXES
102
procedure g_thread_init_with_errorcheck_mutexes(vtable:PGThreadFunctions);cdecl;external gthreadlib name 'g_thread_init_with_errorcheck_mutexes';
104
{ A random number to recognize debug calls to g_mutex_... }
107
G_MUTEX_DEBUG_MAGIC = $f8e18ad7;
110
{ internal function for fallback static mutex implementation }
111
function g_static_mutex_get_mutex_impl(mutex:PPGMutex):PGMutex;cdecl;external gthreadlib name 'g_static_mutex_get_mutex_impl';
114
{ shorthands for conditional and unconditional function calls }
116
function g_thread_supported: gboolean;
118
procedure g_mutex_lock (mutex: PGMutex);
120
function g_mutex_trylock (mutex: PGMutex):gboolean;
122
procedure g_mutex_unlock (mutex: PGMutex);
124
procedure g_mutex_free (mutex: PGMutex);
126
procedure g_cond_wait (cond: PGCond; mutex: PGMutex);
128
function g_cond_timed_wait (cond: PGCond;
130
end_time: PGTimeVal):gboolean;
132
function g_mutex_new : PGMutex;
133
function g_cond_new : PGCond;
135
procedure g_cond_signal (cond: PGCond);
136
procedure g_cond_broadcast (cond: PGCond);
137
procedure g_cond_free (cond: PGCond);
138
function g_private_new (dest: TGDestroyNotify): PGPrivate;
139
function g_private_get (private_key: PGPrivate): gpointer;
140
procedure g_private_set (var private_key: PGPrivate; data: gpointer);
142
procedure g_thread_yield;
145
function g_thread_create (func: TGThreadFunc;
148
error: PPGError): PGThread;
150
function g_thread_create_full (func : TGThreadFunc;
153
joinable, bound: gboolean;
154
priority : TGThreadPriority;
155
error : ppGError): PGThread; cdecl; external gthreadlib name 'g_thread_create_full';
158
function g_thread_self:PGThread;cdecl;external gthreadlib name 'g_thread_self';
160
procedure g_thread_exit(retval:gpointer);cdecl;external gthreadlib name 'g_thread_exit';
162
function g_thread_join(thread:PGThread):gpointer;cdecl;external gthreadlib name 'g_thread_join';
164
procedure g_thread_set_priority(thread:PGThread; priority:TGThreadPriority);cdecl;external gthreadlib name 'g_thread_set_priority';
166
{ GStaticMutexes can be statically initialized with the value
167
G_STATIC_MUTEX_INIT, and then they can directly be used, that is
168
much easier, than having to explicitly allocate the mutex before
172
procedure g_static_mutex_lock (mutex: PGStaticMutex);
174
function g_static_mutex_trylock (mutex: PGStaticMutex): gboolean;
176
procedure g_static_mutex_unlock (mutex: PGStaticMutex);
179
procedure g_static_mutex_free(mutex:PGStaticMutex);cdecl;external gthreadlib name 'g_static_mutex_free';
183
PGStaticPrivate = ^TGStaticPrivate;
184
TGStaticPrivate = record
189
nG_STATIC_PRIVATE_INIT = 0; //renamed because of conflict with function_name
191
procedure g_static_private_init(private_key:PGStaticPrivate);cdecl;external gthreadlib name 'g_static_private_init';
193
function g_static_private_get(private_key:PGStaticPrivate):gpointer;cdecl;external gthreadlib name 'g_static_private_get';
195
procedure g_static_private_set(private_key:PGStaticPrivate; data:gpointer; notify:TGDestroyNotify);cdecl;external gthreadlib name 'g_static_private_set';
197
procedure g_static_private_free(private_key:PGStaticPrivate);cdecl;external gthreadlib name 'g_static_private_free';
200
PGStaticRecMutex = ^TGStaticRecMutex;
201
TGStaticRecMutex = record
202
mutex : TGStaticMutex;
204
owner : TGSystemThread; // defined in glibconfig.inc
208
nG_STATIC_MUTEX_INIT = nil; // from glibconfig.h
209
nG_STATIC_REC_MUTEX_INIT = nG_STATIC_MUTEX_INIT;
212
procedure g_static_rec_mutex_init(mutex:PGStaticRecMutex);cdecl;external gthreadlib name 'g_static_rec_mutex_init';
214
procedure g_static_rec_mutex_lock(mutex:PGStaticRecMutex);cdecl;external gthreadlib name 'g_static_rec_mutex_lock';
216
function g_static_rec_mutex_trylock(mutex:PGStaticRecMutex):gboolean;cdecl;external gthreadlib name 'g_static_rec_mutex_trylock';
218
procedure g_static_rec_mutex_unlock(mutex:PGStaticRecMutex);cdecl;external gthreadlib name 'g_static_rec_mutex_unlock';
220
procedure g_static_rec_mutex_lock_full(mutex:PGStaticRecMutex; depth:guint);cdecl;external gthreadlib name 'g_static_rec_mutex_lock_full';
222
function g_static_rec_mutex_unlock_full(mutex:PGStaticRecMutex):guint;cdecl;external gthreadlib name 'g_static_rec_mutex_unlock_full';
224
procedure g_static_rec_mutex_free(mutex:PGStaticRecMutex);cdecl;external gthreadlib name 'g_static_rec_mutex_free';
228
PGStaticRWLock = ^TGStaticRWLock;
229
TGStaticRWLock = record
230
mutex : TGStaticMutex;
233
read_counter : guint;
235
want_to_read : guint;
236
want_to_write : guint;
239
{* i can't translate this macro. any ideas???
241
* #define G_STATIC_RW_LOCK_INIT G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0
244
procedure g_static_rw_lock_init(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_init';
246
procedure g_static_rw_lock_reader_lock(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_reader_lock';
248
function g_static_rw_lock_reader_trylock(lock:PGStaticRWLock):gboolean;cdecl;external gthreadlib name 'g_static_rw_lock_reader_trylock';
250
procedure g_static_rw_lock_reader_unlock(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_reader_unlock';
252
procedure g_static_rw_lock_writer_lock(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_writer_lock';
254
function g_static_rw_lock_writer_trylock(lock:PGStaticRWLock):gboolean;cdecl;external gthreadlib name 'g_static_rw_lock_writer_trylock';
256
procedure g_static_rw_lock_writer_unlock(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_writer_unlock';
258
procedure g_static_rw_lock_free(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_free';
260
{ these are some convenience macros that expand to nothing if GLib
261
was configured with --disable-threads. for using StaticMutexes,
262
you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
263
if you need to export the mutex. With G_LOCK_EXTERN (name) you can
264
declare such an globally defined lock. name is a unique identifier
265
for the protected varibale or code portion. locking, testing and
266
unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
267
G_TRYLOCK() respectively.
269
procedure glib_dummy_decl;cdecl;external gthreadlib name 'glib_dummy_decl';
272
#define G_LOCK_NAME(name) g__ ## name ## _lock
273
#define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
274
#define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
275
#define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))