(declare (block) (standard-bindings) (extended-bindings))
(begin
  (define std/misc/concurrent-plan#max-actual-workers (make-parameter '0))
  (define std/misc/concurrent-plan#perform-plan/threads__%
    (lambda (_g4257_
             _perform400408_
             _announce401410_
             _on-success402412_
             _on-failure403414_
             _deterministic-order404416_
             _max-workers405418_
             _fg-queue420_
             _bg-queue421_)
      (let* ((_perform423_
              (if (eq? _perform400408_ absent-value)
                  (error '"Missing required keyword argument" 'perform:)
                  _perform400408_))
             (_announce425_
              (if (eq? _announce401410_ absent-value)
                  (error '"Missing required keyword argument" 'announce:)
                  _announce401410_))
             (_on-success427_
              (if (eq? _on-success402412_ absent-value)
                  (error '"Missing required keyword argument" 'on-success:)
                  _on-success402412_))
             (_on-failure429_
              (if (eq? _on-failure403414_ absent-value)
                  (error '"Missing required keyword argument" 'on-failure:)
                  _on-failure403414_))
             (_deterministic-order431_
              (if (eq? _deterministic-order404416_ absent-value)
                  (error '"Missing required keyword argument"
                         'deterministic-order:)
                  _deterministic-order404416_))
             (_max-workers433_
              (if (eq? _max-workers405418_ absent-value)
                  (max (let () (declare (not safe)) (##cpu-count)) '1)
                  _max-workers405418_)))
        (letrec* ((_foreground-thread536_ (current-thread))
                  (_fg-perform537_
                   (lambda (_item628_)
                     (_announce425_ _item628_)
                     (with-exception-catcher
                      (lambda (_g629631_)
                        (_on-failure429_ _item628_ _g629631_))
                      (lambda ()
                        (_on-success427_
                         _item628_
                         (_perform423_ _item628_))))))
                  (_serial?538_ (< _max-workers433_ '2))
                  (_parallel?539_ (not _serial?538_))
                  (_workers540_ '0))
          (let _loop542_ ()
            (letrec* ((_no-fg-item?545_
                       (std/misc/pqueue#pqueue-empty? _fg-queue420_))
                      (_fg-item?546_ (not _no-fg-item?545_))
                      (_no-bg-item?547_
                       (std/misc/pqueue#pqueue-empty? _bg-queue421_))
                      (_bg-item?548_ (not _no-bg-item?547_))
                      (_no-workers?549_ (zero? _workers540_))
                      (_workers?550_ (not _no-workers?549_))
                      (_no-worker-slots?551_
                       (>= _workers540_ _max-workers433_))
                      (_worker-slots?552_ (not _no-worker-slots?551_))
                      (_ready-to-spawn?553_
                       (if _bg-item?548_ _worker-slots?552_ '#f))
                      (_wait?554_
                       (if (not _ready-to-spawn?553_)
                           (let ((_$e625_ _no-fg-item?545_))
                             (if _$e625_ _$e625_ _deterministic-order431_))
                           '#f)))
              (let ((_$e556_ (if _parallel?539_
                                 (if _workers?550_
                                     (thread-receive
                                      (if _wait?554_ '#f '0)
                                      '#f)
                                     '#f)
                                 '#f)))
                (if _$e556_
                    ((lambda (_e558560_)
                       (let* ((_g562573_ _e558560_)
                              (_E565577_
                               (lambda ()
                                 (error '"No clause matching" _g562573_))))
                         (let ((_K567599_
                                (lambda (_item591_ _worker592_)
                                  (set! _workers540_ (- _workers540_ '1))
                                  (with-exception-catcher
                                   (lambda (_g593595_)
                                     (_on-failure429_ _item591_ _g593595_))
                                   (lambda ()
                                     (_on-success427_
                                      _item591_
                                      (thread-join! _worker592_))))
                                  (_loop542_)))
                               (_K566583_
                                (lambda (_m581_)
                                  (error '"unexpected message" _m581_))))
                           (if (let ()
                                 (declare (not safe))
                                 (##pair? _g562573_))
                               (let ((_tl569604_
                                      (let ()
                                        (declare (not safe))
                                        (##cdr _g562573_)))
                                     (_hd568602_
                                      (let ()
                                        (declare (not safe))
                                        (##car _g562573_))))
                                 (if (let ()
                                       (declare (not safe))
                                       (##pair? _tl569604_))
                                     (let ((_tl571611_
                                            (let ()
                                              (declare (not safe))
                                              (##cdr _tl569604_)))
                                           (_hd570609_
                                            (let ()
                                              (declare (not safe))
                                              (##car _tl569604_))))
                                       (if (let ()
                                             (declare (not safe))
                                             (##null? _tl571611_))
                                           (let ((_worker607_ _hd568602_)
                                                 (_item614_ _hd570609_))
                                             (_K567599_ _item614_ _worker607_))
                                           (let ((_m586_ _g562573_))
                                             (_K566583_ _m586_))))
                                     (let ((_m586_ _g562573_))
                                       (_K566583_ _m586_))))
                               (let ((_m586_ _g562573_))
                                 (_K566583_ _m586_))))))
                     _$e556_)
                    (if (and _parallel?539_ _ready-to-spawn?553_)
                        (begin
                          (let ((_item616_
                                 (std/misc/pqueue#pqueue-pop!__%
                                  _bg-queue421_
                                  '#f)))
                            (_announce425_ _item616_)
                            (set! _workers540_ (+ _workers540_ '1))
                            (gerbil/gambit/threads#spawn/name
                             (cons 'worker: (cons _item616_ '()))
                             (lambda ()
                               (with-unwind-protect
                                (lambda () (_perform423_ _item616_))
                                (lambda ()
                                  (thread-send
                                   _foreground-thread536_
                                   (cons (current-thread)
                                         (cons _item616_ '())))))))
                            (if (< (std/misc/concurrent-plan#max-actual-workers)
                                   _workers540_)
                                (std/misc/concurrent-plan#max-actual-workers
                                 _workers540_)
                                '#!void))
                          (_loop542_))
                        (if (and _serial?538_ _bg-item?548_)
                            (begin
                              (_fg-perform537_
                               (std/misc/pqueue#pqueue-pop!__%
                                _bg-queue421_
                                '#f))
                              (_loop542_))
                            (if (and _fg-item?546_
                                     (not _deterministic-order431_))
                                (begin
                                  (_fg-perform537_
                                   (std/misc/pqueue#pqueue-pop!__%
                                    _fg-queue420_
                                    '#f))
                                  (_loop542_))
                                (if (and _fg-item?546_
                                         _deterministic-order431_
                                         _no-workers?549_
                                         _no-bg-item?547_)
                                    (begin
                                      (let _lp621_ ()
                                        (if (std/misc/pqueue#pqueue-empty?
                                             _fg-queue420_)
                                            '#!void
                                            (begin
                                              (_fg-perform537_
                                               (std/misc/pqueue#pqueue-pop!__%
                                                _fg-queue420_
                                                '#f))
                                              (_lp621_))))
                                      (_loop542_))
                                    (begin
                                      (if (and _no-fg-item?545_
                                               _no-bg-item?547_
                                               _no-workers?549_)
                                          '#!void
                                          (error '"Assertion failed"
                                                 '(and no-fg-item?
                                                       no-bg-item?
                                                       no-workers?)))
                                      '#!void)))))))))))))
  (define std/misc/concurrent-plan#perform-plan/threads__@
    (lambda (_keys399638_ . _args640_)
      (apply std/misc/concurrent-plan#perform-plan/threads__%
             _keys399638_
             (table-ref _keys399638_ 'perform: absent-value)
             (table-ref _keys399638_ 'announce: absent-value)
             (table-ref _keys399638_ 'on-success: absent-value)
             (table-ref _keys399638_ 'on-failure: absent-value)
             (table-ref _keys399638_ 'deterministic-order: absent-value)
             (table-ref _keys399638_ 'max-workers: absent-value)
             _args640_)))
  (define std/misc/concurrent-plan#perform-plan/threads
    (lambda _args406646_
      (apply keyword-dispatch
             '#(#f
                #f
                #f
                #f
                announce:
                #f
                #f
                on-success:
                perform:
                max-workers:
                deterministic-order:
                on-failure:
                #f)
             std/misc/concurrent-plan#perform-plan/threads__@
             _args406646_))))
