kunt

golang IRC bot
git clone git://git.2f30.org/kunt.git
Log | Files | Refs | LICENSE

commit 63d46d46917273ae2ec5c3f2bbe415c207843579
parent ea3b094dbc60551708a3824c754ad495a733e269
Author: sin <sin@2f30.org>
Date:   Tue Apr 30 13:55:21 +0100

use embedded locks in mapfs

Diffstat:
src/mapfs/mapfs.go | 50+++++++++++++++++++++++++-------------------------
1 file changed, 25 insertions(+), 25 deletions(-)
diff --git a/src/mapfs/mapfs.go b/src/mapfs/mapfs.go @@ -18,10 +18,10 @@ type Mapfs struct { path string prefix string dict map[int]mapfsVal - lock sync.Mutex cacheOnly bool encrypt bool cipher *blowfish.Cipher + sync.Mutex } type mapfsVal struct { @@ -34,8 +34,8 @@ type MapIter func() (key int, data []byte, ok bool) func MakeMapIter(m *Mapfs) MapIter { i := 0 return func() (key int, data []byte, ok bool) { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() for i < len(m.dict) { v, ok := m.dict[i] if ok { @@ -81,8 +81,8 @@ func NewEncryptedMapfs(name string, path string, prefix string, key string) *Map // Sync dirty entries to disk func (m *Mapfs) Sync() error { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() if m.cacheOnly { return nil } @@ -106,15 +106,15 @@ func (m *Mapfs) Sync() error { } func (m *Mapfs) CacheOnly(flag bool) { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() m.cacheOnly = flag } // Print map func (m *Mapfs) String() string { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() s := fmt.Sprintf("*** %s MAP DUMP ***\n", m.name) for k, v := range m.dict { dirty := "" @@ -131,8 +131,8 @@ func (m *Mapfs) String() string { // Mark all entries as synced but do not actually write them out to disk func (m *Mapfs) virtSync() { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() for k, v := range m.dict { m.dict[k] = mapfsVal{false, v.data} } @@ -177,8 +177,8 @@ func (m *Mapfs) Load() error { // Count how many instances of `buf' exist in the map func (m *Mapfs) CountMatches(buf []byte) int { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() i := 0 raw := buf if m.encrypt { @@ -194,8 +194,8 @@ func (m *Mapfs) CountMatches(buf []byte) int { } func (m *Mapfs) Put(key int, buf []byte) error { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() if key < 0 { return fmt.Errorf("Invalid key: %d", key) } @@ -214,8 +214,8 @@ func (m *Mapfs) Put(key int, buf []byte) error { // Append buf to the map func (m *Mapfs) Append(buf []byte) (int, error) { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() if m.encrypt { ct := m.encryptBuf(buf) key := len(m.dict) @@ -229,8 +229,8 @@ func (m *Mapfs) Append(buf []byte) (int, error) { // Return the raw data based on the key func (m *Mapfs) Get(key int) ([]byte, error) { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() if len(m.dict) == 0 { return nil, fmt.Errorf("Empty map, can't fetch entry") } @@ -246,8 +246,8 @@ func (m *Mapfs) Get(key int) ([]byte, error) { } func (m *Mapfs) Rand() ([]byte, int) { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() idx := rand.Intn(len(m.dict)) i := 0 for k, _ := range m.dict { @@ -265,8 +265,8 @@ func (m *Mapfs) Rand() ([]byte, int) { } func (m *Mapfs) Empty() bool { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() if len(m.dict) == 0 { return true } @@ -274,8 +274,8 @@ func (m *Mapfs) Empty() bool { } func (m *Mapfs) Len() int { - m.lock.Lock() - defer m.lock.Unlock() + m.Lock() + defer m.Unlock() return len(m.dict) }