diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2012-02-23 10:41:20 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2012-02-23 10:41:20 +0100 |
commit | ddd6826b161e2bb7735b152c15c9ba9bab79aa29 (patch) | |
tree | aa4b28e90c0b5b3b9cee81c4a2535f20ffce25e6 | |
parent | bedfbd43af19a4b5798406d9b6736f2e79b3bebf (diff) |
Remove deadlock in session store.
-rw-r--r-- | server/src/session.cc | 41 | ||||
-rw-r--r-- | server/src/session.h | 1 |
2 files changed, 39 insertions, 3 deletions
diff --git a/server/src/session.cc b/server/src/session.cc index 234f004..6bb4440 100644 --- a/server/src/session.cc +++ b/server/src/session.cc @@ -82,10 +82,20 @@ std::string Session::id() void Session::lock() { mutex.lock(); + DEBUG(session, "lock() %p (%s)\n", this, sessionid.c_str()); +} + +bool Session::trylock() +{ + bool r = mutex.trylock(); + DEBUG(session, "trylock() %p (%s) == %s\n", + this, sessionid.c_str(), r?"true":"false"); + return r; } void Session::unlock() { + DEBUG(session, "unlock() %p (%s)\n", this, sessionid.c_str()); mutex.unlock(); } @@ -299,17 +309,41 @@ size_t Sessions::size() void Sessions::store() { + Session *session = NULL; + std::string sessionid; + do { + session = NULL; + { + MutexAutolock lock(mutex); + std::map<std::string, Session*>::iterator head = sessions.begin(); + if(head != sessions.end()) { + session = head->second; + sessionid = head->first; + if(session->trylock()) sessions.erase(sessionid); + else continue; + } + } + if(session != NULL) { + SessionSerialiser ser(env, Conf::session_path); + ser.save(session); + delete session; + } + } while(session != NULL); + + /* MutexAutolock lock(mutex); std::map<std::string, Session*>::iterator i = sessions.begin(); while(i != sessions.end()) { SessionSerialiser ser(env, Conf::session_path); - ser.save(i->second); - delete i->second; - sessions.erase(i); + Session *s = i->second; + s->lock(); + ser.save(s); + delete s; i++; } sessions.clear(); + */ } std::vector<std::string> Sessions::activeSessions() @@ -335,6 +369,7 @@ SessionAutounlock::SessionAutounlock(Session **s) SessionAutounlock::~SessionAutounlock() { + DEBUG(session, "SessionAutounlock(%p)\n", *session); if(*session) (*session)->unlock(); } diff --git a/server/src/session.h b/server/src/session.h index 85382f5..9ad84b5 100644 --- a/server/src/session.h +++ b/server/src/session.h @@ -53,6 +53,7 @@ public: std::string id(); void lock(); + bool trylock(); void unlock(); void commit() throw(LUAScript::Exception, Journal::Exception); |