Guest User

Untitled

a guest
Oct 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. package semaphore
  2.  
  3. type Semaphore struct {
  4. ch chan struct{}
  5. }
  6.  
  7. // Create semaphore
  8. func New(size int) *Semaphore {
  9. if size < 1 {
  10. size = 1
  11. }
  12. return &Semaphore{ch: make(chan struct{}, size)}
  13. }
  14.  
  15. func (s *Semaphore) Acquire() {
  16. s.ch <- struct{}{}
  17. }
  18.  
  19. func (s *Semaphore) Release() {
  20. <-s.ch
  21. }
  22.  
  23. func (s *Semaphore) Size() int {
  24. return cap(s.ch)
  25. }
  26.  
  27. func (s *Semaphore) Free() int {
  28. return cap(s.ch) - len(s.ch)
  29. }
Add Comment
Please, Sign In to add comment