@@ -43,7 +43,8 @@ being specified here.
43
43
* [ ` canon resource.rep ` ] ( #canon-resourcerep )
44
44
* [ ` canon context.get ` ] ( #-canon-contextget ) 🔀
45
45
* [ ` canon context.set ` ] ( #-canon-contextset ) 🔀
46
- * [ ` canon backpressure.set ` ] ( #-canon-backpressureset ) 🔀
46
+ * [ ` canon backpressure.set ` ] ( #-canon-backpressureset ) 🔀✕
47
+ * [ ` canon backpressure.{inc,dec} ` ] ( #-canon-backpressureincdec ) 🔀
47
48
* [ ` canon task.return ` ] ( #-canon-taskreturn ) 🔀
48
49
* [ ` canon task.cancel ` ] ( #-canon-taskcancel ) 🔀
49
50
* [ ` canon yield ` ] ( #-canon-yield ) 🔀
@@ -279,15 +280,15 @@ class ComponentInstance:
279
280
store: Store
280
281
table: Table
281
282
may_leave: bool
282
- backpressure: bool
283
+ backpressure: int
283
284
exclusive: bool
284
285
num_waiting_to_enter: int
285
286
286
287
def __init__ (self , store ):
287
288
self .store = store
288
289
self .table = Table()
289
290
self .may_leave = True
290
- self .backpressure = False
291
+ self .backpressure = 0
291
292
self .exclusive = False
292
293
self .num_waiting_to_enter = 0
293
294
```
@@ -829,8 +830,8 @@ to avoid the need to otherwise-endlessly allocate guest memory for blocked
829
830
async calls until OOM. When backpressure is enabled, ` enter ` will block until
830
831
backpressure is disabled. There are three sources of backpressure:
831
832
1 . * Explicit backpressure* is triggered by core wasm calling
832
- ` backpressure.set ` which, in ` canon_backpressure_set ` (defined below),
833
- sets the ` ComponentInstance.backpressure ` flag .
833
+ ` backpressure.{inc,dec} ` which modify the ` ComponentInstance.backpressure `
834
+ counter .
834
835
2 . * Implicit backpressure* triggered when ` Task.needs_exclusive() ` is true and
835
836
the ` exclusive ` lock is already held.
836
837
3 . * Residual backpressure* triggered by explicit or implicit backpressure
@@ -842,7 +843,7 @@ backpressure is disabled. There are three sources of backpressure:
842
843
def enter (self ):
843
844
assert (self .thread is not None )
844
845
def has_backpressure ():
845
- return self .inst.backpressure or (self .needs_exclusive() and self .inst.exclusive)
846
+ return self .inst.backpressure > 0 or (self .needs_exclusive() and self .inst.exclusive)
846
847
if has_backpressure() or self .inst.num_waiting_to_enter > 0 :
847
848
self .inst.num_waiting_to_enter += 1
848
849
completed = self .thread.suspend_until(lambda : not has_backpressure(), cancellable = True )
@@ -3533,7 +3534,12 @@ def canon_context_set(t, i, task, v):
3533
3534
```
3534
3535
3535
3536
3536
- ### 🔀 ` canon backpressure.set `
3537
+ ### 🔀✕ ` canon backpressure.set `
3538
+
3539
+ > This built-in is deprecated in favor of ` backpressure.{inc,dec} ` and will be
3540
+ > removed once producer tools have transitioned. Producer tools should avoid
3541
+ > emitting calls to both ` set ` and ` inc ` /` dec ` since ` set ` will clobber the
3542
+ > counter.
3537
3543
3538
3544
For a canonical definition:
3539
3545
``` wat
@@ -3542,16 +3548,42 @@ For a canonical definition:
3542
3548
validation specifies:
3543
3549
* ` $f ` is given type ` (func (param $enabled i32)) `
3544
3550
3545
- Calling ` $f ` invokes the following function, which sets or clears the
3546
- ` ComponentInstance.backpressure ` flag . ` Task.enter ` waits for this flag to be
3547
- clear before allowing new tasks to start.
3551
+ Calling ` $f ` invokes the following function, which sets the ` backpressure `
3552
+ counter to ` 1 ` or ` 0 ` . ` Task.enter ` waits for ` backpressure ` to be ` 0 ` before
3553
+ allowing new tasks to start.
3548
3554
``` python
3549
3555
def canon_backpressure_set (task , flat_args ):
3550
- trap_if(task.opts.sync)
3551
3556
assert (len (flat_args) == 1 )
3552
- task.inst.backpressure = bool (flat_args[0 ])
3557
+ task.inst.backpressure = int (bool (flat_args[0 ]))
3558
+ return []
3559
+ ```
3560
+
3561
+ ### 🔀 ` canon backpressure.{inc,dec} `
3562
+
3563
+ For a canonical definition:
3564
+ ``` wat
3565
+ (canon backpressure.inc (core func $inc))
3566
+ (canon backpressure.dec (core func $dec))
3567
+ ```
3568
+ validation specifies:
3569
+ * ` $inc ` /` $dec ` are given type ` (func) `
3570
+
3571
+ Calling ` $inc ` or ` $dec ` invokes one of the following functions:
3572
+ ``` python
3573
+ def canon_backpressure_inc (task ):
3574
+ assert (0 <= task.inst.backpressure < 2 ** 16 )
3575
+ task.inst.backpressure += 1
3576
+ trap_if(task.inst.backpressure == 2 ** 16 )
3577
+ return []
3578
+
3579
+ def canon_backpressure_dec (task ):
3580
+ assert (0 <= task.inst.backpressure < 2 ** 16 )
3581
+ task.inst.backpressure -= 1
3582
+ trap_if(task.inst.backpressure < 0 )
3553
3583
return []
3554
3584
```
3585
+ ` Task.enter ` waits for ` backpressure ` to return to ` 0 ` before allowing new
3586
+ tasks to start, implementing [ backpressure] .
3555
3587
3556
3588
3557
3589
### 🔀 ` canon task.return `
0 commit comments