Skip to content

Commit

Permalink
xsync -> xcontext
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Mar 3, 2023
1 parent 6df14ff commit dcc1cf5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
37 changes: 37 additions & 0 deletions xcontext/mutex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package xcontext

type Mutex struct {
ch chan struct{}
}

func NewMutex() *Mutex {
return &Mutex{
ch: make(chan struct{}, 1),
}
}

func (m *Mutex) TryLock() bool {
select {
case m.ch <- struct{}{}:
return true
default:
return false
}
}

func (m *Mutex) Lock(ctx context.Context) error {
select {
case <-ctx.Done():
return fmt.Errorf("failed to acquire lock: %w", ctx.Err())
case m.ch <- struct{}{}:
return nil
}
}

func (m *Mutex) Unlock() {
select {
case <-m.ch:
default:
panic("xcontext.Mutex: Unlock before Lock")
}
}
37 changes: 0 additions & 37 deletions xsync/xsync.go

This file was deleted.

0 comments on commit dcc1cf5

Please sign in to comment.