switched to use a spin lock on behalf of a mutex

This commit is contained in:
hyung-hwan 2018-06-28 04:08:43 +00:00
parent 6519b1e4db
commit 40cdf684de
3 changed files with 15 additions and 29 deletions

View File

@ -30,9 +30,10 @@
#include <qse/si/Socket.hpp> #include <qse/si/Socket.hpp>
#include <qse/si/SocketAddress.hpp> #include <qse/si/SocketAddress.hpp>
#include <qse/si/Thread.hpp> #include <qse/si/Thread.hpp>
#include <qse/si/SpinLock.hpp>
#include <qse/cmn/LinkedList.hpp> #include <qse/cmn/LinkedList.hpp>
#include <qse/Uncopyable.hpp> #include <qse/Uncopyable.hpp>
#include <qse/si/mtx.h>
QSE_BEGIN_NAMESPACE(QSE) QSE_BEGIN_NAMESPACE(QSE)
@ -114,7 +115,6 @@ protected:
friend class TcpServer; friend class TcpServer;
Client (Listener* listener) QSE_CPP_NOEXCEPT : listener(listener) {} Client (Listener* listener) QSE_CPP_NOEXCEPT : listener(listener) {}
~Client ();
int main (); int main ();
int stop () QSE_CPP_NOEXCEPT; int stop () QSE_CPP_NOEXCEPT;
@ -129,8 +129,7 @@ protected:
Listener* listener; Listener* listener;
QSE::Socket socket; QSE::Socket socket;
SocketAddress address; SocketAddress address;
SpinLock csspl; /* spin lock for client stop */
qse_mtx_t* csmtx; /* mutex for client stop */
}; };
struct ListenerList struct ListenerList

View File

@ -39,12 +39,6 @@ QSE_BEGIN_NAMESPACE(QSE)
#include "../cmn/syserr.h" #include "../cmn/syserr.h"
IMPLEMENT_SYSERR_TO_ERRNUM (TcpServer::ErrorCode, TcpServer::) IMPLEMENT_SYSERR_TO_ERRNUM (TcpServer::ErrorCode, TcpServer::)
TcpServer::Client::~Client()
{
if (this->csmtx) qse_mtx_close(this->csmtx);
}
// //
// NOTICE: the guarantee class below could have been placed // NOTICE: the guarantee class below could have been placed
// inside TCPServer::Client::run () without supporting // inside TCPServer::Client::run () without supporting
@ -52,16 +46,16 @@ TcpServer::Client::~Client()
// //
class guarantee_tcpsocket_close { class guarantee_tcpsocket_close {
public: public:
guarantee_tcpsocket_close (Socket* socket, qse_mtx_t* mtx): psck(socket), mtx(mtx) {} guarantee_tcpsocket_close (Socket* socket, SpinLock* spl): psck(socket), spl(spl) {}
~guarantee_tcpsocket_close () ~guarantee_tcpsocket_close ()
{ {
qse_mtx_lock (this->mtx, QSE_NULL); spl->lock ();
/*psck->shutdown ();*/ /*psck->shutdown ();*/
psck->close (); psck->close ();
qse_mtx_unlock (this->mtx); spl->unlock ();
} }
Socket* psck; Socket* psck;
qse_mtx_t* mtx; SpinLock* spl;
}; };
int TcpServer::Client::main () int TcpServer::Client::main ()
@ -72,7 +66,7 @@ int TcpServer::Client::main ()
// it would just block signals to the TcpProxy thread. // it would just block signals to the TcpProxy thread.
this->blockAllSignals (); // don't care about the result. this->blockAllSignals (); // don't care about the result.
guarantee_tcpsocket_close close_socket (&this->socket, this->csmtx); guarantee_tcpsocket_close close_socket (&this->socket, &this->csspl);
if (this->listener->server->handle_client(&this->socket, &this->address) <= -1) return -1; if (this->listener->server->handle_client(&this->socket, &this->address) <= -1) return -1;
return 0; return 0;
} }
@ -88,10 +82,9 @@ int TcpServer::Client::stop () QSE_CPP_NOEXCEPT
// as it might not be thread-safe. // as it might not be thread-safe.
// but it is still ok because Client::stop() // but it is still ok because Client::stop()
// is rarely called. // is rarely called.
qse_mtx_lock (this->csmtx, QSE_NULL); this->csspl.lock ();
this->socket.shutdown (); this->socket.shutdown ();
//this->socket.close (); this->csspl.unlock ();
qse_mtx_unlock (this->csmtx);
return 0; return 0;
} }
@ -364,15 +357,6 @@ int TcpServer::start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
break; break;
} }
client->csmtx = qse_mtx_open(QSE_MMGR_GETDFL(), 0);
if (!client->csmtx)
{
// TODO: logging ....
// don't delete client. just close the socket. for reuse.
client->socket.close ();
continue;
}
client->setStackSize (this->thread_stack_size); client->setStackSize (this->thread_stack_size);
#if defined(_WIN32) #if defined(_WIN32)
if (client->start(Thread::DETACHED) <= -1) if (client->start(Thread::DETACHED) <= -1)

View File

@ -36,18 +36,21 @@ static int test1 (void)
{ {
#if defined(QSE_LANG_CPP11) #if defined(QSE_LANG_CPP11)
QSE::TcpServerL<int(QSE::Socket*,QSE::SocketAddress*)> server ( QSE::TcpServerL<int(QSE::Socket*,QSE::SocketAddress*)> server (
([&server](QSE::Socket* clisock, QSE::SocketAddress* cliaddr) {
// workload by lambda
([&server](QSE::Socket* clisock, QSE::SocketAddress* cliaddr) {
qse_char_t buf[128]; qse_char_t buf[128];
qse_uint8_t bb[256]; qse_uint8_t bb[256];
while (!server.isStopRequested()) while (!server.isStopRequested())
{ {
qse_printf (QSE_T("hello word..from %s\n"), cliaddr->toStrBuf(buf, QSE_COUNTOF(buf))); qse_printf (QSE_T("hello word..from %s\n"), cliaddr->toStrBuf(buf, QSE_COUNTOF(buf)));
if (clisock->receive (bb, QSE_COUNTOF(bb)) <= 0) break; if (clisock->receive(bb, QSE_COUNTOF(bb)) <= 0) break;
} }
qse_printf (QSE_T("bye..to %s\n"), cliaddr->toStrBuf(buf, QSE_COUNTOF(buf))); qse_printf (QSE_T("bye..to %s\n"), cliaddr->toStrBuf(buf, QSE_COUNTOF(buf)));
return 0; return 0;
}) })
); );
#else #else
QSE::TcpServerF<ClientHandler> server; QSE::TcpServerF<ClientHandler> server;