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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // NewServerSemaphores creates a new ServerSemaphores.
  29. func NewServerSemaphores() (result *ServerSemaphores) {
  30. capacity := runtime.NumCPU()
  31. if capacity > MaxServerSemaphoreCapacity {
  32. capacity = MaxServerSemaphoreCapacity
  33. }
  34. result = new(ServerSemaphores)
  35. result.ClientDestroy.Initialize(capacity)
  36. return
  37. }
  38. // Initialize initializes a semaphore to a given capacity.
  39. func (semaphore *Semaphore) Initialize(capacity int) {
  40. *semaphore = make(chan bool, capacity)
  41. for i := 0; i < capacity; i++ {
  42. (*semaphore) <- true
  43. }
  44. }
  45. // Acquire acquires a semaphore, blocking if necessary.
  46. func (semaphore *Semaphore) Acquire() {
  47. <-(*semaphore)
  48. }
  49. // TryAcquire tries to acquire a semaphore, returning whether the acquire was
  50. // successful. It never blocks.
  51. func (semaphore *Semaphore) TryAcquire() (acquired bool) {
  52. select {
  53. case <-(*semaphore):
  54. return true
  55. default:
  56. return false
  57. }
  58. }
  59. // Release releases a semaphore. It never blocks. (This is not a license
  60. // to program spurious releases.)
  61. func (semaphore *Semaphore) Release() {
  62. select {
  63. case (*semaphore) <- true:
  64. // good
  65. default:
  66. // spurious release
  67. log.Printf("spurious semaphore release (full to capacity %d)", cap(*semaphore))
  68. debug.PrintStack()
  69. }
  70. }