@@ -43,7 +43,8 @@ being specified here.
4343 * [ ` canon resource.rep ` ] ( #canon-resourcerep )
4444 * [ ` canon context.get ` ] ( #-canon-contextget ) 🔀
4545 * [ ` canon context.set ` ] ( #-canon-contextset ) 🔀
46- * [ ` canon backpressure.set ` ] ( #-canon-backpressureset ) 🔀
46+ * [ ` canon backpressure.set ` ] ( #-canon-backpressureset ) 🔀✕
47+ * [ ` canon backpressure.{inc,dec} ` ] ( #-canon-backpressureincdec ) 🔀
4748 * [ ` canon task.return ` ] ( #-canon-taskreturn ) 🔀
4849 * [ ` canon task.cancel ` ] ( #-canon-taskcancel ) 🔀
4950 * [ ` canon yield ` ] ( #-canon-yield ) 🔀
@@ -279,15 +280,15 @@ class ComponentInstance:
279280 store: Store
280281 table: Table
281282 may_leave: bool
282- backpressure: bool
283+ backpressure: int
283284 exclusive: bool
284285 num_waiting_to_enter: int
285286
286287 def __init__ (self , store ):
287288 self .store = store
288289 self .table = Table()
289290 self .may_leave = True
290- self .backpressure = False
291+ self .backpressure = 0
291292 self .exclusive = False
292293 self .num_waiting_to_enter = 0
293294```
@@ -829,8 +830,8 @@ to avoid the need to otherwise-endlessly allocate guest memory for blocked
829830async calls until OOM. When backpressure is enabled, ` enter ` will block until
830831backpressure is disabled. There are three sources of backpressure:
831832 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 .
834835 2 . * Implicit backpressure* triggered when ` Task.needs_exclusive() ` is true and
835836 the ` exclusive ` lock is already held.
836837 3 . * Residual backpressure* triggered by explicit or implicit backpressure
@@ -842,7 +843,7 @@ backpressure is disabled. There are three sources of backpressure:
842843 def enter (self ):
843844 assert (self .thread is not None )
844845 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)
846847 if has_backpressure() or self .inst.num_waiting_to_enter > 0 :
847848 self .inst.num_waiting_to_enter += 1
848849 completed = self .thread.suspend_until(lambda : not has_backpressure(), cancellable = True )
@@ -3533,7 +3534,12 @@ def canon_context_set(t, i, task, v):
35333534```
35343535
35353536
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.
35373543
35383544For a canonical definition:
35393545``` wat
@@ -3542,16 +3548,42 @@ For a canonical definition:
35423548validation specifies:
35433549* ` $f ` is given type ` (func (param $enabled i32)) `
35443550
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.
35483554``` python
35493555def canon_backpressure_set (task , flat_args ):
3550- trap_if(task.opts.sync)
35513556 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 )
35533583 return []
35543584```
3585+ ` Task.enter ` waits for ` backpressure ` to return to ` 0 ` before allowing new
3586+ tasks to start, implementing [ backpressure] .
35553587
35563588
35573589### 🔀 ` canon task.return `
0 commit comments