fixed: in Synchro: we should have a table (map) of reference counters

each one for each thread
fixed: on Linux: pthread mutexes by default behaves differently than on FreeBSD
       we have to set PTHREAD_MUTEX_ERRORCHECK attribute 
       when creating a mutex
       



git-svn-id: svn://ttmath.org/publicrep/winix/trunk@953 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2014-02-14 11:20:22 +00:00
parent 37b22c3559
commit 222955a2e7
5 changed files with 43 additions and 59 deletions

View File

@ -1661,8 +1661,9 @@ void App::FetchPageOnExit()
if( curl ) if( curl )
{ {
curl_easy_setopt(curl, CURLOPT_URL, url_to_fetch_on_exit.c_str()); curl_easy_setopt(curl, CURLOPT_URL, url_to_fetch_on_exit.c_str());
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1); curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_perform(curl); curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }

View File

@ -11,15 +11,29 @@
#include "synchro.h" #include "synchro.h"
namespace Winix namespace Winix
{ {
Synchro::Synchro() : mutex(PTHREAD_MUTEX_INITIALIZER) Synchro::Synchro()
{ {
was_stop_signal = false; was_stop_signal = false;
ref = 0;
#ifdef __FreeBSD__
/*
* on FreeBSD a pthread's pthread_mutex_lock() is checking for deadlocks by default
*/
mutex = PTHREAD_MUTEX_INITIALIZER;
#else
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutex_init(&mutex, &attr);
pthread_mutexattr_destroy(&attr);
#endif
} }
@ -28,29 +42,34 @@ bool Synchro::Lock()
{ {
int res = pthread_mutex_lock(&mutex); int res = pthread_mutex_lock(&mutex);
if( res == EDEADLK ) if( res == 0 )
{ {
// Lock() method in this thread was called before ref[pthread_self()] = 1;
ref += 1;
return true; return true;
} }
else else
if( res == EDEADLK )
{ {
ref = 0; // Lock() method in this thread was called before
ref[pthread_self()] += 1;
return true;
} }
return res == 0; return false;
} }
void Synchro::Unlock() void Synchro::Unlock()
{ {
if( ref > 0 ) int & r = ref[pthread_self()];
if( r > 1 )
{ {
ref -= 1; r -= 1;
} }
else else
if( r == 1 )
{ {
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
} }

View File

@ -11,6 +11,7 @@
#define headerfile_winix_core_synchro #define headerfile_winix_core_synchro
#include <pthread.h> #include <pthread.h>
#include <map>
namespace Winix namespace Winix
@ -35,9 +36,9 @@ struct Synchro
private: private:
// deadlock counter // deadlock counter for each thread
// we can call Lock() more than one in the same thread // we can call Lock() more than one in the same thread
int ref; std::map<pthread_t, int> ref;
}; };

View File

@ -5,8 +5,10 @@ name = export.so
all: $(name) all: $(name)
# -lcurl is used when linking the main winix.so so we don't have to use it here
$(name): $(o) $(name): $(o)
$(CXX) -shared -Wl,-soname,$(name) -o $(name) $(CXXFLAGS) $(CXXWINIXINCLUDEFLAGS) $(LDFLAGS) *.o -lcurl $(CXX) -shared -Wl,-soname,$(name) -o $(name) $(CXXFLAGS) $(CXXWINIXINCLUDEFLAGS) $(LDFLAGS) *.o

View File

@ -135,10 +135,6 @@ void ExportThread::DoMessage()
{ {
bool sent_ok = false; bool sent_ok = false;
Lock()
log << log1 << "export thread: c" << logsave << logend;
Unlock();
Convert(message_work.url, url_a); Convert(message_work.url, url_a);
if( message_work.type == WINIX_PL_EXPORT_TYPE_CREATE_FILE ) if( message_work.type == WINIX_PL_EXPORT_TYPE_CREATE_FILE )
@ -157,10 +153,6 @@ void ExportThread::DoMessage()
sent_ok = true; sent_ok = true;
} }
Lock()
log << log1 << "export thread: d" << logsave << logend;
Unlock();
if( sent_ok ) if( sent_ok )
{ {
@ -180,11 +172,6 @@ void ExportThread::DoMessage()
Unlock(); Unlock();
} }
} }
Lock()
log << log1 << "export thread: e" << logsave << logend;
Unlock();
} }
@ -196,7 +183,9 @@ void ExportThread::DoMessage()
// objects are not locked // objects are not locked
bool ExportThread::Fetch(const char * url) bool ExportThread::Fetch(const char * url)
{ {
Lock();
CURL * curl = curl_easy_init(); CURL * curl = curl_easy_init();
Unlock();
if( !curl ) if( !curl )
{ {
@ -214,15 +203,14 @@ bool ExportThread::Fetch(const char * url)
curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERAGENT, browser_name.c_str()); curl_easy_setopt(curl, CURLOPT_USERAGENT, browser_name.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, conn_timeout); curl_easy_setopt(curl, CURLOPT_TIMEOUT, conn_timeout);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, conn_timeout);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 20); curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 20);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
CURLcode res = curl_easy_perform(curl); CURLcode res = curl_easy_perform(curl);
//long code; // http code
//curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if( res != 0 ) if( res != 0 )
@ -281,10 +269,6 @@ bool ExportThread::Put()
{ {
FILE * file = 0; FILE * file = 0;
Lock()
log << log1 << "export thread: 1" << logsave << logend;
Unlock();
if( message_work.type == WINIX_PL_EXPORT_TYPE_CREATE_FILE_STATIC ) if( message_work.type == WINIX_PL_EXPORT_TYPE_CREATE_FILE_STATIC )
{ {
Convert(message_work.url, local_path); Convert(message_work.url, local_path);
@ -304,14 +288,8 @@ FILE * file = 0;
} }
Lock() Lock();
log << log1 << "export thread: 2" << logsave << logend;
Unlock();
CURL * curl = curl_easy_init(); CURL * curl = curl_easy_init();
Lock()
log << log1 << "export thread: 3" << logsave << logend;
Unlock(); Unlock();
@ -337,12 +315,6 @@ FILE * file = 0;
Convert(message_work.ftp_login, ftp_login); Convert(message_work.ftp_login, ftp_login);
Convert(message_work.ftp_pass, ftp_pass); Convert(message_work.ftp_pass, ftp_pass);
Lock()
log << log1 << "export thread: 4" << logsave << logend;
Unlock();
curl_easy_setopt(curl, CURLOPT_URL, ftp_server.c_str()); curl_easy_setopt(curl, CURLOPT_URL, ftp_server.c_str());
curl_easy_setopt(curl, CURLOPT_USERNAME, ftp_login.c_str()); curl_easy_setopt(curl, CURLOPT_USERNAME, ftp_login.c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, ftp_pass.c_str()); curl_easy_setopt(curl, CURLOPT_PASSWORD, ftp_pass.c_str());
@ -351,6 +323,7 @@ FILE * file = 0;
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT, conn_timeout); curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT, conn_timeout);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
if( file ) if( file )
{ {
@ -362,21 +335,9 @@ FILE * file = 0;
curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer.size()); curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer.size());
} }
Lock()
log << log1 << "export thread: x" << logsave << logend;
Unlock();
CURLcode res = curl_easy_perform(curl); CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
Lock()
log << log1 << "export thread: y" << logsave << logend;
Unlock();
if( file ) if( file )
fclose(file); fclose(file);