You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

semaphores.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2018 Shivaram Lingamneni
  2. package irc
  3. import (
  4. "log"
  5. "runtime"
  6. "runtime/debug"
  7. )
  8. // See #237 for context. Operations that might allocate large amounts of temporary
  9. // garbage, or temporarily tie up some other resource, may cause thrashing unless
  10. // their concurrency is artificially restricted. We use `chan bool` as a
  11. // (regrettably, unary-encoded) counting semaphore to enforce these restrictions.
  12. const (
  13. // this is a tradeoff between exploiting CPU-level parallelism (higher values better)
  14. // and not thrashing the allocator (lower values better). really this is all just
  15. // guesswork. oragono *can* make use of cores beyond this limit --- just not for
  16. // the protected operations.
  17. MaxServerSemaphoreCapacity = 32
  18. )
  19. // Semaphore is a counting semaphore. Note that a capacity of n requires O(n) storage.
  20. type Semaphore (chan bool)
  21. // ServerSemaphores includes a named Semaphore corresponding to each concurrency-limited
  22. // sever operation.
  23. type ServerSemaphores struct {
  24. // each distinct operation MUST have its own semaphore;
  25. // methods that acquire a semaphore MUST NOT call methods that acquire another
  26. ClientDestroy Semaphore
  27. }
  28. // Initialize initializes a set of server semaphores.
  29. func (serversem *ServerSemaphores) Initialize() {
  30. capacity := runtime.NumCPU()
  31. if capacity > MaxServerSemaphoreCapacity {
  32. capacity = MaxServerSemaphoreCapacity
  33. }
  34. serversem.ClientDestroy.Initialize(capacity)
  35. return
  36. }
  37. // Initialize initializes a semaphore to a given capacity.
  38. func (semaphore *Semaphore) Initialize(capacity int) {
  39. *semaphore = make(chan bool, capacity)
  40. for i := 0; i < capacity; i++ {
  41. (*semaphore) <- true
  42. }
  43. }
  44. // Acquire acquires a semaphore, blocking if necessary.
  45. func (semaphore *Semaphore) Acquire() {
  46. <-(*semaphore)
  47. }
  48. // TryAcquire tries to acquire a semaphore, returning whether the acquire was
  49. // successful. It never blocks.
  50. func (semaphore *Semaphore) TryAcquire() (acquired bool) {
  51. select {
  52. case <-(*semaphore):
  53. return true
  54. default:
  55. return false
  56. }
  57. }
  58. // Release releases a semaphore. It never blocks. (This is not a license
  59. // to program spurious releases.)
  60. func (semaphore *Semaphore) Release() {
  61. select {
  62. case (*semaphore) <- true:
  63. // good
  64. default:
  65. // spurious release
  66. log.Printf("spurious semaphore release (full to capacity %d)", cap(*semaphore))
  67. debug.PrintStack()
  68. }
  69. }