This commit is contained in:
Iwasaki Yudai 2017-02-26 07:37:07 +09:00
parent 54403dd678
commit a6133f34b7
54 changed files with 2140 additions and 1334 deletions

View file

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2015 Iwasaki Yudai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -1,53 +0,0 @@
# Unblocking Mutex
This simple package provides unblocking mutexes for those who don't want to write many `select` clauses or get confused by numerous channels.
## Usage Example
```go
package main
import (
"fmt"
"github.com/yudai/umutex"
)
func main() {
// Create mutex
mutex := umutex.New()
// First time, try should succeed
if mutex.TryLock() {
fmt.Println("SUCCESS")
} else {
fmt.Println("FAILURE")
}
// Second time, try should fail as it's locked
if mutex.TryLock() {
fmt.Println("SUCCESS")
} else {
fmt.Println("FAILURE")
}
// Unclock mutex
mutex.Unlock()
// Third time, try should succeed again
if mutex.TryLock() {
fmt.Println("SUCCESS")
} else {
fmt.Println("FAILURE")
}
}
```
The output is;
```sh
SUCCESS
FAILURE
SUCCESS
```
`ForceLock()` method is also availale for normal blocking lock.

View file

@ -1,38 +0,0 @@
// Package umutex provides unblocking mutex
package umutex
// UnblockingMutex represents an unblocking mutex.
type UnblockingMutex struct {
// Raw channel
C chan bool
}
// New returnes a new unblocking mutex instance.
func New() *UnblockingMutex {
return &UnblockingMutex{
C: make(chan bool, 1),
}
}
// TryLock tries to lock the mutex.
// When the mutex is free at the time, the function locks the mutex and return
// true. Otherwise false will be returned. In the both cases, this function
// doens't block and return the result immediately.
func (m UnblockingMutex) TryLock() (result bool) {
select {
case m.C <- true:
return true
default:
return false
}
}
// Unlock unclocks the mutex.
func (m UnblockingMutex) Unlock() {
<-m.C
}
// ForceLock surely locks the mutex, however, this function blocks when the mutex is locked at the time.
func (m UnblockingMutex) ForceLock() {
m.C <- false
}