feat: basic RwMutex implementation

This commit is contained in:
2025-09-26 08:35:21 +07:00
parent 147e25b116
commit 1ce17bff21
3 changed files with 106 additions and 0 deletions

31
mutex.go Normal file
View File

@@ -0,0 +1,31 @@
package custom_rwmutex
import "sync"
type CustomRwMutex struct {
mu *sync.RWMutex
}
func NewCustomRwMutex() *CustomRwMutex {
return &CustomRwMutex{
mu: &sync.RWMutex{},
}
}
func (rwMu *CustomRwMutex) WriteHandler(fn func() error) error {
rwMu.mu.Lock()
defer rwMu.mu.Unlock()
if err := fn(); err != nil {
return err
}
return nil
}
func (rwMu *CustomRwMutex) ReadHandler(fn func() error) error {
rwMu.mu.RLock()
defer rwMu.mu.RUnlock()
if err := fn(); err != nil {
return err
}
return nil
}