diff options
author | Gluzskiy Alexandr <sss123next@list.ru> | 2010-10-08 07:01:44 +0300 |
---|---|---|
committer | Gluzskiy Alexandr <sss123next@list.ru> | 2010-10-08 07:01:44 +0300 |
commit | d5721b974a58ed5a26f33346dae728d93a7c1803 (patch) | |
tree | 2679ec39d8a44b5c3bdaf947d21881135460e7b7 /messages.cpp | |
parent | 7df7cc0f3a8435c18e4491b04827deb47078b6d5 (diff) |
boost threads and mutexes, better error handling (thread's timeouts)
Diffstat (limited to 'messages.cpp')
-rw-r--r-- | messages.cpp | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/messages.cpp b/messages.cpp index 99b1687..b18a93e 100644 --- a/messages.cpp +++ b/messages.cpp @@ -19,7 +19,7 @@ wstring new_key; HANDLE new_key_hcnt = NULL; -HANDLE new_key_hcnt_mutex = NULL; +boost::mutex new_key_hcnt_mutex; bool _terminate = false; BOOL isProtoMetaContacts(HANDLE hContact); @@ -80,8 +80,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) s2 += _tcslen(_T("-----END PGP PRIVATE KEY BLOCK-----")); } new_key.append(str.substr(s1,s2-s1)); - WaitForSingleObject(new_key_hcnt_mutex, INFINITE); - new_key_hcnt_mutex = CreateMutex(NULL, FALSE, NULL); + new_key_hcnt_mutex.lock(); new_key_hcnt = hContact; ShowNewKeyDialog(); return CallService(MS_PROTO_CHAINRECV, w, l); @@ -198,11 +197,12 @@ int RecvMsgSvc(WPARAM w, LPARAM l) params.out = &out; params.code = &code; params.result = &result; - HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); - if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + delete gpg_thread; MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINRECV, w, l); } if(result == pxNotFound) { @@ -224,8 +224,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) DBWriteContactSettingString(hContact, szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str()); } void ShowLoadKeyPasswordWindow(); - WaitForSingleObject(new_key_hcnt_mutex, INFINITE); - new_key_hcnt_mutex = CreateMutex(NULL, FALSE, NULL); + new_key_hcnt_mutex.lock(); new_key_hcnt = hContact; ShowLoadKeyPasswordWindow(); wstring cmd2 = cmd; @@ -245,11 +244,12 @@ int RecvMsgSvc(WPARAM w, LPARAM l) params.out = &out; params.code = &code; params.result = &result; - HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); - if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + gpg_thread = gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + delete gpg_thread; MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINRECV, w, l); } if(result == pxNotFound) { @@ -259,11 +259,12 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } } out.clear(); - gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); - if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + delete gpg_thread; MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINRECV, w, l); } if(result == pxNotFound) { @@ -423,11 +424,12 @@ int SendMsgSvc(WPARAM w, LPARAM l) params.out = &out; params.code = &code; params.result = &result; - HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); - if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + gpg_thread.~thread(); MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINSEND, w, l); } if(result == pxNotFound) { @@ -448,11 +450,12 @@ int SendMsgSvc(WPARAM w, LPARAM l) params.out = &out; params.code = &code; params.result = &result; - HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); - if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + gpg_thread.~thread(); MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINSEND, w, l); } if(result == pxNotFound) { @@ -536,7 +539,7 @@ int TestHook(WPARAM w, LPARAM l) static BOOL CALLBACK DlgProcKeyPassword(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { char *inkeyid = UniGetContactSettingUtf(new_key_hcnt, szGPGModuleName, "InKeyID", ""); - ReleaseMutex(new_key_hcnt_mutex); + new_key_hcnt_mutex.unlock(); TCHAR *tmp = NULL; switch (msg) |