sync.go 399 B

12345678910111213141516171819202122232425262728293031
  1. package common
  2. import "sync"
  3. type Condition interface {
  4. Wait()
  5. Notify()
  6. NotifyAll()
  7. }
  8. type condition struct {
  9. sync.Mutex
  10. c *sync.Cond
  11. }
  12. func NewCond() Condition {
  13. c := new(condition)
  14. c.c = sync.NewCond(c)
  15. return c
  16. }
  17. func (c *condition) Wait() {
  18. c.Lock()
  19. defer c.Unlock()
  20. c.c.Wait()
  21. }
  22. func (c *condition) Notify() {
  23. c.c.Signal()
  24. }
  25. func (c *condition) NotifyAll() {
  26. c.c.Broadcast()
  27. }