From ddd6826b161e2bb7735b152c15c9ba9bab79aa29 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 23 Feb 2012 10:41:20 +0100 Subject: Remove deadlock in session store. --- server/src/session.cc | 41 ++++++++++++++++++++++++++++++++++++++--- server/src/session.h | 1 + 2 files changed, 39 insertions(+), 3 deletions(-) (limited to 'server') 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::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::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 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); -- cgit v1.2.3