1 | /* |
1 | /* |
2 | * libetp implementation |
2 | * libetp implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann <libetp@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013,2015 Marc Alexander Lehmann <libetp@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
35 | * and other provisions required by the GPL. If you do not delete the |
35 | * and other provisions required by the GPL. If you do not delete the |
36 | * provisions above, a recipient may use your version of this file under |
36 | * provisions above, a recipient may use your version of this file under |
37 | * either the BSD or the GPL. |
37 | * either the BSD or the GPL. |
38 | */ |
38 | */ |
39 | |
39 | |
|
|
40 | #if HAVE_SYS_PRCTL_H |
|
|
41 | # include <sys/prctl.h> |
|
|
42 | #endif |
|
|
43 | |
40 | #ifndef ETP_API_DECL |
44 | #ifndef ETP_API_DECL |
41 | # define ETP_API_DECL static |
45 | # define ETP_API_DECL static |
42 | #endif |
46 | #endif |
43 | |
47 | |
44 | #ifndef ETP_PRI_MIN |
48 | #ifndef ETP_PRI_MIN |
… | |
… | |
50 | # define ETP_TYPE_QUIT 0 |
54 | # define ETP_TYPE_QUIT 0 |
51 | #endif |
55 | #endif |
52 | |
56 | |
53 | #ifndef ETP_TYPE_GROUP |
57 | #ifndef ETP_TYPE_GROUP |
54 | # define ETP_TYPE_GROUP 1 |
58 | # define ETP_TYPE_GROUP 1 |
|
|
59 | #endif |
|
|
60 | |
|
|
61 | #ifndef ETP_WANT_POLL |
|
|
62 | # define ETP_WANT_POLL(pool) pool->want_poll_cb (pool->userdata) |
|
|
63 | #endif |
|
|
64 | #ifndef ETP_DONE_POLL |
|
|
65 | # define ETP_DONE_POLL(pool) pool->done_poll_cb (pool->userdata) |
55 | #endif |
66 | #endif |
56 | |
67 | |
57 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
68 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
58 | |
69 | |
59 | #define ETP_TICKS ((1000000 + 1023) >> 10) |
70 | #define ETP_TICKS ((1000000 + 1023) >> 10) |
… | |
… | |
69 | { |
80 | { |
70 | return (tv2->tv_sec - tv1->tv_sec ) * ETP_TICKS |
81 | return (tv2->tv_sec - tv1->tv_sec ) * ETP_TICKS |
71 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
82 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
72 | } |
83 | } |
73 | |
84 | |
74 | static unsigned int started, idle, wanted = 4; |
|
|
75 | |
|
|
76 | static void (*want_poll_cb) (void); |
|
|
77 | static void (*done_poll_cb) (void); |
|
|
78 | |
|
|
79 | static unsigned int max_poll_time; /* reslock */ |
|
|
80 | static unsigned int max_poll_reqs; /* reslock */ |
|
|
81 | |
|
|
82 | static unsigned int nreqs; /* reqlock */ |
|
|
83 | static unsigned int nready; /* reqlock */ |
|
|
84 | static unsigned int npending; /* reqlock */ |
|
|
85 | static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ |
|
|
86 | static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ |
|
|
87 | |
|
|
88 | static xmutex_t wrklock; |
|
|
89 | static xmutex_t reslock; |
|
|
90 | static xmutex_t reqlock; |
|
|
91 | static xcond_t reqwait; |
|
|
92 | |
|
|
93 | struct etp_tmpbuf |
85 | struct etp_tmpbuf |
94 | { |
86 | { |
95 | void *ptr; |
87 | void *ptr; |
96 | int len; |
88 | int len; |
97 | }; |
89 | }; |
… | |
… | |
104 | free (buf->ptr); |
96 | free (buf->ptr); |
105 | buf->ptr = malloc (buf->len = len); |
97 | buf->ptr = malloc (buf->len = len); |
106 | } |
98 | } |
107 | |
99 | |
108 | return buf->ptr; |
100 | return buf->ptr; |
109 | } |
|
|
110 | |
|
|
111 | typedef struct etp_worker |
|
|
112 | { |
|
|
113 | struct etp_tmpbuf tmpbuf; |
|
|
114 | |
|
|
115 | /* locked by wrklock */ |
|
|
116 | struct etp_worker *prev, *next; |
|
|
117 | |
|
|
118 | xthread_t tid; |
|
|
119 | |
|
|
120 | #ifdef ETP_WORKER_COMMON |
|
|
121 | ETP_WORKER_COMMON |
|
|
122 | #endif |
|
|
123 | } etp_worker; |
|
|
124 | |
|
|
125 | static etp_worker wrk_first; /* NOT etp */ |
|
|
126 | |
|
|
127 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
|
|
128 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
|
|
129 | |
|
|
130 | /* worker threads management */ |
|
|
131 | |
|
|
132 | static void |
|
|
133 | etp_worker_clear (etp_worker *wrk) |
|
|
134 | { |
|
|
135 | } |
|
|
136 | |
|
|
137 | static void ecb_cold |
|
|
138 | etp_worker_free (etp_worker *wrk) |
|
|
139 | { |
|
|
140 | free (wrk->tmpbuf.ptr); |
|
|
141 | |
|
|
142 | wrk->next->prev = wrk->prev; |
|
|
143 | wrk->prev->next = wrk->next; |
|
|
144 | |
|
|
145 | free (wrk); |
|
|
146 | } |
|
|
147 | |
|
|
148 | ETP_API_DECL unsigned int |
|
|
149 | etp_nreqs (void) |
|
|
150 | { |
|
|
151 | int retval; |
|
|
152 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
153 | retval = nreqs; |
|
|
154 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
|
|
155 | return retval; |
|
|
156 | } |
|
|
157 | |
|
|
158 | ETP_API_DECL unsigned int |
|
|
159 | etp_nready (void) |
|
|
160 | { |
|
|
161 | unsigned int retval; |
|
|
162 | |
|
|
163 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
164 | retval = nready; |
|
|
165 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
|
|
166 | |
|
|
167 | return retval; |
|
|
168 | } |
|
|
169 | |
|
|
170 | ETP_API_DECL unsigned int |
|
|
171 | etp_npending (void) |
|
|
172 | { |
|
|
173 | unsigned int retval; |
|
|
174 | |
|
|
175 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
176 | retval = npending; |
|
|
177 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
|
|
178 | |
|
|
179 | return retval; |
|
|
180 | } |
|
|
181 | |
|
|
182 | ETP_API_DECL unsigned int |
|
|
183 | etp_nthreads (void) |
|
|
184 | { |
|
|
185 | unsigned int retval; |
|
|
186 | |
|
|
187 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
188 | retval = started; |
|
|
189 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
|
|
190 | |
|
|
191 | return retval; |
|
|
192 | } |
101 | } |
193 | |
102 | |
194 | /* |
103 | /* |
195 | * a somewhat faster data structure might be nice, but |
104 | * a somewhat faster data structure might be nice, but |
196 | * with 8 priorities this actually needs <20 insns |
105 | * with 8 priorities this actually needs <20 insns |
197 | * per shift, the most expensive operation. |
106 | * per shift, the most expensive operation. |
198 | */ |
107 | */ |
199 | typedef struct { |
108 | typedef struct |
|
|
109 | { |
200 | ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */ |
110 | ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */ |
201 | int size; |
111 | int size; |
202 | } etp_reqq; |
112 | } etp_reqq; |
203 | |
113 | |
|
|
114 | typedef struct etp_pool *etp_pool; |
|
|
115 | |
|
|
116 | typedef struct etp_worker |
|
|
117 | { |
|
|
118 | etp_pool pool; |
|
|
119 | |
|
|
120 | struct etp_tmpbuf tmpbuf; |
|
|
121 | |
|
|
122 | /* locked by pool->wrklock */ |
|
|
123 | struct etp_worker *prev, *next; |
|
|
124 | |
|
|
125 | xthread_t tid; |
|
|
126 | |
|
|
127 | #ifdef ETP_WORKER_COMMON |
|
|
128 | ETP_WORKER_COMMON |
|
|
129 | #endif |
|
|
130 | } etp_worker; |
|
|
131 | |
|
|
132 | struct etp_pool |
|
|
133 | { |
|
|
134 | void *userdata; |
|
|
135 | |
204 | static etp_reqq req_queue; |
136 | etp_reqq req_queue; |
205 | static etp_reqq res_queue; |
137 | etp_reqq res_queue; |
|
|
138 | |
|
|
139 | unsigned int started, idle, wanted; |
|
|
140 | |
|
|
141 | unsigned int max_poll_time; /* pool->reslock */ |
|
|
142 | unsigned int max_poll_reqs; /* pool->reslock */ |
|
|
143 | |
|
|
144 | unsigned int nreqs; /* pool->reqlock */ |
|
|
145 | unsigned int nready; /* pool->reqlock */ |
|
|
146 | unsigned int npending; /* pool->reqlock */ |
|
|
147 | unsigned int max_idle; /* maximum number of threads that can pool->idle indefinitely */ |
|
|
148 | unsigned int idle_timeout; /* number of seconds after which an pool->idle threads exit */ |
|
|
149 | |
|
|
150 | void (*want_poll_cb) (void *userdata); |
|
|
151 | void (*done_poll_cb) (void *userdata); |
|
|
152 | |
|
|
153 | xmutex_t wrklock; |
|
|
154 | xmutex_t reslock; |
|
|
155 | xmutex_t reqlock; |
|
|
156 | xcond_t reqwait; |
|
|
157 | |
|
|
158 | etp_worker wrk_first; |
|
|
159 | }; |
|
|
160 | |
|
|
161 | #define ETP_WORKER_LOCK(wrk) X_LOCK (pool->wrklock) |
|
|
162 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (pool->wrklock) |
|
|
163 | |
|
|
164 | /* worker threads management */ |
|
|
165 | |
|
|
166 | static void |
|
|
167 | etp_worker_clear (etp_worker *wrk) |
|
|
168 | { |
|
|
169 | } |
|
|
170 | |
|
|
171 | static void ecb_cold |
|
|
172 | etp_worker_free (etp_worker *wrk) |
|
|
173 | { |
|
|
174 | free (wrk->tmpbuf.ptr); |
|
|
175 | |
|
|
176 | wrk->next->prev = wrk->prev; |
|
|
177 | wrk->prev->next = wrk->next; |
|
|
178 | |
|
|
179 | free (wrk); |
|
|
180 | } |
|
|
181 | |
|
|
182 | ETP_API_DECL unsigned int |
|
|
183 | etp_nreqs (etp_pool pool) |
|
|
184 | { |
|
|
185 | int retval; |
|
|
186 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reqlock); |
|
|
187 | retval = pool->nreqs; |
|
|
188 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
|
|
189 | return retval; |
|
|
190 | } |
|
|
191 | |
|
|
192 | ETP_API_DECL unsigned int |
|
|
193 | etp_nready (etp_pool pool) |
|
|
194 | { |
|
|
195 | unsigned int retval; |
|
|
196 | |
|
|
197 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reqlock); |
|
|
198 | retval = pool->nready; |
|
|
199 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
|
|
200 | |
|
|
201 | return retval; |
|
|
202 | } |
|
|
203 | |
|
|
204 | ETP_API_DECL unsigned int |
|
|
205 | etp_npending (etp_pool pool) |
|
|
206 | { |
|
|
207 | unsigned int retval; |
|
|
208 | |
|
|
209 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reqlock); |
|
|
210 | retval = pool->npending; |
|
|
211 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
|
|
212 | |
|
|
213 | return retval; |
|
|
214 | } |
|
|
215 | |
|
|
216 | ETP_API_DECL unsigned int |
|
|
217 | etp_nthreads (etp_pool pool) |
|
|
218 | { |
|
|
219 | unsigned int retval; |
|
|
220 | |
|
|
221 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reqlock); |
|
|
222 | retval = pool->started; |
|
|
223 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
|
|
224 | |
|
|
225 | return retval; |
|
|
226 | } |
206 | |
227 | |
207 | static void ecb_noinline ecb_cold |
228 | static void ecb_noinline ecb_cold |
208 | reqq_init (etp_reqq *q) |
229 | reqq_init (etp_reqq *q) |
209 | { |
230 | { |
210 | int pri; |
231 | int pri; |
… | |
… | |
257 | |
278 | |
258 | abort (); |
279 | abort (); |
259 | } |
280 | } |
260 | |
281 | |
261 | ETP_API_DECL int ecb_cold |
282 | ETP_API_DECL int ecb_cold |
262 | etp_init (void (*want_poll)(void), void (*done_poll)(void)) |
283 | etp_init (etp_pool pool, void *userdata, void (*want_poll)(void *userdata), void (*done_poll)(void *userdata)) |
263 | { |
284 | { |
264 | X_MUTEX_CREATE (wrklock); |
285 | X_MUTEX_CREATE (pool->wrklock); |
265 | X_MUTEX_CREATE (reslock); |
286 | X_MUTEX_CREATE (pool->reslock); |
266 | X_MUTEX_CREATE (reqlock); |
287 | X_MUTEX_CREATE (pool->reqlock); |
267 | X_COND_CREATE (reqwait); |
288 | X_COND_CREATE (pool->reqwait); |
268 | |
289 | |
269 | reqq_init (&req_queue); |
290 | reqq_init (&pool->req_queue); |
270 | reqq_init (&res_queue); |
291 | reqq_init (&pool->res_queue); |
271 | |
292 | |
272 | wrk_first.next = |
293 | pool->wrk_first.next = |
273 | wrk_first.prev = &wrk_first; |
294 | pool->wrk_first.prev = &pool->wrk_first; |
274 | |
295 | |
275 | started = 0; |
296 | pool->started = 0; |
276 | idle = 0; |
297 | pool->idle = 0; |
277 | nreqs = 0; |
298 | pool->nreqs = 0; |
278 | nready = 0; |
299 | pool->nready = 0; |
279 | npending = 0; |
300 | pool->npending = 0; |
|
|
301 | pool->wanted = 4; |
280 | |
302 | |
|
|
303 | pool->max_idle = 4; /* maximum number of threads that can pool->idle indefinitely */ |
|
|
304 | pool->idle_timeout = 10; /* number of seconds after which an pool->idle threads exit */ |
|
|
305 | |
|
|
306 | pool->userdata = userdata; |
281 | want_poll_cb = want_poll; |
307 | pool->want_poll_cb = want_poll; |
282 | done_poll_cb = done_poll; |
308 | pool->done_poll_cb = done_poll; |
283 | |
309 | |
284 | return 0; |
310 | return 0; |
285 | } |
311 | } |
286 | |
312 | |
287 | static void ecb_noinline ecb_cold |
313 | static void ecb_noinline ecb_cold |
… | |
… | |
304 | X_THREAD_PROC (etp_proc) |
330 | X_THREAD_PROC (etp_proc) |
305 | { |
331 | { |
306 | ETP_REQ *req; |
332 | ETP_REQ *req; |
307 | struct timespec ts; |
333 | struct timespec ts; |
308 | etp_worker *self = (etp_worker *)thr_arg; |
334 | etp_worker *self = (etp_worker *)thr_arg; |
|
|
335 | etp_pool pool = self->pool; |
309 | |
336 | |
310 | etp_proc_init (); |
337 | etp_proc_init (); |
311 | |
338 | |
312 | /* try to distribute timeouts somewhat evenly */ |
339 | /* try to distribute timeouts somewhat evenly */ |
313 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
340 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
314 | |
341 | |
315 | for (;;) |
342 | for (;;) |
316 | { |
343 | { |
317 | ts.tv_sec = 0; |
344 | ts.tv_sec = 0; |
318 | |
345 | |
319 | X_LOCK (reqlock); |
346 | X_LOCK (pool->reqlock); |
320 | |
347 | |
321 | for (;;) |
348 | for (;;) |
322 | { |
349 | { |
323 | req = reqq_shift (&req_queue); |
350 | req = reqq_shift (&pool->req_queue); |
324 | |
351 | |
325 | if (req) |
352 | if (ecb_expect_true (req)) |
326 | break; |
353 | break; |
327 | |
354 | |
328 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
355 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
329 | { |
356 | { |
330 | X_UNLOCK (reqlock); |
357 | X_UNLOCK (pool->reqlock); |
331 | X_LOCK (wrklock); |
358 | X_LOCK (pool->wrklock); |
332 | --started; |
359 | --pool->started; |
333 | X_UNLOCK (wrklock); |
360 | X_UNLOCK (pool->wrklock); |
334 | goto quit; |
361 | goto quit; |
335 | } |
362 | } |
336 | |
363 | |
337 | ++idle; |
364 | ++pool->idle; |
338 | |
365 | |
339 | if (idle <= max_idle) |
366 | if (pool->idle <= pool->max_idle) |
340 | /* we are allowed to idle, so do so without any timeout */ |
367 | /* we are allowed to pool->idle, so do so without any timeout */ |
341 | X_COND_WAIT (reqwait, reqlock); |
368 | X_COND_WAIT (pool->reqwait, pool->reqlock); |
342 | else |
369 | else |
343 | { |
370 | { |
344 | /* initialise timeout once */ |
371 | /* initialise timeout once */ |
345 | if (!ts.tv_sec) |
372 | if (!ts.tv_sec) |
346 | ts.tv_sec = time (0) + idle_timeout; |
373 | ts.tv_sec = time (0) + pool->idle_timeout; |
347 | |
374 | |
348 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
375 | if (X_COND_TIMEDWAIT (pool->reqwait, pool->reqlock, ts) == ETIMEDOUT) |
349 | ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ |
376 | ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ |
350 | } |
377 | } |
351 | |
378 | |
352 | --idle; |
379 | --pool->idle; |
353 | } |
380 | } |
354 | |
381 | |
355 | --nready; |
382 | --pool->nready; |
356 | |
383 | |
357 | X_UNLOCK (reqlock); |
384 | X_UNLOCK (pool->reqlock); |
358 | |
385 | |
359 | if (req->type == ETP_TYPE_QUIT) |
386 | if (ecb_expect_false (req->type == ETP_TYPE_QUIT)) |
360 | goto quit; |
387 | goto quit; |
361 | |
388 | |
362 | ETP_EXECUTE (self, req); |
389 | ETP_EXECUTE (self, req); |
363 | |
390 | |
364 | X_LOCK (reslock); |
391 | X_LOCK (pool->reslock); |
365 | |
392 | |
366 | ++npending; |
393 | ++pool->npending; |
367 | |
394 | |
368 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
395 | if (!reqq_push (&pool->res_queue, req)) |
369 | want_poll_cb (); |
396 | ETP_WANT_POLL (pool); |
370 | |
397 | |
371 | etp_worker_clear (self); |
398 | etp_worker_clear (self); |
372 | |
399 | |
373 | X_UNLOCK (reslock); |
400 | X_UNLOCK (pool->reslock); |
374 | } |
401 | } |
375 | |
402 | |
376 | quit: |
403 | quit: |
377 | free (req); |
404 | free (req); |
378 | |
405 | |
379 | X_LOCK (wrklock); |
406 | X_LOCK (pool->wrklock); |
380 | etp_worker_free (self); |
407 | etp_worker_free (self); |
381 | X_UNLOCK (wrklock); |
408 | X_UNLOCK (pool->wrklock); |
382 | |
409 | |
383 | return 0; |
410 | return 0; |
384 | } |
411 | } |
385 | |
412 | |
386 | static void ecb_cold |
413 | static void ecb_cold |
387 | etp_start_thread (void) |
414 | etp_start_thread (etp_pool pool) |
388 | { |
415 | { |
389 | etp_worker *wrk = calloc (1, sizeof (etp_worker)); |
416 | etp_worker *wrk = calloc (1, sizeof (etp_worker)); |
390 | |
417 | |
391 | /*TODO*/ |
418 | /*TODO*/ |
392 | assert (("unable to allocate worker thread data", wrk)); |
419 | assert (("unable to allocate worker thread data", wrk)); |
393 | |
420 | |
|
|
421 | wrk->pool = pool; |
|
|
422 | |
394 | X_LOCK (wrklock); |
423 | X_LOCK (pool->wrklock); |
395 | |
424 | |
396 | if (xthread_create (&wrk->tid, etp_proc, (void *)wrk)) |
425 | if (xthread_create (&wrk->tid, etp_proc, (void *)wrk)) |
397 | { |
426 | { |
398 | wrk->prev = &wrk_first; |
427 | wrk->prev = &pool->wrk_first; |
399 | wrk->next = wrk_first.next; |
428 | wrk->next = pool->wrk_first.next; |
400 | wrk_first.next->prev = wrk; |
429 | pool->wrk_first.next->prev = wrk; |
401 | wrk_first.next = wrk; |
430 | pool->wrk_first.next = wrk; |
402 | ++started; |
431 | ++pool->started; |
403 | } |
432 | } |
404 | else |
433 | else |
405 | free (wrk); |
434 | free (wrk); |
406 | |
435 | |
407 | X_UNLOCK (wrklock); |
436 | X_UNLOCK (pool->wrklock); |
408 | } |
437 | } |
409 | |
438 | |
410 | static void |
439 | static void |
411 | etp_maybe_start_thread (void) |
440 | etp_maybe_start_thread (etp_pool pool) |
412 | { |
441 | { |
413 | if (ecb_expect_true (etp_nthreads () >= wanted)) |
442 | if (ecb_expect_true (etp_nthreads (pool) >= pool->wanted)) |
414 | return; |
443 | return; |
415 | |
444 | |
416 | /* todo: maybe use idle here, but might be less exact */ |
445 | /* todo: maybe use pool->idle here, but might be less exact */ |
417 | if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
446 | if (ecb_expect_true (0 <= (int)etp_nthreads (pool) + (int)etp_npending (pool) - (int)etp_nreqs (pool))) |
418 | return; |
447 | return; |
419 | |
448 | |
420 | etp_start_thread (); |
449 | etp_start_thread (pool); |
421 | } |
450 | } |
422 | |
451 | |
423 | static void ecb_cold |
452 | static void ecb_cold |
424 | etp_end_thread (void) |
453 | etp_end_thread (etp_pool pool) |
425 | { |
454 | { |
426 | ETP_REQ *req = calloc (1, sizeof (ETP_REQ)); /* will be freed by worker */ |
455 | ETP_REQ *req = calloc (1, sizeof (ETP_REQ)); /* will be freed by worker */ |
427 | |
456 | |
428 | req->type = ETP_TYPE_QUIT; |
457 | req->type = ETP_TYPE_QUIT; |
429 | req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
458 | req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
430 | |
459 | |
431 | X_LOCK (reqlock); |
460 | X_LOCK (pool->reqlock); |
432 | reqq_push (&req_queue, req); |
461 | reqq_push (&pool->req_queue, req); |
433 | X_COND_SIGNAL (reqwait); |
462 | X_COND_SIGNAL (pool->reqwait); |
434 | X_UNLOCK (reqlock); |
463 | X_UNLOCK (pool->reqlock); |
435 | |
464 | |
436 | X_LOCK (wrklock); |
465 | X_LOCK (pool->wrklock); |
437 | --started; |
466 | --pool->started; |
438 | X_UNLOCK (wrklock); |
467 | X_UNLOCK (pool->wrklock); |
439 | } |
468 | } |
440 | |
469 | |
441 | ETP_API_DECL int |
470 | ETP_API_DECL int |
442 | etp_poll (void) |
471 | etp_poll (etp_pool pool) |
443 | { |
472 | { |
444 | unsigned int maxreqs; |
473 | unsigned int maxreqs; |
445 | unsigned int maxtime; |
474 | unsigned int maxtime; |
446 | struct timeval tv_start, tv_now; |
475 | struct timeval tv_start, tv_now; |
447 | |
476 | |
448 | X_LOCK (reslock); |
477 | X_LOCK (pool->reslock); |
449 | maxreqs = max_poll_reqs; |
478 | maxreqs = pool->max_poll_reqs; |
450 | maxtime = max_poll_time; |
479 | maxtime = pool->max_poll_time; |
451 | X_UNLOCK (reslock); |
480 | X_UNLOCK (pool->reslock); |
452 | |
481 | |
453 | if (maxtime) |
482 | if (maxtime) |
454 | gettimeofday (&tv_start, 0); |
483 | gettimeofday (&tv_start, 0); |
455 | |
484 | |
456 | for (;;) |
485 | for (;;) |
457 | { |
486 | { |
458 | ETP_REQ *req; |
487 | ETP_REQ *req; |
459 | |
488 | |
460 | etp_maybe_start_thread (); |
489 | etp_maybe_start_thread (pool); |
461 | |
490 | |
462 | X_LOCK (reslock); |
491 | X_LOCK (pool->reslock); |
463 | req = reqq_shift (&res_queue); |
492 | req = reqq_shift (&pool->res_queue); |
464 | |
493 | |
465 | if (req) |
494 | if (ecb_expect_true (req)) |
466 | { |
495 | { |
467 | --npending; |
496 | --pool->npending; |
468 | |
497 | |
469 | if (!res_queue.size && done_poll_cb) |
498 | if (!pool->res_queue.size) |
470 | done_poll_cb (); |
499 | ETP_DONE_POLL (pool); |
471 | } |
500 | } |
472 | |
501 | |
473 | X_UNLOCK (reslock); |
502 | X_UNLOCK (pool->reslock); |
474 | |
503 | |
475 | if (!req) |
504 | if (ecb_expect_false (!req)) |
476 | return 0; |
505 | return 0; |
477 | |
506 | |
478 | X_LOCK (reqlock); |
507 | X_LOCK (pool->reqlock); |
479 | --nreqs; |
508 | --pool->nreqs; |
480 | X_UNLOCK (reqlock); |
509 | X_UNLOCK (pool->reqlock); |
481 | |
510 | |
482 | if (ecb_expect_false (req->type == ETP_TYPE_GROUP && req->size)) |
511 | if (ecb_expect_false (req->type == ETP_TYPE_GROUP && req->size)) |
483 | { |
512 | { |
484 | req->flags |= ETP_FLAG_DELAYED; /* mark request as delayed */ |
513 | req->flags |= ETP_FLAG_DELAYED; /* mark request as delayed */ |
485 | continue; |
514 | continue; |
… | |
… | |
506 | errno = EAGAIN; |
535 | errno = EAGAIN; |
507 | return -1; |
536 | return -1; |
508 | } |
537 | } |
509 | |
538 | |
510 | ETP_API_DECL void |
539 | ETP_API_DECL void |
511 | etp_grp_cancel (ETP_REQ *grp); |
540 | etp_grp_cancel (etp_pool pool, ETP_REQ *grp); |
512 | |
541 | |
513 | ETP_API_DECL void |
542 | ETP_API_DECL void |
514 | etp_cancel (ETP_REQ *req) |
543 | etp_cancel (etp_pool pool, ETP_REQ *req) |
515 | { |
544 | { |
516 | req->cancelled = 1; |
545 | req->cancelled = 1; |
517 | |
546 | |
518 | etp_grp_cancel (req); |
547 | etp_grp_cancel (pool, req); |
519 | } |
548 | } |
520 | |
549 | |
521 | ETP_API_DECL void |
550 | ETP_API_DECL void |
522 | etp_grp_cancel (ETP_REQ *grp) |
551 | etp_grp_cancel (etp_pool pool, ETP_REQ *grp) |
523 | { |
552 | { |
524 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
553 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
525 | etp_cancel (grp); |
554 | etp_cancel (pool, grp); |
526 | } |
555 | } |
527 | |
556 | |
528 | ETP_API_DECL void |
557 | ETP_API_DECL void |
529 | etp_submit (ETP_REQ *req) |
558 | etp_submit (etp_pool pool, ETP_REQ *req) |
530 | { |
559 | { |
531 | req->pri -= ETP_PRI_MIN; |
560 | req->pri -= ETP_PRI_MIN; |
532 | |
561 | |
533 | if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
562 | if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
534 | if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
563 | if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
535 | |
564 | |
536 | if (ecb_expect_false (req->type == ETP_TYPE_GROUP)) |
565 | if (ecb_expect_false (req->type == ETP_TYPE_GROUP)) |
537 | { |
566 | { |
538 | /* I hope this is worth it :/ */ |
567 | /* I hope this is worth it :/ */ |
539 | X_LOCK (reqlock); |
568 | X_LOCK (pool->reqlock); |
540 | ++nreqs; |
569 | ++pool->nreqs; |
541 | X_UNLOCK (reqlock); |
570 | X_UNLOCK (pool->reqlock); |
542 | |
571 | |
543 | X_LOCK (reslock); |
572 | X_LOCK (pool->reslock); |
544 | |
573 | |
545 | ++npending; |
574 | ++pool->npending; |
546 | |
575 | |
547 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
576 | if (!reqq_push (&pool->res_queue, req)) |
548 | want_poll_cb (); |
577 | ETP_WANT_POLL (pool); |
549 | |
578 | |
550 | X_UNLOCK (reslock); |
579 | X_UNLOCK (pool->reslock); |
551 | } |
580 | } |
552 | else |
581 | else |
553 | { |
582 | { |
554 | X_LOCK (reqlock); |
583 | X_LOCK (pool->reqlock); |
555 | ++nreqs; |
584 | ++pool->nreqs; |
556 | ++nready; |
585 | ++pool->nready; |
557 | reqq_push (&req_queue, req); |
586 | reqq_push (&pool->req_queue, req); |
558 | X_COND_SIGNAL (reqwait); |
587 | X_COND_SIGNAL (pool->reqwait); |
559 | X_UNLOCK (reqlock); |
588 | X_UNLOCK (pool->reqlock); |
560 | |
589 | |
561 | etp_maybe_start_thread (); |
590 | etp_maybe_start_thread (pool); |
562 | } |
591 | } |
563 | } |
592 | } |
564 | |
593 | |
565 | ETP_API_DECL void ecb_cold |
594 | ETP_API_DECL void ecb_cold |
566 | etp_set_max_poll_time (double nseconds) |
595 | etp_set_max_poll_time (etp_pool pool, double seconds) |
567 | { |
596 | { |
568 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
597 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reslock); |
569 | max_poll_time = nseconds * ETP_TICKS; |
598 | pool->max_poll_time = seconds * ETP_TICKS; |
570 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
599 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reslock); |
571 | } |
600 | } |
572 | |
601 | |
573 | ETP_API_DECL void ecb_cold |
602 | ETP_API_DECL void ecb_cold |
574 | etp_set_max_poll_reqs (unsigned int maxreqs) |
603 | etp_set_max_poll_reqs (etp_pool pool, unsigned int maxreqs) |
575 | { |
604 | { |
576 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
605 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reslock); |
577 | max_poll_reqs = maxreqs; |
606 | pool->max_poll_reqs = maxreqs; |
578 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
607 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reslock); |
579 | } |
608 | } |
580 | |
609 | |
581 | ETP_API_DECL void ecb_cold |
610 | ETP_API_DECL void ecb_cold |
582 | etp_set_max_idle (unsigned int nthreads) |
611 | etp_set_max_idle (etp_pool pool, unsigned int threads) |
583 | { |
612 | { |
584 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
613 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reqlock); |
585 | max_idle = nthreads; |
614 | pool->max_idle = threads; |
586 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
615 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
587 | } |
616 | } |
588 | |
617 | |
589 | ETP_API_DECL void ecb_cold |
618 | ETP_API_DECL void ecb_cold |
590 | etp_set_idle_timeout (unsigned int seconds) |
619 | etp_set_idle_timeout (etp_pool pool, unsigned int seconds) |
591 | { |
620 | { |
592 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
621 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reqlock); |
593 | idle_timeout = seconds; |
622 | pool->idle_timeout = seconds; |
594 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
623 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
595 | } |
624 | } |
596 | |
625 | |
597 | ETP_API_DECL void ecb_cold |
626 | ETP_API_DECL void ecb_cold |
598 | etp_set_min_parallel (unsigned int nthreads) |
627 | etp_set_min_parallel (etp_pool pool, unsigned int threads) |
599 | { |
628 | { |
600 | if (wanted < nthreads) |
629 | if (pool->wanted < threads) |
601 | wanted = nthreads; |
630 | pool->wanted = threads; |
602 | } |
631 | } |
603 | |
632 | |
604 | ETP_API_DECL void ecb_cold |
633 | ETP_API_DECL void ecb_cold |
605 | etp_set_max_parallel (unsigned int nthreads) |
634 | etp_set_max_parallel (etp_pool pool, unsigned int threads) |
606 | { |
635 | { |
607 | if (wanted > nthreads) |
636 | if (pool->wanted > threads) |
608 | wanted = nthreads; |
637 | pool->wanted = threads; |
609 | |
638 | |
610 | while (started > wanted) |
639 | while (pool->started > pool->wanted) |
611 | etp_end_thread (); |
640 | etp_end_thread (pool); |
612 | } |
641 | } |
613 | |
642 | |