Skip to content

Commit

Permalink
Use system wide file locks to synchronize log files.
Browse files Browse the repository at this point in the history
The global semaphore has been observed to not be released on unix like
platforms when the process exits. So, we convert to file locks, which
will be released on process exit on unix like platforms. Unfortunately,
Win32 file locks and POSIX file locks have slightly different semantics
in that Win32 locks are mandatory and POSIX locks are advisory, which
can result in slightly different behavior. OTOH, this should fix the
client deadlocking until the system is restarted, so this should be an
overall win.
  • Loading branch information
Hoikas committed Jul 20, 2024
1 parent 44063ab commit b331c67
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Sources/Plasma/CoreLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(CoreLib_SOURCES
hsExceptions.cpp
hsExceptionStack.cpp
hsFastMath.cpp
hsFILELock.cpp
hsGeometry3.cpp
hsMatrix33.cpp
hsMatrix44.cpp
Expand Down Expand Up @@ -43,6 +44,7 @@ set(CoreLib_HEADERS
hsExceptions.h
hsExceptionStack.h
hsFastMath.h
hsFILELock.h
hsGeometry3.h
hsLockGuard.h
hsMatrix44.h
Expand Down
14 changes: 4 additions & 10 deletions Sources/Plasma/PubUtilLib/plStatusLog/plStatusLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include "plStatusLog.h"
#include "plEncryptLogLine.h"

#include "hsFILELock.h"
#include "plProduct.h"
#include "hsThread.h"
#include "hsTimer.h"
Expand Down Expand Up @@ -259,20 +260,17 @@ bool plStatusLogMgr::DumpLogs( const plFileName &newFolderName )
uint32_t plStatusLog::fLoggingOff = false;

plStatusLog::plStatusLog( uint8_t numDisplayLines, const plFileName &filename, uint32_t flags )
: fFileHandle(), fSema(), fSize(), fForceLog(), fMaxNumLines(numDisplayLines),
: fFileHandle(), fSize(), fForceLog(), fMaxNumLines(numDisplayLines),
fDisplayPointer()
{
if (filename.IsValid())
{
fFilename = filename;
fSema = new hsGlobalSemaphore(1, fFilename.AsString().c_str());
}
else
{
fFilename = "";
flags |= kDontWriteFile;

fSema = new hsGlobalSemaphore(1);
}

fOrigFlags = fFlags = flags;
Expand Down Expand Up @@ -366,9 +364,6 @@ void plStatusLog::IFini()
if (fBack != nullptr || fNext != nullptr)
IUnlink();

if (fSema)
delete fSema;

delete [] fLines;
delete [] fColors;
}
Expand Down Expand Up @@ -431,7 +426,8 @@ bool plStatusLog::IAddLine(const ST::string& line, uint32_t color)
return true;

/// Scroll pointers up
fSema->Wait();
hsFILELock fileLock(fFileHandle);
hsLockGuard(fileLock);

if (fMaxNumLines > 0)
{
Expand All @@ -448,8 +444,6 @@ bool plStatusLog::IAddLine(const ST::string& line, uint32_t color)

bool ret = IPrintLineToFile(line);

fSema->Signal();

return ret;
}

Expand Down
2 changes: 0 additions & 2 deletions Sources/Plasma/PubUtilLib/plStatusLog/plStatusLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#define _plStatusLog_h

#include "HeadSpin.h"
#include "hsThread.h"
#include "plFileSystem.h"
#include "plLoggable.h"

Expand Down Expand Up @@ -88,7 +87,6 @@ class plStatusLog : public plLog
plFileName fFilename;
ST::string* fLines;
uint32_t* fColors;
hsGlobalSemaphore* fSema;
FILE* fFileHandle;
uint32_t fSize;
bool fForceLog;
Expand Down

0 comments on commit b331c67

Please sign in to comment.