pike.git / lib / modules / Concurrent.pmod

version» Context lines:

pike.git/lib/modules/Concurrent.pmod:84:    mixed result;    State state;       protected array(array(function(mixed, mixed ...: void)|mixed))    success_cbs = ({});    protected array(array(function(mixed, mixed ...: void)|mixed))    failure_cbs = ({});       protected Pike.Backend backend;    -  protected array timeout_call_out_handle; -  +     //! Set the backend to use for calling any callbacks.    //!    //! @note    //! This overides the mode set by @[use_backend()].    //!    //! @seealso    //! @[get_backend()], @[use_backend()]    void set_backend(Pike.Backend backend)    {    this::backend = backend;
pike.git/lib/modules/Concurrent.pmod:148:    //!    //! The default implementation calls @[cb] via the    //! backend set via @[set_backend()] (if any), and    //! otherwise falls back the the mode set by    //! @[use_backend()].    //!    //! @seealso    //! @[set_backend()], @[use_backend()]    protected void call_callback(function cb, mixed ... args)    { -  if (timeout_call_out_handle) { -  // Remove the timeout call_out, as it will not be relevant, -  // but holds a reference to us. -  (backend?backend->remove_call_out:remove_call_out) -  (timeout_call_out_handle); -  timeout_call_out_handle = UNDEFINED; +  (backend ? backend->call_out : callout)(cb, 0, @args);    } -  if (backend) { -  backend->call_out(cb, 0, @args); -  } else { -  callout(cb, 0, @args); -  } -  } +        //! Wait for fulfillment.    //!    //! @seealso    //! @[get()]    this_program wait()    {    if (state <= STATE_PENDING) {    Thread.MutexKey key = mux->lock();    while (state <= STATE_PENDING) {
pike.git/lib/modules/Concurrent.pmod:654:    //!    //! @seealso    //! @[recover()], @[recover_with()], @[then()], @[on_failure()],    //! @[Promise.failure()],    //! @url{https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise@}    inline this_program thencatch(function(mixed, mixed ... : mixed) onrejected,    mixed ... extra) {    return then(0, onrejected, @extra);    }    -  //! Return a @[Future] that will either be fulfilled with the fulfilled -  //! result of this @[Future], or be failed after @[seconds] have expired. -  this_program timeout(int|float seconds) +  private this_program setup_call_out(int|float seconds, void|int tout)    { -  +  array call_out_handle;    Promise p = promise_factory(); -  +  void cancelcout(mixed value) +  { +  (backend ? backend->remove_call_out : remove_call_out)(call_out_handle); +  p->try_success(0); +  }    /* NB: try_* variants as the original promise may get fulfilled    * after the timeout has occurred.    */ -  on_failure(p->try_failure); -  on_success(p->try_success); -  if (timeout_call_out_handle) { -  // Remove the previous timeout call_out. -  (backend?backend->remove_call_out:remove_call_out) -  (timeout_call_out_handle); -  } -  timeout_call_out_handle = (backend?backend->call_out:call_out) -  (p->try_failure, seconds, ({ "Timeout.\n", backtrace() })); +  on_failure(cancelcout); +  call_out_handle = (backend ? backend->call_out : call_out) +  (p[tout ? "try_failure" : "try_success"], seconds, +  tout && ({ "Timeout.\n", backtrace() })); +  if (tout) +  on_success(cancelcout);    return p->future();    }    -  +  //! Return a @[Future] that will either be fulfilled with the fulfilled +  //! result of this @[Future], or be failed after @[seconds] have expired. +  this_program timeout(int|float seconds) +  { +  return first_completed( +  ({ this_program::this, setup_call_out(seconds, 1) }) +  ); +  } +  +  //! Return a @[Future] that will be fulfilled with the fulfilled +  //! result of this @[Future], but not until at least @[seconds] have passed. +  this_program delay(int|float seconds) +  { +  return results( +  ({ this_program::this, setup_call_out(seconds) }) +  )->map(`[], 0); +  } +     protected string _sprintf(int t)    {    return t=='O' && sprintf("%O(%s,%O)", this_program,    ([ STATE_NO_FUTURE : "no future",    STATE_PENDING : "pending",    STATE_REJECTED : "rejected",    STATE_REJECTION_REPORTED : "rejection_reported",    STATE_FULFILLED : "fulfilled" ])[state],    result);    }