added Mutex.
added QSE_CPP_THREXCEPT1() and similar macros changed the termination code of TcpServer
This commit is contained in:
@ -38,6 +38,11 @@
|
||||
#define QSE_LANG_CPP11 1
|
||||
|
||||
#define QSE_CPP_NOEXCEPT noexcept(true)
|
||||
#define QSE_CPP_THREXCEPT1(e1) throw(e1)
|
||||
#define QSE_CPP_THREXCEPT2(e1,e2) throw(e1,e2)
|
||||
#define QSE_CPP_THREXCEPT3(e1,e2,e3) throw(e1,e2,e3)
|
||||
#define QSE_CPP_THREXCEPT4(e1,e2,e3,e4) throw(e1,e2,e3,e4)
|
||||
#define QSE_CPP_THREXCEPT5(e1,e2,e3,e4,e5) throw(e1,e2,e3,e4,e5)
|
||||
#define QSE_CPP_EXPLICIT explicit
|
||||
|
||||
/// The QSE_CPP_ENABLE_CPP11_MOVE macro enables C++11 move semantics
|
||||
@ -58,6 +63,11 @@
|
||||
#undef QSE_LANG_CPP11
|
||||
|
||||
#define QSE_CPP_NOEXCEPT throw()
|
||||
#define QSE_CPP_THREXCEPT1(e1) throw(e1)
|
||||
#define QSE_CPP_THREXCEPT2(e1,e2) throw(e1,e2)
|
||||
#define QSE_CPP_THREXCEPT3(e1,e2,e3) throw(e1,e2,e3)
|
||||
#define QSE_CPP_THREXCEPT4(e1,e2,e3,e4) throw(e1,e2,e3,e4)
|
||||
#define QSE_CPP_THREXCEPT5(e1,e2,e3,e4,e5) throw(e1,e2,e3,e4,e5)
|
||||
#define QSE_CPP_EXPLICIT
|
||||
|
||||
#define QSE_CPP_CALL_DESTRUCTOR(ptr, class_name) ((ptr)->~class_name())
|
||||
@ -65,6 +75,11 @@
|
||||
#define QSE_CPP_TEMPLATE_QUALIFIER template
|
||||
#else
|
||||
#define QSE_CPP_NOEXCEPT
|
||||
#define QSE_CPP_THREXCEPT1(e1)
|
||||
#define QSE_CPP_THREXCEPT2(e1,e2)
|
||||
#define QSE_CPP_THREXCEPT3(e1,e2,e3)
|
||||
#define QSE_CPP_THREXCEPT4(e1,e2,e3,e4)
|
||||
#define QSE_CPP_THREXCEPT5(e1,e2,e3,e4,e5)
|
||||
#define QSE_CPP_EXPLICIT
|
||||
|
||||
#if defined(__BORLANDC__)
|
||||
|
@ -81,7 +81,7 @@ public:
|
||||
/// allocation. if it fails, it raise an exception if it's
|
||||
/// configured to do so.
|
||||
///
|
||||
void* allocate (qse_size_t n, bool raise_exception = true) throw(MemoryError)
|
||||
void* allocate (qse_size_t n, bool raise_exception = true) QSE_CPP_THREXCEPT1(MemoryError)
|
||||
{
|
||||
void* xptr = this->allocMem (n);
|
||||
if (!xptr && raise_exception) QSE_THROW (MemoryError);
|
||||
@ -92,14 +92,14 @@ public:
|
||||
/// The callocate() function allocates memory like allocate() and
|
||||
/// clears the memory before returning.
|
||||
///
|
||||
void* callocate (qse_size_t n, bool raise_exception = true) throw(MemoryError);
|
||||
void* callocate (qse_size_t n, bool raise_exception = true) QSE_CPP_THREXCEPT1(MemoryError);
|
||||
|
||||
///
|
||||
/// The reallocate() function calls reallocMem() for memory
|
||||
/// reallocation. if it fails, it raise an exception if it's
|
||||
/// configured to do so.
|
||||
///
|
||||
void* reallocate (void* ptr, qse_size_t n, bool raise_exception = true) throw(MemoryError)
|
||||
void* reallocate (void* ptr, qse_size_t n, bool raise_exception = true) QSE_CPP_THREXCEPT1(MemoryError)
|
||||
{
|
||||
void* xptr = this->reallocMem (ptr, n);
|
||||
if (!xptr && raise_exception) QSE_THROW (MemoryError);
|
||||
@ -171,7 +171,7 @@ protected:
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr) throw(QSE::Mmgr::MemoryError);
|
||||
QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr) QSE_CPP_THREXCEPT1(QSE::Mmgr::MemoryError);
|
||||
|
||||
#if defined(QSE_CPP_NO_OPERATOR_DELETE_OVERLOADING)
|
||||
QSE_EXPORT void qse_operator_delete (void* ptr, QSE::Mmgr* mmgr);
|
||||
@ -179,7 +179,7 @@ QSE_EXPORT void qse_operator_delete (void* ptr, QSE::Mmgr* mmgr);
|
||||
QSE_EXPORT void operator delete (void* ptr, QSE::Mmgr* mmgr);
|
||||
#endif
|
||||
|
||||
QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr, void* existing_ptr) throw(QSE::Mmgr::MemoryError);
|
||||
QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr, void* existing_ptr) QSE_CPP_THREXCEPT1(QSE::Mmgr::MemoryError);
|
||||
|
||||
#if 0
|
||||
// i found no way to delete an array allocated with
|
||||
|
@ -43,9 +43,10 @@
|
||||
# include <atomic>
|
||||
#endif
|
||||
|
||||
#include <qse/si/mtx.h>
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
|
||||
class SpinLock
|
||||
class SpinLock: public Uncopyable
|
||||
{
|
||||
public:
|
||||
#if defined(QSE_SUPPORT_SPL)
|
||||
@ -114,6 +115,39 @@ protected:
|
||||
SpinLock& spl;
|
||||
};
|
||||
|
||||
|
||||
class Mutex: public Uncopyable
|
||||
{
|
||||
public:
|
||||
Mutex() QSE_CPP_NOEXCEPT
|
||||
{
|
||||
qse_mtx_init (&this->mtx, QSE_NULL);
|
||||
}
|
||||
~Mutex() QSE_CPP_NOEXCEPT
|
||||
{
|
||||
qse_mtx_fini (&this->mtx);
|
||||
}
|
||||
|
||||
#if 0
|
||||
bool tryock() QSE_CPP_NOEXCEPT
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
void lock () QSE_CPP_NOEXCEPT
|
||||
{
|
||||
qse_mtx_lock (&this->mtx, QSE_NULL);
|
||||
}
|
||||
|
||||
void unlock () QSE_CPP_NOEXCEPT
|
||||
{
|
||||
qse_mtx_unlock (&this->mtx);
|
||||
}
|
||||
|
||||
protected:
|
||||
qse_mtx_t mtx;
|
||||
};
|
||||
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
|
||||
#endif
|
||||
|
@ -31,7 +31,6 @@
|
||||
#include <qse/si/SocketAddress.hpp>
|
||||
#include <qse/si/Thread.hpp>
|
||||
#include <qse/si/SpinLock.hpp>
|
||||
#include <qse/cmn/LinkedList.hpp>
|
||||
#include <qse/cmn/Mmged.hpp>
|
||||
#include <qse/Uncopyable.hpp>
|
||||
#include <qse/si/mux.h>
|
||||
@ -82,11 +81,11 @@ public:
|
||||
|
||||
qse_size_t getClientCount () const QSE_CPP_NOEXCEPT
|
||||
{
|
||||
return this->client_list.getSize();
|
||||
return this->client_list[Client::LIVE].getSize();
|
||||
}
|
||||
qse_size_t getConnectionCount () const QSE_CPP_NOEXCEPT
|
||||
{
|
||||
return this->client_list.getSize();
|
||||
return this->client_list[Client::LIVE].getSize();
|
||||
}
|
||||
|
||||
qse_size_t getThreadStackSize () const QSE_CPP_NOEXCEPT
|
||||
@ -115,7 +114,12 @@ protected:
|
||||
public:
|
||||
friend class TcpServer;
|
||||
|
||||
Client (Listener* listener) QSE_CPP_NOEXCEPT : listener(listener) {}
|
||||
enum State
|
||||
{
|
||||
DEAD = 0,
|
||||
LIVE = 1
|
||||
};
|
||||
Client (Listener* listener) QSE_CPP_NOEXCEPT: listener(listener), prev_client(QSE_NULL), next_client(QSE_NULL), claimed(false) {}
|
||||
|
||||
int main ();
|
||||
int stop () QSE_CPP_NOEXCEPT;
|
||||
@ -126,11 +130,18 @@ protected:
|
||||
TcpServer* getServer() QSE_CPP_NOEXCEPT { return this->listener->server; }
|
||||
const TcpServer* getServer() const QSE_CPP_NOEXCEPT { return this->listener->server; }
|
||||
|
||||
private:
|
||||
|
||||
Client* getNextClient() { return this->next_client; }
|
||||
Client* getPrevClient() { return this->prev_client; }
|
||||
|
||||
Listener* listener;
|
||||
Client* prev_client;
|
||||
Client* next_client;
|
||||
bool claimed;
|
||||
|
||||
QSE::Socket socket;
|
||||
SocketAddress address;
|
||||
SpinLock csspl; /* spin lock for client stop */
|
||||
QSE::SocketAddress address;
|
||||
QSE::SpinLock csspl; // spin lock for client stop
|
||||
};
|
||||
|
||||
struct ListenerList
|
||||
@ -149,7 +160,60 @@ protected:
|
||||
Listener* tail;
|
||||
|
||||
qse_size_t count;
|
||||
} listener_list;
|
||||
};
|
||||
|
||||
struct ClientList
|
||||
{
|
||||
ClientList() QSE_CPP_NOEXCEPT: head(QSE_NULL), tail(QSE_NULL), count(0) {}
|
||||
|
||||
qse_size_t getSize() const { return this->count; }
|
||||
Client* getHead() { return this->head; }
|
||||
Client* getTail() { return this->tail; }
|
||||
|
||||
void append (Client* client)
|
||||
{
|
||||
client->next_client = QSE_NULL;
|
||||
if (this->count == 0)
|
||||
{
|
||||
this->head = this->tail = client;
|
||||
client->prev_client = QSE_NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
client->prev_client = this->tail;
|
||||
this->tail->next_client = client;
|
||||
this->tail = client;
|
||||
}
|
||||
|
||||
this->count++;
|
||||
}
|
||||
|
||||
void remove (Client* client)
|
||||
{
|
||||
if (client->next_client)
|
||||
client->next_client->prev_client = client->prev_client;
|
||||
else
|
||||
this->tail = client->prev_client;
|
||||
|
||||
if (client->prev_client)
|
||||
client->prev_client->next_client = client->next_client;
|
||||
else
|
||||
this->head = client->next_client;
|
||||
|
||||
client->prev_client = QSE_NULL;
|
||||
client->next_client = QSE_NULL;
|
||||
this->count--;
|
||||
|
||||
}
|
||||
|
||||
Client* head;
|
||||
Client* tail;
|
||||
qse_size_t count;
|
||||
};
|
||||
|
||||
ListenerList listener_list;
|
||||
ClientList client_list[2];
|
||||
QSE::SpinLock client_list_spl;
|
||||
|
||||
ErrorCode errcode;
|
||||
bool stop_requested;
|
||||
@ -157,15 +221,11 @@ protected:
|
||||
qse_size_t max_connections;
|
||||
qse_size_t thread_stack_size;
|
||||
|
||||
typedef QSE::LinkedList<Client*> ClientList;
|
||||
ClientList client_list;
|
||||
|
||||
friend class TcpServer::Client;
|
||||
virtual int handle_client (Socket* sock, SocketAddress* addr) = 0;
|
||||
|
||||
private:
|
||||
void delete_dead_clients () QSE_CPP_NOEXCEPT;
|
||||
void delete_all_clients () QSE_CPP_NOEXCEPT;
|
||||
void delete_all_clients (Client::State state) QSE_CPP_NOEXCEPT;
|
||||
|
||||
int setup_listeners (const qse_char_t* addrs) QSE_CPP_NOEXCEPT;
|
||||
void free_all_listeners () QSE_CPP_NOEXCEPT;
|
||||
@ -270,7 +330,6 @@ protected:
|
||||
|
||||
Callable* __lfunc;
|
||||
|
||||
|
||||
int handle_client (Socket* sock, SocketAddress* addr)
|
||||
{
|
||||
if (!this->__lfunc)
|
||||
|
Reference in New Issue
Block a user