PageRenderTime 95ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wrappers/glib/library/core/g_thread.e

http://github.com/tybor/Liberty
Specman e | 1516 lines | 57 code | 32 blank | 1427 comment | 0 complexity | 45a13d9f8e1473733c6bc698c8dcb4e3 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  1. indexing
  2. description: "Threads รข€” thread abstraction; including threads, different mutexes, conditions and thread private data"
  3. copyright: "(C) 2011 Paolo Redaelli"
  4. license: "LGPL v2 or later"
  5. version: "2.28"
  6. deferred class G_THREAD
  7. obsolete "G_THREAD is currently unusable because standard library is NOT thread safe. At all"
  8. -- A thread of work.
  9. -- Threads act almost like processes, but unlike processes all threads of
  10. -- one process share the same memory. This is good, as it provides easy
  11. -- communication between the involved threads via this shared memory, and
  12. -- it is bad, because strange things (so called "Heisenbugs") might happen
  13. -- if the program is not carefully designed. In particular, due to the
  14. -- concurrent nature of threads, no assumptions on the order of execution
  15. -- of code running in different threads can be made, unless order is
  16. -- explicitly forced by the programmer through synchronization primitives.
  17. -- The aim of the thread related functions in GLib is to provide a portable
  18. -- means for writing multi-threaded software. There are primitives for
  19. -- mutexes to protect the access to portions of memory (G_MUTEX,
  20. -- GStaticMutex, G_LOCK_DEFINE, GStaticRecMutex and GStaticRWLock). There
  21. -- are primitives for condition variables to allow synchronization of
  22. -- threads (GCond). There are primitives for thread-private data - data
  23. -- that every thread has a private instance of (GPrivate, GStaticPrivate).
  24. -- Last but definitely not least there are primitives to portably create
  25. -- and manage threads (GThread).
  26. -- When using G_THREADs in your program, you have to link with the
  27. -- libraries that the command pkg-config --libs gthread-2.0 outputs. This
  28. -- is not the case for all the other thread related functions of GLib.
  29. -- Those can be used without having to link with the thread libraries
  30. -- This will be hopefully automatized in further wrappers releases
  31. inherit
  32. WRAPPER
  33. redefine default_create end
  34. EIFFEL_OWNED
  35. redefine default_create end
  36. insert
  37. GTHREAD_EXTERNALS
  38. redefine default_create end
  39. feature
  40. default_create is
  41. do
  42. -- Please note that since version 2.24 the GObject initialization
  43. -- function g_type_init() initializes threads (with a NULL
  44. -- argument), so most applications, including those using Gtk+ will
  45. -- run with threads enabled. If you want a special thread
  46. -- implementation, make sure you call g_thread_init() before
  47. -- g_type_init() is called.
  48. -- We are going to use threads so we need either to call
  49. g_thread_init(default_pointer)
  50. -- since version 2.24 g_thread_init may be called multiple times and nothing happens except for the first call.
  51. -- If no thread system is available and vtable is NULL or if not all elements of vtable are non-NULL, then g_thread_init() will abort.
  52. -- check is_thread_system_initialized end
  53. end
  54. run is
  55. -- The command invoked when the thread is started
  56. deferred
  57. end
  58. start is
  59. -- Start Current thread as joinable.
  60. -- TODO: add proper queries and setters for joinability, stack_size, priority
  61. local this: G_THREAD; priority: GTHREADPRIORITY_ENUM; stack_size: NATURAL_32
  62. do
  63. priority.set_normal
  64. this := Current
  65. from_external_pointer(g_thread_create_full
  66. ($run,$this,stack_size,1,1,priority.value,default_pointer))
  67. -- GThread * g_thread_create (GThreadFunc func, gpointer data, gboolean joinable, GError **error);
  68. -- This function creates a new thread with the default priority.
  69. -- If joinable is TRUE, you can wait for this threads termination calling g_thread_join(). Otherwise the thread will just disappear when it terminates.
  70. -- The new thread executes the function func with the argument data. If the thread was created successfully, it is returned.
  71. -- error can be NULL to ignore errors, or non-NULL to report errors. The error is set, if and only if the function returns NULL.
  72. -- func :a function to execute in the new thread.
  73. -- data :an argument to supply to the new thread.
  74. -- joinable : should this thread be joinable?
  75. -- error : return location for error.
  76. -- Returns : the new GThread on success.
  77. -- This function is currently implemented with a macro: #define g_thread_create(func, data, joinable, error) (g_thread_create_full (func, data, 0, joinable, FALSE,G_THREAD_PRIORITY_NORMAL, error))
  78. end
  79. feature -- Commands
  80. set_priority (a_priority: GTHREADPRIORITY_ENUM) is
  81. -- Changes the priority of Current thread to `a_priority'
  82. -- It is not guaranteed that threads with different priorities really
  83. -- behave accordingly. On some systems (e.g. Linux) there are no thread
  84. -- priorities. On other systems (e.g. Solaris) there doesn't seem to be
  85. -- different scheduling for different priorities. All in all try to
  86. -- avoid being dependent on priorities.
  87. do
  88. g_thread_set_priority (handle, a_priority.value)
  89. end
  90. join is
  91. -- Waits until Current thread finishes, i.e. the command `run` finish
  92. -- or `thread_exit' is called by thread. All resources of thread including the
  93. -- GThread struct are released. thread must have been created with
  94. -- joinable=TRUE in g_thread_create(). The value returned by func or
  95. -- given to g_thread_exit() by thread is returned by this function.
  96. -- Returns : the return value of the thread.
  97. local p: POINTER
  98. do
  99. p := g_thread_join(handle)
  100. end
  101. yield is
  102. -- Gives way to other threads waiting to be scheduled
  103. -- This command is often used as a method to make busy wait less evil.
  104. -- But in most cases you will encounter, there are better methods to do
  105. -- that. So in general you shouldn't use it.
  106. -- Currenty unimplemented
  107. do
  108. not_yet_implemented
  109. -- g_thread_yield is a macro. wrappers_generator cannot handle this
  110. end
  111. thread_exit, finish is
  112. -- Exits the thread. If another thread is waiting for that thread using join and the current thread is joinable, the waiting thread will be woken up and get some_results as the result of join. If the current thread is not joinable, some_results are ignored.
  113. require current_shall_not_be_part_of_a_pool:
  114. -- Never call exit from within a thread of a G_THREAD_POOL, as that
  115. -- will mess up the bookkeeping and lead to funny and unwanted results.
  116. do
  117. g_thread_exit(default_pointer) --$some_results)
  118. end
  119. feature -- Process-wide queries
  120. is_thread_system_initialized: BOOLEAN is
  121. -- Has the Glib thread system been initialized?
  122. do
  123. Result:=g_thread_get_initialized.to_boolean
  124. end
  125. feature {} -- Unwrapped features
  126. -- g_thread_create_full ()
  127. --
  128. -- GThread * g_thread_create_full (GThreadFunc func,
  129. -- gpointer data,
  130. -- gulong stack_size,
  131. -- gboolean joinable,
  132. -- gboolean bound,
  133. -- GThreadPriority priority,
  134. -- GError **error);
  135. --
  136. -- This function creates a new thread with the priority priority. If the underlying thread implementation supports it, the thread gets a stack size of stack_size or the default value for the current platform, if stack_size is 0.
  137. --
  138. -- If joinable is TRUE, you can wait for this threads termination calling g_thread_join(). Otherwise the thread will just disappear when it terminates. If bound is TRUE, this thread will be scheduled in the system scope, otherwise the implementation is free to do scheduling in the process scope. The first variant is more expensive resource-wise, but generally faster. On some systems (e.g. Linux) all threads are bound.
  139. --
  140. -- The new thread executes the function func with the argument data. If the thread was created successfully, it is returned.
  141. --
  142. -- error can be NULL to ignore errors, or non-NULL to report errors. The error is set, if and only if the function returns NULL.
  143. --
  144. -- Note
  145. --
  146. -- It is not guaranteed that threads with different priorities really behave accordingly. On some systems (e.g. Linux) there are no thread priorities. On other systems (e.g. Solaris) there doesn't seem to be different scheduling for different priorities. All in all try to avoid being dependent on priorities. Use G_THREAD_PRIORITY_NORMAL here as a default.
  147. --
  148. -- Note
  149. --
  150. -- Only use g_thread_create_full() if you really can't use g_thread_create() instead. g_thread_create() does not take stack_size, bound, and priority as arguments, as they should only be used in cases in which it is unavoidable.
  151. --
  152. -- func :
  153. -- a function to execute in the new thread.
  154. --
  155. -- data :
  156. -- an argument to supply to the new thread.
  157. --
  158. -- stack_size :
  159. -- a stack size for the new thread.
  160. --
  161. -- joinable :
  162. -- should this thread be joinable?
  163. --
  164. -- bound :
  165. -- should this thread be bound to a system thread?
  166. --
  167. -- priority :
  168. -- a priority for the thread.
  169. --
  170. -- error :
  171. -- return location for error.
  172. --
  173. -- Returns :
  174. -- the new GThread on success.
  175. -- g_thread_self ()
  176. --
  177. -- GThread * g_thread_self (void);
  178. --
  179. -- This functions returns the GThread corresponding to the calling thread.
  180. --
  181. -- Returns :
  182. -- the current thread.
  183. -- g_thread_join ()
  184. --
  185. -- gpointer g_thread_join (GThread *thread);
  186. --
  187. -- Waits until thread finishes, i.e. the function func, as given to g_thread_create(), returns or g_thread_exit() is called by thread. All resources of thread including the GThread struct are released. thread must have been created with joinable=TRUE in g_thread_create(). The value returned by func or given to g_thread_exit() by thread is returned by this function.
  188. --
  189. -- thread :
  190. -- a GThread to be waited for.
  191. --
  192. -- Returns :
  193. -- the return value of the thread.
  194. -- g_thread_foreach ()
  195. --
  196. -- void g_thread_foreach (GFunc thread_func,
  197. -- gpointer user_data);
  198. --
  199. -- Call thread_func on all existing GThread structures. Note that threads may decide to exit while thread_func is running, so without intimate knowledge about the lifetime of foreign threads, thread_func shouldn't access the GThread* pointer passed in as first argument. However, thread_func will not be called for threads which are known to have exited already.
  200. --
  201. -- Due to thread lifetime checks, this function has an execution complexity which is quadratic in the number of existing threads.
  202. --
  203. -- thread_func :
  204. -- function to call for all GThread structures
  205. --
  206. -- user_data :
  207. -- second argument to thread_func
  208. --
  209. -- Since 2.10
  210. -- GMutex
  211. --
  212. -- typedef struct _GMutex GMutex;
  213. --
  214. -- The GMutex struct is an opaque data structure to represent a mutex (mutual exclusion). It can be used to protect data against shared access. Take for example the following function:
  215. --
  216. -- Example 2. A function which will not work in a threaded environment
  217. --
  218. -- 1
  219. -- 2
  220. -- 3
  221. -- 4
  222. -- 5
  223. -- 6
  224. -- 7
  225. -- 8
  226. -- 9
  227. -- 10
  228. -- 11
  229. -- 12
  230. --
  231. --
  232. --
  233. -- int
  234. -- give_me_next_number (void)
  235. -- {
  236. -- static int current_number = 0;
  237. --
  238. -- /* now do a very complicated calculation to calculate the new
  239. -- * number, this might for example be a random number generator
  240. -- */
  241. -- current_number = calc_next_number (current_number);
  242. --
  243. -- return current_number;
  244. -- }
  245. --
  246. --
  247. --
  248. -- It is easy to see that this won't work in a multi-threaded application. There current_number must be protected against shared access. A first naive implementation would be:
  249. --
  250. -- Example 3. The wrong way to write a thread-safe function
  251. --
  252. -- 1
  253. -- 2
  254. -- 3
  255. -- 4
  256. -- 5
  257. -- 6
  258. -- 7
  259. -- 8
  260. -- 9
  261. -- 10
  262. -- 11
  263. -- 12
  264. -- 13
  265. -- 14
  266. -- 15
  267. --
  268. --
  269. --
  270. -- int
  271. -- give_me_next_number (void)
  272. -- {
  273. -- static int current_number = 0;
  274. -- int ret_val;
  275. -- static GMutex * mutex = NULL;
  276. --
  277. -- if (!mutex) mutex = g_mutex_new ();
  278. --
  279. -- g_mutex_lock (mutex);
  280. -- ret_val = current_number = calc_next_number (current_number);
  281. -- g_mutex_unlock (mutex);
  282. --
  283. -- return ret_val;
  284. -- }
  285. --
  286. --
  287. --
  288. -- This looks like it would work, but there is a race condition while constructing the mutex and this code cannot work reliable. Please do not use such constructs in your own programs! One working solution is:
  289. --
  290. -- Example 4. A correct thread-safe function
  291. --
  292. -- 1
  293. -- 2
  294. -- 3
  295. -- 4
  296. -- 5
  297. -- 6
  298. -- 7
  299. -- 8
  300. -- 9
  301. -- 10
  302. -- 11
  303. -- 12
  304. -- 13
  305. -- 14
  306. -- 15
  307. -- 16
  308. -- 17
  309. -- 18
  310. -- 19
  311. -- 20
  312. -- 21
  313. -- 22
  314. -- 23
  315. -- 24
  316. -- 25
  317. -- 26
  318. --
  319. --
  320. --
  321. -- static GMutex *give_me_next_number_mutex = NULL;
  322. --
  323. -- /* this function must be called before any call to
  324. -- * give_me_next_number()
  325. -- *
  326. -- * it must be called exactly once.
  327. -- */
  328. -- void
  329. -- init_give_me_next_number (void)
  330. -- {
  331. -- g_assert (give_me_next_number_mutex == NULL);
  332. -- give_me_next_number_mutex = g_mutex_new ();
  333. -- }
  334. --
  335. -- int
  336. -- give_me_next_number (void)
  337. -- {
  338. -- static int current_number = 0;
  339. -- int ret_val;
  340. --
  341. -- g_mutex_lock (give_me_next_number_mutex);
  342. -- ret_val = current_number = calc_next_number (current_number);
  343. -- g_mutex_unlock (give_me_next_number_mutex);
  344. --
  345. -- return ret_val;
  346. -- }
  347. --
  348. --
  349. --
  350. -- GStaticMutex provides a simpler and safer way of doing this.
  351. --
  352. -- If you want to use a mutex, and your code should also work without calling g_thread_init() first, then you can not use a GMutex, as g_mutex_new() requires that the thread system be initialized. Use a GStaticMutex instead.
  353. --
  354. -- A GMutex should only be accessed via the following functions.
  355. --
  356. -- Note
  357. --
  358. -- All of the g_mutex_* functions are actually macros. Apart from taking their addresses, you can however use them as if they were functions.
  359. --
  360. -- g_mutex_new ()
  361. --
  362. -- GMutex * g_mutex_new ();
  363. --
  364. -- Creates a new GMutex.
  365. --
  366. -- Note
  367. --
  368. -- This function will abort if g_thread_init() has not been called yet.
  369. --
  370. -- Returns :
  371. -- a new GMutex.
  372. -- g_mutex_lock ()
  373. --
  374. -- void g_mutex_lock (GMutex *mutex);
  375. --
  376. -- Locks mutex. If mutex is already locked by another thread, the current thread will block until mutex is unlocked by the other thread.
  377. --
  378. -- This function can be used even if g_thread_init() has not yet been called, and, in that case, will do nothing.
  379. --
  380. -- Note
  381. --
  382. -- GMutex is neither guaranteed to be recursive nor to be non-recursive, i.e. a thread could deadlock while calling g_mutex_lock(), if it already has locked mutex. Use GStaticRecMutex, if you need recursive mutexes.
  383. --
  384. -- mutex :
  385. -- a GMutex.
  386. -- g_mutex_trylock ()
  387. --
  388. -- gboolean g_mutex_trylock (GMutex *mutex);
  389. --
  390. -- Tries to lock mutex. If mutex is already locked by another thread, it immediately returns FALSE. Otherwise it locks mutex and returns TRUE.
  391. --
  392. -- This function can be used even if g_thread_init() has not yet been called, and, in that case, will immediately return TRUE.
  393. --
  394. -- Note
  395. --
  396. -- GMutex is neither guaranteed to be recursive nor to be non-recursive, i.e. the return value of g_mutex_trylock() could be both FALSE or TRUE, if the current thread already has locked mutex. Use GStaticRecMutex, if you need recursive mutexes.
  397. --
  398. -- mutex :
  399. -- a GMutex.
  400. --
  401. -- Returns :
  402. -- TRUE, if mutex could be locked.
  403. -- g_mutex_unlock ()
  404. --
  405. -- void g_mutex_unlock (GMutex *mutex);
  406. --
  407. -- Unlocks mutex. If another thread is blocked in a g_mutex_lock() call for mutex, it will be woken and can lock mutex itself.
  408. --
  409. -- This function can be used even if g_thread_init() has not yet been called, and, in that case, will do nothing.
  410. --
  411. -- mutex :
  412. -- a GMutex.
  413. -- g_mutex_free ()
  414. --
  415. -- void g_mutex_free (GMutex *mutex);
  416. --
  417. -- Destroys mutex.
  418. --
  419. -- Note
  420. --
  421. -- Calling g_mutex_free() on a locked mutex may result in undefined behaviour.
  422. --
  423. -- mutex :
  424. -- a GMutex.
  425. -- GStaticMutex
  426. --
  427. -- typedef struct _GStaticMutex GStaticMutex;
  428. --
  429. -- A GStaticMutex works like a GMutex, but it has one significant advantage. It doesn't need to be created at run-time like a GMutex, but can be defined at compile-time. Here is a shorter, easier and safer version of our give_me_next_number() example:
  430. --
  431. -- Example 5. Using GStaticMutex to simplify thread-safe programming
  432. --
  433. -- 1
  434. -- 2
  435. -- 3
  436. -- 4
  437. -- 5
  438. -- 6
  439. -- 7
  440. -- 8
  441. -- 9
  442. -- 10
  443. -- 11
  444. -- 12
  445. -- 13
  446. --
  447. --
  448. --
  449. -- int
  450. -- give_me_next_number (void)
  451. -- {
  452. -- static int current_number = 0;
  453. -- int ret_val;
  454. -- static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
  455. --
  456. -- g_static_mutex_lock (&mutex);
  457. -- ret_val = current_number = calc_next_number (current_number);
  458. -- g_static_mutex_unlock (&mutex);
  459. --
  460. -- return ret_val;
  461. -- }
  462. --
  463. --
  464. --
  465. -- Sometimes you would like to dynamically create a mutex. If you don't want to require prior calling to g_thread_init(), because your code should also be usable in non-threaded programs, you are not able to use g_mutex_new() and thus GMutex, as that requires a prior call to g_thread_init(). In theses cases you can also use a GStaticMutex. It must be initialized with g_static_mutex_init() before using it and freed with with g_static_mutex_free() when not needed anymore to free up any allocated resources.
  466. --
  467. -- Even though GStaticMutex is not opaque, it should only be used with the following functions, as it is defined differently on different platforms.
  468. --
  469. -- All of the g_static_mutex_* functions apart from g_static_mutex_get_mutex can also be used even if g_thread_init() has not yet been called. Then they do nothing, apart from g_static_mutex_trylock, which does nothing but returning TRUE.
  470. --
  471. -- Note
  472. --
  473. -- All of the g_static_mutex_* functions are actually macros. Apart from taking their addresses, you can however use them as if they were functions.
  474. --
  475. -- G_STATIC_MUTEX_INIT
  476. --
  477. -- #define G_STATIC_MUTEX_INIT
  478. --
  479. -- A GStaticMutex must be initialized with this macro, before it can be used. This macro can used be to initialize a variable, but it cannot be assigned to a variable. In that case you have to use g_static_mutex_init().
  480. --
  481. -- 1
  482. --
  483. --
  484. --
  485. -- GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
  486. --
  487. -- g_static_mutex_init ()
  488. --
  489. -- void g_static_mutex_init (GStaticMutex *mutex);
  490. --
  491. -- Initializes mutex. Alternatively you can initialize it with G_STATIC_MUTEX_INIT.
  492. --
  493. -- mutex :
  494. -- a GStaticMutex to be initialized.
  495. -- g_static_mutex_lock ()
  496. --
  497. -- void g_static_mutex_lock (GStaticMutex *mutex);
  498. --
  499. -- Works like g_mutex_lock(), but for a GStaticMutex.
  500. --
  501. -- mutex :
  502. -- a GStaticMutex.
  503. -- g_static_mutex_trylock ()
  504. --
  505. -- gboolean g_static_mutex_trylock (GStaticMutex *mutex);
  506. --
  507. -- Works like g_mutex_trylock(), but for a GStaticMutex.
  508. --
  509. -- mutex :
  510. -- a GStaticMutex.
  511. --
  512. -- Returns :
  513. -- TRUE, if the GStaticMutex could be locked.
  514. -- g_static_mutex_unlock ()
  515. --
  516. -- void g_static_mutex_unlock (GStaticMutex *mutex);
  517. --
  518. -- Works like g_mutex_unlock(), but for a GStaticMutex.
  519. --
  520. -- mutex :
  521. -- a GStaticMutex.
  522. -- g_static_mutex_get_mutex ()
  523. --
  524. -- GMutex * g_static_mutex_get_mutex (GStaticMutex *mutex);
  525. --
  526. -- For some operations (like g_cond_wait()) you must have a GMutex instead of a GStaticMutex. This function will return the corresponding GMutex for mutex.
  527. --
  528. -- mutex :
  529. -- a GStaticMutex.
  530. --
  531. -- Returns :
  532. -- the GMutex corresponding to mutex.
  533. -- g_static_mutex_free ()
  534. --
  535. -- void g_static_mutex_free (GStaticMutex *mutex);
  536. --
  537. -- Releases all resources allocated to mutex.
  538. --
  539. -- You don't have to call this functions for a GStaticMutex with an unbounded lifetime, i.e. objects declared 'static', but if you have a GStaticMutex as a member of a structure and the structure is freed, you should also free the GStaticMutex.
  540. --
  541. -- Note
  542. --
  543. -- Calling g_static_mutex_free() on a locked mutex may result in undefined behaviour.
  544. --
  545. -- mutex :
  546. -- a GStaticMutex to be freed.
  547. -- G_LOCK_DEFINE()
  548. --
  549. -- #define G_LOCK_DEFINE(name)
  550. --
  551. -- The G_LOCK_* macros provide a convenient interface to GStaticMutex with the advantage that they will expand to nothing in programs compiled against a thread-disabled GLib, saving code and memory there. G_LOCK_DEFINE defines a lock. It can appear anywhere variable definitions may appear in programs, i.e. in the first block of a function or outside of functions. The name parameter will be mangled to get the name of the GStaticMutex. This means that you can use names of existing variables as the parameter - e.g. the name of the variable you intent to protect with the lock. Look at our give_me_next_number() example using the G_LOCK_* macros:
  552. --
  553. -- Example 6. Using the G_LOCK_* convenience macros
  554. --
  555. -- 1
  556. -- 2
  557. -- 3
  558. -- 4
  559. -- 5
  560. -- 6
  561. -- 7
  562. -- 8
  563. -- 9
  564. -- 10
  565. -- 11
  566. -- 12
  567. -- 13
  568. -- 14
  569. --
  570. --
  571. --
  572. -- G_LOCK_DEFINE (current_number);
  573. --
  574. -- int
  575. -- give_me_next_number (void)
  576. -- {
  577. -- static int current_number = 0;
  578. -- int ret_val;
  579. --
  580. -- G_LOCK (current_number);
  581. -- ret_val = current_number = calc_next_number (current_number);
  582. -- G_UNLOCK (current_number);
  583. --
  584. -- return ret_val;
  585. -- }
  586. --
  587. --
  588. --
  589. -- name :
  590. -- the name of the lock.
  591. -- G_LOCK_DEFINE_STATIC()
  592. --
  593. -- #define G_LOCK_DEFINE_STATIC(name)
  594. --
  595. -- This works like G_LOCK_DEFINE, but it creates a static object.
  596. --
  597. -- name :
  598. -- the name of the lock.
  599. -- G_LOCK_EXTERN()
  600. --
  601. -- #define G_LOCK_EXTERN(name)
  602. --
  603. -- This declares a lock, that is defined with G_LOCK_DEFINE in another module.
  604. --
  605. -- name :
  606. -- the name of the lock.
  607. -- G_LOCK()
  608. --
  609. -- #define G_LOCK(name)
  610. --
  611. -- Works like g_mutex_lock(), but for a lock defined with G_LOCK_DEFINE.
  612. --
  613. -- name :
  614. -- the name of the lock.
  615. -- G_TRYLOCK()
  616. --
  617. -- #define G_TRYLOCK(name)
  618. --
  619. -- Works like g_mutex_trylock(), but for a lock defined with G_LOCK_DEFINE.
  620. --
  621. -- name :
  622. -- the name of the lock.
  623. --
  624. -- Returns :
  625. -- TRUE, if the lock could be locked.
  626. -- G_UNLOCK()
  627. --
  628. -- #define G_UNLOCK(name)
  629. --
  630. -- Works like g_mutex_unlock(), but for a lock defined with G_LOCK_DEFINE.
  631. --
  632. -- name :
  633. -- the name of the lock.
  634. -- struct GStaticRecMutex
  635. --
  636. -- struct GStaticRecMutex {
  637. -- };
  638. --
  639. -- A GStaticRecMutex works like a GStaticMutex, but it can be locked multiple times by one thread. If you enter it n times, you have to unlock it n times again to let other threads lock it. An exception is the function g_static_rec_mutex_unlock_full(): that allows you to unlock a GStaticRecMutex completely returning the depth, (i.e. the number of times this mutex was locked). The depth can later be used to restore the state of the GStaticRecMutex by calling g_static_rec_mutex_lock_full().
  640. --
  641. -- Even though GStaticRecMutex is not opaque, it should only be used with the following functions.
  642. --
  643. -- All of the g_static_rec_mutex_* functions can be used even if g_thread_init() has not been called. Then they do nothing, apart from g_static_rec_mutex_trylock, which does nothing but returning TRUE.
  644. -- G_STATIC_REC_MUTEX_INIT
  645. --
  646. -- #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
  647. --
  648. -- A GStaticRecMutex must be initialized with this macro before it can be used. This macro can used be to initialize a variable, but it cannot be assigned to a variable. In that case you have to use g_static_rec_mutex_init().
  649. --
  650. -- 1
  651. --
  652. --
  653. --
  654. -- GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
  655. --
  656. -- g_static_rec_mutex_init ()
  657. --
  658. -- void g_static_rec_mutex_init (GStaticRecMutex *mutex);
  659. --
  660. -- A GStaticRecMutex must be initialized with this function before it can be used. Alternatively you can initialize it with G_STATIC_REC_MUTEX_INIT.
  661. --
  662. -- mutex :
  663. -- a GStaticRecMutex to be initialized.
  664. -- g_static_rec_mutex_lock ()
  665. --
  666. -- void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
  667. --
  668. -- Locks mutex. If mutex is already locked by another thread, the current thread will block until mutex is unlocked by the other thread. If mutex is already locked by the calling thread, this functions increases the depth of mutex and returns immediately.
  669. --
  670. -- mutex :
  671. -- a GStaticRecMutex to lock.
  672. -- g_static_rec_mutex_trylock ()
  673. --
  674. -- gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
  675. --
  676. -- Tries to lock mutex. If mutex is already locked by another thread, it immediately returns FALSE. Otherwise it locks mutex and returns TRUE. If mutex is already locked by the calling thread, this functions increases the depth of mutex and immediately returns TRUE.
  677. --
  678. -- mutex :
  679. -- a GStaticRecMutex to lock.
  680. --
  681. -- Returns :
  682. -- TRUE, if mutex could be locked.
  683. -- g_static_rec_mutex_unlock ()
  684. --
  685. -- void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
  686. --
  687. -- Unlocks mutex. Another thread will be allowed to lock mutex only when it has been unlocked as many times as it had been locked before. If mutex is completely unlocked and another thread is blocked in a g_static_rec_mutex_lock() call for mutex, it will be woken and can lock mutex itself.
  688. --
  689. -- mutex :
  690. -- a GStaticRecMutex to unlock.
  691. -- g_static_rec_mutex_lock_full ()
  692. --
  693. -- void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
  694. -- guint depth);
  695. --
  696. -- Works like calling g_static_rec_mutex_lock() for mutex depth times.
  697. --
  698. -- mutex :
  699. -- a GStaticRecMutex to lock.
  700. --
  701. -- depth :
  702. -- number of times this mutex has to be unlocked to be completely unlocked.
  703. -- g_static_rec_mutex_unlock_full ()
  704. --
  705. -- guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
  706. --
  707. -- Completely unlocks mutex. If another thread is blocked in a g_static_rec_mutex_lock() call for mutex, it will be woken and can lock mutex itself. This function returns the number of times that mutex has been locked by the current thread. To restore the state before the call to g_static_rec_mutex_unlock_full() you can call g_static_rec_mutex_lock_full() with the depth returned by this function.
  708. --
  709. -- mutex :
  710. -- a GStaticRecMutex to completely unlock.
  711. --
  712. -- Returns :
  713. -- number of times mutex has been locked by the current thread.
  714. -- g_static_rec_mutex_free ()
  715. --
  716. -- void g_static_rec_mutex_free (GStaticRecMutex *mutex);
  717. --
  718. -- Releases all resources allocated to a GStaticRecMutex.
  719. --
  720. -- You don't have to call this functions for a GStaticRecMutex with an unbounded lifetime, i.e. objects declared 'static', but if you have a GStaticRecMutex as a member of a structure and the structure is freed, you should also free the GStaticRecMutex.
  721. --
  722. -- mutex :
  723. -- a GStaticRecMutex to be freed.
  724. -- struct GStaticRWLock
  725. --
  726. -- struct GStaticRWLock {
  727. -- };
  728. --
  729. -- The GStaticRWLock struct represents a read-write lock. A read-write lock can be used for protecting data that some portions of code only read from, while others also write. In such situations it is desirable that several readers can read at once, whereas of course only one writer may write at a time. Take a look at the following example:
  730. --
  731. -- Example 7. An array with access functions
  732. --
  733. -- 1
  734. -- 2
  735. -- 3
  736. -- 4
  737. -- 5
  738. -- 6
  739. -- 7
  740. -- 8
  741. -- 9
  742. -- 10
  743. -- 11
  744. -- 12
  745. -- 13
  746. -- 14
  747. -- 15
  748. -- 16
  749. -- 17
  750. -- 18
  751. -- 19
  752. -- 20
  753. -- 21
  754. -- 22
  755. -- 23
  756. -- 24
  757. -- 25
  758. -- 26
  759. -- 27
  760. -- 28
  761. -- 29
  762. -- 30
  763. -- 31
  764. -- 32
  765. -- 33
  766. --
  767. --
  768. --
  769. -- GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
  770. -- GPtrArray *array;
  771. --
  772. -- gpointer
  773. -- my_array_get (guint index)
  774. -- {
  775. -- gpointer retval = NULL;
  776. --
  777. -- if (!array)
  778. -- return NULL;
  779. --
  780. -- g_static_rw_lock_reader_lock (&rwlock);
  781. -- if (index < array->len)
  782. -- retval = g_ptr_array_index (array, index);
  783. -- g_static_rw_lock_reader_unlock (&rwlock);
  784. --
  785. -- return retval;
  786. -- }
  787. --
  788. -- void
  789. -- my_array_set (guint index, gpointer data)
  790. -- {
  791. -- g_static_rw_lock_writer_lock (&rwlock);
  792. --
  793. -- if (!array)
  794. -- array = g_ptr_array_new ();
  795. --
  796. -- if (index >= array->len)
  797. -- g_ptr_array_set_size (array, index+1);
  798. -- g_ptr_array_index (array, index) = data;
  799. --
  800. -- g_static_rw_lock_writer_unlock (&rwlock);
  801. -- }
  802. --
  803. --
  804. --
  805. -- This example shows an array which can be accessed by many readers (the my_array_get() function) simultaneously, whereas the writers (the my_array_set() function) will only be allowed once at a time and only if no readers currently access the array. This is because of the potentially dangerous resizing of the array. Using these functions is fully multi-thread safe now.
  806. --
  807. -- Most of the time, writers should have precedence over readers. That means, for this implementation, that as soon as a writer wants to lock the data, no other reader is allowed to lock the data, whereas, of course, the readers that already have locked the data are allowed to finish their operation. As soon as the last reader unlocks the data, the writer will lock it.
  808. --
  809. -- Even though GStaticRWLock is not opaque, it should only be used with the following functions.
  810. --
  811. -- All of the g_static_rw_lock_* functions can be used even if g_thread_init() has not been called. Then they do nothing, apart from g_static_rw_lock_*_trylock, which does nothing but returning TRUE.
  812. --
  813. -- Note
  814. --
  815. -- A read-write lock has a higher overhead than a mutex. For example, both g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock() have to lock and unlock a GStaticMutex, so it takes at least twice the time to lock and unlock a GStaticRWLock that it does to lock and unlock a GStaticMutex. So only data structures that are accessed by multiple readers, and which keep the lock for a considerable time justify a GStaticRWLock. The above example most probably would fare better with a GStaticMutex.
  816. --
  817. -- G_STATIC_RW_LOCK_INIT
  818. --
  819. -- #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0 }
  820. --
  821. -- A GStaticRWLock must be initialized with this macro before it can be used. This macro can used be to initialize a variable, but it cannot be assigned to a variable. In that case you have to use g_static_rw_lock_init().
  822. --
  823. -- 1
  824. --
  825. --
  826. --
  827. -- GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
  828. --
  829. -- g_static_rw_lock_init ()
  830. --
  831. -- void g_static_rw_lock_init (GStaticRWLock *lock);
  832. --
  833. -- A GStaticRWLock must be initialized with this function before it can be used. Alternatively you can initialize it with G_STATIC_RW_LOCK_INIT.
  834. --
  835. -- lock :
  836. -- a GStaticRWLock to be initialized.
  837. -- g_static_rw_lock_reader_lock ()
  838. --
  839. -- void g_static_rw_lock_reader_lock (GStaticRWLock *lock);
  840. --
  841. -- Locks lock for reading. There may be unlimited concurrent locks for reading of a GStaticRWLock at the same time. If lock is already locked for writing by another thread or if another thread is already waiting to lock lock for writing, this function will block until lock is unlocked by the other writing thread and no other writing threads want to lock lock. This lock has to be unlocked by g_static_rw_lock_reader_unlock().
  842. --
  843. -- GStaticRWLock is not recursive. It might seem to be possible to recursively lock for reading, but that can result in a deadlock, due to writer preference.
  844. --
  845. -- lock :
  846. -- a GStaticRWLock to lock for reading.
  847. -- g_static_rw_lock_reader_trylock ()
  848. --
  849. -- gboolean g_static_rw_lock_reader_trylock (GStaticRWLock *lock);
  850. --
  851. -- Tries to lock lock for reading. If lock is already locked for writing by another thread or if another thread is already waiting to lock lock for writing, immediately returns FALSE. Otherwise locks lock for reading and returns TRUE. This lock has to be unlocked by g_static_rw_lock_reader_unlock().
  852. --
  853. -- lock :
  854. -- a GStaticRWLock to lock for reading.
  855. --
  856. -- Returns :
  857. -- TRUE, if lock could be locked for reading.
  858. -- g_static_rw_lock_reader_unlock ()
  859. --
  860. -- void g_static_rw_lock_reader_unlock (GStaticRWLock *lock);
  861. --
  862. -- Unlocks lock. If a thread waits to lock lock for writing and all locks for reading have been unlocked, the waiting thread is woken up and can lock lock for writing.
  863. --
  864. -- lock :
  865. -- a GStaticRWLock to unlock after reading.
  866. -- g_static_rw_lock_writer_lock ()
  867. --
  868. -- void g_static_rw_lock_writer_lock (GStaticRWLock *lock);
  869. --
  870. -- Locks lock for writing. If lock is already locked for writing or reading by other threads, this function will block until lock is completely unlocked and then lock lock for writing. While this functions waits to lock lock, no other thread can lock lock for reading. When lock is locked for writing, no other thread can lock lock (neither for reading nor writing). This lock has to be unlocked by g_static_rw_lock_writer_unlock().
  871. --
  872. -- lock :
  873. -- a GStaticRWLock to lock for writing.
  874. -- g_static_rw_lock_writer_trylock ()
  875. --
  876. -- gboolean g_static_rw_lock_writer_trylock (GStaticRWLock *lock);
  877. --
  878. -- Tries to lock lock for writing. If lock is already locked (for either reading or writing) by another thread, it immediately returns FALSE. Otherwise it locks lock for writing and returns TRUE. This lock has to be unlocked by g_static_rw_lock_writer_unlock().
  879. --
  880. -- lock :
  881. -- a GStaticRWLock to lock for writing.
  882. --
  883. -- Returns :
  884. -- TRUE, if lock could be locked for writing.
  885. -- g_static_rw_lock_writer_unlock ()
  886. --
  887. -- void g_static_rw_lock_writer_unlock (GStaticRWLock *lock);
  888. --
  889. -- Unlocks lock. If a thread is waiting to lock lock for writing and all locks for reading have been unlocked, the waiting thread is woken up and can lock lock for writing. If no thread is waiting to lock lock for writing, and some thread or threads are waiting to lock lock for reading, the waiting threads are woken up and can lock lock for reading.
  890. --
  891. -- lock :
  892. -- a GStaticRWLock to unlock after writing.
  893. -- g_static_rw_lock_free ()
  894. --
  895. -- void g_static_rw_lock_free (GStaticRWLock *lock);
  896. --
  897. -- Releases all resources allocated to lock.
  898. --
  899. -- You don't have to call this functions for a GStaticRWLock with an unbounded lifetime, i.e. objects declared 'static', but if you have a GStaticRWLock as a member of a structure, and the structure is freed, you should also free the GStaticRWLock.
  900. --
  901. -- lock :
  902. -- a GStaticRWLock to be freed.
  903. -- GCond
  904. --
  905. -- typedef struct _GCond GCond;
  906. --
  907. -- The GCond struct is an opaque data structure that represents a condition. Threads can block on a GCond if they find a certain condition to be false. If other threads change the state of this condition they signal the GCond, and that causes the waiting threads to be woken up.
  908. --
  909. -- Example 8. Using GCond to block a thread until a condition is satisfied
  910. --
  911. -- 1
  912. -- 2
  913. -- 3
  914. -- 4
  915. -- 5
  916. -- 6
  917. -- 7
  918. -- 8
  919. -- 9
  920. -- 10
  921. -- 11
  922. -- 12
  923. -- 13
  924. -- 14
  925. -- 15
  926. -- 16
  927. -- 17
  928. -- 18
  929. -- 19
  930. -- 20
  931. -- 21
  932. -- 22
  933. -- 23
  934. -- 24
  935. -- 25
  936. -- 26
  937. -- 27
  938. --
  939. --
  940. --
  941. -- GCond* data_cond = NULL; /* Must be initialized somewhere */
  942. -- GMutex* data_mutex = NULL; /* Must be initialized somewhere */
  943. -- gpointer current_data = NULL;
  944. --
  945. -- void
  946. -- push_data (gpointer data)
  947. -- {
  948. -- g_mutex_lock (data_mutex);
  949. -- current_data = data;
  950. -- g_cond_signal (data_cond);
  951. -- g_mutex_unlock (data_mutex);
  952. -- }
  953. --
  954. -- gpointer
  955. -- pop_data (void)
  956. -- {
  957. -- gpointer data;
  958. --
  959. -- g_mutex_lock (data_mutex);
  960. -- while (!current_data)
  961. -- g_cond_wait (data_cond, data_mutex);
  962. -- data = current_data;
  963. -- current_data = NULL;
  964. -- g_mutex_unlock (data_mutex);
  965. --
  966. -- return data;
  967. -- }
  968. --
  969. --
  970. --
  971. -- Whenever a thread calls pop_data() now, it will wait until current_data is non-NULL, i.e. until some other thread has called push_data().
  972. --
  973. -- Note
  974. --
  975. -- It is important to use the g_cond_wait() and g_cond_timed_wait() functions only inside a loop which checks for the condition to be true. It is not guaranteed that the waiting thread will find the condition fulfilled after it wakes up, even if the signaling thread left the condition in that state: another thread may have altered the condition before the waiting thread got the chance to be woken up, even if the condition itself is protected by a GMutex, like above.
  976. --
  977. -- A GCond should only be accessed via the following functions.
  978. --
  979. -- Note
  980. --
  981. -- All of the g_cond_* functions are actually macros. Apart from taking their addresses, you can however use them as if they were functions.
  982. --
  983. -- g_cond_new ()
  984. --
  985. -- GCond* g_cond_new ();
  986. --
  987. -- Creates a new GCond. This function will abort, if g_thread_init() has not been called yet.
  988. --
  989. -- Returns :
  990. -- a new GCond.
  991. -- g_cond_signal ()
  992. --
  993. -- void g_cond_signal (GCond *cond);
  994. --
  995. -- If threads are waiting for cond, exactly one of them is woken up. It is good practice to hold the same lock as the waiting thread while calling this function, though not required.
  996. --
  997. -- This function can be used even if g_thread_init() has not yet been called, and, in that case, will do nothing.
  998. --
  999. -- cond :
  1000. -- a GCond.
  1001. -- g_cond_broadcast ()
  1002. --
  1003. -- void g_cond_broadcast (GCond *cond);
  1004. --
  1005. -- If threads are waiting for cond, all of them are woken up. It is good practice to lock the same mutex as the waiting threads, while calling this function, though not required.
  1006. --
  1007. -- This function can be used even if g_thread_init() has not yet been called, and, in that case, will do nothing.
  1008. --
  1009. -- cond :
  1010. -- a GCond.
  1011. -- g_cond_wait ()
  1012. --
  1013. -- void g_cond_wait (GCond *cond,
  1014. -- GMutex *mutex);
  1015. --
  1016. -- Waits until this thread is woken up on cond. The mutex is unlocked before falling asleep and locked again before resuming.
  1017. --
  1018. -- This function can be used even if g_thread_init() has not yet been called, and, in that case, will immediately return.
  1019. --
  1020. -- cond :
  1021. -- a GCond.
  1022. --
  1023. -- mutex :
  1024. -- a GMutex, that is currently locked.
  1025. -- g_cond_timed_wait ()
  1026. --
  1027. -- gboolean g_cond_timed_wait (GCond *cond,
  1028. -- GMutex *mutex,
  1029. -- GTimeVal *abs_time);
  1030. --
  1031. -- Waits until this thread is woken up on cond, but not longer than until the time specified by abs_time. The mutex is unlocked before falling asleep and locked again before resuming.
  1032. --
  1033. -- If abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
  1034. --
  1035. -- This function can be used even if g_thread_init() has not yet been called, and, in that case, will immediately return TRUE.
  1036. --
  1037. -- To easily calculate abs_time a combination of g_get_current_time() and g_time_val_add() can be used.
  1038. --
  1039. -- cond :
  1040. -- a GCond.
  1041. --
  1042. -- mutex :
  1043. -- a GMutex that is currently locked.
  1044. --
  1045. -- abs_time :
  1046. -- a GTimeVal, determining the final time.
  1047. --
  1048. -- Returns :
  1049. -- TRUE if cond was signalled, or FALSE on timeout.
  1050. -- g_cond_free ()
  1051. --
  1052. -- void g_cond_free (GCond *cond);
  1053. --
  1054. -- Destroys the GCond.
  1055. --
  1056. -- cond :
  1057. -- a GCond.
  1058. -- GPrivate
  1059. --
  1060. -- typedef struct _GPrivate GPrivate;
  1061. --
  1062. -- The GPrivate struct is an opaque data structure to represent a thread private data key. Threads can thereby obtain and set a pointer which is private to the current thread. Take our give_me_next_number() example from above. Suppose we don't want current_number to be shared between the threads, but instead to be private to each thread. This can be done as follows:
  1063. --
  1064. -- Example 9. Using GPrivate for per-thread data
  1065. --
  1066. -- 1
  1067. -- 2
  1068. -- 3
  1069. -- 4
  1070. -- 5
  1071. -- 6
  1072. -- 7
  1073. -- 8
  1074. -- 9
  1075. -- 10
  1076. -- 11
  1077. -- 12
  1078. -- 13
  1079. -- 14
  1080. -- 15
  1081. -- 16
  1082. -- 17
  1083. -- 18
  1084. -- 19
  1085. --
  1086. --
  1087. --
  1088. -- GPrivate* current_number_key = NULL; /* Must be initialized somewhere
  1089. -- with g_private_new (g_free); */
  1090. --
  1091. -- int
  1092. -- give_me_next_number (void)
  1093. -- {
  1094. -- int *current_number = g_private_get (current_number_key);
  1095. --
  1096. -- if (!current_number)
  1097. -- {
  1098. -- current_number = g_new (int, 1);
  1099. -- *current_number = 0;
  1100. -- g_private_set (current_number_key, current_number);
  1101. -- }
  1102. --
  1103. -- *current_number = calc_next_number (*current_number);
  1104. --
  1105. -- return *current_number;
  1106. -- }
  1107. --
  1108. --
  1109. --
  1110. -- Here the pointer belonging to the key current_number_key is read. If it is NULL, it has not been set yet. Then get memory for an integer value, assign this memory to the pointer and write the pointer back. Now we have an integer value that is private to the current thread.
  1111. --
  1112. -- The GPrivate struct should only be accessed via the following functions.
  1113. --
  1114. -- Note
  1115. --
  1116. -- All of the g_private_* functions are actually macros. Apart from taking their addresses, you can however use them as if they were functions.
  1117. --
  1118. -- g_private_new ()
  1119. --
  1120. -- GPrivate* g_private_new (GDestroyNotify destructor);
  1121. --
  1122. -- Creates a new GPrivate. If destructor is non-NULL, it is a pointer to a destructor function. Whenever a thread ends and the corresponding pointer keyed to this instance of GPrivate is non-NULL, the destructor is called with this pointer as the argument.
  1123. --
  1124. -- Note
  1125. --
  1126. -- destructor is used quite differently from notify in g_static_private_set().
  1127. --
  1128. -- Note
  1129. --
  1130. -- A GPrivate can not be freed. Reuse it instead, if you can, to avoid shortage, or use GStaticPrivate.
  1131. --
  1132. -- Note
  1133. --
  1134. -- This function will abort if g_thread_init() has not been called yet.
  1135. --
  1136. -- destructor :
  1137. -- a function to destroy the data keyed to GPrivate when a thread ends.
  1138. --
  1139. -- Returns :
  1140. -- a new GPrivate.
  1141. -- g_private_get ()
  1142. --
  1143. -- gpointer g_private_get (GPrivate *private_key);
  1144. --
  1145. -- Returns the pointer keyed to private_key for the current thread. If g_private_set() hasn't been called for the current private_key and thread yet, this pointer will be NULL.
  1146. --
  1147. -- This function can be used even if g_thread_init() has not yet been called, and, in that case, will return the value of private_key casted to gpointer. Note however, that private data set before g_thread_init() will not be retained after the call. Instead, NULL will be returned in all threads directly after g_thread_init(), regardless of any g_private_set() calls issued before threading system intialization.
  1148. --
  1149. -- private_key :
  1150. -- a GPrivate.
  1151. --
  1152. -- Returns :
  1153. -- the corresponding pointer.
  1154. -- g_private_set ()
  1155. --
  1156. -- void g_private_set (GPrivate *private_key,
  1157. -- gpointer data);
  1158. --
  1159. -- Sets the pointer keyed to private_key for the current thread.
  1160. --
  1161. -- This function can be used even if g_thread_init() has not yet been called, and, in that case, will set private_key to data casted to GPrivate*. See g_private_get() for resulting caveats.
  1162. --
  1163. -- private_key :
  1164. -- a GPrivate.
  1165. --
  1166. -- data :
  1167. -- the new pointer.
  1168. -- struct GStaticPrivate
  1169. --
  1170. -- struct GStaticPrivate {
  1171. -- };
  1172. --
  1173. -- A GStaticPrivate works almost like a GPrivate, but it has one significant advantage. It doesn't need to be created at run-time like a GPrivate, but can be defined at compile-time. This is similar to the difference between GMutex and GStaticMutex. Now look at our give_me_next_number() example with GStaticPrivate:
  1174. --
  1175. -- Example 10. Using GStaticPrivate for per-thread data
  1176. --
  1177. -- 1
  1178. -- 2
  1179. -- 3
  1180. -- 4
  1181. -- 5
  1182. -- 6
  1183. -- 7
  1184. -- 8
  1185. -- 9
  1186. -- 10
  1187. -- 11
  1188. -- 12
  1189. -- 13
  1190. -- 14
  1191. -- 15
  1192. -- 16
  1193. -- 17
  1194. --
  1195. --
  1196. --
  1197. -- int
  1198. -- give_me_next_number ()
  1199. -- {
  1200. -- static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
  1201. -- int *current_number = g_static_private_get (&current_number_key);
  1202. --
  1203. -- if (!current_number)
  1204. -- {
  1205. -- current_number = g_new (int,1);
  1206. -- *current_number = 0;
  1207. -- g_static_private_set (&current_number_key, current_number, g_free);
  1208. -- }
  1209. --
  1210. -- *current_number = calc_next_number (*current_number);
  1211. --
  1212. -- return *current_number;
  1213. -- }
  1214. --
  1215. --
  1216. --
  1217. -- G_STATIC_PRIVATE_INIT
  1218. --
  1219. -- #define G_STATIC_PRIVATE_INIT
  1220. --
  1221. -- Every GStaticPrivate must be initialized with this macro, before it can be used.
  1222. --
  1223. -- 1
  1224. --
  1225. --
  1226. --
  1227. -- GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
  1228. --
  1229. -- g_static_private_init ()
  1230. --
  1231. -- void g_static_private_init (GStaticPrivate *private_key);
  1232. --
  1233. -- Initializes private_key. Alternatively you can initialize it with G_STATIC_PRIVATE_INIT.
  1234. --
  1235. -- private_key :
  1236. -- a GStaticPrivate to be initialized.
  1237. -- g_static_private_get ()
  1238. --
  1239. -- gpointer g_static_private_get (GStaticPrivate *private_key);
  1240. --
  1241. -- Works like g_private_get() only for a GStaticPrivate.
  1242. --
  1243. -- This function works even if g_thread_init() has not yet been called.
  1244. --
  1245. -- private_key :
  1246. -- a GStaticPrivate.
  1247. --
  1248. -- Returns :
  1249. -- the corresponding pointer.
  1250. -- g_static_private_set ()
  1251. --
  1252. -- void g_static_private_set (GStaticPrivate *private_key,
  1253. -- gpointer data,
  1254. -- GDestroyNotify notify);
  1255. --
  1256. -- Sets the pointer keyed to private_key for the current thread and the function notify to be called with that pointer (NULL or non-NULL), whenever the pointer is set again or whenever the current thread ends.
  1257. --
  1258. -- This function works even if g_thread_init() has not yet been called. If g_thread_init() is called later, the data keyed to private_key will be inherited only by the main thread, i.e. the one that called g_thread_init().
  1259. --
  1260. -- Note
  1261. --
  1262. -- notify is used quite differently from destructor in g_private_new().
  1263. --
  1264. -- private_key :
  1265. -- a GStaticPrivate.
  1266. --
  1267. -- data :
  1268. -- the new pointer.
  1269. --
  1270. -- notify :
  1271. -- a function to be called with the pointer whenever the current thread ends or sets this pointer again.
  1272. -- g_static_private_free ()
  1273. --
  1274. -- void g_static_private_free (GStaticPrivate *private_key);
  1275. --
  1276. -- Releases all resources allocated to private_key.
  1277. --
  1278. -- You don't have to call this functions for a GStaticPrivate with an unbounded lifetime, i.e. objects declared 'static', but if you have a GStaticPrivate as a member of a structure and the structure is freed, you should also free the GStaticPrivate.
  1279. --
  1280. -- private_key :
  1281. -- a GStaticPrivate to be freed.
  1282. -- struct GOnce
  1283. --
  1284. -- struct GOnce {
  1285. -- volatile GOnceStatus status;
  1286. -- volatile gpointer retval;
  1287. -- };
  1288. --
  1289. -- A GOnce struct controls a one-time initialization function. Any one-time initialization function must have its own unique GOnce struct.
  1290. --
  1291. -- volatile GOnceStatus status;
  1292. -- the status of the GOnce
  1293. --
  1294. -- volatile gpointer retval;
  1295. -- the value returned by the call to the function, if status is G_ONCE_STATUS_READY
  1296. --
  1297. -- Since 2.4
  1298. -- enum GOnceStatus
  1299. --
  1300. -- typedef enum
  1301. -- {
  1302. -- G_ONCE_STATUS_NOTCALLED,
  1303. -- G_ONCE_STATUS_PROGRESS,
  1304. -- G_ONCE_STATUS_READY
  1305. -- } GOnceStatus;
  1306. --
  1307. -- The possible statuses of a one-time initialization function controlled by a GOnce struct.
  1308. --
  1309. -- G_ONCE_STATUS_NOTCALLED
  1310. -- the function has not been called yet.
  1311. --
  1312. -- G_ONCE_STATUS_PROGRESS
  1313. -- the function call is currently in progress.
  1314. --
  1315. -- G_ONCE_STATUS_READY
  1316. -- the function has been called.
  1317. --
  1318. -- Since 2.4
  1319. -- G_ONCE_INIT
  1320. --
  1321. -- #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
  1322. --
  1323. -- A GOnce must be initialized with this macro before it can be used.
  1324. --
  1325. -- 1
  1326. --
  1327. --
  1328. --
  1329. -- GOnce my_once = G_ONCE_INIT;
  1330. --
  1331. -- Since 2.4
  1332. -- g_once()
  1333. --
  1334. -- #define g_once(once, func, arg)
  1335. --
  1336. -- The first call to this routine by a process with a given GOnce struct calls func with the given argument. Thereafter, subsequent calls to g_once() with the same GOnce struct do not call func again, but return the stored result of the first call. On return from g_once(), the status of once will be G_ONCE_STATUS_READY.
  1337. --
  1338. -- For example, a mutex or a thread-specific data key must be created exactly once. In a threaded environment, calling g_once() ensures that the initialization is serialized across multiple threads.
  1339. --
  1340. -- Note
  1341. --
  1342. -- Calling g_once() recursively on the same GOnce struct in func will lead to a deadlock.
  1343. --
  1344. -- 1
  1345. -- 2
  1346. -- 3
  1347. -- 4
  1348. -- 5
  1349. -- 6
  1350. -- 7
  1351. -- 8
  1352. -- 9
  1353. --
  1354. --
  1355. --
  1356. -- gpointer
  1357. -- get_debug_flags (void)
  1358. -- {
  1359. -- static GOnce my_once = G_ONCE_INIT;
  1360. --
  1361. -- g_once (&my_once, parse_debug_flags, NULL);
  1362. --
  1363. -- return my_once.retval;
  1364. -- }
  1365. --
  1366. -- once :
  1367. -- a GOnce structure
  1368. --
  1369. -- func :
  1370. -- the GThreadFunc function associated to once. This function is called only once, regardless of the number of times it and its associated GOnce struct are passed to g_once().
  1371. --
  1372. -- arg :
  1373. -- data to be passed to func
  1374. --
  1375. -- Since 2.4
  1376. -- g_once_init_enter ()
  1377. --
  1378. -- gboolean g_once_init_enter (volatile gsize *value_location);
  1379. --
  1380. -- Function to be called when starting a critical initialization section. The argument value_location must point to a static 0-initialized variable that will be set to a value other than 0 at the end of the initialization section. In combination with g_once_init_leave() and the unique address value_location, it can be ensured that an initialization section will be executed only once during a program's life time, and that concurrent threads are blocked until initialization completed. To be used in constructs like this:
  1381. --
  1382. -- 1
  1383. -- 2
  1384. -- 3
  1385. -- 4
  1386. -- 5
  1387. -- 6
  1388. -- 7
  1389. -- 8
  1390. -- 9
  1391. -- 10
  1392. --
  1393. --
  1394. --
  1395. -- static gsize initialization_value = 0;
  1396. --
  1397. -- if (g_once_init_enter (&initialization_value))
  1398. -- {
  1399. -- gsize setup_value = 42; /* initialization code here */
  1400. --
  1401. -- g_once_init_leave (&initialization_value, setup_value);
  1402. -- }
  1403. --
  1404. -- /* use initialization_value here */
  1405. --
  1406. -- value_location :
  1407. -- location of a static initializable variable containing 0.
  1408. --
  1409. -- Returns :
  1410. -- TRUE if the initialization section should be entered, FALSE and blocks otherwise
  1411. --
  1412. -- Since 2.14
  1413. -- g_once_init_leave ()
  1414. --
  1415. -- void g_once_init_leave (volatile gsize *value_location,
  1416. -- gsize initialization_value);
  1417. --
  1418. -- Counterpart to g_once_init_enter(). Expects a location of a static 0-initialized initialization variable, and an initialization value other than 0. Sets the variable to the initialization value, and releases concurrent threads blocking in g_once_init_enter() on this initialization variable.
  1419. --
  1420. -- value_location :
  1421. -- location of a static initializable variable containing 0.
  1422. --
  1423. -- initialization_value :
  1424. -- new non-0 value for *value_location.
  1425. --
  1426. -- Since 2.14
  1427. -- g_bit_lock ()
  1428. --
  1429. -- void g_bit_lock (volatile gint *address,
  1430. -- gint lock_bit);
  1431. --
  1432. -- Sets the indicated lock_bit in address. If the bit is already set, this call will block until g_bit_unlock() unsets the corresponding bit.
  1433. --
  1434. -- Attempting to lock on two different bits within the same integer is not supported and will very probably cause deadlocks.
  1435. --
  1436. -- The value of the bit that is set is (1u << bit). If bit is not between 0 and 31 then the result is undefined.
  1437. --
  1438. -- This function accesses address atomically. All other accesses to address must be atomic in order for this function to work reliably.
  1439. --
  1440. -- address :
  1441. -- a pointer to an integer
  1442. --
  1443. -- lock_bit :
  1444. -- a bit value between 0 and 31
  1445. --
  1446. -- Since 2.24
  1447. -- g_bit_trylock ()
  1448. --
  1449. -- gboolean g_bit_trylock (volatile gint *address,
  1450. -- gint lock_bit);
  1451. --
  1452. -- Sets the indicated lock_bit in address, returning TRUE if successful. If the bit is already set, returns FALSE immediately.
  1453. --
  1454. -- Attempting to lock on two different bits within the same integer is not supported.
  1455. --
  1456. -- The value of the bit that is set is (1u << bit). If bit is not between 0 and 31 then the result is undefined.
  1457. --
  1458. -- This function accesses address atomically. All other accesses to address must be atomic in order for this function to work reliably.
  1459. --
  1460. -- address :
  1461. -- a pointer to an integer
  1462. --
  1463. -- lock_bit :
  1464. -- a bit value between 0 and 31
  1465. --
  1466. -- Returns :
  1467. -- TRUE if the lock was acquired
  1468. --
  1469. -- Since 2.24
  1470. -- g_bit_unlock ()
  1471. --
  1472. -- void g_bit_unlock (volatile gint *address,
  1473. -- gint lock_bit);
  1474. --
  1475. -- Clears the indicated lock_bit in address. If another thread is currently blocked in g_bit_lock() on this same bit then it will be woken up.
  1476. --
  1477. -- This function accesses address atomically. All other accesses to address must be atomic in order for this function to work reliably.
  1478. --
  1479. -- address :
  1480. -- a pointer to an integer
  1481. --
  1482. -- lock_bit :
  1483. -- a bit value between 0 and 31
  1484. end -- class G_THREAD