diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-05-17 01:34:10 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-05-17 01:34:10 +0200 |
commit | a3e566c4d3631076e29f3651554603184b6351a7 (patch) | |
tree | 71effee59fd4364174f7a845204f946dbc18ff2a /src/Net | |
parent | c8d469cc3de8ef2fb95f7b47355ebf5318a4c22f (diff) | |
download | mad-a3e566c4d3631076e29f3651554603184b6351a7.tar mad-a3e566c4d3631076e29f3651554603184b6351a7.zip |
Gnulib durch boost ersetzt
Diffstat (limited to 'src/Net')
-rw-r--r-- | src/Net/ClientConnection.cpp | 10 | ||||
-rw-r--r-- | src/Net/Connection.cpp | 96 | ||||
-rw-r--r-- | src/Net/Connection.h | 38 | ||||
-rw-r--r-- | src/Net/FdManager.cpp | 62 | ||||
-rw-r--r-- | src/Net/FdManager.h | 11 | ||||
-rw-r--r-- | src/Net/Makefile.am | 2 | ||||
-rw-r--r-- | src/Net/Makefile.in | 67 | ||||
-rw-r--r-- | src/Net/ServerConnection.cpp | 6 | ||||
-rw-r--r-- | src/Net/ThreadManager.cpp | 105 | ||||
-rw-r--r-- | src/Net/ThreadManager.h | 56 |
10 files changed, 164 insertions, 289 deletions
diff --git a/src/Net/ClientConnection.cpp b/src/Net/ClientConnection.cpp index e4de735..688dbc7 100644 --- a/src/Net/ClientConnection.cpp +++ b/src/Net/ClientConnection.cpp @@ -21,6 +21,8 @@ #include "FdManager.h" #include "IPAddress.h" +#include <boost/thread/locks.hpp> + #include <cstring> #include <cerrno> #include <sys/socket.h> @@ -56,19 +58,17 @@ void ClientConnection::connectionHeader() { } void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Exception) { - gl_rwlock_wrlock(stateLock); + boost::unique_lock<boost::shared_mutex> lock(stateLock); daemon = daemon0; if(_isConnected()) { - gl_rwlock_unlock(stateLock); return; // TODO Error } sock = socket(PF_INET, SOCK_STREAM, 0); if(sock < 0) { - gl_rwlock_unlock(stateLock); throw Exception("socket()", Exception::INTERNAL_ERRNO, errno); } @@ -81,7 +81,6 @@ void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Exc delete peer; peer = 0; - gl_rwlock_unlock(stateLock); throw Exception("connect()", Exception::INTERNAL_ERRNO, errno); } @@ -91,7 +90,6 @@ void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Exc if(flags < 0) { close(sock); - gl_rwlock_unlock(stateLock); throw Exception("fcntl()", Exception::INTERNAL_ERRNO, errno); } @@ -110,7 +108,7 @@ void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Exc state = CONNECT; - gl_rwlock_unlock(stateLock); + lock.unlock(); updateEvents(); } diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 2ccfddb..cf675c9 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -48,50 +48,40 @@ Connection::~Connection() { gnutls_certificate_free_credentials(x509_cred); - gl_rwlock_destroy(stateLock); - gl_lock_destroy(sendLock); - gl_lock_destroy(receiveLock); - if(peer) delete peer; } void Connection::handshake() { - gl_rwlock_wrlock(stateLock); - if(state != CONNECT) { - gl_rwlock_unlock(stateLock); + boost::unique_lock<boost::shared_mutex> lock(stateLock); + if(state != CONNECT) return; - } state = HANDSHAKE; - gl_rwlock_unlock(stateLock); + lock.unlock(); doHandshake(); } void Connection::bye() { - gl_rwlock_wrlock(stateLock); - if(state != DISCONNECT) { - gl_rwlock_unlock(stateLock); + boost::unique_lock<boost::shared_mutex> lock(stateLock); + if(state != DISCONNECT) return; - } state = BYE; - gl_rwlock_unlock(stateLock); + lock.unlock(); doBye(); } void Connection::doHandshake() { - gl_rwlock_rdlock(stateLock); - if(state != HANDSHAKE) { - gl_rwlock_unlock(stateLock); + boost::shared_lock<boost::shared_mutex> lock(stateLock); + if(state != HANDSHAKE) return; - } int ret = gnutls_handshake(session); if(ret < 0) { - gl_rwlock_unlock(stateLock); + lock.unlock(); if(ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) { updateEvents(); @@ -104,7 +94,7 @@ void Connection::doHandshake() { } state = CONNECTION_HEADER; - gl_rwlock_unlock(stateLock); + lock.unlock(); connectionHeader(); } @@ -129,18 +119,16 @@ void Connection::doBye() { } void Connection::enterReceiveLoop() { - gl_rwlock_wrlock(stateLock); + boost::unique_lock<boost::shared_mutex> lock(stateLock); - if(!_isConnected() || _isDisconnecting()) { - gl_rwlock_unlock(stateLock); + if(!_isConnected() || _isDisconnecting()) return; - } if(_isConnecting()) ThreadManager::get()->pushWork(sigc::mem_fun(connectedSignal, &sigc::signal<void>::emit)); state = PACKET_HEADER; - gl_rwlock_unlock(stateLock); + lock.unlock(); rawReceive(sizeof(Packet::Data), sigc::mem_fun(this, &Connection::packetHeaderReceiveHandler)); } @@ -187,17 +175,15 @@ void Connection::doReceive() { if(!isConnected()) return; - gl_lock_lock(receiveLock); + boost::unique_lock<boost::mutex> lock(receiveLock); - if(_receiveComplete()) { - gl_lock_unlock(receiveLock); + if(_receiveComplete()) return; - } ssize_t ret = gnutls_record_recv(session, transR.data+transR.transmitted, transR.length-transR.transmitted); if(ret < 0) { - gl_lock_unlock(receiveLock); + lock.unlock(); if(ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) return; @@ -213,14 +199,14 @@ void Connection::doReceive() { uint8_t *data = transR.data; transR.data = 0; - gl_lock_unlock(receiveLock); + lock.unlock(); transR.notify(data, transR.length); delete [] data; } else { - gl_lock_unlock(receiveLock); + lock.unlock(); } updateEvents(); @@ -232,18 +218,16 @@ bool Connection::rawReceive(unsigned long length, if(!isConnected()) return false; - gl_lock_lock(receiveLock); - if(!_receiveComplete()) { - gl_lock_unlock(receiveLock); + boost::unique_lock<boost::mutex> lock(receiveLock); + if(!_receiveComplete()) return false; - } transR.data = new uint8_t[length]; transR.length = length; transR.transmitted = 0; transR.notify = notify; - gl_lock_unlock(receiveLock); + lock.unlock(); updateEvents(); @@ -254,13 +238,13 @@ void Connection::doSend() { if(!isConnected()) return; - gl_lock_lock(sendLock); + boost::unique_lock<boost::mutex> lock(sendLock); while(!_sendQueueEmpty()) { ssize_t ret = gnutls_record_send(session, transS.front().data+transS.front().transmitted, transS.front().length-transS.front().transmitted); if(ret < 0) { - gl_lock_unlock(sendLock); + lock.unlock(); if(ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) return; @@ -278,7 +262,7 @@ void Connection::doSend() { } } - gl_lock_unlock(sendLock); + lock.unlock(); updateEvents(); } @@ -290,9 +274,9 @@ bool Connection::rawSend(const uint8_t *data, unsigned long length) { Transmission trans = {length, 0, new uint8_t[length], sigc::slot<void,const void*,unsigned long>()}; std::memcpy(trans.data, data, length); - gl_lock_lock(sendLock); + sendLock.lock(); transS.push(trans); - gl_lock_unlock(sendLock); + sendLock.unlock(); updateEvents(); @@ -333,9 +317,9 @@ void Connection::sendReceive(short events) { } bool Connection::send(const Packet &packet) { - gl_rwlock_rdlock(stateLock); + stateLock.lock_shared(); bool err = (!_isConnected() || _isConnecting() || _isDisconnecting()); - gl_rwlock_unlock(stateLock); + stateLock.unlock_shared(); if(err) return false; @@ -344,21 +328,19 @@ bool Connection::send(const Packet &packet) { } void Connection::disconnect() { - gl_rwlock_wrlock(stateLock); - if(!_isConnected() || _isDisconnecting()) { - gl_rwlock_unlock(stateLock); + boost::unique_lock<boost::shared_mutex> lock(stateLock); + if(!_isConnected() || _isDisconnecting()) return; - } state = DISCONNECT; - gl_rwlock_unlock(stateLock); + lock.unlock(); updateEvents(); } void Connection::doDisconnect() { - gl_rwlock_wrlock(stateLock); + boost::unique_lock<boost::shared_mutex> lock(stateLock); if(_isConnected()) { FdManager::get()->unregisterFd(sock); @@ -372,27 +354,25 @@ void Connection::doDisconnect() { state = DISCONNECTED; } - - gl_rwlock_unlock(stateLock); } void Connection::updateEvents() { - gl_lock_lock(receiveLock); + receiveLock.lock(); short events = (_receiveComplete() ? 0 : POLLIN); - gl_lock_unlock(receiveLock); + receiveLock.unlock(); - gl_lock_lock(sendLock); + sendLock.lock(); events |= (_sendQueueEmpty() ? 0 : POLLOUT); - gl_lock_unlock(sendLock); + sendLock.unlock(); - gl_rwlock_rdlock(stateLock); + stateLock.lock_shared(); if(state == HANDSHAKE || state == BYE) events = ((gnutls_record_get_direction(session) == 0) ? POLLIN : POLLOUT); else if(state == CONNECT || state == DISCONNECT) events |= POLLOUT; FdManager::get()->setFdEvents(sock, events); - gl_rwlock_unlock(stateLock); + stateLock.unlock_shared(); } } diff --git a/src/Net/Connection.h b/src/Net/Connection.h index 1176f92..1a3e72b 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -30,7 +30,8 @@ #include <sigc++/signal.h> #include <poll.h> -#include "glthread/lock.h" +#include <boost/thread/mutex.hpp> +#include <boost/thread/shared_mutex.hpp> #include <iostream> @@ -63,10 +64,10 @@ class Connection { sigc::slot<void,const void*,unsigned long> notify; }; - gl_lock_t receiveLock; + boost::mutex receiveLock; Transmission transR; - gl_lock_t sendLock; + boost::mutex sendLock; std::queue<Transmission> transS; Packet::Data header; @@ -112,7 +113,7 @@ class Connection { uint8_t protVerMax; }; - gl_rwlock_t stateLock; + boost::shared_mutex stateLock; enum State { DISCONNECTED, CONNECT, HANDSHAKE, CONNECTION_HEADER, PACKET_HEADER, PACKET_DATA, DISCONNECT, BYE @@ -151,38 +152,25 @@ class Connection { transR.length = transR.transmitted = 0; transR.data = 0; - gl_rwlock_init(stateLock); - gl_lock_init(sendLock); - gl_lock_init(receiveLock); - gnutls_certificate_allocate_credentials(&x509_cred); } virtual ~Connection(); bool isConnected() { - gl_rwlock_rdlock(stateLock); - bool ret = _isConnected(); - gl_rwlock_unlock(stateLock); - - return ret; + boost::shared_lock<boost::shared_mutex> lock(stateLock); + return _isConnected(); } bool isConnecting() { - gl_rwlock_rdlock(stateLock); - bool ret = _isConnecting(); - gl_rwlock_unlock(stateLock); - - return ret; + boost::shared_lock<boost::shared_mutex> lock(stateLock); + return _isConnecting(); } - /*bool isDisconnecting() { - gl_rwlock_rdlock(stateLock); - bool ret = (state == DISCONNECT || state == BYE); - gl_rwlock_unlock(stateLock); - - return ret; - }*/ + bool isDisconnecting() { + boost::shared_lock<boost::shared_mutex> lock(stateLock); + return _isDisconnecting(); + } const gnutls_datum_t* getCertificate() const { // TODO Thread-safeness diff --git a/src/Net/FdManager.cpp b/src/Net/FdManager.cpp index 499ad62..c7abe73 100644 --- a/src/Net/FdManager.cpp +++ b/src/Net/FdManager.cpp @@ -35,10 +35,6 @@ FdManager FdManager::fdManager; FdManager::FdManager() : running(false) { - gl_rwlock_init(handlerLock); - gl_rwlock_init(eventLock); - gl_rwlock_init(runLock); - pipe(interruptPipe); int flags = fcntl(interruptPipe[0], F_GETFL, 0); @@ -55,23 +51,19 @@ FdManager::~FdManager() { close(interruptPipe[0]); close(interruptPipe[1]); - - gl_rwlock_destroy(runLock); - gl_rwlock_destroy(eventLock); - gl_rwlock_destroy(handlerLock); } bool FdManager::registerFd(int fd, const sigc::slot<void, short> &handler, short events) { struct pollfd pollfd = {fd, events, 0}; - gl_rwlock_wrlock(handlerLock); - gl_rwlock_wrlock(eventLock); + boost::lock(handlerLock, eventLock); pollfds.insert(std::make_pair(fd, pollfd)); bool ret = handlers.insert(std::make_pair(fd, handler)).second; - gl_rwlock_unlock(eventLock); - gl_rwlock_unlock(handlerLock); + + eventLock.unlock(); + handlerLock.unlock(); interrupt(); @@ -79,12 +71,11 @@ bool FdManager::registerFd(int fd, const sigc::slot<void, short> &handler, short } bool FdManager::unregisterFd(int fd) { - gl_rwlock_wrlock(handlerLock); - gl_rwlock_wrlock(eventLock); + boost::lock(handlerLock, eventLock); pollfds.erase(fd); bool ret = handlers.erase(fd); - gl_rwlock_unlock(eventLock); - gl_rwlock_unlock(handlerLock); + eventLock.unlock(); + handlerLock.unlock(); interrupt(); @@ -92,36 +83,30 @@ bool FdManager::unregisterFd(int fd) { } bool FdManager::setFdEvents(int fd, short events) { - gl_rwlock_wrlock(eventLock); + boost::unique_lock<boost::shared_mutex> lock(eventLock); + std::map<int, struct pollfd>::iterator pollfd = pollfds.find(fd); - if(pollfd == pollfds.end()) { - gl_rwlock_unlock(eventLock); + if(pollfd == pollfds.end()) return false; - } if(pollfd->second.events != events) { pollfd->second.events = events; interrupt(); } - gl_rwlock_unlock(eventLock); - return true; } short FdManager::getFdEvents(int fd) { - gl_rwlock_rdlock(eventLock); + boost::shared_lock<boost::shared_mutex> lock(eventLock); std::map<int, struct pollfd>::const_iterator pollfd = pollfds.find(fd); if(pollfd == pollfds.end()) return -1; - short ret = pollfd->second.events; - gl_rwlock_unlock(eventLock); - - return ret; + return pollfd->second.events; } void FdManager::readInterrupt() { @@ -137,16 +122,15 @@ void FdManager::interrupt() { } void FdManager::ioThread() { - gl_rwlock_wrlock(runLock); + runLock.lock(); running = true; - gl_rwlock_unlock(runLock); + runLock.unlock_and_lock_shared(); - gl_rwlock_rdlock(runLock); while(running) { - gl_rwlock_unlock(runLock); + runLock.unlock_shared(); - gl_rwlock_rdlock(handlerLock); - gl_rwlock_rdlock(eventLock); + handlerLock.lock_shared(); + eventLock.lock_shared(); readInterrupt(); size_t count = pollfds.size(); @@ -159,11 +143,11 @@ void FdManager::ioThread() { ++pollfd; } - gl_rwlock_unlock(eventLock); - gl_rwlock_unlock(handlerLock); + eventLock.unlock_shared(); + handlerLock.unlock_shared(); if(poll(fdarray, count, -1) > 0) { - gl_rwlock_rdlock(handlerLock); + handlerLock.lock_shared(); std::queue<sigc::slot<void> > calls; @@ -172,7 +156,7 @@ void FdManager::ioThread() { calls.push(sigc::bind(handlers[fdarray[n].fd], fdarray[n].revents)); } - gl_rwlock_unlock(handlerLock); + handlerLock.unlock_shared(); while(!calls.empty()) { calls.front()(); @@ -183,10 +167,10 @@ void FdManager::ioThread() { delete [] fdarray; - gl_rwlock_rdlock(runLock); + runLock.lock_shared(); } - gl_rwlock_unlock(runLock); + runLock.unlock_shared(); } } diff --git a/src/Net/FdManager.h b/src/Net/FdManager.h index 8c2ec12..406110d 100644 --- a/src/Net/FdManager.h +++ b/src/Net/FdManager.h @@ -20,14 +20,11 @@ #ifndef MAD_NET_FDMANAGER_H_ #define MAD_NET_FDMANAGER_H_ -#include <config.h> - #include <map> #include <poll.h> #include <sigc++/signal.h> -#include "glthread/lock.h" -#include "glthread/cond.h" +#include <boost/thread/shared_mutex.hpp> namespace Mad { namespace Net { @@ -40,7 +37,7 @@ class FdManager { static FdManager fdManager; - gl_rwlock_t runLock, handlerLock, eventLock; + boost::shared_mutex runLock, handlerLock, eventLock; bool running; std::map<int, struct pollfd> pollfds; @@ -55,9 +52,9 @@ class FdManager { void ioThread(); void stopIOThread() { - gl_rwlock_wrlock(runLock); + runLock.lock(); running = false; - gl_rwlock_unlock(runLock); + runLock.unlock(); interrupt(); } diff --git a/src/Net/Makefile.am b/src/Net/Makefile.am index 65fc504..1bd1d74 100644 --- a/src/Net/Makefile.am +++ b/src/Net/Makefile.am @@ -1,6 +1,8 @@ noinst_LTLIBRARIES = libnet.la libnet_la_SOURCES = ClientConnection.cpp Exception.cpp ServerConnection.cpp Connection.cpp FdManager.cpp IPAddress.cpp \ Listener.cpp Packet.cpp ThreadManager.cpp +libnet_la_LDFLAGS = $(BOOST_LDFLAGS) +libnet_la_LIBADD = $(BOOST_THREAD_LIB) noinst_HEADERS = ClientConnection.h Exception.h ServerConnection.h Connection.h FdManager.h IPAddress.h Listener.h \ Packet.h ThreadManager.h diff --git a/src/Net/Makefile.in b/src/Net/Makefile.in index 5d421fb..c51039d 100644 --- a/src/Net/Makefile.in +++ b/src/Net/Makefile.in @@ -37,33 +37,29 @@ subdir = src/Net DIST_COMMON = $(noinst_HEADERS) $(srcdir)/Makefile.am \ $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 -am__aclocal_m4_deps = $(top_srcdir)/m4/00gnulib.m4 \ - $(top_srcdir)/m4/argz.m4 $(top_srcdir)/m4/ax_lib_mysql.m4 \ - $(top_srcdir)/m4/base64.m4 $(top_srcdir)/m4/cond.m4 \ - $(top_srcdir)/m4/errno_h.m4 $(top_srcdir)/m4/extensions.m4 \ - $(top_srcdir)/m4/gettimeofday.m4 \ - $(top_srcdir)/m4/gnulib-common.m4 \ - $(top_srcdir)/m4/gnulib-comp.m4 \ - $(top_srcdir)/m4/include_next.m4 $(top_srcdir)/m4/lib-ld.m4 \ - $(top_srcdir)/m4/lib-link.m4 $(top_srcdir)/m4/lib-prefix.m4 \ - $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/lock.m4 \ +am__aclocal_m4_deps = $(top_srcdir)/m4/argz.m4 \ + $(top_srcdir)/m4/ax_boost_base.m4 \ + $(top_srcdir)/m4/ax_boost_thread.m4 \ + $(top_srcdir)/m4/ax_lib_mysql.m4 $(top_srcdir)/m4/libtool.m4 \ $(top_srcdir)/m4/ltdl.m4 $(top_srcdir)/m4/ltoptions.m4 \ $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/m4/pkg.m4 \ - $(top_srcdir)/m4/stdbool.m4 $(top_srcdir)/m4/sys_time_h.m4 \ - $(top_srcdir)/m4/thread.m4 $(top_srcdir)/m4/threadlib.m4 \ - $(top_srcdir)/m4/time_h.m4 $(top_srcdir)/configure.ac + $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4) mkinstalldirs = $(install_sh) -d CONFIG_HEADER = $(top_builddir)/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) -libnet_la_LIBADD = +am__DEPENDENCIES_1 = +libnet_la_DEPENDENCIES = $(am__DEPENDENCIES_1) am_libnet_la_OBJECTS = ClientConnection.lo Exception.lo \ ServerConnection.lo Connection.lo FdManager.lo IPAddress.lo \ Listener.lo Packet.lo ThreadManager.lo libnet_la_OBJECTS = $(am_libnet_la_OBJECTS) +libnet_la_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \ + $(CXXFLAGS) $(libnet_la_LDFLAGS) $(LDFLAGS) -o $@ DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/config/depcomp am__depfiles_maybe = depfiles @@ -90,6 +86,9 @@ AUTOCONF = @AUTOCONF@ AUTOHEADER = @AUTOHEADER@ AUTOMAKE = @AUTOMAKE@ AWK = @AWK@ +BOOST_CPPFLAGS = @BOOST_CPPFLAGS@ +BOOST_LDFLAGS = @BOOST_LDFLAGS@ +BOOST_THREAD_LIB = @BOOST_THREAD_LIB@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ @@ -108,25 +107,13 @@ ECHO_C = @ECHO_C@ ECHO_N = @ECHO_N@ ECHO_T = @ECHO_T@ EGREP = @EGREP@ -EMULTIHOP_HIDDEN = @EMULTIHOP_HIDDEN@ -EMULTIHOP_VALUE = @EMULTIHOP_VALUE@ -ENOLINK_HIDDEN = @ENOLINK_HIDDEN@ -ENOLINK_VALUE = @ENOLINK_VALUE@ -EOVERFLOW_HIDDEN = @EOVERFLOW_HIDDEN@ -EOVERFLOW_VALUE = @EOVERFLOW_VALUE@ -ERRNO_H = @ERRNO_H@ EXEEXT = @EXEEXT@ FGREP = @FGREP@ GREP = @GREP@ GSSAPI_LIBS = @GSSAPI_LIBS@ GnuTLS_CFLAGS = @GnuTLS_CFLAGS@ GnuTLS_LIBS = @GnuTLS_LIBS@ -HAVE_STRUCT_TIMEVAL = @HAVE_STRUCT_TIMEVAL@ -HAVE_SYS_TIME_H = @HAVE_SYS_TIME_H@ -HAVE__BOOL = @HAVE__BOOL@ INCLTDL = @INCLTDL@ -INCLUDE_NEXT = @INCLUDE_NEXT@ -INCLUDE_NEXT_AS_FIRST_DIRECTIVE = @INCLUDE_NEXT_AS_FIRST_DIRECTIVE@ INSTALL = @INSTALL@ INSTALL_DATA = @INSTALL_DATA@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ @@ -139,22 +126,15 @@ LIBADD_DLD_LINK = @LIBADD_DLD_LINK@ LIBADD_DLOPEN = @LIBADD_DLOPEN@ LIBADD_SHL_LOAD = @LIBADD_SHL_LOAD@ LIBLTDL = @LIBLTDL@ -LIBMULTITHREAD = @LIBMULTITHREAD@ LIBOBJS = @LIBOBJS@ -LIBPTH = @LIBPTH@ -LIBPTH_PREFIX = @LIBPTH_PREFIX@ LIBS = @LIBS@ -LIBTHREAD = @LIBTHREAD@ LIBTOOL = @LIBTOOL@ LIPO = @LIPO@ LN_S = @LN_S@ LTDLDEPS = @LTDLDEPS@ LTDLINCL = @LTDLINCL@ LTDLOPEN = @LTDLOPEN@ -LTLIBMULTITHREAD = @LTLIBMULTITHREAD@ LTLIBOBJS = @LTLIBOBJS@ -LTLIBPTH = @LTLIBPTH@ -LTLIBTHREAD = @LTLIBTHREAD@ LT_CONFIG_H = @LT_CONFIG_H@ LT_DLLOADERS = @LT_DLLOADERS@ LT_DLPREOPEN = @LT_DLPREOPEN@ @@ -165,9 +145,6 @@ MYSQL_CFLAGS = @MYSQL_CFLAGS@ MYSQL_CONFIG = @MYSQL_CONFIG@ MYSQL_LDFLAGS = @MYSQL_LDFLAGS@ MYSQL_VERSION = @MYSQL_VERSION@ -NEXT_ERRNO_H = @NEXT_ERRNO_H@ -NEXT_SYS_TIME_H = @NEXT_SYS_TIME_H@ -NEXT_TIME_H = @NEXT_TIME_H@ NM = @NM@ NMEDIT = @NMEDIT@ OBJEXT = @OBJEXT@ @@ -181,22 +158,12 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ PKG_CONFIG = @PKG_CONFIG@ -PRAGMA_SYSTEM_HEADER = @PRAGMA_SYSTEM_HEADER@ RANLIB = @RANLIB@ READLINE_LIBS = @READLINE_LIBS@ -REPLACE_GETTIMEOFDAY = @REPLACE_GETTIMEOFDAY@ -REPLACE_LOCALTIME_R = @REPLACE_LOCALTIME_R@ -REPLACE_NANOSLEEP = @REPLACE_NANOSLEEP@ -REPLACE_STRPTIME = @REPLACE_STRPTIME@ -REPLACE_TIMEGM = @REPLACE_TIMEGM@ SED = @SED@ SET_MAKE = @SET_MAKE@ SHELL = @SHELL@ -STDBOOL_H = @STDBOOL_H@ STRIP = @STRIP@ -SYS_TIME_H = @SYS_TIME_H@ -SYS_TIME_H_DEFINES_STRUCT_TIMESPEC = @SYS_TIME_H_DEFINES_STRUCT_TIMESPEC@ -TIME_H_DEFINES_STRUCT_TIMESPEC = @TIME_H_DEFINES_STRUCT_TIMESPEC@ VERSION = @VERSION@ abs_builddir = @abs_builddir@ abs_srcdir = @abs_srcdir@ @@ -222,10 +189,6 @@ datarootdir = @datarootdir@ docdir = @docdir@ dvidir = @dvidir@ exec_prefix = @exec_prefix@ -gl_LIBOBJS = @gl_LIBOBJS@ -gl_LTLIBOBJS = @gl_LTLIBOBJS@ -gltests_LIBOBJS = @gltests_LIBOBJS@ -gltests_LTLIBOBJS = @gltests_LTLIBOBJS@ have_df = @have_df@ host = @host@ host_alias = @host_alias@ @@ -269,6 +232,8 @@ noinst_LTLIBRARIES = libnet.la libnet_la_SOURCES = ClientConnection.cpp Exception.cpp ServerConnection.cpp Connection.cpp FdManager.cpp IPAddress.cpp \ Listener.cpp Packet.cpp ThreadManager.cpp +libnet_la_LDFLAGS = $(BOOST_LDFLAGS) +libnet_la_LIBADD = $(BOOST_THREAD_LIB) noinst_HEADERS = ClientConnection.h Exception.h ServerConnection.h Connection.h FdManager.h IPAddress.h Listener.h \ Packet.h ThreadManager.h @@ -315,7 +280,7 @@ clean-noinstLTLIBRARIES: rm -f "$${dir}/so_locations"; \ done libnet.la: $(libnet_la_OBJECTS) $(libnet_la_DEPENDENCIES) - $(CXXLINK) $(libnet_la_OBJECTS) $(libnet_la_LIBADD) $(LIBS) + $(libnet_la_LINK) $(libnet_la_OBJECTS) $(libnet_la_LIBADD) $(LIBS) mostlyclean-compile: -rm -f *.$(OBJEXT) diff --git a/src/Net/ServerConnection.cpp b/src/Net/ServerConnection.cpp index aa8042e..c415cb3 100644 --- a/src/Net/ServerConnection.cpp +++ b/src/Net/ServerConnection.cpp @@ -21,6 +21,8 @@ #include "FdManager.h" #include "IPAddress.h" +#include <boost/thread/locks.hpp> + #include <cstring> #include <cerrno> #include <sys/socket.h> @@ -61,7 +63,7 @@ void ServerConnection::connectionHeaderReceiveHandler(const void *data, unsigned ServerConnection::ServerConnection(int sock0, const IPAddress &address, gnutls_dh_params_t dh_params, const std::string &x905CertFile, const std::string &x905KeyFile) : daemon(false) { - gl_rwlock_wrlock(stateLock); + boost::unique_lock<boost::shared_mutex> lock(stateLock); sock = sock0; @@ -79,7 +81,7 @@ ServerConnection::ServerConnection(int sock0, const IPAddress &address, gnutls_d state = CONNECT; - gl_rwlock_unlock(stateLock); + lock.unlock(); updateEvents(); } diff --git a/src/Net/ThreadManager.cpp b/src/Net/ThreadManager.cpp index 3495196..b1f4d4b 100644 --- a/src/Net/ThreadManager.cpp +++ b/src/Net/ThreadManager.cpp @@ -20,12 +20,13 @@ #include "ThreadManager.h" #include "FdManager.h" +#include <Common/Logger.h> +#include <Common/LogManager.h> + #include <fcntl.h> #include <sigc++/bind.h> #include <sigc++/hide.h> -#include <ignore-value.h> - namespace Mad { namespace Net { @@ -34,35 +35,31 @@ ThreadManager ThreadManager::threadManager; void ThreadManager::workerFunc() { while(true) { - gl_lock_lock(runLock); + boost::unique_lock<boost::mutex> lock(runLock); - if(!running || !isThisWorkerThread()) { - gl_lock_unlock(runLock); - return; - } + if(!running || !isThisWorkerThread()) + break; - gl_lock_unlock(runLock); + lock.unlock(); - gl_lock_lock(workLock); + boost::unique_lock<boost::mutex> lock2(workLock); while(work.empty()) { - gl_cond_wait(workCond, workLock); + workCond.wait(lock2); - if(!running) { - gl_lock_unlock(workLock); + if(!running) return; - } } sigc::slot<void> nextWork = work.front(); work.pop(); - gl_lock_unlock(workLock); + lock2.unlock(); nextWork(); } // And let the new worker thread join us... - pushWork(sigc::bind(sigc::mem_fun(this, &ThreadManager::threadFinished), (gl_thread_t)gl_thread_self())); + // TODO pushWork(sigc::bind(sigc::mem_fun(this, &ThreadManager::threadFinished), (gl_thread_t)gl_thread_self())); } void ThreadManager::detach() { @@ -71,49 +68,43 @@ void ThreadManager::detach() { return; } - gl_lock_lock(runLock); + runLock.lock(); bool isRunning = running; - gl_lock_unlock(runLock); + runLock.unlock(); if(!isRunning) // There's no point in creating a new worker thread when we aren't running anymore return; - gl_lock_lock(threadLock); + threadLock.lock(); + + if(workerThread->get_id() == boost::this_thread::get_id()) {// Already detached? + threads.add_thread(workerThread); - if(workerThread == (gl_thread_t)gl_thread_self()) {// Already detached? - threads.insert(workerThread); - workerThread = gl_thread_create(&ThreadManager::workerStart, 0); + workerThread = new boost::thread(std::mem_fun(&ThreadManager::workerFunc), this); } - gl_lock_unlock(threadLock); + threadLock.unlock(); } void ThreadManager::pushWork(const sigc::slot<void> &newWork) { - gl_lock_lock(workLock); - + workLock.lock(); work.push(newWork); + workLock.unlock(); - gl_cond_signal(workCond); - gl_lock_unlock(workLock); + workCond.notify_one(); } void ThreadManager::doInit() { - gl_lock_init(threadLock); - gl_lock_init(runLock); - - gl_lock_init(workLock); - gl_cond_init(workCond); - running = true; - gl_lock_lock(threadLock); + threadLock.lock(); - mainThread = (gl_thread_t)gl_thread_self(); - workerThread = gl_thread_create(&ThreadManager::workerStart, 0); - loggerThread = gl_thread_create(&ThreadManager::loggerStart, 0); - ioThread = gl_thread_create(&ThreadManager::ioStart, 0); + mainThreadId = boost::this_thread::get_id(); + workerThread = new boost::thread(std::mem_fun(&ThreadManager::workerFunc), this); + loggerThread = new boost::thread(std::mem_fun(&Common::LogManager::loggerThread), Common::LogManager::get()); + ioThread = new boost::thread(std::mem_fun(&FdManager::ioThread), FdManager::get()); - gl_lock_unlock(threadLock); + threadLock.unlock(); } void ThreadManager::doDeinit() { @@ -123,44 +114,30 @@ void ThreadManager::doDeinit() { } // First set running = false so the worker threads quit - gl_lock_lock(runLock); - gl_lock_lock(workLock); - + boost::lock(runLock, workLock); running = false; - gl_cond_signal(workCond); - gl_lock_unlock(workLock); - gl_lock_unlock(runLock); + workLock.unlock(); + runLock.unlock(); + + workCond.notify_one(); // We don't have to lock threadLock as detach() won't change workerThread when running is false - gl_thread_join(workerThread, 0); + workerThread->join(); + delete workerThread; // Now wait for all detached threads - gl_lock_lock(threadLock); - while(!threads.empty()) { - gl_thread_t thread = *threads.begin(); - gl_lock_unlock(threadLock); - - gl_thread_join(thread, 0); - - gl_lock_lock(threadLock); - threads.erase(thread); - } - gl_lock_unlock(threadLock); + threads.join_all(); // IO thread is next FdManager::get()->stopIOThread(); - gl_thread_join(ioThread, 0); + ioThread->join(); + delete ioThread; // Finally, the logger thread has to die Common::LogManager::get()->stopLoggerThread(); - gl_thread_join(loggerThread, 0); - - gl_cond_destroy(workCond); - gl_lock_destroy(workLock); - - gl_lock_destroy(runLock); - gl_lock_destroy(threadLock); + loggerThread->join(); + delete loggerThread; } } diff --git a/src/Net/ThreadManager.h b/src/Net/ThreadManager.h index 327ba67..9394329 100644 --- a/src/Net/ThreadManager.h +++ b/src/Net/ThreadManager.h @@ -22,63 +22,47 @@ #include <config.h> -#include "FdManager.h" - #include <Common/Initializable.h> -#include <Common/LogManager.h> #include <queue> #include <set> #include <sigc++/slot.h> -#include "glthread/thread.h" -#include "glthread/lock.h" -#include "glthread/cond.h" +#include <boost/thread/thread.hpp> +#include <boost/thread/condition_variable.hpp> +#include <boost/thread/locks.hpp> namespace Mad { namespace Net { class ThreadManager : public Common::Initializable { private: - gl_thread_t mainThread, workerThread, loggerThread, ioThread; - std::set<gl_thread_t> threads; + boost::thread::id mainThreadId; + boost::thread *workerThread, *loggerThread, *ioThread; + boost::thread_group threads; - gl_lock_t threadLock; + boost::mutex threadLock; - gl_lock_t runLock; + boost::mutex runLock; bool running; - gl_lock_t workLock; - gl_cond_t workCond; + boost::mutex workLock; + boost::condition_variable workCond; std::queue<sigc::slot<void> > work; static ThreadManager threadManager; ThreadManager() {} - static void* workerStart(void*) { - threadManager.workerFunc(); - return 0; - } - - static void* loggerStart(void*) { - Common::LogManager::get()->loggerThread(); - return 0; - } - - static void* ioStart(void*) { - FdManager::get()->ioThread(); - return 0; - } - void workerFunc(); - void threadFinished(gl_thread_t thread) { - gl_lock_lock(threadLock); - threads.erase(thread); - gl_lock_unlock(threadLock); + void threadFinished(boost::thread *thread) { + threadLock.lock(); + threads.remove_thread(thread); + threadLock.unlock(); - gl_thread_join(thread, 0); + thread->join(); + delete thread; } protected: @@ -87,14 +71,12 @@ class ThreadManager : public Common::Initializable { public: bool isThisMainThread() { - return (mainThread == (gl_thread_t)gl_thread_self()); + return (mainThreadId == boost::this_thread::get_id()); } bool isThisWorkerThread() { - gl_lock_lock(threadLock); - bool ret = (workerThread == (gl_thread_t)gl_thread_self()); - gl_lock_unlock(threadLock); - return ret; + boost::lock_guard<boost::mutex> lock(threadLock); + return (workerThread->get_id() == boost::this_thread::get_id()); } void detach(); |