1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
//! Module for handling multiple concurrent events. 
 
/* 
 * Inspired by https://github.com/couchdeveloper/FutureLib 
 */ 
 
protected enum State { 
  STATE_PENDING = 0, 
  STATE_FULFILLED, 
  STATE_REJECTED, 
}; 
 
protected Thread.Mutex mux = Thread.Mutex(); 
protected Thread.Condition cond = Thread.Condition(); 
 
//! Value that will be provided asynchronously 
//! sometime in the future. 
//! 
//! @seealso 
//!   @[Promise] 
class Future 
{ 
  mixed result = UNDEFINED; 
  State state; 
 
  protected function(mixed:void) success_cb; 
  protected array(mixed) success_ctx; 
  protected function(mixed:void) failure_cb; 
  protected array(mixed) failure_ctx; 
 
  //! Wait for fullfillment and return the value. 
  //! 
  //! @throws 
  //!   Throws on rejection. 
  mixed get() 
  { 
    State s = state; 
    mixed res = result; 
    if (!s) { 
      object key = mux->lock(); 
      while (!state) { 
        cond->wait(key); 
      } 
 
      s = state; 
      res = result; 
      key = 0; 
    } 
 
    if (s == STATE_REJECTED) { 
      throw(res); 
    } 
    return res; 
  } 
 
  //! Register a callback that is to be called on fulfillment. 
  this_program on_success(function(mixed, mixed ... : void) cb, mixed ... extra) 
  { 
    object key = mux->lock(); 
    success_cb = cb; 
    success_ctx = extra; 
    State s = state; 
    mixed res = result; 
    key = 0; 
    if (s == STATE_FULFILLED) { 
      call_out(cb, 0, res, @extra); 
    } 
 
    return this; 
  } 
 
  //! Register a callback that is to be called on failure. 
  this_program on_failure(function(mixed, mixed ... : void) cb, mixed ... extra) 
  { 
    object key = mux->lock(); 
    failure_cb = cb; 
    failure_ctx = extra; 
    State s = state; 
    mixed res = result; 
    key = 0; 
    if (s == STATE_REJECTED) { 
      call_out(cb, 0, res, @extra); 
    } 
    return this; 
  } 
 
  //! Apply @[fun] with @[val] followed by the contents of @[ctx], 
  //! and update @[p] with the result. 
  protected void apply(mixed val, Promise p, 
                       function(mixed, mixed ... : mixed) fun, 
                       array(mixed) ctx) 
  { 
    mixed err = catch { 
        p->success(fun(val, @ctx)); 
        return; 
      }; 
    p->failure(err); 
  } 
 
  //! Apply @[fun] with @[val] followed by the contents of @[ctx], 
  //! and update @[p] with the eventual result. 
  protected void apply_flat(mixed val, Promise p, 
                            function(mixed, mixed ... : Future) fun, 
                            array(mixed) ctx) 
  { 
    mixed err = catch { 
        Future f = fun(val, @ctx); 
        if (!objectp(f) || !f->on_failure || !f->on_success) { 
          error("Expected %O to return a Future. Got: %O.\n", 
                fun, f); 
        } 
        f->on_failure(p->failure); 
        f->on_success(p->success); 
        return; 
      }; 
    p->failure(err); 
  } 
 
  //! Apply @[fun] with @[val] followed by the contents of @[ctx], 
  //! and update @[p] with @[val] if @[fun] didn't return false. 
  //! If @[fun] returned false fail @[p] with @[UNDEFINED]. 
  protected void apply_filter(mixed val, Promise p, 
                              function(mixed, mixed ... : int(0..1)) fun, 
                              array(mixed) ctx) 
  { 
    mixed err = catch { 
        if (fun(val, @ctx)) { 
          p->success(val); 
        } else { 
          p->failure(UNDEFINED); 
        } 
        return; 
      }; 
    p->failure(err); 
  } 
 
  //! Return a @[Future] that will be fulfilled with the result 
  //! of applying @[fun] with the fulfilled result of this @[Future] 
  //! followed by @[extra]. 
  this_program map(function(mixed, mixed ... : mixed) fun, mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_failure(p->failure); 
    on_success(apply, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with the fulfilled result 
  //! of applying @[fun] with the fulfilled result of this @[Future] 
  //! followed by @[extra]. 
  this_program flat_map(function(mixed, mixed ... : this_program) fun, 
                        mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_failure(p->failure); 
    on_success(apply_flat, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with either 
  //! the fulfilled result of this @[Future], or the result 
  //! of applying @[fun] with the failed result followed 
  //! by @[extra]. 
  this_program recover(function(mixed, mixed ... : mixed) fun, 
                       mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_success(p->success); 
    on_failure(apply, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with either 
  //! the fulfilled result of this @[Future], or the fulfilled result 
  //! of applying @[fun] with the failed result followed 
  //! by @[extra]. 
  this_program recover_with(function(mixed, mixed ... : this_program) fun, 
                            mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_success(p->success); 
    on_failure(apply_flat, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that either will by fulfilled by the 
  //! fulfilled result of this @[Future] if applying @[fun] 
  //! with the result followed by @[extra] returns true, 
  //! or will fail with @[UNDEFINED] if it returns false. 
  this_program filter(function(mixed, mixed ... : int(0..1)) fun, 
                      mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_failure(p->failure); 
    on_success(apply_filter, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with either 
  //! the result of applying @[success] with the fulfilled result 
  //! followed by @[extra], or the result of applying @[failure] 
  //! with the failed result followed by @[extra]. 
  //! 
  //! @[failure] defaults to @[success]. 
  //! 
  //! @seealso 
  //!   @[map] 
  this_program transform(function(mixed, mixed ... : mixed) success, 
                         function(mixed, mixed ... : mixed)|void failure, 
                         mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_success(apply, p, success, extra); 
    on_failure(apply, p, failure || success, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with either 
  //! the fulfilled result of applying @[success] with the fulfilled result 
  //! followed by @[extra], or the fulfilled result of applying @[failure] 
  //! with the failed result followed by @[extra]. 
  //! 
  //! @[failure] defaults to @[success]. 
  //! 
  //! @seealso 
  //!   @[flat_map] 
  this_program transform_with(function(mixed, mixed ... : Future) success, 
                              function(mixed, mixed ... : Future)|void failure, 
                              mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_success(apply_flat, p, success, extra); 
    on_failure(apply_flat, p, failure || success, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with an 
  //! array of the fulfilled result of this object followed 
  //! by the fulfilled results of @[others]. 
  //! 
  //! @seealso 
  //!   @[results()] 
  this_program zip(this_program ... others) 
  { 
    if (!sizeof(others)) return this_program::this; 
    return results(({ this_program::this }) + others); 
  } 
} 
 
//! Promise to provide a @[Future] value. 
//! 
//! Objects of this class are typically kept internal to the 
//! code that provides the @[Future] value. The only thing 
//! that is directly returned to the user is the return 
//! value from @[future()]. 
//! 
//! @seealso 
//!   @[Future], @[future()] 
class Promise 
{ 
  inherit Future; 
 
  //! The future value that we promise. 
  Future future() 
  { 
    return Future::this; 
  } 
 
  protected void unlocked_success(mixed value) 
  { 
    if (state < STATE_REJECTED) { 
      result = value; 
      state = STATE_FULFILLED; 
      function cb = success_cb; 
      array(mixed) extra = success_ctx; 
      cond->broadcast(); 
      if (cb) { 
        call_out(cb, 0, value, @extra); 
      } 
    } 
  } 
 
  //! Fulfill the @[Future] value. 
  void success(mixed value) 
  { 
    object key = mux->lock(); 
    unlocked_success(value); 
    key = 0; 
  } 
 
  protected void unlocked_failure(mixed value) 
  { 
    state = STATE_REJECTED; 
    result = value; 
    function cb = failure_cb; 
    array(mixed) extra = failure_ctx; 
    cond->broadcast(); 
    if (cb) { 
      call_out(cb, 0, value, @extra); 
    } 
  } 
 
  //! Reject the @[Future] value. 
  void failure(mixed value) 
  { 
    object key = mux->lock(); 
    unlocked_failure(value); 
    key = 0; 
  } 
 
  //! Reject the @[Future] value unless it has already been fulfilled. 
  void maybe_failure(mixed value) 
  { 
    object key = mux->lock(); 
    if (!state) return; 
    unlocked_failure(value); 
  } 
 
  protected void destroy() 
  { 
    if (!state) { 
      failure(({ "Promise broken.\n", backtrace() })); 
    } 
  } 
} 
 
protected class FirstCompleted 
{ 
  inherit Promise; 
 
  protected void create(array(Future) futures) 
  { 
    if (!sizeof(futures)) { 
      state = STATE_FULFILLED; 
      return; 
    } 
    futures->on_failure(got_failure); 
    futures->on_success(got_success); 
  } 
 
  protected void got_failure(mixed err) 
  { 
    if (state) return; 
    failure(err); 
  } 
 
  protected void got_success(mixed val) 
  { 
    if (state) return; 
    success(val); 
  } 
} 
 
//! Return a @[Future] that represents the first 
//! of the @[futures] that completes. 
Future first_completed(array(Future) futures) 
{ 
  Promise p = FirstCompleted(futures); 
  return p->future(); 
} 
 
protected class Results 
{ 
  inherit Promise; 
 
  protected void create(array(Future) futures) 
  { 
    if (!sizeof(futures)) { 
      success(({})); 
      return; 
    } 
 
    array(mixed) results = allocate(sizeof(futures), UNDEFINED); 
    array(State) states  = allocate(sizeof(futures), STATE_PENDING); 
 
    futures->on_failure(failure); 
 
    foreach(futures; int i; Future f) { 
      f->on_success(got_success, i, results, states); 
    } 
  } 
 
  protected void got_success(mixed value, int i, 
                             array(mixed) results, array(State) states) 
  { 
    if (state == STATE_REJECTED) return; 
    results[i] = value; 
    states[i] = STATE_FULFILLED; 
    if (!state) { 
      if (has_value(states, STATE_PENDING)) { 
        return; 
      } 
    } 
    success(results); 
  } 
} 
 
//! Return a @[Future] that represents the array of all 
//! the completed @[futures]. 
Future results(array(Future) futures) 
{ 
  Promise p = Results(futures); 
  return p->future(); 
} 
 
//! Return a @[Future] that represents the array of mapping @[fun] 
//! over the results of the competed @[futures]. 
Future traverse(array(Future) futures, 
                function(mixed, mixed ... : mixed) fun, 
                mixed ... extra) 
{ 
  return results(futures->map(fun, @extra)); 
} 
 
protected class Fold 
{ 
  inherit Promise; 
 
  protected mixed accumulated; 
 
  protected void create(array(Future) futures, 
                        mixed initial, 
                        function(mixed, mixed, mixed ... : mixed) fun, 
                        array(mixed) ctx) 
  { 
    if (!sizeof(futures)) { 
      success(initial); 
      return; 
    } 
    accumulated = initial; 
    futures->on_failure(failure); 
    foreach(futures; int i; Future f) { 
      f->on_success(got_success, i, fun, ctx, 
                    allocate(sizeof(futures), STATE_PENDING)); 
    } 
  } 
 
  protected void got_success(mixed val, int i, 
                             function(mixed, mixed, mixed ... : mixed) fun, 
                             array(mixed) ctx, 
                             array(State) states) 
  { 
    if (state || states[i]) return; 
    states[i] = STATE_FULFILLED; 
    mixed err = catch { 
        // FIXME: What if fun triggers a recursive call? 
        accumulated = fun(val, accumulated, @ctx); 
        if (!state) { 
          if (has_value(states, STATE_PENDING)) return; 
        } 
        success(accumulated); 
        return; 
      }; 
    failure(err); 
  } 
} 
 
//! Return a @[Future] that represents the accumulated results of 
//! applying @[fun] to the results of the @[futures] in turn. 
//! 
//! @param initial 
//!   Initial value of the accumulator. 
//! 
//! @param fun 
//!   Function to apply. The first argument is the result of 
//!   one of the @[futures], the second the current accumulated 
//!   value, and any further from @[extra]. 
//! 
//! @note 
//!   @[fun] may be called in any order, and will only be called 
//!   once for every @[Future] in @[futures]. 
Future fold(array(Future) futures, 
            mixed initial, 
            function(mixed, mixed, mixed ... : mixed) fun, 
            mixed ... extra) 
{ 
  Promise p = Fold(futures, initial, fun, extra); 
  return p->future(); 
}