@@ -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, in ` canon_backpressure_{inc,dec} `
834
+ (defined below) modify the ` ComponentInstance.backpressure ` 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 )
@@ -3523,7 +3524,12 @@ def canon_context_set(t, i, task, v):
3523
3524
```
3524
3525
3525
3526
3526
- ### 🔀 ` canon backpressure.set `
3527
+ ### 🔀✕ ` canon backpressure.set `
3528
+
3529
+ > This built-in is deprecated in favor of ` backpressure.{inc,dec} ` and will be
3530
+ > removed once producer tools have transitioned. Producer tools should avoid
3531
+ > emitting calls to both ` set ` and ` inc ` /` dec ` since ` set ` will clobber the
3532
+ > counter.
3527
3533
3528
3534
For a canonical definition:
3529
3535
``` wat
@@ -3532,16 +3538,42 @@ For a canonical definition:
3532
3538
validation specifies:
3533
3539
* ` $f ` is given type ` (func (param $enabled i32)) `
3534
3540
3535
- Calling ` $f ` invokes the following function, which sets or clears the
3536
- ` ComponentInstance.backpressure ` flag . ` Task.enter ` waits for this flag to be
3537
- clear before allowing new tasks to start.
3541
+ Calling ` $f ` invokes the following function, which sets the ` backpressure `
3542
+ counter to ` 1 ` or ` 0 ` . ` Task.enter ` waits for ` backpressure ` to be ` 0 ` before
3543
+ allowing new tasks to start.
3538
3544
``` python
3539
3545
def canon_backpressure_set (task , flat_args ):
3540
- trap_if(task.opts.sync)
3541
3546
assert (len (flat_args) == 1 )
3542
- task.inst.backpressure = bool (flat_args[0 ])
3547
+ task.inst.backpressure = int (bool (flat_args[0 ]))
3548
+ return []
3549
+ ```
3550
+
3551
+ ### 🔀 ` canon backpressure.{inc,dec} `
3552
+
3553
+ For a canonical definition:
3554
+ ``` wat
3555
+ (canon backpressure.inc (core func $inc))
3556
+ (canon backpressure.dec (core func $dec))
3557
+ ```
3558
+ validation specifies:
3559
+ * ` $inc ` /` $dec ` are given type ` (func) `
3560
+
3561
+ Calling ` $inc ` or ` $dec ` invokes one of the following functions:
3562
+ ``` python
3563
+ def canon_backpressure_inc (task ):
3564
+ assert (0 <= task.inst.backpressure < 2 ** 16 )
3565
+ task.inst.backpressure += 1
3566
+ trap_if(task.inst.backpressure == 2 ** 16 )
3567
+ return []
3568
+
3569
+ def canon_backpressure_dec (task ):
3570
+ assert (0 <= task.inst.backpressure < 2 ** 16 )
3571
+ task.inst.backpressure -= 1
3572
+ trap_if(task.inst.backpressure < 0 )
3543
3573
return []
3544
3574
```
3575
+ ` Task.enter ` waits for ` backpressure ` to return to ` 0 ` before allowing new
3576
+ tasks to start, implementing [ backpressure] .
3545
3577
3546
3578
3547
3579
### 🔀 ` canon task.return `
0 commit comments