diff --git a/connection.cpp b/connection.cpp index 87b5e27..7321559 100644 --- a/connection.cpp +++ b/connection.cpp @@ -3,7 +3,6 @@ #include "utils.h" #include "protocol.h" -#include "connection.h" Connection::Connection(): @@ -17,7 +16,7 @@ Protocol& Connection::getProtocol() const { assert(socket); return socket->protocol; } const Address& Connection::getRemoteAddress() const { assert(socket); return socket->address; } -const ::Handle& Connection::getServer() const +const Server::Handle& Connection::getServer() const { assert(socket); return socket->server; } diff --git a/connection.h b/connection.h index a26eb98..dbe6916 100644 --- a/connection.h +++ b/connection.h @@ -27,7 +27,7 @@ class Server; class Connection: public Shared { public: - typedef ::Handle Handle; + typedef THandle Handle; typedef unsigned long long ReqId; typedef std::recursive_mutex Mutex; @@ -137,7 +137,7 @@ protected: // mutex must be locked before calling of all following methods Protocol& getProtocol() const; const Address& getRemoteAddress() const; - const ::Handle& getServer() const; + const THandle& getServer() const; virtual void onOpen(); virtual void onCloseRequested(); diff --git a/handle.h b/handle.h index 1017b5f..ac67f74 100644 --- a/handle.h +++ b/handle.h @@ -10,6 +10,8 @@ class Shared; +template class THandle; +template class TWeakHandle; @@ -32,6 +34,9 @@ private: class Shared { +public: + typedef THandle Handle; + private: friend class HandleBase; friend class WeakHandleBase; @@ -60,9 +65,9 @@ private: protected: inline WeakHandleBase(): counter() { } - void set(Shared* pointer) { set(pointer ? pointer->counter : nullptr); } - void set(const WeakHandleBase &other) { set(other.counter); } - void swap(WeakHandleBase &other) { std::swap(counter, other.counter); } + inline void set(Shared* pointer) { set(pointer ? pointer->counter : nullptr); } + inline void set(const WeakHandleBase &other) { set(other.counter); } + inline void swap(WeakHandleBase &other) { std::swap(counter, other.counter); } public: inline ~WeakHandleBase() @@ -137,75 +142,69 @@ public: -template class Handle; - - template -class WeakHandle: public WeakHandleBase { +class TWeakHandle: public WeakHandleBase { public: typedef T Type; - typedef ::Handle Handle; + typedef THandle Handle; private: inline void typeChecker(const Type*) { } public: - inline WeakHandle() { } - inline WeakHandle(const std::nullptr_t) { } - inline WeakHandle(WeakHandle &&other) { WeakHandleBase::swap(other); } - inline WeakHandle(const WeakHandle &other) { WeakHandleBase::set(other.get()); } - inline explicit WeakHandle(const Handle &handle) { WeakHandleBase::set(handle.pointer()); } - inline explicit WeakHandle(Type *pointer) { WeakHandleBase::set(pointer); } - - inline WeakHandle& operator=(WeakHandle &&other) + inline TWeakHandle() { } + inline TWeakHandle(const std::nullptr_t) { } + inline TWeakHandle(TWeakHandle &&other) { WeakHandleBase::swap(other); } + inline TWeakHandle(const TWeakHandle &other) { WeakHandleBase::set(other); } + inline explicit TWeakHandle(const Handle &handle) { WeakHandleBase::set(handle.pointer()); } + + inline TWeakHandle& operator=(TWeakHandle &&other) { WeakHandleBase::swap(other); return *this; } - inline WeakHandle& operator=(const WeakHandle &other) + inline TWeakHandle& operator=(const TWeakHandle &other) { WeakHandleBase::set(other); return *this; } - inline WeakHandle& operator=(const Handle &handle) + inline TWeakHandle& operator=(const Handle &handle) { WeakHandleBase::set(handle.pointer()); return *this; } - inline WeakHandle& operator=(Type *pointer) - { WeakHandleBase::set(pointer); return *this; } - inline void swap(WeakHandle &other) + inline void swap(TWeakHandle &other) { WeakHandleBase::swap(other); return *this; } template - inline operator WeakHandle&() - { typeChecker((TT*)nullptr); return *reinterpret_cast*>(this); } + inline operator TWeakHandle&() + { typeChecker((TT*)nullptr); return *reinterpret_cast*>(this); } template - inline operator const WeakHandle&() const - { typeChecker((TT*)nullptr); return *reinterpret_cast*>(this); } + inline operator const TWeakHandle&() const + { typeChecker((TT*)nullptr); return *reinterpret_cast*>(this); } }; template -class Handle: public HandleBase { +class THandle: public HandleBase { public: typedef T Type; - typedef WeakHandle Weak; + typedef TWeakHandle Weak; private: inline void typeChecker(const Type*) { } public: - inline Handle() { } - inline Handle(const std::nullptr_t) { } - inline Handle(Handle &&other) { HandleBase::swap(other); } - inline Handle(const Handle &other) { HandleBase::set(other.get()); } - inline explicit Handle(const Weak &weak) { HandleBase::set(weak); } - inline explicit Handle(Type *pointer) { HandleBase::set(pointer); } - - inline Handle& operator=(Handle &&other) + inline THandle() { } + inline THandle(const std::nullptr_t) { } + inline THandle(THandle &&other) { HandleBase::swap(other); } + inline THandle(const THandle &other) { HandleBase::set(other.get()); } + inline explicit THandle(const Weak &weak) { HandleBase::set(weak); } + inline explicit THandle(Type *pointer) { HandleBase::set(pointer); } + + inline THandle& operator=(THandle &&other) { HandleBase::swap(other); return *this; } - inline Handle& operator=(const Handle &other) + inline THandle& operator=(const THandle &other) { HandleBase::set(other.get()); return *this; } - inline Handle& operator=(const Weak &weak) + inline THandle& operator=(const Weak &weak) { HandleBase::set(weak); return *this; } - inline Handle& operator=(Type *pointer) + inline THandle& operator=(Type *pointer) { HandleBase::set(pointer); return *this; } - inline void swap(Handle &other) + inline void swap(THandle &other) { HandleBase::swap(other); return *this; } inline Type* pointer() const { return (Type*)HandleBase::get(); } @@ -213,11 +212,11 @@ public: inline Type& operator*() const { assert(pointer()); return *pointer(); } template - inline operator Handle&() - { typeChecker((TT*)nullptr); return *reinterpret_cast*>(this); } + inline operator THandle&() + { typeChecker((TT*)nullptr); return *reinterpret_cast*>(this); } template - inline operator const Handle&() const - { typeChecker((TT*)nullptr); return *reinterpret_cast*>(this); } + inline operator const THandle&() const + { typeChecker((TT*)nullptr); return *reinterpret_cast*>(this); } }; diff --git a/protocol.h b/protocol.h index 71fb875..1fec707 100644 --- a/protocol.h +++ b/protocol.h @@ -64,7 +64,7 @@ public: class Protocol: public Shared { public: - typedef ::Handle Handle; + typedef THandle Handle; typedef std::recursive_mutex Mutex; typedef std::lock_guard Lock; diff --git a/server.h b/server.h index 5f45272..1253ea5 100644 --- a/server.h +++ b/server.h @@ -23,7 +23,7 @@ enum : ErrorCode { class Server: public Shared { public: - typedef ::Handle Handle; + typedef THandle Handle; typedef std::recursive_mutex Mutex; typedef std::lock_guard Lock; @@ -47,8 +47,8 @@ protected: void onOpen(); void onClose(ErrorCode errorCode); - ::Handle onConnect(const Address &remoteAddres); - void onDisconnect(const ::Handle &connection); + THandle onConnect(const Address &remoteAddres); + void onDisconnect(const THandle &connection); }; diff --git a/tcpprotocol.h b/tcpprotocol.h index 5f2ef2f..7da4d78 100644 --- a/tcpprotocol.h +++ b/tcpprotocol.h @@ -43,8 +43,8 @@ public: TcpProtocol(); ErrorCode resolve(Address &address) override; - ErrorCode connect(const ::Handle &connection, const Address &address) override; - ErrorCode listen(const ::Handle &server, const Address &address) override; + ErrorCode connect(const Connection::Handle &connection, const Address &address) override; + ErrorCode listen(const Server::Handle &server, const Address &address) override; void start(); void stop(); }; diff --git a/threadpool.cpp b/threadpool.cpp index 3a83c8d..bb62e10 100644 --- a/threadpool.cpp +++ b/threadpool.cpp @@ -17,7 +17,7 @@ void ThreadPool::threadRun(Thread &thread) { std::unique_lock lock(mutex); thread.taskId = 0; - thread.taskOwner = nullptr; + thread.taskOwner.reset(); thread.condition.notify_all(); while(queue.empty() && !thread.stopping) @@ -28,7 +28,7 @@ void ThreadPool::threadRun(Thread &thread) { TaskDesc &desc = queue.front(); thread.taskId = desc.id; thread.taskOwner = desc.owner; - task = desc.task; + task = thread.taskOwner ? desc.task : nullptr; queue.pop_front(); } @@ -39,9 +39,9 @@ void ThreadPool::threadRun(Thread &thread) { } -ThreadPool::TaskId ThreadPool::enqueue(const Task &task, const void *owner) { +ThreadPool::TaskId ThreadPool::enqueue(const Task &task, const TaskOwner::Handle &owner) { std::lock_guard lock(mutex); - queue.emplace_back(++lastId, owner, task); + queue.emplace_back(++lastId, TaskOwner::Handle::Weak(owner), task); condition.notify_one(); return lastId; } @@ -60,11 +60,11 @@ int ThreadPool::cancelById(TaskId id, bool wait) { } -int ThreadPool::cancelByOwner(const void *owner, bool wait) { +int ThreadPool::cancelByOwner(const TaskOwner::Handle &owner, bool wait) { std::unique_lock lock(mutex); int count = 0; for(Queue::iterator i = queue.begin(); i != queue.end(); ++i) - if (i->owner == owner) + if (TaskOwner::Handle(i->owner) == owner) { i = queue.erase(i); ++count; } if (wait) for(ThreadList::iterator i = threads.begin(); i != threads.end(); ++i) diff --git a/threadpool.h b/threadpool.h index 5ab5448..2fffd23 100644 --- a/threadpool.h +++ b/threadpool.h @@ -10,12 +10,10 @@ #include +#include "common.h" -class TaskOwner { -public: - TaskOwner(); - virtual ~TaskOwner(); -}; + +typedef Shared TaskOwner; class ThreadPool { @@ -25,12 +23,12 @@ public: struct TaskDesc { TaskId id; - const void* owner; + TaskOwner::Handle::Weak owner; Task task; inline TaskDesc(): id(), owner(), task(nullptr) { } - inline TaskDesc(TaskId id, const void* owner, const Task &task): + inline TaskDesc(TaskId id, const TaskOwner::Handle::Weak &owner, const Task &task): id(id), owner(owner), task(task) { } }; @@ -38,7 +36,7 @@ public: std::thread *thread; std::condition_variable condition; TaskId taskId; - const void* taskOwner; + TaskOwner::Handle taskOwner; bool stopping; inline Thread(): @@ -62,9 +60,9 @@ public: ThreadPool(); ~ThreadPool(); - TaskId enqueue(const Task &task, const void *owner = nullptr); + TaskId enqueue(const Task &task, const TaskOwner::Handle &owner = nullptr); int cancelById(TaskId id, bool wait = true); - int cancelByOwner(const void *owner, bool wait = true); + int cancelByOwner(const TaskOwner::Handle &owner, bool wait = true); int cancelAll(bool wait = true); void start(int count, bool cancelAllTasks = false);