Skip to content

Commit

Permalink
libcore|Log: Multithreading robustness
Browse files Browse the repository at this point in the history
Theoretically, a race condition might occur in theLogs(), if it were
called from multiple threads for the first time.
  • Loading branch information
skyjake committed Nov 2, 2014
1 parent 2af910d commit 209a3cb
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions doomsday/libcore/src/core/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@ namespace internal {
* The logs table is lockable so that multiple threads can access their logs at
* the same time.
*/
class Logs : public QMap<QThread *, Log *>, public Lockable
class Logs : public std::map<QThread *, Log *>, public Lockable
{
public:
Logs() {}
~Logs() {
DENG2_GUARD(this);
// The logs are owned by the logs table.
foreach(Log *log, values()) {
delete log;
}
for(auto log : *this) delete log.second;
}
};

Expand Down Expand Up @@ -685,6 +683,8 @@ LogEntry &Log::enter(duint32 metadata, String const &format, LogEntry::Args argu

static internal::Logs &theLogs()
{
static Lockable lock;
DENG2_GUARD(lock);
if(!logsPtr.get()) logsPtr.reset(new internal::Logs);
return *logsPtr;
}
Expand All @@ -697,17 +697,17 @@ Log &Log::threadLog()

// Each thread has its own log.
QThread *thread = QThread::currentThread();
internal::Logs::const_iterator found = logs.constFind(thread);
if(found == logs.constEnd())
auto found = logs.find(thread);
if(found == logs.end())
{
// Create a new log.
Log* theLog = new Log;
logs.insert(thread, theLog);
logs[thread] = theLog;
return *theLog;
}
else
{
return *found.value();
return *found->second;
}
}

Expand All @@ -718,11 +718,11 @@ void Log::disposeThreadLog()
DENG2_GUARD(logs);

QThread *thread = QThread::currentThread();
internal::Logs::iterator found = logs.find(thread);
auto found = logs.find(thread);
if(found != logs.end())
{
delete found.value();
logs.remove(found.key());
delete found->second;
logs.erase(found);
}
}

Expand Down

0 comments on commit 209a3cb

Please sign in to comment.