From d5721b974a58ed5a26f33346dae728d93a7c1803 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Fri, 8 Oct 2010 07:01:44 +0300 Subject: boost threads and mutexes, better error handling (thread's timeouts) --- main.cpp | 133 +++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 69 insertions(+), 64 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index c5ed2cf..799db0e 100644 --- a/main.cpp +++ b/main.cpp @@ -88,11 +88,12 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM 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); + break; } if(result == pxNotFound) { @@ -183,11 +184,12 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM 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); + break; } if(result == pxNotFound) { @@ -247,11 +249,12 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM 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); + break; } if(result == pxNotFound) { @@ -319,12 +322,13 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM params.useless = ""; 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) + params.result = &result; + 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); + break; } if(result == pxNotFound) { @@ -349,11 +353,12 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM cmd += _T("--batch --delete-secret-and-public-key --fingerprint "); cmd += fp; mir_free(fp); - 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); + break; } if(result == pxNotFound) { @@ -445,6 +450,10 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szGpgBinPath", (SHGetValue(HKEY_CURRENT_USER, _T("Software\\GNU\\GnuPG"), _T("gpgProgram"), 0, path, &len) == ERROR_SUCCESS)?path:_T("")); if(tmp[0]) { + char *mir_path = new char [MAX_PATH]; + CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); + SetCurrentDirectoryA(mir_path); + delete [] mir_path; if(_waccess(tmp, 0) == -1) { if(errno == ENOENT) @@ -472,16 +481,9 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP 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) - { - TerminateThread(gpg_thread, 0); - MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); - } - if(result == pxNotFound) - { - MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); - } + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + gpg_thread.~thread(); DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); string::size_type p1 = out.find("(GnuPG) "); if(p1 != string::npos) @@ -556,6 +558,10 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP GetDlgItemText(hwndDlg, IDC_BIN_PATH, tmp, 512); if(tmp[0]) { + char *mir_path = new char [MAX_PATH]; + CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); + SetCurrentDirectoryA(mir_path); + delete [] mir_path; if(_waccess(tmp, 0) == -1) { if(errno == ENOENT) @@ -583,16 +589,9 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP 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) - { - TerminateThread(gpg_thread, 0); - MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); - } - if(result == pxNotFound) - { - MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); - } + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + gpg_thread.~thread(); DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); string::size_type p1 = out.find("(GnuPG) "); if(p1 != string::npos) @@ -651,9 +650,9 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP static BOOL CALLBACK DlgProcNewKeyDialog(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { extern HANDLE new_key_hcnt; - extern HANDLE new_key_hcnt_mutex; + extern boost::mutex new_key_hcnt_mutex; HANDLE hContact = new_key_hcnt; - ReleaseMutex(new_key_hcnt_mutex); + new_key_hcnt_mutex.unlock(); void ImportKey(); TCHAR *tmp = NULL; switch (msg) @@ -817,12 +816,12 @@ static BOOL CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wParam, tmp = new TCHAR [5]; GetDlgItemText(hwndDlg, IDC_KEY_TYPE, tmp, 5); tmp2 = mir_t2a(tmp); + delete [] tmp; char *subkeytype = new char [6]; if(strstr(tmp2, "RSA")) strcpy(subkeytype, "RSA"); else if(strstr(tmp2, "DSA")) //this is useless check for now, but it will be required if someone add another key types support strcpy(subkeytype, "ELG-E"); - delete [] tmp; f<