gthread.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //{$ifndef __G_THREAD_H__}
  2. //{$define __G_THREAD_H__}
  3. //{$include gerror.inc}
  4. // {$include gtypes.inc}
  5. {* GLib Thread support *}
  6. function g_thread_error_quark : TGQuark; cdecl; external gthreadlib name 'g_thread_error_quark';
  7. function G_THREAD_ERROR: TGQuark;
  8. type
  9. PGThreadError = ^TGThreadError;
  10. TGThreadError = ( G_THREAD_ERROR_AGAIN { Resource temporarily unavailable });
  11. TGThreadFunc = function (data:gpointer):gpointer;cdecl;
  12. PGThreadPriority = ^TGThreadPriority;
  13. TGThreadPriority = (G_THREAD_PRIORITY_LOW,
  14. G_THREAD_PRIORITY_NORMAL,
  15. G_THREAD_PRIORITY_HIGH,
  16. G_THREAD_PRIORITY_URGENT);
  17. PGThread = ^TGThread;
  18. TGThread = record
  19. func : TGThreadFunc;
  20. data : gpointer;
  21. joinable : gboolean;
  22. priority : TGThreadPriority;
  23. end;
  24. PPGMutex = ^PGMutex;
  25. PGMutex = pointer; // GMutex;
  26. PGCond = pointer; // GCond;
  27. PGPrivate = pointer; // GPrivate;
  28. // typedef struct _GMutex* GStaticMutex;
  29. PGStaticMutex = ^TGStaticMutex;
  30. TGStaticMutex = PGMutex;
  31. PGThreadFunctions = ^TGThreadFunctions;
  32. TGThreadFunctions = record
  33. mutex_new : function :PGMutex; cdecl;
  34. mutex_lock : procedure (mutex:PGMutex); cdecl;
  35. mutex_trylock : function (mutex:PGMutex):gboolean; cdecl;
  36. mutex_unlock : procedure (mutex:PGMutex); cdecl;
  37. mutex_free : procedure (mutex:PGMutex); cdecl;
  38. cond_new : function :PGCond; cdecl;
  39. cond_signal : procedure (cond:PGCond); cdecl;
  40. cond_broadcast : procedure (cond:PGCond); cdecl;
  41. cond_wait : procedure (cond:PGCond; mutex:PGMutex); cdecl;
  42. cond_timed_wait : function (cond:PGCond; mutex:PGMutex; end_time:PGTimeVal):gboolean; cdecl;
  43. cond_free : procedure (cond:PGCond); cdecl;
  44. private_new : function (dest:TGDestroyNotify):PGPrivate; cdecl;
  45. private_get : function (private_key:PGPrivate):gpointer; cdecl;
  46. private_set : procedure (private_key:PGPrivate; data:gpointer); cdecl;
  47. thread_create : procedure (func:TGThreadFunc; data:gpointer; stack_size:gulong; joinable:gboolean; bound:gboolean;
  48. priority:TGThreadPriority; thread:gpointer; error:PPGError); cdecl;
  49. thread_yield : procedure ; cdecl;
  50. thread_join : procedure (thread:gpointer); cdecl;
  51. thread_exit : procedure ; cdecl;
  52. thread_set_priority : procedure (thread:gpointer; priority:TGThreadPriority); cdecl;
  53. thread_self : procedure (thread:gpointer); cdecl;
  54. thread_equal : function (thread1:gpointer; thread2:gpointer):gboolean; cdecl;
  55. end;
  56. {$IFNDEF KYLIX}
  57. var
  58. g_thread_functions_for_glib_use : TGThreadFunctions;cvar;external; // ?????
  59. g_thread_use_default_impl : gboolean;cvar;external;
  60. g_threads_got_initialized : gboolean;cvar;external;
  61. {$ENDIF}
  62. { initializes the mutex/cond/private implementation for glib, might
  63. only be called once, and must not be called directly or indirectly
  64. from another glib-function, e.g. as a callback.
  65. }
  66. procedure g_thread_init(vtable:PGThreadFunctions);cdecl;external gthreadlib name 'g_thread_init';
  67. { Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all
  68. mutexes will check for re-locking and re-unlocking }
  69. { Initialize thread system with errorcheck mutexes. vtable must be
  70. NULL. Do not call directly. Use #define G_ERRORCHECK_MUTEXES
  71. instead.
  72. }
  73. procedure g_thread_init_with_errorcheck_mutexes(vtable:PGThreadFunctions);cdecl;external gthreadlib name 'g_thread_init_with_errorcheck_mutexes';
  74. { A random number to recognize debug calls to g_mutex_... }
  75. const
  76. G_MUTEX_DEBUG_MAGIC = $f8e18ad7;
  77. { internal function for fallback static mutex implementation }
  78. function g_static_mutex_get_mutex_impl(mutex:PPGMutex):PGMutex;cdecl;external gthreadlib name 'g_static_mutex_get_mutex_impl';
  79. { shorthands for conditional and unconditional function calls }
  80. function g_thread_supported: gboolean;
  81. procedure g_mutex_lock (mutex: PGMutex);
  82. function g_mutex_trylock (mutex: PGMutex):gboolean;
  83. procedure g_mutex_unlock (mutex: PGMutex);
  84. procedure g_mutex_free (mutex: PGMutex);
  85. procedure g_cond_wait (cond: PGCond; mutex: PGMutex);
  86. function g_cond_timed_wait (cond: PGCond;
  87. mutex: PGMutex;
  88. end_time: PGTimeVal):gboolean;
  89. function g_mutex_new : PGMutex;
  90. function g_cond_new : PGCond;
  91. procedure g_cond_signal (cond: PGCond);
  92. procedure g_cond_broadcast (cond: PGCond);
  93. procedure g_cond_free (cond: PGCond);
  94. function g_private_new (dest: TGDestroyNotify): PGPrivate;
  95. function g_private_get (private_key: PGPrivate): gpointer;
  96. procedure g_private_set (var private_key: PGPrivate; data: gpointer);
  97. procedure g_thread_yield;
  98. function g_thread_create (func: TGThreadFunc;
  99. data: gpointer;
  100. joinable: gboolean;
  101. error: PPGError): PGThread;
  102. function g_thread_create_full (func : TGThreadFunc;
  103. data : gpointer;
  104. stack_size: gulong;
  105. joinable, bound: gboolean;
  106. priority : TGThreadPriority;
  107. error : ppGError): PGThread; cdecl; external gthreadlib name 'g_thread_create_full';
  108. function g_thread_self:PGThread;cdecl;external gthreadlib name 'g_thread_self';
  109. procedure g_thread_exit(retval:gpointer);cdecl;external gthreadlib name 'g_thread_exit';
  110. function g_thread_join(thread:PGThread):gpointer;cdecl;external gthreadlib name 'g_thread_join';
  111. procedure g_thread_set_priority(thread:PGThread; priority:TGThreadPriority);cdecl;external gthreadlib name 'g_thread_set_priority';
  112. { GStaticMutexes can be statically initialized with the value
  113. G_STATIC_MUTEX_INIT, and then they can directly be used, that is
  114. much easier, than having to explicitly allocate the mutex before
  115. use
  116. }
  117. procedure g_static_mutex_lock (mutex: PGStaticMutex);
  118. function g_static_mutex_trylock (mutex: PGStaticMutex): gboolean;
  119. procedure g_static_mutex_unlock (mutex: PGStaticMutex);
  120. procedure g_static_mutex_free(mutex:PGStaticMutex);cdecl;external gthreadlib name 'g_static_mutex_free';
  121. type
  122. PGStaticPrivate = ^TGStaticPrivate;
  123. TGStaticPrivate = record
  124. index : guint;
  125. end;
  126. const
  127. nG_STATIC_PRIVATE_INIT = 0; //renamed because of conflict with function_name
  128. procedure g_static_private_init(private_key:PGStaticPrivate);cdecl;external gthreadlib name 'g_static_private_init';
  129. function g_static_private_get(private_key:PGStaticPrivate):gpointer;cdecl;external gthreadlib name 'g_static_private_get';
  130. procedure g_static_private_set(private_key:PGStaticPrivate; data:gpointer; notify:TGDestroyNotify);cdecl;external gthreadlib name 'g_static_private_set';
  131. procedure g_static_private_free(private_key:PGStaticPrivate);cdecl;external gthreadlib name 'g_static_private_free';
  132. type
  133. PGStaticRecMutex = ^TGStaticRecMutex;
  134. TGStaticRecMutex = record
  135. mutex : TGStaticMutex;
  136. depth : guint;
  137. owner : TGSystemThread; // defined in glibconfig.inc
  138. end;
  139. const
  140. nG_STATIC_MUTEX_INIT = nil; // from glibconfig.h
  141. nG_STATIC_REC_MUTEX_INIT = nG_STATIC_MUTEX_INIT;
  142. procedure g_static_rec_mutex_init(mutex:PGStaticRecMutex);cdecl;external gthreadlib name 'g_static_rec_mutex_init';
  143. procedure g_static_rec_mutex_lock(mutex:PGStaticRecMutex);cdecl;external gthreadlib name 'g_static_rec_mutex_lock';
  144. function g_static_rec_mutex_trylock(mutex:PGStaticRecMutex):gboolean;cdecl;external gthreadlib name 'g_static_rec_mutex_trylock';
  145. procedure g_static_rec_mutex_unlock(mutex:PGStaticRecMutex);cdecl;external gthreadlib name 'g_static_rec_mutex_unlock';
  146. procedure g_static_rec_mutex_lock_full(mutex:PGStaticRecMutex; depth:guint);cdecl;external gthreadlib name 'g_static_rec_mutex_lock_full';
  147. function g_static_rec_mutex_unlock_full(mutex:PGStaticRecMutex):guint;cdecl;external gthreadlib name 'g_static_rec_mutex_unlock_full';
  148. procedure g_static_rec_mutex_free(mutex:PGStaticRecMutex);cdecl;external gthreadlib name 'g_static_rec_mutex_free';
  149. type
  150. PGStaticRWLock = ^TGStaticRWLock;
  151. TGStaticRWLock = record
  152. mutex : TGStaticMutex;
  153. read_cond : PGCond;
  154. write_cond : PGCond;
  155. read_counter : guint;
  156. write : gboolean;
  157. want_to_read : guint;
  158. want_to_write : guint;
  159. end;
  160. {* i can't translate this macro. any ideas???
  161. *
  162. * #define G_STATIC_RW_LOCK_INIT G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0
  163. *}
  164. procedure g_static_rw_lock_init(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_init';
  165. procedure g_static_rw_lock_reader_lock(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_reader_lock';
  166. function g_static_rw_lock_reader_trylock(lock:PGStaticRWLock):gboolean;cdecl;external gthreadlib name 'g_static_rw_lock_reader_trylock';
  167. procedure g_static_rw_lock_reader_unlock(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_reader_unlock';
  168. procedure g_static_rw_lock_writer_lock(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_writer_lock';
  169. function g_static_rw_lock_writer_trylock(lock:PGStaticRWLock):gboolean;cdecl;external gthreadlib name 'g_static_rw_lock_writer_trylock';
  170. procedure g_static_rw_lock_writer_unlock(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_writer_unlock';
  171. procedure g_static_rw_lock_free(lock:PGStaticRWLock);cdecl;external gthreadlib name 'g_static_rw_lock_free';
  172. { these are some convenience macros that expand to nothing if GLib
  173. was configured with --disable-threads. for using StaticMutexes,
  174. you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
  175. if you need to export the mutex. With G_LOCK_EXTERN (name) you can
  176. declare such an globally defined lock. name is a unique identifier
  177. for the protected varibale or code portion. locking, testing and
  178. unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
  179. G_TRYLOCK() respectively.
  180. }
  181. procedure glib_dummy_decl;cdecl;external gthreadlib name 'glib_dummy_decl';
  182. { anyone an idea???
  183. #define G_LOCK_NAME(name) g__ ## name ## _lock
  184. #define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
  185. #define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
  186. #define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
  187. }
  188. //{$endif}