/src/wrappers/glib/library/core/g_thread_pool.e
Specman e | 221 lines | 91 code | 35 blank | 95 comment | 0 complexity | 06fb47b6274ee43b6f99178cf8e50c41 MD5 | raw file
1indexing 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 7deferred class G_THREAD_POOL [A_ROUTINE -> G_POOLED_ROUTINE] 8 obsolete "G_THREAD_POOL is currently unusable because standard library is NOT thread safe. At all" 9 -- A pool of threads executing A_ROUTINE concurrently. 10 11 -- Sometimes you wish to asynchronously fork out the execution of work and 12 -- continue working in your own thread. If that will happen often, the 13 -- overhead of starting and destroying a thread each time might be too 14 -- high. In such cases reusing already started threads seems like a good 15 -- idea. And it indeed is, but implementing this can be tedious and 16 -- error-prone. 17 18 -- Therefore GLib provides thread pools for your convenience. An added 19 -- advantage is, that the threads can be shared between the different 20 -- subsystems of your program, when they are using GLib. 21 22 -- To create a new thread pool, you use `make'. 23 24 -- If you want to execute a certain task within a thread pool, you call `push'. 25 26 -- To get the current number of running threads you call `threads_count'. 27 -- To get the number of still unprocessed tasks you call 28 -- `unprocessed_count'. To control the maximal number of threads for a 29 -- thread pool, you use `max_threads' and `set_max_threads`. 30 31 -- Finally you can control the number of unused threads, that are kept 32 -- alive by GLib for future use. The current number can be fetched with 33 -- `unused_threads_count'. The maximal number can be controlled by 34 -- `max_unused_threads' and `set_max_unused_threads'. All currently unused 35 -- threads can be stopped by calling `stop_unused_threads`. 36 37 -- Note for Liberty users: 38inherit 39 WRAPPER 40 -- 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. 41 42 FACTORY[A_ROUTINE] rename new as new_routine end 43 44insert 45 GTHREADPOOL_EXTERNALS 46 GTHREAD_EXTERNALS 47 48feature {} -- Creation 49 make (a_max_threads_count: INTEGER; is_exclusive: BOOLEAN;) is 50 -- Creation command of a new thread pool. 51 52 -- Whenever you call `push', either a new thread is created or an 53 -- unused one is reused . At most a_max_threads_count threads are 54 -- running concurrently for this thread pool. `a_max_threads_count' = -1 allows 55 -- unlimited threads to be created for this thread pool. 56 57 -- The newly created or reused thread now executes A_ROUTINE.command with a reference to Current G_THREAD_POOL. 58 59 -- `is_exclusive' determines, whether the thread pool owns all threads 60 -- exclusive or whether the threads are shared globally. If 61 -- `is_exclusive' is True, `a_max_threads_count' threads are started immediately 62 -- and they will run exclusively for this thread pool until it is 63 -- destroyed by dispose. If `is_exclusive' is False, threads 64 -- are created, when needed and shared between all non-exclusive thread 65 -- pools. This implies that `a_max_threads_count' may not be -1 for exclusive 66 -- thread pools. 67 68 -- 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..") 69 local self: like Current; a_routine: like new_routine 70 do 71 g_thread_init(default_pointer) 72 self := Current 73 a_routine := new_routine 74 from_external_pointer(g_thread_pool_new(a_routine.invoke_address, $self, a_max_threads_count, is_exclusive.to_integer, 75 default_pointer -- could be GError **error); 76 )) 77 end 78 79feature -- Commands 80 push (some_data: like new_routine) is 81 -- Inserts data into the list of tasks to be executed by pool. When the 82 -- number of currently running threads is lower than the maximal 83 -- allowed number of threads, a new thread is started (or reused) with 84 -- the properties given to `make'. Otherwise data stays in 85 -- the queue until a thread in this pool finishes its previous task and 86 -- processes data. 87 88 -- TODO: add GError support "error can be NULL to ignore errors, or non-NULL to report errors. An 89 -- error can only occur when a new thread couldn't be created. In that 90 -- case data is simply appended to the queue of work to do." 91 local a_data: like new_routine 92 do 93 a_data := some_data -- this step is needed to circumvent a limitation of SmartEiffel $ operator 94 g_thread_pool_push 95 (handle, $a_data, 96 default_pointer -- could be GError **error); 97 ) 98 end 99 100 set_max_threads (a_number: INTEGER) is 101 -- Sets the maximal allowed number of threads for pool. A value of -1 means, that the maximal number of threads is unlimited. 102 103 -- 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. 104 -- 105 -- 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. 106 -- 107 -- 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. 108 -- 109 do 110 g_thread_pool_set_max_threads (handle, a_number, default_pointer -- could be GError **error); 111 ) 112 end 113 114feature -- Disposing 115 dispose is 116 --Frees all resources allocated for pool. 117 118 -- 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. 119 120 -- 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. 121 122 -- After calling this function pool must not be used anymore. 123 do 124 g_thread_pool_free (handle, 0 -- gboolean immediate 125 , 1 -- gboolean wait_ 126 ) 127 -- immediate : should pool shut down immediately? 128 -- wait_ : should the function wait for all tasks to be finished? 129 end 130 131 set_max_unused_threads (a_maximum: INTEGER_32) is 132 -- Sets the maximal number of unused threads to `a_maximum'. If it is 133 -- -1, no limit is imposed on the number of unused threads. 134 do 135 g_thread_pool_set_max_unused_threads(a_maximum) 136 end 137 138 stop_unused_threads is 139 -- Stops all currently unused threads. This does not change the maximal 140 -- number of unused threads. This function can be used to regularly 141 -- stop all unused threads e.g. from g_timeout_add(). 142 do 143 g_thread_pool_stop_unused_threads 144 end 145 146 set_max_idle_time (an_interval: NATURAL_32) is 147 -- Set to `an_interval' the maximum interval (in 1/1000ths of a second) 148 -- that a thread waiting in the pool for new tasks can be idle for 149 -- before being stopped. This command is similar to invoking 150 -- `stop_unused_threads' on a regular timeout, except, this is done on 151 -- a per thread basis. 152 153 -- By setting interval to 0, idle threads will not be stopped. 154 -- (Low-level note: this function makes use of 155 -- g_async_queue_timed_pop() using interval). 156 do 157 g_thread_pool_set_max_idle_time (an_interval) 158 end 159 160feature -- Queries 161 exit_immidiatly: BOOLEAN is 162 -- 163 attribute 164 end 165 166 does_exit_on_dispose: BOOLEAN is 167 -- 168 attribute 169 end 170 171 max_threads: INTEGER is 172 -- The maximum number of threads in Current pool 173 do 174 Result:=g_thread_pool_get_max_threads (handle) 175 end 176 177 threads_count: NATURAL_32 is 178 -- The number of threads currently running in the pool 179 do 180 Result := g_thread_pool_get_num_threads (handle) 181 end 182 183 unprocessed_count: NATURAL_32 is 184 -- The number of tasks still unprocessed in the pool 185 do 186 Result := g_thread_pool_unprocessed (handle) 187 end 188 189 max_unused_threads: INTEGER is 190 -- The maximal allowed number of unused threads 191 do 192 Result:=g_thread_pool_get_max_unused_threads 193 end 194 195 unused_threads_count: NATURAL_32 is 196 -- the number of currently unused threads. 197 do 198 Result := g_thread_pool_get_num_unused_threads 199 end 200 201 max_idle_time: NATURAL_32 is 202 -- the maximum interval to wait for new tasks in the thread pool before stopping the thread (1/1000ths of a second). 203 -- When 0, threads waiting in the thread pool for new work are not stopped. 204 do 205 Result := g_thread_pool_get_max_idle_time 206 end 207feature {} -- Unwrapped 208-- void g_thread_pool_set_sort_function (GThreadPool *pool, 209-- GCompareDataFunc func, 210-- gpointer user_data); 211-- 212-- 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. 213-- 214-- 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. 215-- 216-- pool : 217-- a GThreadPool 218-- 219-- func : 220-- 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. 221end -- class G_THREAD_POOL