Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions homeworks/13_sync_primitives/homework.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ func (m *RWMutex) Lock() {
m.mutex.Lock()
defer m.mutex.Unlock()

m.underLock()
m.writersWaiting++
for m.writerActive || m.readerCount != 0 {
m.writerCond.Wait()
}
m.writerActive = true
m.writersWaiting--
}

func (m *RWMutex) Unlock() {
Expand All @@ -36,7 +41,10 @@ func (m *RWMutex) RLock() {
m.mutex.Lock()
defer m.mutex.Unlock()

m.underRLock()
for m.writerActive || m.writersWaiting > 0 {
m.writerCond.Wait()
}
m.readerCount++
}

func (m *RWMutex) RUnlock() {
Expand All @@ -49,41 +57,25 @@ func (m *RWMutex) RUnlock() {
}

func (m *RWMutex) TryLock() bool {
ok := m.mutex.TryLock()
if !ok {
return false
}
m.mutex.Lock()
defer m.mutex.Unlock()

m.underLock()
if m.writerActive || m.readerCount > 0 {
return false
}

m.writerActive = true
return true
}

func (m *RWMutex) TryRLock() bool {
ok := m.mutex.TryLock()
if !ok {
return false
}
m.mutex.Lock()
defer m.mutex.Unlock()

m.underRLock()

return true
}

func (m *RWMutex) underLock() {
m.writersWaiting++
for m.writerActive || m.readerCount != 0 {
m.writerCond.Wait()
if m.writerActive || m.writersWaiting > 0 {
return false
}
m.writerActive = true
m.writersWaiting--
}

func (m *RWMutex) underRLock() {
for m.writerActive || m.writersWaiting > 0 {
m.writerCond.Wait()
}
m.readerCount++
return true
}
36 changes: 36 additions & 0 deletions homeworks/13_sync_primitives/homework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,39 @@ func TestRWMutexWithWriterPriority(t *testing.T) {
assert.True(t, mutualExlusionWithWriter.Load())
assert.Equal(t, int32(1), readersCount.Load())
}

func TestTryLockFailsWhenWriterActive(t *testing.T) {
mutex := NewRWMutex()
mutex.Lock() // writer

ok := mutex.TryLock()
assert.False(t, ok)
}

func TestTryLockFailsWhenReadersActive(t *testing.T) {
mutex := NewRWMutex()
mutex.RLock() // reader

ok := mutex.TryLock()
assert.False(t, ok)
}

func TestTryLockSucceedsWhenFree(t *testing.T) {
mutex := NewRWMutex()

ok := mutex.TryLock()
assert.True(t, ok)

var entered atomic.Bool
entered.Store(false)

go func() {
mutex.Lock()
entered.Store(true)
}()

time.Sleep(100 * time.Millisecond)
assert.False(t, entered.Load())

mutex.Unlock()
}