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

http://github.com/tybor/Liberty · Specman e · 221 lines · 91 code · 35 blank · 95 comment · 0 complexity · 06fb47b6274ee43b6f99178cf8e50c41 MD5 · raw file

  1. indexing
  2. description: "Thread Pools — pools of threads to execute work concurrently"
  3. copyright: "(C) 2011 Paolo Redaelli"
  4. license: "LGPL v2 or later"
  5. version: "2.28"
  6. deferred class G_THREAD_POOL [A_ROUTINE -> G_POOLED_ROUTINE]
  7. obsolete "G_THREAD_POOL is currently unusable because standard library is NOT thread safe. At all"
  8. -- A pool of threads executing A_ROUTINE concurrently.
  9. -- Sometimes you wish to asynchronously fork out the execution of work and
  10. -- continue working in your own thread. If that will happen often, the
  11. -- overhead of starting and destroying a thread each time might be too
  12. -- high. In such cases reusing already started threads seems like a good
  13. -- idea. And it indeed is, but implementing this can be tedious and
  14. -- error-prone.
  15. -- Therefore GLib provides thread pools for your convenience. An added
  16. -- advantage is, that the threads can be shared between the different
  17. -- subsystems of your program, when they are using GLib.
  18. -- To create a new thread pool, you use `make'.
  19. -- If you want to execute a certain task within a thread pool, you call `push'.
  20. -- To get the current number of running threads you call `threads_count'.
  21. -- To get the number of still unprocessed tasks you call
  22. -- `unprocessed_count'. To control the maximal number of threads for a
  23. -- thread pool, you use `max_threads' and `set_max_threads`.
  24. -- Finally you can control the number of unused threads, that are kept
  25. -- alive by GLib for future use. The current number can be fetched with
  26. -- `unused_threads_count'. The maximal number can be controlled by
  27. -- `max_unused_threads' and `set_max_unused_threads'. All currently unused
  28. -- threads can be stopped by calling `stop_unused_threads`.
  29. -- Note for Liberty users:
  30. inherit
  31. WRAPPER
  32. -- A thread pool should be "EIFFEL_OWNED redefine dispose end", but since EIFFEL_OWNED contains only a simplicistic definition of `dispose' it is actully cleaner just to define dispose here.
  33. FACTORY[A_ROUTINE] rename new as new_routine end
  34. insert
  35. GTHREADPOOL_EXTERNALS
  36. GTHREAD_EXTERNALS
  37. feature {} -- Creation
  38. make (a_max_threads_count: INTEGER; is_exclusive: BOOLEAN;) is
  39. -- Creation command of a new thread pool.
  40. -- Whenever you call `push', either a new thread is created or an
  41. -- unused one is reused . At most a_max_threads_count threads are
  42. -- running concurrently for this thread pool. `a_max_threads_count' = -1 allows
  43. -- unlimited threads to be created for this thread pool.
  44. -- The newly created or reused thread now executes A_ROUTINE.command with a reference to Current G_THREAD_POOL.
  45. -- `is_exclusive' determines, whether the thread pool owns all threads
  46. -- exclusive or whether the threads are shared globally. If
  47. -- `is_exclusive' is True, `a_max_threads_count' threads are started immediately
  48. -- and they will run exclusively for this thread pool until it is
  49. -- destroyed by dispose. If `is_exclusive' is False, threads
  50. -- are created, when needed and shared between all non-exclusive thread
  51. -- pools. This implies that `a_max_threads_count' may not be -1 for exclusive
  52. -- thread pools.
  53. -- TODO: provide GError support ("..error can be NULL to ignore errors, or non-NULL to report errors. An error can only occur when exclusive is set to TRUE and not all max_threads threads could be created..")
  54. local self: like Current; a_routine: like new_routine
  55. do
  56. g_thread_init(default_pointer)
  57. self := Current
  58. a_routine := new_routine
  59. from_external_pointer(g_thread_pool_new(a_routine.invoke_address, $self, a_max_threads_count, is_exclusive.to_integer,
  60. default_pointer -- could be GError **error);
  61. ))
  62. end
  63. feature -- Commands
  64. push (some_data: like new_routine) is
  65. -- Inserts data into the list of tasks to be executed by pool. When the
  66. -- number of currently running threads is lower than the maximal
  67. -- allowed number of threads, a new thread is started (or reused) with
  68. -- the properties given to `make'. Otherwise data stays in
  69. -- the queue until a thread in this pool finishes its previous task and
  70. -- processes data.
  71. -- TODO: add GError support "error can be NULL to ignore errors, or non-NULL to report errors. An
  72. -- error can only occur when a new thread couldn't be created. In that
  73. -- case data is simply appended to the queue of work to do."
  74. local a_data: like new_routine
  75. do
  76. a_data := some_data -- this step is needed to circumvent a limitation of SmartEiffel $ operator
  77. g_thread_pool_push
  78. (handle, $a_data,
  79. default_pointer -- could be GError **error);
  80. )
  81. end
  82. set_max_threads (a_number: INTEGER) is
  83. -- Sets the maximal allowed number of threads for pool. A value of -1 means, that the maximal number of threads is unlimited.
  84. -- Setting max_threads to 0 means stopping all work for pool. It is effectively frozen until max_threads is set to a non-zero value again.
  85. --
  86. -- A thread is never terminated while calling func, as supplied by g_thread_pool_new(). Instead the maximal number of threads only has effect for the allocation of new threads in g_thread_pool_push(). A new thread is allocated, whenever the number of currently running threads in pool is smaller than the maximal number.
  87. --
  88. -- error can be NULL to ignore errors, or non-NULL to report errors. An error can only occur when a new thread couldn't be created.
  89. --
  90. do
  91. g_thread_pool_set_max_threads (handle, a_number, default_pointer -- could be GError **error);
  92. )
  93. end
  94. feature -- Disposing
  95. dispose is
  96. --Frees all resources allocated for pool.
  97. -- If immediate is TRUE, no new task is processed for pool. Otherwise pool is not freed before the last task is processed. Note however, that no thread of this pool is interrupted, while processing a task. Instead at least all still running threads can finish their tasks before the pool is freed.
  98. -- If wait_ is TRUE, the functions does not return before all tasks to be processed (dependent on immediate, whether all or only the currently running) are ready. Otherwise the function returns immediately.
  99. -- After calling this function pool must not be used anymore.
  100. do
  101. g_thread_pool_free (handle, 0 -- gboolean immediate
  102. , 1 -- gboolean wait_
  103. )
  104. -- immediate : should pool shut down immediately?
  105. -- wait_ : should the function wait for all tasks to be finished?
  106. end
  107. set_max_unused_threads (a_maximum: INTEGER_32) is
  108. -- Sets the maximal number of unused threads to `a_maximum'. If it is
  109. -- -1, no limit is imposed on the number of unused threads.
  110. do
  111. g_thread_pool_set_max_unused_threads(a_maximum)
  112. end
  113. stop_unused_threads is
  114. -- Stops all currently unused threads. This does not change the maximal
  115. -- number of unused threads. This function can be used to regularly
  116. -- stop all unused threads e.g. from g_timeout_add().
  117. do
  118. g_thread_pool_stop_unused_threads
  119. end
  120. set_max_idle_time (an_interval: NATURAL_32) is
  121. -- Set to `an_interval' the maximum interval (in 1/1000ths of a second)
  122. -- that a thread waiting in the pool for new tasks can be idle for
  123. -- before being stopped. This command is similar to invoking
  124. -- `stop_unused_threads' on a regular timeout, except, this is done on
  125. -- a per thread basis.
  126. -- By setting interval to 0, idle threads will not be stopped.
  127. -- (Low-level note: this function makes use of
  128. -- g_async_queue_timed_pop() using interval).
  129. do
  130. g_thread_pool_set_max_idle_time (an_interval)
  131. end
  132. feature -- Queries
  133. exit_immidiatly: BOOLEAN is
  134. --
  135. attribute
  136. end
  137. does_exit_on_dispose: BOOLEAN is
  138. --
  139. attribute
  140. end
  141. max_threads: INTEGER is
  142. -- The maximum number of threads in Current pool
  143. do
  144. Result:=g_thread_pool_get_max_threads (handle)
  145. end
  146. threads_count: NATURAL_32 is
  147. -- The number of threads currently running in the pool
  148. do
  149. Result := g_thread_pool_get_num_threads (handle)
  150. end
  151. unprocessed_count: NATURAL_32 is
  152. -- The number of tasks still unprocessed in the pool
  153. do
  154. Result := g_thread_pool_unprocessed (handle)
  155. end
  156. max_unused_threads: INTEGER is
  157. -- The maximal allowed number of unused threads
  158. do
  159. Result:=g_thread_pool_get_max_unused_threads
  160. end
  161. unused_threads_count: NATURAL_32 is
  162. -- the number of currently unused threads.
  163. do
  164. Result := g_thread_pool_get_num_unused_threads
  165. end
  166. max_idle_time: NATURAL_32 is
  167. -- the maximum interval to wait for new tasks in the thread pool before stopping the thread (1/1000ths of a second).
  168. -- When 0, threads waiting in the thread pool for new work are not stopped.
  169. do
  170. Result := g_thread_pool_get_max_idle_time
  171. end
  172. feature {} -- Unwrapped
  173. -- void g_thread_pool_set_sort_function (GThreadPool *pool,
  174. -- GCompareDataFunc func,
  175. -- gpointer user_data);
  176. --
  177. -- Sets the function used to sort the list of tasks. This allows the tasks to be processed by a priority determined by func, and not just in the order in which they were added to the pool.
  178. --
  179. -- Note, if the maximum number of threads is more than 1, the order that threads are executed can not be guranteed 100%. Threads are scheduled by the operating system and are executed at random. It cannot be assumed that threads are executed in the order they are created.
  180. --
  181. -- pool :
  182. -- a GThreadPool
  183. --
  184. -- func :
  185. -- the GCompareDataFunc used to sort the list of tasks. This function is passed two tasks. It should return 0 if the order in which they are handled does not matter, a negative value if the first task should be processed before the second or a positive value if the second task should be processed first.
  186. end -- class G_THREAD_POOL