Branch: Tag:

2019-02-06

2019-02-06 22:17:42 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Concurrent.Promise: Reduce number of complaints.

Don't complain about dropping promises that have no corresponding
future on the floor. As the promises have no future, nobody is
affected by them not being fulfilled.

5:   //! The @[Future] and @[Promise] API was inspired by   //! @url{https://github.com/couchdeveloper/FutureLib@}.    - protected enum State { + local protected enum State { +  STATE_NO_FUTURE = -1,    STATE_PENDING = 0,    STATE_FULFILLED,    STATE_REJECTED,
90:    mixed res = result;    if (!s) {    Thread.MutexKey key = mux->lock(); -  while (!state) { +  while (state <= STATE_PENDING) {    cond->wait(key);    }   
125:    case STATE_FULFILLED:    callout(cb, 0, result, @extra);    break; +  case STATE_NO_FUTURE:    case STATE_PENDING:    // Rely on interpreter lock to add to success_cbs before state changes    // again
154:    case STATE_REJECTED:    callout(cb, 0, result, @extra);    break; +  case STATE_NO_FUTURE:    case STATE_PENDING:    // Rely on interpreter lock to add to failure_cbs before state changes    // again
572:    protected string _sprintf(int t)    {    return t=='O' && sprintf("%O(%s,%O)", this_program, -  ([ STATE_PENDING : "pending", +  ([ STATE_NO_FUTURE : "no future", +  STATE_PENDING : "pending",    STATE_REJECTED : "rejected",    STATE_FULFILLED : "fulfilled" ])[state],    result);
636:    Thread.MutexKey key = mux->lock();    do    { -  if (!p->state) +  if (p->state <= STATE_PENDING)    {    ++failed;    if (max_failures < failed && max_failures >= 0)
671:    Thread.MutexKey key = mux->lock();    do    { -  if (!p->state) +  if (p->state <= STATE_PENDING)    {    ++succeeded;    if (promises - min_failures < succeeded)
716:    final AggregateState _astate;       //! Creates a new promise, optionally initialised from a traditional callback -  //! driven method via @expr{executor(resolve, reject, extra ... )@}. +  //! driven method via @expr{executor(success, failure, @@extra)@}.    //!    //! @seealso    //! @url{https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise@}
724:    function(function(mixed:void),    function(mixed:void), mixed ...:void) executor, mixed ... extra)    { +  state = STATE_NO_FUTURE;    if (executor)    executor(success, failure, @extra);    }
752:    //! The future value that we promise.    Future future()    { +  if (state == STATE_NO_FUTURE) state = STATE_PENDING;    return Future::this;    }   
760:    void|function(mixed : void) globalfailure)    {    Thread.MutexKey key = mux->lock(); -  if (!state) +  if (state <= STATE_PENDING)    {    state = newstate;    result = value;
819:    //! @[success()], @[try_failure()], @[failure()], @[on_success()]    inline this_program try_success(mixed value)    { -  return state ? this_program::this : success(value, 1); +  return (state > STATE_PENDING) ? this_program::this : success(value, 1);    }       //! @decl this_program failure(mixed value)
857:    //! @[failure()], @[success()], @[on_failure()]    inline this_program try_failure(mixed value)    { -  return state ? this_program::this : failure(value, 1); +  return (state > STATE_PENDING) ? this_program::this : failure(value, 1);    }       inline private void fill_astate()
1018:       protected void _destruct()    { -  if (!state) +  // NB: Don't complain about dropping STATE_NO_FUTURE on the floor. +  if (state == STATE_PENDING)    try_failure(({ "Promise broken.\n", backtrace() }));    }   }