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 | * |
… | |
… | |
343 | |
343 | |
344 | for (;;) |
344 | for (;;) |
345 | { |
345 | { |
346 | req = reqq_shift (&pool->req_queue); |
346 | req = reqq_shift (&pool->req_queue); |
347 | |
347 | |
348 | if (req) |
348 | if (ecb_expect_true (req)) |
349 | break; |
349 | break; |
350 | |
350 | |
351 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
351 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
352 | { |
352 | { |
353 | X_UNLOCK (pool->reqlock); |
353 | X_UNLOCK (pool->reqlock); |
… | |
… | |
377 | |
377 | |
378 | --pool->nready; |
378 | --pool->nready; |
379 | |
379 | |
380 | X_UNLOCK (pool->reqlock); |
380 | X_UNLOCK (pool->reqlock); |
381 | |
381 | |
382 | if (req->type == ETP_TYPE_QUIT) |
382 | if (ecb_expect_false (req->type == ETP_TYPE_QUIT)) |
383 | goto quit; |
383 | goto quit; |
384 | |
384 | |
385 | ETP_EXECUTE (self, req); |
385 | ETP_EXECUTE (self, req); |
386 | |
386 | |
387 | X_LOCK (pool->reslock); |
387 | X_LOCK (pool->reslock); |
388 | |
388 | |
389 | ++pool->npending; |
389 | ++pool->npending; |
390 | |
390 | |
391 | if (!reqq_push (&pool->res_queue, req)) |
391 | if (!reqq_push (&pool->res_queue, req)) |
392 | ETP_WANT_POLL (poll); |
392 | ETP_WANT_POLL (pool); |
393 | |
393 | |
394 | etp_worker_clear (self); |
394 | etp_worker_clear (self); |
395 | |
395 | |
396 | X_UNLOCK (pool->reslock); |
396 | X_UNLOCK (pool->reslock); |
397 | } |
397 | } |
… | |
… | |
485 | etp_maybe_start_thread (pool); |
485 | etp_maybe_start_thread (pool); |
486 | |
486 | |
487 | X_LOCK (pool->reslock); |
487 | X_LOCK (pool->reslock); |
488 | req = reqq_shift (&pool->res_queue); |
488 | req = reqq_shift (&pool->res_queue); |
489 | |
489 | |
490 | if (req) |
490 | if (ecb_expect_true (req)) |
491 | { |
491 | { |
492 | --pool->npending; |
492 | --pool->npending; |
493 | |
493 | |
494 | if (!pool->res_queue.size) |
494 | if (!pool->res_queue.size) |
495 | ETP_DONE_POLL (pool->userdata); |
495 | ETP_DONE_POLL (pool); |
496 | } |
496 | } |
497 | |
497 | |
498 | X_UNLOCK (pool->reslock); |
498 | X_UNLOCK (pool->reslock); |
499 | |
499 | |
500 | if (!req) |
500 | if (ecb_expect_false (!req)) |
501 | return 0; |
501 | return 0; |
502 | |
502 | |
503 | X_LOCK (pool->reqlock); |
503 | X_LOCK (pool->reqlock); |
504 | --pool->nreqs; |
504 | --pool->nreqs; |
505 | X_UNLOCK (pool->reqlock); |
505 | X_UNLOCK (pool->reqlock); |
… | |
… | |
586 | etp_maybe_start_thread (pool); |
586 | etp_maybe_start_thread (pool); |
587 | } |
587 | } |
588 | } |
588 | } |
589 | |
589 | |
590 | ETP_API_DECL void ecb_cold |
590 | ETP_API_DECL void ecb_cold |
591 | etp_set_max_poll_time (etp_pool pool, double nseconds) |
591 | etp_set_max_poll_time (etp_pool pool, double seconds) |
592 | { |
592 | { |
593 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reslock); |
593 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reslock); |
594 | pool->max_poll_time = nseconds * ETP_TICKS; |
594 | pool->max_poll_time = seconds * ETP_TICKS; |
595 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reslock); |
595 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reslock); |
596 | } |
596 | } |
597 | |
597 | |
598 | ETP_API_DECL void ecb_cold |
598 | ETP_API_DECL void ecb_cold |
599 | etp_set_max_poll_reqs (etp_pool pool, unsigned int maxreqs) |
599 | etp_set_max_poll_reqs (etp_pool pool, unsigned int maxreqs) |
… | |
… | |
602 | pool->max_poll_reqs = maxreqs; |
602 | pool->max_poll_reqs = maxreqs; |
603 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reslock); |
603 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reslock); |
604 | } |
604 | } |
605 | |
605 | |
606 | ETP_API_DECL void ecb_cold |
606 | ETP_API_DECL void ecb_cold |
607 | etp_set_max_idle (etp_pool pool, unsigned int nthreads) |
607 | etp_set_max_idle (etp_pool pool, unsigned int threads) |
608 | { |
608 | { |
609 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reqlock); |
609 | if (WORDACCESS_UNSAFE) X_LOCK (pool->reqlock); |
610 | pool->max_idle = nthreads; |
610 | pool->max_idle = threads; |
611 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
611 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
612 | } |
612 | } |
613 | |
613 | |
614 | ETP_API_DECL void ecb_cold |
614 | ETP_API_DECL void ecb_cold |
615 | etp_set_idle_timeout (etp_pool pool, unsigned int seconds) |
615 | etp_set_idle_timeout (etp_pool pool, unsigned int seconds) |
… | |
… | |
618 | pool->idle_timeout = seconds; |
618 | pool->idle_timeout = seconds; |
619 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
619 | if (WORDACCESS_UNSAFE) X_UNLOCK (pool->reqlock); |
620 | } |
620 | } |
621 | |
621 | |
622 | ETP_API_DECL void ecb_cold |
622 | ETP_API_DECL void ecb_cold |
623 | etp_set_min_parallel (etp_pool pool, unsigned int nthreads) |
623 | etp_set_min_parallel (etp_pool pool, unsigned int threads) |
624 | { |
624 | { |
625 | if (pool->wanted < nthreads) |
625 | if (pool->wanted < threads) |
626 | pool->wanted = nthreads; |
626 | pool->wanted = threads; |
627 | } |
627 | } |
628 | |
628 | |
629 | ETP_API_DECL void ecb_cold |
629 | ETP_API_DECL void ecb_cold |
630 | etp_set_max_parallel (etp_pool pool, unsigned int nthreads) |
630 | etp_set_max_parallel (etp_pool pool, unsigned int threads) |
631 | { |
631 | { |
632 | if (pool->wanted > nthreads) |
632 | if (pool->wanted > threads) |
633 | pool->wanted = nthreads; |
633 | pool->wanted = threads; |
634 | |
634 | |
635 | while (pool->started > pool->wanted) |
635 | while (pool->started > pool->wanted) |
636 | etp_end_thread (pool); |
636 | etp_end_thread (pool); |
637 | } |
637 | } |
638 | |
638 | |