1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
7 #include <boost/program_options/detail/config_file.hpp>
8 #include <boost/program_options/parsers.hpp>
9 #include <boost/filesystem.hpp>
10 #include <boost/filesystem/fstream.hpp>
11 #include <boost/interprocess/sync/interprocess_mutex.hpp>
12 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
13 #include <boost/foreach.hpp>
16 using namespace boost;
18 map<string, string> mapArgs;
19 map<string, vector<string> > mapMultiArgs;
21 bool fPrintToConsole = false;
22 bool fPrintToDebugger = false;
23 char pszSetDataDir[MAX_PATH] = "";
24 bool fRequestShutdown = false;
25 bool fShutdown = false;
28 bool fCommandLine = false;
29 string strMiscWarning;
30 bool fTestNet = false;
31 bool fNoListen = false;
32 bool fLogTimestamps = false;
37 // Workaround for "multiple definition of `_tls_used'"
38 // http://svn.boost.org/trac/boost/ticket/4258
39 extern "C" void tss_cleanup_implemented() { }
45 // Init openssl library multithreading support
46 static boost::interprocess::interprocess_mutex** ppmutexOpenSSL;
47 void locking_callback(int mode, int i, const char* file, int line)
49 if (mode & CRYPTO_LOCK)
50 ppmutexOpenSSL[i]->lock();
52 ppmutexOpenSSL[i]->unlock();
61 // Init openssl library multithreading support
62 ppmutexOpenSSL = (boost::interprocess::interprocess_mutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(boost::interprocess::interprocess_mutex*));
63 for (int i = 0; i < CRYPTO_num_locks(); i++)
64 ppmutexOpenSSL[i] = new boost::interprocess::interprocess_mutex();
65 CRYPTO_set_locking_callback(locking_callback);
68 // Seed random number generator with screen scrape and other hardware sources
72 // Seed random number generator with performance counter
77 // Shutdown openssl library multithreading support
78 CRYPTO_set_locking_callback(NULL);
79 for (int i = 0; i < CRYPTO_num_locks(); i++)
80 delete ppmutexOpenSSL[i];
81 OPENSSL_free(ppmutexOpenSSL);
95 // Seed with CPU performance counter
96 int64 nCounter = GetPerformanceCounter();
97 RAND_add(&nCounter, sizeof(nCounter), 1.5);
98 memset(&nCounter, 0, sizeof(nCounter));
101 void RandAddSeedPerfmon()
105 // This can take up to 2 seconds, so only do it every 10 minutes
106 static int64 nLastPerfmon;
107 if (GetTime() < nLastPerfmon + 10 * 60)
109 nLastPerfmon = GetTime();
112 // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
113 // Seed with the entire set of perfmon data
114 unsigned char pdata[250000];
115 memset(pdata, 0, sizeof(pdata));
116 unsigned long nSize = sizeof(pdata);
117 long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
118 RegCloseKey(HKEY_PERFORMANCE_DATA);
119 if (ret == ERROR_SUCCESS)
121 RAND_add(pdata, nSize, nSize/100.0);
122 memset(pdata, 0, nSize);
123 printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize);
128 uint64 GetRand(uint64 nMax)
133 // The range of the random source must be a multiple of the modulus
134 // to give every possible output value an equal possibility
135 uint64 nRange = (UINT64_MAX / nMax) * nMax;
138 RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
139 while (nRand >= nRange);
140 return (nRand % nMax);
143 int GetRandInt(int nMax)
145 return GetRand(nMax);
158 inline int OutputDebugStringF(const char* pszFormat, ...)
165 va_start(arg_ptr, pszFormat);
166 ret = vprintf(pszFormat, arg_ptr);
171 // print to debug.log
172 static FILE* fileout = NULL;
176 char pszFile[MAX_PATH+100];
178 strlcat(pszFile, "/debug.log", sizeof(pszFile));
179 fileout = fopen(pszFile, "a");
180 if (fileout) setbuf(fileout, NULL); // unbuffered
184 static bool fStartedNewLine = true;
186 // Debug print useful for profiling
187 if (fLogTimestamps && fStartedNewLine)
188 fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
189 if (pszFormat[strlen(pszFormat) - 1] == '\n')
190 fStartedNewLine = true;
192 fStartedNewLine = false;
195 va_start(arg_ptr, pszFormat);
196 ret = vfprintf(fileout, pszFormat, arg_ptr);
202 if (fPrintToDebugger)
204 static CCriticalSection cs_OutputDebugStringF;
206 // accumulate a line at a time
207 CRITICAL_BLOCK(cs_OutputDebugStringF)
209 static char pszBuffer[50000];
214 va_start(arg_ptr, pszFormat);
215 int limit = END(pszBuffer) - pend - 2;
216 int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr);
218 if (ret < 0 || ret >= limit)
220 pend = END(pszBuffer) - 2;
226 char* p1 = pszBuffer;
228 while (p2 = strchr(p1, '\n'))
233 OutputDebugStringA(p1);
238 memmove(pszBuffer, p1, pend - p1 + 1);
239 pend -= (p1 - pszBuffer);
248 // - prints up to limit-1 characters
249 // - output string is always null terminated even if limit reached
250 // - return value is the number of characters actually printed
251 int my_snprintf(char* buffer, size_t limit, const char* format, ...)
256 va_start(arg_ptr, format);
257 int ret = _vsnprintf(buffer, limit, format, arg_ptr);
259 if (ret < 0 || ret >= limit)
268 string strprintf(const char* format, ...)
272 int limit = sizeof(buffer);
277 va_start(arg_ptr, format);
278 ret = _vsnprintf(p, limit, format, arg_ptr);
280 if (ret >= 0 && ret < limit)
287 throw std::bad_alloc();
289 string str(p, p+ret);
296 bool error(const char* format, ...)
299 int limit = sizeof(buffer);
301 va_start(arg_ptr, format);
302 int ret = _vsnprintf(buffer, limit, format, arg_ptr);
304 if (ret < 0 || ret >= limit)
309 printf("ERROR: %s\n", buffer);
314 void ParseString(const string& str, char c, vector<string>& v)
318 string::size_type i1 = 0;
319 string::size_type i2;
322 i2 = str.find(c, i1);
325 v.push_back(str.substr(i1));
328 v.push_back(str.substr(i1, i2-i1));
334 string FormatMoney(int64 n, bool fPlus)
336 // Note: not using straight sprintf here because we do NOT want
337 // localized number formatting.
338 int64 n_abs = (n > 0 ? n : -n);
339 int64 quotient = n_abs/COIN;
340 int64 remainder = n_abs%COIN;
341 string str = strprintf("%"PRI64d".%08"PRI64d, quotient, remainder);
343 // Right-trim excess 0's before the decimal point:
345 for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
348 str.erase(str.size()-nTrim, nTrim);
351 str.insert((unsigned int)0, 1, '-');
352 else if (fPlus && n > 0)
353 str.insert((unsigned int)0, 1, '+');
358 bool ParseMoney(const string& str, int64& nRet)
360 return ParseMoney(str.c_str(), nRet);
363 bool ParseMoney(const char* pszIn, int64& nRet)
367 const char* p = pszIn;
375 int64 nMult = CENT*10;
376 while (isdigit(*p) && (nMult > 0))
378 nUnits += nMult * (*p++ - '0');
387 strWhole.insert(strWhole.end(), *p);
392 if (strWhole.size() > 14)
394 if (nUnits < 0 || nUnits > COIN)
396 int64 nWhole = atoi64(strWhole);
397 int64 nValue = nWhole*COIN + nUnits;
404 vector<unsigned char> ParseHex(const char* psz)
406 static char phexdigit[256] =
407 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
408 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
409 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
410 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
411 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
412 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
413 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1
414 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
415 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
416 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
417 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
418 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
419 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
420 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
421 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
422 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
424 // convert hex dump to vector
425 vector<unsigned char> vch;
428 while (isspace(*psz))
430 char c = phexdigit[(unsigned char)*psz++];
433 unsigned char n = (c << 4);
434 c = phexdigit[(unsigned char)*psz++];
443 vector<unsigned char> ParseHex(const string& str)
445 return ParseHex(str.c_str());
449 void ParseParameters(int argc, char* argv[])
452 mapMultiArgs.clear();
453 for (int i = 1; i < argc; i++)
456 strlcpy(psz, argv[i], sizeof(psz));
457 char* pszValue = (char*)"";
458 if (strchr(psz, '='))
460 pszValue = strchr(psz, '=');
470 mapArgs[psz] = pszValue;
471 mapMultiArgs[psz].push_back(pszValue);
476 const char* wxGetTranslation(const char* pszEnglish)
479 // Wrapper of wxGetTranslation returning the same const char* type as was passed in
480 static CCriticalSection cs;
484 static map<string, char*> mapCache;
485 map<string, char*>::iterator mi = mapCache.find(pszEnglish);
486 if (mi != mapCache.end())
489 // wxWidgets translation
490 wxString strTranslated = wxGetTranslation(wxString(pszEnglish, wxConvUTF8));
492 // We don't cache unknown strings because caller might be passing in a
493 // dynamic string and we would keep allocating memory for each variation.
494 if (strcmp(pszEnglish, strTranslated.utf8_str()) == 0)
497 // Add to cache, memory doesn't need to be freed. We only cache because
498 // we must pass back a pointer to permanently allocated memory.
499 char* pszCached = new char[strlen(strTranslated.utf8_str())+1];
500 strcpy(pszCached, strTranslated.utf8_str());
501 mapCache[pszEnglish] = pszCached;
511 bool WildcardMatch(const char* psz, const char* mask)
518 return (*psz == '\0');
520 return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
535 bool WildcardMatch(const string& str, const string& mask)
537 return WildcardMatch(str.c_str(), mask.c_str());
547 void FormatException(char* pszMessage, std::exception* pex, const char* pszThread)
550 char pszModule[MAX_PATH];
552 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
554 const char* pszModule = "bitcoin";
557 snprintf(pszMessage, 1000,
558 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
560 snprintf(pszMessage, 1000,
561 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
564 void LogException(std::exception* pex, const char* pszThread)
566 char pszMessage[10000];
567 FormatException(pszMessage, pex, pszThread);
568 printf("\n%s", pszMessage);
571 void PrintException(std::exception* pex, const char* pszThread)
573 char pszMessage[10000];
574 FormatException(pszMessage, pex, pszThread);
575 printf("\n\n************************\n%s\n", pszMessage);
576 fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
577 strMiscWarning = pszMessage;
579 if (wxTheApp && !fDaemon)
580 MyMessageBox(pszMessage, "Bitcoin", wxOK | wxICON_ERROR);
585 void ThreadOneMessageBox(string strMessage)
587 // Skip message boxes if one is already open
588 static bool fMessageBoxOpen;
591 fMessageBoxOpen = true;
592 ThreadSafeMessageBox(strMessage, "Bitcoin", wxOK | wxICON_EXCLAMATION);
593 fMessageBoxOpen = false;
596 void PrintExceptionContinue(std::exception* pex, const char* pszThread)
598 char pszMessage[10000];
599 FormatException(pszMessage, pex, pszThread);
600 printf("\n\n************************\n%s\n", pszMessage);
601 fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
602 strMiscWarning = pszMessage;
604 if (wxTheApp && !fDaemon)
605 boost::thread(boost::bind(ThreadOneMessageBox, string(pszMessage)));
617 typedef WINSHELLAPI BOOL (WINAPI *PSHGETSPECIALFOLDERPATHA)(HWND hwndOwner, LPSTR lpszPath, int nFolder, BOOL fCreate);
619 string MyGetSpecialFolderPath(int nFolder, bool fCreate)
621 char pszPath[MAX_PATH+100] = "";
623 // SHGetSpecialFolderPath isn't always available on old Windows versions
624 HMODULE hShell32 = LoadLibraryA("shell32.dll");
627 PSHGETSPECIALFOLDERPATHA pSHGetSpecialFolderPath =
628 (PSHGETSPECIALFOLDERPATHA)GetProcAddress(hShell32, "SHGetSpecialFolderPathA");
629 if (pSHGetSpecialFolderPath)
630 (*pSHGetSpecialFolderPath)(NULL, pszPath, nFolder, fCreate);
631 FreeModule(hShell32);
635 if (pszPath[0] == '\0')
637 if (nFolder == CSIDL_STARTUP)
639 strcpy(pszPath, getenv("USERPROFILE"));
640 strcat(pszPath, "\\Start Menu\\Programs\\Startup");
642 else if (nFolder == CSIDL_APPDATA)
644 strcpy(pszPath, getenv("APPDATA"));
652 string GetDefaultDataDir()
654 // Windows: C:\Documents and Settings\username\Application Data\Bitcoin
655 // Mac: ~/Library/Application Support/Bitcoin
659 return MyGetSpecialFolderPath(CSIDL_APPDATA, true) + "\\Bitcoin";
661 char* pszHome = getenv("HOME");
662 if (pszHome == NULL || strlen(pszHome) == 0)
663 pszHome = (char*)"/";
664 string strHome = pszHome;
665 if (strHome[strHome.size()-1] != '/')
669 strHome += "Library/Application Support/";
670 filesystem::create_directory(strHome.c_str());
671 return strHome + "Bitcoin";
674 return strHome + ".bitcoin";
679 void GetDataDir(char* pszDir)
681 // pszDir must be at least MAX_PATH length.
683 if (pszSetDataDir[0] != 0)
685 strlcpy(pszDir, pszSetDataDir, MAX_PATH);
690 // This can be called during exceptions by printf, so we cache the
691 // value so we don't have to do memory allocations after that.
692 static char pszCachedDir[MAX_PATH];
693 if (pszCachedDir[0] == 0)
694 strlcpy(pszCachedDir, GetDefaultDataDir().c_str(), sizeof(pszCachedDir));
695 strlcpy(pszDir, pszCachedDir, MAX_PATH);
700 char* p = pszDir + strlen(pszDir);
701 if (p > pszDir && p[-1] != '/' && p[-1] != '\\')
703 strcpy(p, "testnet");
706 static bool pfMkdir[4];
707 if (!pfMkdir[nVariation])
709 pfMkdir[nVariation] = true;
710 boost::filesystem::create_directory(pszDir);
716 char pszDir[MAX_PATH];
721 string GetConfigFile()
723 namespace fs = boost::filesystem;
724 fs::path pathConfig(GetArg("-conf", "bitcoin.conf"));
725 if (!pathConfig.is_complete())
726 pathConfig = fs::path(GetDataDir()) / pathConfig;
727 return pathConfig.string();
730 void ReadConfigFile(map<string, string>& mapSettingsRet,
731 map<string, vector<string> >& mapMultiSettingsRet)
733 namespace fs = boost::filesystem;
734 namespace pod = boost::program_options::detail;
736 fs::ifstream streamConfig(GetConfigFile());
737 if (!streamConfig.good())
740 set<string> setOptions;
741 setOptions.insert("*");
743 for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
745 // Don't overwrite existing settings so command line settings override bitcoin.conf
746 string strKey = string("-") + it->string_key;
747 if (mapSettingsRet.count(strKey) == 0)
748 mapSettingsRet[strKey] = it->value[0];
749 mapMultiSettingsRet[strKey].push_back(it->value[0]);
755 namespace fs = boost::filesystem;
756 fs::path pathConfig(GetArg("-pid", "bitcoind.pid"));
757 if (!pathConfig.is_complete())
758 pathConfig = fs::path(GetDataDir()) / pathConfig;
759 return pathConfig.string();
762 void CreatePidFile(string pidFile, pid_t pid)
764 FILE* file = fopen(pidFile.c_str(), "w");
767 fprintf(file, "%d\n", pid);
772 int GetFilesize(FILE* file)
774 int nSavePos = ftell(file);
776 if (fseek(file, 0, SEEK_END) == 0)
777 nFilesize = ftell(file);
778 fseek(file, nSavePos, SEEK_SET);
782 void ShrinkDebugFile()
784 // Scroll debug.log if it's getting too big
785 string strFile = GetDataDir() + "/debug.log";
786 FILE* file = fopen(strFile.c_str(), "r");
787 if (file && GetFilesize(file) > 10 * 1000000)
789 // Restart the file with some of the end
791 fseek(file, -sizeof(pch), SEEK_END);
792 int nBytes = fread(pch, 1, sizeof(pch), file);
795 file = fopen(strFile.c_str(), "w");
798 fwrite(pch, 1, nBytes, file);
812 // "Never go to sea with two chronometers; take one or three."
813 // Our three time sources are:
815 // - Median of other nodes's clocks
816 // - The user (asking the user to fix the system clock if the first two disagree)
823 static int64 nTimeOffset = 0;
825 int64 GetAdjustedTime()
827 return GetTime() + nTimeOffset;
830 void AddTimeData(unsigned int ip, int64 nTime)
832 int64 nOffsetSample = nTime - GetTime();
835 static set<unsigned int> setKnown;
836 if (!setKnown.insert(ip).second)
840 static vector<int64> vTimeOffsets;
841 if (vTimeOffsets.empty())
842 vTimeOffsets.push_back(0);
843 vTimeOffsets.push_back(nOffsetSample);
844 printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), vTimeOffsets.back(), vTimeOffsets.back()/60);
845 if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
847 sort(vTimeOffsets.begin(), vTimeOffsets.end());
848 int64 nMedian = vTimeOffsets[vTimeOffsets.size()/2];
849 // Only let other nodes change our time by so much
850 if (abs64(nMedian) < 70 * 60)
852 nTimeOffset = nMedian;
861 // If nobody has a time different than ours but within 5 minutes of ours, give a warning
863 BOOST_FOREACH(int64 nOffset, vTimeOffsets)
864 if (nOffset != 0 && abs64(nOffset) < 5 * 60)
870 string strMessage = _("Warning: Please check that your computer's date and time are correct. If your clock is wrong Bitcoin will not work properly.");
871 strMiscWarning = strMessage;
872 printf("*** %s\n", strMessage.c_str());
873 boost::thread(boost::bind(ThreadSafeMessageBox, strMessage+" ", string("Bitcoin"), wxOK | wxICON_EXCLAMATION, (wxWindow*)NULL, -1, -1));
877 BOOST_FOREACH(int64 n, vTimeOffsets)
878 printf("%+"PRI64d" ", n);
879 printf("| nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
891 string FormatVersion(int nVersion)
893 if (nVersion%100 == 0)
894 return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
896 return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
899 string FormatFullVersion()
901 string s = FormatVersion(VERSION) + pszSubVer;
902 if (VERSION_IS_BETA) {
912 #ifdef DEBUG_LOCKORDER
914 // Early deadlock detection.
915 // Problem being solved:
916 // Thread 1 locks A, then B, then C
917 // Thread 2 locks D, then C, then A
918 // --> may result in deadlock between the two threads, depending on when they run.
919 // Solution implemented here:
920 // Keep track of pairs of locks: (A before B), (A before C), etc.
921 // Complain if any thread trys to lock in a different order.
926 std::string mutexName;
927 std::string sourceFile;
930 CLockLocation(const char* pszName, const char* pszFile, int nLine)
933 sourceFile = pszFile;
938 typedef std::vector< std::pair<CCriticalSection*, CLockLocation> > LockStack;
940 static boost::interprocess::interprocess_mutex dd_mutex;
941 static std::map<std::pair<CCriticalSection*, CCriticalSection*>, LockStack> lockorders;
942 static boost::thread_specific_ptr<LockStack> lockstack;
945 static void potential_deadlock_detected(const std::pair<CCriticalSection*, CCriticalSection*>& mismatch, const LockStack& s1, const LockStack& s2)
947 printf("POTENTIAL DEADLOCK DETECTED\n");
948 printf("Previous lock order was:\n");
949 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, s2)
951 if (i.first == mismatch.first) printf(" (1)");
952 if (i.first == mismatch.second) printf(" (2)");
953 printf(" %s %s:%d\n", i.second.mutexName.c_str(), i.second.sourceFile.c_str(), i.second.sourceLine);
955 printf("Current lock order is:\n");
956 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, s1)
958 if (i.first == mismatch.first) printf(" (1)");
959 if (i.first == mismatch.second) printf(" (2)");
960 printf(" %s %s:%d\n", i.second.mutexName.c_str(), i.second.sourceFile.c_str(), i.second.sourceLine);
964 static void push_lock(CCriticalSection* c, const CLockLocation& locklocation)
966 bool fOrderOK = true;
967 if (lockstack.get() == NULL)
968 lockstack.reset(new LockStack);
972 (*lockstack).push_back(std::make_pair(c, locklocation));
974 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, (*lockstack))
976 if (i.first == c) break;
978 std::pair<CCriticalSection*, CCriticalSection*> p1 = std::make_pair(i.first, c);
979 if (lockorders.count(p1))
981 lockorders[p1] = (*lockstack);
983 std::pair<CCriticalSection*, CCriticalSection*> p2 = std::make_pair(c, i.first);
984 if (lockorders.count(p2))
986 potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
993 static void pop_lock()
995 (*lockstack).pop_back();
998 void CCriticalSection::Enter(const char* pszName, const char* pszFile, int nLine)
1000 push_lock(this, CLockLocation(pszName, pszFile, nLine));
1003 void CCriticalSection::Leave()
1008 bool CCriticalSection::TryEnter(const char* pszName, const char* pszFile, int nLine)
1010 push_lock(this, CLockLocation(pszName, pszFile, nLine));
1011 bool result = mutex.try_lock();
1012 if (!result) pop_lock();
1018 void CCriticalSection::Enter(const char*, const char*, int)
1023 void CCriticalSection::Leave()
1028 bool CCriticalSection::TryEnter(const char*, const char*, int)
1030 bool result = mutex.try_lock();
1034 #endif /* DEBUG_LOCKORDER */