w3c / web-locks

Cross-tab resource coordination API
https://w3c.github.io/web-locks/
Other
124 stars 16 forks source link

Support semaphores? #12

Open inexorabletash opened 7 years ago

inexorabletash commented 7 years ago

As sketched, the API would allow implementing mutexes and reader/writer locks..

It doesn't support semaphores, where a fixed number of holders can exist simultaneously. (use case: spawning up to N tasks of a given type; further requests must wait until a previous one completes)

We still want to avoid having a separate "register" step prior to the request. We could bolt this on by having a resource become a tuple (name, maxcount), or opening up the mode to be "shared"/"exclusive"/max count. That's still a bit odd since all the cooperators need to agree on what the count is (i.e. each tasks needs to know the global limit, not just some central coordinator)

I think we'd need to work through some examples here, but it at least seems plausible to add on.

inexorabletash commented 7 years ago

A few plausible APIs:

const flag = await requestFlag({name:"my_resource", count: 12}, "exclusive")
const flag = await requestFlag("my_resource", 12)

The first makes sense to me, even if it reads a bit odd. I don't know how the second composes with multiple resource requests.