github.com/argoproj/argo-events@v1.9.1/eventsources/common/naivewatcher/mutex.go (about)

     1  package naivewatcher
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // Mutex is the extended mutex
     8  type Mutex struct {
     9  	in      sync.RWMutex
    10  	locking bool
    11  }
    12  
    13  // Lock locks the mutex
    14  func (m *Mutex) Lock() {
    15  	m.in.Lock()
    16  	defer m.in.Unlock()
    17  
    18  	m.locking = true
    19  }
    20  
    21  // Unlock unlocks the mutex
    22  func (m *Mutex) Unlock() {
    23  	m.in.Lock()
    24  	defer m.in.Unlock()
    25  
    26  	m.locking = false
    27  }
    28  
    29  // TryLock returns true if it gets the lock, otherwise returns false
    30  func (m *Mutex) TryLock() bool {
    31  	m.in.Lock()
    32  	defer m.in.Unlock()
    33  
    34  	if m.locking {
    35  		return false
    36  	}
    37  	m.locking = true
    38  	return true
    39  }
    40  
    41  // IsLocked returs whether the mutex is locked
    42  func (m *Mutex) IsLocked() bool {
    43  	m.in.RLock()
    44  	defer m.in.RUnlock()
    45  
    46  	return m.locking
    47  }