Commit graph

8 commits

Author SHA1 Message Date
徐志强
43a5402ce7 semaphore: unblock waiters when the front waiter cancels
When `Release`, if the remaining tokens are not enough for the front waiter, no waiters will be notified.

So if the canceled waiter is the front one, it should try to notify following waiters if there are remaining tokens.

I found this bug when implementing a cancelable rwmutex based on semaphore:

https://github.com/zhiqiangxu/util/blob/master/mutex/crwmutex.go

This bug can be verified by this test:

https://github.com/zhiqiangxu/util/blob/master/mutex/crwmutex_test.go#L43

Change-Id: Id8564976bd375a82c4fbc6cb08b0bb83118a346c
GitHub-Last-Rev: 29b6ff26bf779d23239cfe64a395378a9e41d1fc
GitHub-Pull-Request: golang/sync#10
Reviewed-on: https://go-review.googlesource.com/c/sync/+/223418
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2020-03-17 01:50:54 +00:00
Jean de Klerk
112230192c semaphore: better error message when releasing more than held
Change-Id: Ibe69007b2d7565a33d9e64ae51142478f495509a
Reviewed-on: https://go-review.googlesource.com/c/sync/+/173377
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-04-23 02:48:10 +00:00
Jean de Klerk
37e7f081c4 semaphore: add more Acquire documentation
Change-Id: Ie64db438145a307ec8aef8fd412d445a2fe74d40
Reviewed-on: https://go-review.googlesource.com/c/136995
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-12-21 19:32:16 +00:00
Jean de Klerk
42b317875d all: remove x/net/context in favour of context
Change-Id: Idbfe40e7cb9f99ca3dc7d4c214de6b9e028eefb7
Reviewed-on: https://go-review.googlesource.com/c/148277
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-08 01:04:31 +00:00
Bryan C. Mills
fd80eb99c8 semaphore: add worker-pool example
I've commented several times in various forums that basically every
time I've seen the “worker goroutine” pattern in Go, there has turned
out to be a cleaner implementation using semaphores.

This change adds a simple such example. (For more complex usage, I
would generally pair the semaphore with an errgroup.Group.)

Change-Id: Ibf69ee761d14ba59c1acc6a2d595b4fcf0d8f6d6
Reviewed-on: https://go-review.googlesource.com/75170
Reviewed-by: Ross Light <light@google.com>
2017-11-01 21:47:15 +00:00
Jonathan Amsterdam
f52d1811a6 semaphore: make test time independent of GOMAXPROCS
TestWeighted's runtime was roughly proportional to the number of CPUs,
so it ran a long time on PPCs (160 CPUs).

Make the number of loops in the test an inverse function of the number
of CPUs, to keep the test short.

Change-Id: Id853dbb5e5e2f9fb95966d19ef0c511e3f8080e0
Reviewed-on: https://go-review.googlesource.com/43632
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-05-17 21:12:32 +00:00
Jonathan Amsterdam
de49d9dcd2 semaphore: avoid loop variable capture by closure
Change-Id: I21c5dc48ffe43aa27c50a42f9c9a01ac1f0c747c
Reviewed-on: https://go-review.googlesource.com/40991
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Glenn Lewis <gmlewis@google.com>
2017-04-18 21:08:38 +00:00
Jonathan Amsterdam
5a06fca2c3 semaphore: add a weighted semaphore implementation
This package provides a weighted semaphore that is context-aware.

The code is derived from a similar package inside Google.

Change-Id: Id1dad96d79e8ccfd289e4299e8265aa5bdad3a5b
Reviewed-on: https://go-review.googlesource.com/38298
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-03-17 23:16:01 +00:00