|
37 | 37 | // |
38 | 38 | #include <stdafx.h> |
39 | 39 | #include <htmlhelp.h> |
40 | | -#include <boost/interprocess/sync/named_mutex.hpp> |
| 40 | +#include <boost/interprocess/sync/file_lock.hpp> |
| 41 | +#include <boost/interprocess/sync/scoped_lock.hpp> |
41 | 42 | #include <boost/interprocess/exceptions.hpp> |
42 | 43 |
|
| 44 | +#include <fstream> |
| 45 | + |
43 | 46 | namespace bip = boost::interprocess; |
44 | 47 |
|
45 | 48 | #include "DeepSkyStacker.h" |
@@ -1135,9 +1138,31 @@ int main(int argc, char* argv[]) |
1135 | 1138 | DeepSkyStacker mainWindow; |
1136 | 1139 |
|
1137 | 1140 | ZTRACE_RUNTIME("Checking Mutex"); |
1138 | | - bip::named_mutex dssMutex{ bip::open_or_create, "DeepSkyStacker.Mutex.UniqueID.12354687" }; |
1139 | | - bip::scoped_lock<bip::named_mutex> lk(dssMutex, bip::defer_lock); |
1140 | | - const bool firstInstance{ lk.try_lock() }; |
| 1141 | + // |
| 1142 | + // Get the name of the writable local applicaiton data directory |
| 1143 | + // and create the directories if necessary |
| 1144 | + // |
| 1145 | + |
| 1146 | + QString mutexFileName{ QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) }; |
| 1147 | + fs::path file{ mutexFileName.toStdU16String() }; |
| 1148 | + create_directories(file); |
| 1149 | + |
| 1150 | + // |
| 1151 | + // Append the file name to the directory name |
| 1152 | + // |
| 1153 | + mutexFileName += "/DeepSkyStacker.Interprocess.Mutex"; |
| 1154 | + |
| 1155 | + // |
| 1156 | + // Create the file it doesn't exist. It is intentionally never deleted. |
| 1157 | + // |
| 1158 | + auto newFile = std::ofstream(mutexFileName.toUtf8().constData()); |
| 1159 | + |
| 1160 | + // |
| 1161 | + // Use a boost::interprocess::file_lock as unlike a named_mutex, the OS removes the lock in the case of abnormal termination |
| 1162 | + // |
| 1163 | + bip::file_lock dssMutex{ mutexFileName.toUtf8().constData() }; |
| 1164 | + bip::scoped_lock<bip::file_lock> lock(dssMutex, bip::try_to_lock); |
| 1165 | + const bool firstInstance{ lock.owns() }; |
1141 | 1166 | ZTRACE_RUNTIME(" firstInstance: %s", firstInstance ? "true" : "false"); |
1142 | 1167 |
|
1143 | 1168 | if (firstInstance) |
|
0 commit comments