Skip to content

lrita/gosync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A mutex implementation with TryLock

gosync.svg

safe guaranteed

Here is a general situations:

mutex.Lock()
A()                       mutex.Lock()
mutex.UnLock()            B()
                          mutex.UnLock()

When we synchronize using mutex, we should guarantee unlock() happens before lock() to make no data race.

A send on a channel happens before the corresponding receive from that channel completes, so we use a buffered channel to guarantee this.

interface

type Mutex interface {
	Lock()
	UnLock()
	// TryLock return true if it fetch mutex
	TryLock() bool
	// TryLockTimeout return true if it fetch mutex, return false if timeout
	TryLockTimeout(timeout time.Duration) bool
	// TryLockTimeout return true if it fetch mutex, return false if context done
	TryLockContext(ctx context.Context) bool
}

type MutexGroup interface {
	Lock(i interface{})
	UnLock(i interface{})
	// TryLock return true if it fetch mutex
	TryLock(i interface{}) bool
	// TryLockTimeout return true if it fetch mutex, return false if timeout
	TryLockTimeout(i interface{}, timeout time.Duration) bool
	// TryLockTimeout return true if it fetch mutex, return false if context done
	TryLockContext(i interface{}, ctx context.Context) bool
}

Releases

No releases published

Packages

No packages published

Languages