From 61d85071405b99c3734606eed31ea8f615c0c77a Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Tue, 28 Aug 2012 18:26:35 +0200 Subject: [PATCH 1/2] implement CreateThread with boost::thread I'm not sure why this wasn't done before. - Removes typedef of pthread_t on Windows, which fixes a native compile issue on mingw. --- src/util.cpp | 12 ++++++++++++ src/util.h | 53 +--------------------------------------------------- 2 files changed, 13 insertions(+), 52 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 461f42d17..ec24ee403 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1299,3 +1299,15 @@ void RenameThread(const char* name) (void)name; #endif } + +bool CreateThread(void(*pfn)(void*), void* parg) +{ + try + { + boost::thread(pfn, parg); // thread detaches when out of scope + } catch(boost::thread_resource_error &e) { + printf("Error creating thread: %s\n", e.what()); + return false; + } + return true; +} diff --git a/src/util.h b/src/util.h index 709b0e05b..fffa60216 100644 --- a/src/util.h +++ b/src/util.h @@ -539,65 +539,14 @@ public: } }; +bool CreateThread(void(*pfn)(void*), void* parg); - - - - - - - - -// Note: It turns out we might have been able to use boost::thread -// by using TerminateThread(boost::thread.native_handle(), 0); #ifdef WIN32 -typedef HANDLE pthread_t; - -inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false) -{ - DWORD nUnused = 0; - HANDLE hthread = - CreateThread( - NULL, // default security - 0, // inherit stack size from parent - (LPTHREAD_START_ROUTINE)pfn, // function pointer - parg, // argument - 0, // creation option, start immediately - &nUnused); // thread identifier - if (hthread == NULL) - { - printf("Error: CreateThread() returned %d\n", GetLastError()); - return (pthread_t)0; - } - if (!fWantHandle) - { - CloseHandle(hthread); - return (pthread_t)-1; - } - return hthread; -} - inline void SetThreadPriority(int nPriority) { SetThreadPriority(GetCurrentThread(), nPriority); } #else -inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false) -{ - pthread_t hthread = 0; - int ret = pthread_create(&hthread, NULL, (void*(*)(void*))pfn, parg); - if (ret != 0) - { - printf("Error: pthread_create() returned %d\n", ret); - return (pthread_t)0; - } - if (!fWantHandle) - { - pthread_detach(hthread); - return (pthread_t)-1; - } - return hthread; -} #define THREAD_PRIORITY_LOWEST PRIO_MAX #define THREAD_PRIORITY_BELOW_NORMAL 2 From 4d1d94c56cf60e22e9199f8c37c5634752b570f5 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 29 Aug 2012 20:25:37 +0200 Subject: [PATCH 2/2] Rename CreateThread to NewThread Prevent clash with win32 API symbol --- src/bitcoinrpc.cpp | 2 +- src/init.cpp | 8 ++++---- src/main.cpp | 4 ++-- src/net.cpp | 32 ++++++++++++++++---------------- src/qt/qtipcserver.cpp | 2 +- src/rpcwallet.cpp | 4 ++-- src/util.cpp | 2 +- src/util.h | 2 +- src/wallet.cpp | 2 +- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 2271ff07c..84a6d6f89 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -697,7 +697,7 @@ static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptormapWallet.size()); printf("mapAddressBook.size() = %d\n", pwalletMain->mapAddressBook.size()); - if (!CreateThread(StartNode, NULL)) + if (!NewThread(StartNode, NULL)) InitError(_("Error: could not start node")); if (fServer) - CreateThread(ThreadRPCServer, NULL); + NewThread(ThreadRPCServer, NULL); // ********************************************************* Step 11: finished diff --git a/src/main.cpp b/src/main.cpp index 71d425e15..cfbb2d273 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4025,8 +4025,8 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet) printf("Starting %d BitcoinMiner threads\n", nAddThreads); for (int i = 0; i < nAddThreads; i++) { - if (!CreateThread(ThreadBitcoinMiner, pwallet)) - printf("Error: CreateThread(ThreadBitcoinMiner) failed\n"); + if (!NewThread(ThreadBitcoinMiner, pwallet)) + printf("Error: NewThread(ThreadBitcoinMiner) failed\n"); Sleep(10); } } diff --git a/src/net.cpp b/src/net.cpp index dc55fae5c..8e9c4f85f 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1125,7 +1125,7 @@ void MapPort() { if (fUseUPnP && vnThreadsRunning[THREAD_UPNP] < 1) { - if (!CreateThread(ThreadMapPort, NULL)) + if (!NewThread(ThreadMapPort, NULL)) printf("Error: ThreadMapPort(ThreadMapPort) failed\n"); } } @@ -1887,7 +1887,7 @@ void static Discover() // Don't use external IPv4 discovery, when -onlynet="IPv6" if (!IsLimited(NET_IPV4)) - CreateThread(ThreadGetMyExternalIP, NULL); + NewThread(ThreadGetMyExternalIP, NULL); } void StartNode(void* parg) @@ -1913,36 +1913,36 @@ void StartNode(void* parg) if (!GetBoolArg("-dnsseed", true)) printf("DNS seeding disabled\n"); else - if (!CreateThread(ThreadDNSAddressSeed, NULL)) - printf("Error: CreateThread(ThreadDNSAddressSeed) failed\n"); + if (!NewThread(ThreadDNSAddressSeed, NULL)) + printf("Error: NewThread(ThreadDNSAddressSeed) failed\n"); // Map ports with UPnP if (fUseUPnP) MapPort(); // Get addresses from IRC and advertise ours - if (!CreateThread(ThreadIRCSeed, NULL)) - printf("Error: CreateThread(ThreadIRCSeed) failed\n"); + if (!NewThread(ThreadIRCSeed, NULL)) + printf("Error: NewThread(ThreadIRCSeed) failed\n"); // Send and receive from sockets, accept connections - if (!CreateThread(ThreadSocketHandler, NULL)) - printf("Error: CreateThread(ThreadSocketHandler) failed\n"); + if (!NewThread(ThreadSocketHandler, NULL)) + printf("Error: NewThread(ThreadSocketHandler) failed\n"); // Initiate outbound connections from -addnode - if (!CreateThread(ThreadOpenAddedConnections, NULL)) - printf("Error: CreateThread(ThreadOpenAddedConnections) failed\n"); + if (!NewThread(ThreadOpenAddedConnections, NULL)) + printf("Error: NewThread(ThreadOpenAddedConnections) failed\n"); // Initiate outbound connections - if (!CreateThread(ThreadOpenConnections, NULL)) - printf("Error: CreateThread(ThreadOpenConnections) failed\n"); + if (!NewThread(ThreadOpenConnections, NULL)) + printf("Error: NewThread(ThreadOpenConnections) failed\n"); // Process messages - if (!CreateThread(ThreadMessageHandler, NULL)) - printf("Error: CreateThread(ThreadMessageHandler) failed\n"); + if (!NewThread(ThreadMessageHandler, NULL)) + printf("Error: NewThread(ThreadMessageHandler) failed\n"); // Dump network addresses - if (!CreateThread(ThreadDumpAddress, NULL)) - printf("Error; CreateThread(ThreadDumpAddress) failed\n"); + if (!NewThread(ThreadDumpAddress, NULL)) + printf("Error; NewThread(ThreadDumpAddress) failed\n"); // Generate coins in the background GenerateBitcoins(GetBoolArg("-gen", false), pwalletMain); diff --git a/src/qt/qtipcserver.cpp b/src/qt/qtipcserver.cpp index a2fe866e2..ec2d56b9e 100644 --- a/src/qt/qtipcserver.cpp +++ b/src/qt/qtipcserver.cpp @@ -152,7 +152,7 @@ void ipcInit(int argc, char *argv[]) return; } - if (!CreateThread(ipcThread, mq)) + if (!NewThread(ipcThread, mq)) { delete mq; return; diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index be83b85c1..b82b4f999 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -1302,9 +1302,9 @@ Value walletpassphrase(const Array& params, bool fHelp) "walletpassphrase \n" "Stores the wallet decryption key in memory for seconds."); - CreateThread(ThreadTopUpKeyPool, NULL); + NewThread(ThreadTopUpKeyPool, NULL); int64* pnSleepTime = new int64(params[1].get_int64()); - CreateThread(ThreadCleanWalletPassphrase, pnSleepTime); + NewThread(ThreadCleanWalletPassphrase, pnSleepTime); return Value::null; } diff --git a/src/util.cpp b/src/util.cpp index ec24ee403..d1270348e 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1300,7 +1300,7 @@ void RenameThread(const char* name) #endif } -bool CreateThread(void(*pfn)(void*), void* parg) +bool NewThread(void(*pfn)(void*), void* parg) { try { diff --git a/src/util.h b/src/util.h index fffa60216..65923e68a 100644 --- a/src/util.h +++ b/src/util.h @@ -539,7 +539,7 @@ public: } }; -bool CreateThread(void(*pfn)(void*), void* parg); +bool NewThread(void(*pfn)(void*), void* parg); #ifdef WIN32 inline void SetThreadPriority(int nPriority) diff --git a/src/wallet.cpp b/src/wallet.cpp index dc019d492..f2c21129b 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -1385,7 +1385,7 @@ int CWallet::LoadWallet(bool& fFirstRunRet) return nLoadWalletRet; fFirstRunRet = !vchDefaultKey.IsValid(); - CreateThread(ThreadFlushWalletDB, &strWalletFile); + NewThread(ThreadFlushWalletDB, &strWalletFile); return DB_LOAD_OK; }