From 8d6402323ff72e4cad31704cf1e9128d182f15c6 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Sun, 11 Feb 2018 02:01:20 +0100 Subject: [PATCH] misc: small fix or general refactoring i did not bother commenting --- session/module.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/session/module.go b/session/module.go index 2bc0ad50..2b656744 100644 --- a/session/module.go +++ b/session/module.go @@ -21,10 +21,10 @@ type Module interface { } type SessionModule struct { - Name string `json:"name"` - Session *Session `json:"-"` - Started bool `json:"started"` - StatusLock *sync.Mutex `json:"-"` + Name string `json:"name"` + Session *Session `json:"-"` + Started bool `json:"started"` + StatusLock *sync.RWMutex `json:"-"` handlers []ModuleHandler params map[string]*ModuleParam @@ -35,7 +35,7 @@ func NewSessionModule(name string, s *Session) SessionModule { Name: name, Session: s, Started: false, - StatusLock: &sync.Mutex{}, + StatusLock: &sync.RWMutex{}, handlers: make([]ModuleHandler, 0), params: make(map[string]*ModuleParam), @@ -117,22 +117,23 @@ func (m *SessionModule) AddParam(p *ModuleParam) *ModuleParam { } func (m *SessionModule) Running() bool { - m.StatusLock.Lock() - defer m.StatusLock.Unlock() + m.StatusLock.RLock() + defer m.StatusLock.RUnlock() return m.Started } func (m *SessionModule) SetRunning(running bool, cb func()) error { - m.StatusLock.Lock() - defer m.StatusLock.Unlock() - - if running && m.Started == true { - return ErrAlreadyStarted - } else if running == false && m.Started == false { - return ErrAlreadyStopped + if running == m.Running() { + if m.Started { + return ErrAlreadyStarted + } else { + return ErrAlreadyStopped + } } + m.StatusLock.Lock() m.Started = running + m.StatusLock.Unlock() if *m.Session.Options.Debug == true { if running {