Sql: Sql/SqlSession::PerThread

git-svn-id: svn://ultimatepp.org/upp/trunk@4631 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-02-28 09:38:17 +00:00
parent 34f5598ec3
commit 42f9888fea
2 changed files with 44 additions and 13 deletions

View file

@ -122,9 +122,23 @@ void SqlSession::ClearError()
StaticMutex sDefs;
static SqlSession *sGlobalSession;
static SqlSession *sGlobalSessionR;
#ifdef _MULTITHREADED
static bool sPerThread;
thread__ SqlSession *sThreadSession;
thread__ SqlSession *sThreadSessionR;
void Sql::PerThread(bool b)
{
sPerThread = b;
}
void SqlSession::PerThread(bool b)
{
sPerThread = b;
}
#endif
void Sql::operator=(SqlSession& s)
@ -132,16 +146,18 @@ void Sql::operator=(SqlSession& s)
Mutex::Lock __(sDefs);
if(this == &AppCursor()) {
#ifdef _MULTITHREADED
sThreadSession = &s;
if(!sGlobalSession)
if(sPerThread)
sThreadSession = &s;
else
#endif
sGlobalSession = &s;
return;
}
if(this == &AppCursorR()) {
#ifdef _MULTITHREADED
sThreadSessionR = &s;
if(!sGlobalSessionR)
if(sPerThread)
sThreadSessionR = &s;
else
#endif
sGlobalSessionR = &s;
return;
@ -152,11 +168,14 @@ void Sql::operator=(SqlSession& s)
Sql& AppCursor()
{
#ifdef _MULTITHREADED
if(sThreadSession)
return sThreadSession->GetSessionSql();
if(sPerThread) {
if(sThreadSession)
return sThreadSession->GetSessionSql();
}
else
#endif
if(sGlobalSession)
return sGlobalSession->GetSessionSql();
if(sGlobalSession)
return sGlobalSession->GetSessionSql();
static Sql *empty;
ONCELOCK {
static Sql0 h;
@ -168,14 +187,20 @@ Sql& AppCursor()
Sql& AppCursorR()
{
#ifdef _MULTITHREADED
if(sThreadSessionR)
return sThreadSessionR->GetSessionSqlR();
if(sPerThread) {
if(sThreadSessionR)
return sThreadSessionR->GetSessionSqlR();
}
else
#endif
if(sGlobalSessionR)
return sGlobalSessionR->GetSessionSqlR();
#ifdef _MULTITHREADED
if(sThreadSession)
return sThreadSession->GetSessionSqlR();
if(sPerThread) {
if(sThreadSession)
return sThreadSession->GetSessionSqlR();
}
else
#endif
if(sGlobalSession)
return sGlobalSession->GetSessionSqlR();

View file

@ -250,6 +250,9 @@ public:
~Sql();
void operator=(SqlSession& s); // this only works with SQL and SQLR...
#ifdef _MULTITHREADED
static void PerThread(bool b = true); // Activates thread local SQL/SQLR
#endif
private:
void operator=(const Sql&);
@ -394,9 +397,12 @@ public:
Callback1<const SqlSession&> WhenDatabaseActivity;
#ifdef _MULTITHREADED
static void PerThread(bool b = true); // Activates thread local SQL/SQLR
#endif
SqlSession();
virtual ~SqlSession();
};