diff options
author | Determinant <[email protected]> | 2019-07-03 00:42:21 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2019-07-03 00:42:21 -0400 |
commit | 2ef9c99438e5c87651332f47c98da8d397b030e3 (patch) | |
tree | 7c79da536fa4b7b26c5fd595abfb944061a9ca33 | |
parent | b7e06f0f00710765113bbc560fcbaf39e466cbce (diff) |
throw exception when attempt to call on stopped ThreadCall
-rw-r--r-- | include/salticidae/conn.h | 4 | ||||
-rw-r--r-- | include/salticidae/event.h | 52 | ||||
-rw-r--r-- | include/salticidae/util.h | 3 | ||||
-rw-r--r-- | src/util.cpp | 3 |
4 files changed, 44 insertions, 18 deletions
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 0058274..6b9b486 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -460,13 +460,13 @@ class ConnPool { disp_tcall = workers[0].get_tcall(); workers[0].set_dispatcher(); disp_error_cb = [this](const std::exception_ptr err) { + workers[0].stop_tcall(); + disp_ec.stop(); user_tcall->async_call([this, err](ThreadCall::Handle &) { stop_workers(); //std::rethrow_exception(err); if (error_cb) error_cb(err, true, -1); }); - disp_ec.stop(); - workers[0].stop_tcall(); }; worker_error_cb = [this](const std::exception_ptr err) { diff --git a/include/salticidae/event.h b/include/salticidae/event.h index 30d8f3c..960588c 100644 --- a/include/salticidae/event.h +++ b/include/salticidae/event.h @@ -55,7 +55,12 @@ using _event_context_ot = ArcObj<uv_loop_t, _event_context_deleter>; class EventContext: public _event_context_ot { public: EventContext(): _event_context_ot(new uv_loop_t()) { - uv_loop_init(get()); + if (uv_loop_init(get()) < 0) + { + delete obj; + obj = nullptr; + throw SalticidaeError(SALTI_ERROR_LIBUV_INIT); + } } EventContext(uv_loop_t *eb): _event_context_ot(eb) {} EventContext(const EventContext &) = default; @@ -96,7 +101,8 @@ class FdEvent { FdEvent(const EventContext &ec, int fd, callback_t callback): ec(ec), fd(fd), ev_fd(new uv_poll_t()), callback(std::move(callback)) { - uv_poll_init(ec.get(), ev_fd, fd); + if (uv_poll_init(ec.get(), ev_fd, fd) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_INIT); ev_fd->data = this; } @@ -147,7 +153,8 @@ class FdEvent { void add(int events) { assert(ev_fd != nullptr); - uv_poll_start(ev_fd, events, FdEvent::fd_then); + if (uv_poll_start(ev_fd, events, FdEvent::fd_then) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_START); } void del() { @@ -177,7 +184,8 @@ class TimerEvent { TimerEvent(const EventContext &ec, callback_t callback): ec(ec), ev_timer(new uv_timer_t()), callback(std::move(callback)) { - uv_timer_init(ec.get(), ev_timer); + if (uv_timer_init(ec.get(), ev_timer) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_INIT); ev_timer->data = this; } @@ -227,7 +235,8 @@ class TimerEvent { void add(double t_sec) { assert(ev_timer != nullptr); - uv_timer_start(ev_timer, TimerEvent::timer_then, uint64_t(t_sec * 1000), 0); + if (uv_timer_start(ev_timer, TimerEvent::timer_then, uint64_t(t_sec * 1000), 0) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_START); } void del() { @@ -258,8 +267,9 @@ class TimedFdEvent: public FdEvent, public TimerEvent { events |= ERROR; auto event = static_cast<TimedFdEvent *>(h->data); event->TimerEvent::del(); - uv_timer_start(event->ev_timer, TimedFdEvent::timer_then, - event->timeout, 0); + if (uv_timer_start(event->ev_timer, TimedFdEvent::timer_then, + event->timeout, 0) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_START); event->callback(event->fd, events); } @@ -324,9 +334,11 @@ class TimedFdEvent: public FdEvent, public TimerEvent { void add(int events, double t_sec) { assert(ev_fd != nullptr && ev_timer != nullptr); - uv_timer_start(ev_timer, TimedFdEvent::timer_then, - timeout = uint64_t(t_sec * 1000), 0); - uv_poll_start(ev_fd, events, TimedFdEvent::fd_then); + auto ret1 = uv_timer_start(ev_timer, TimedFdEvent::timer_then, + timeout = uint64_t(t_sec * 1000), 0); + auto ret2 = uv_poll_start(ev_fd, events, TimedFdEvent::fd_then); + if (ret1 < 0 || ret2 < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_START); } void del() { @@ -354,7 +366,8 @@ class SigEvent { SigEvent(const EventContext &ec, callback_t callback): ec(ec), ev_sig(new uv_signal_t()), callback(std::move(callback)) { - uv_signal_init(ec.get(), ev_sig); + if (uv_signal_init(ec.get(), ev_sig) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_INIT); ev_sig->data = this; } @@ -404,12 +417,14 @@ class SigEvent { void add(int signum) { assert(ev_sig != nullptr); - uv_signal_start(ev_sig, SigEvent::sig_then, signum); + if (uv_signal_start(ev_sig, SigEvent::sig_then, signum) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_START); } void add_once(int signum) { assert(ev_sig != nullptr); - uv_signal_start_oneshot(ev_sig, SigEvent::sig_then, signum); + if (uv_signal_start_oneshot(ev_sig, SigEvent::sig_then, signum) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_START); } void del() { @@ -438,7 +453,8 @@ class CheckEvent { CheckEvent(const EventContext &ec, callback_t callback): ec(ec), ev_check(new uv_check_t()), callback(std::move(callback)) { - uv_check_init(ec.get(), ev_check); + if (uv_check_init(ec.get(), ev_check) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_INIT); ev_check->data = this; } @@ -488,7 +504,8 @@ class CheckEvent { void add() { assert(ev_check != nullptr); - uv_check_start(ev_check, CheckEvent::check_then); + if (uv_check_start(ev_check, CheckEvent::check_then) < 0) + throw SalticidaeError(SALTI_ERROR_LIBUV_START); } void del() { @@ -708,14 +725,17 @@ class ThreadCall { } template<typename Func> - void async_call(Func &&callback) { + bool async_call(Func &&callback) { + if (stopped) return false; auto h = new Handle(); h->callback = std::forward<Func>(callback); q.enqueue(h); + return true; } template<typename Func> Result call(Func &&callback) { + if (stopped) throw SalticidaeError(SALTI_ERROR_NOT_AVAIL); auto h = new Handle(); h->callback = std::forward<Func>(callback); ThreadNotifier<Result> notifier; diff --git a/include/salticidae/util.h b/include/salticidae/util.h index ee0fbe3..3196042 100644 --- a/include/salticidae/util.h +++ b/include/salticidae/util.h @@ -101,8 +101,11 @@ enum SalticidaeErrorCode { SALTI_ERROR_TLS_KEY_NOT_MATCH, SALTI_ERROR_TLS_NO_PEER_CERT, SALTI_ERROR_FD, + SALTI_ERROR_LIBUV_INIT, + SALTI_ERROR_LIBUV_START, SALTI_ERROR_RAND_SOURCE, SALTI_ERROR_CONN_NOT_READY, + SALTI_ERROR_NOT_AVAIL, SALTI_ERROR_UNKNOWN }; diff --git a/src/util.cpp b/src/util.cpp index 7af1255..19b6915 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -58,8 +58,11 @@ const char *SALTICIDAE_ERROR_STRINGS[] = { "tls key does not match the cert", "tls fail to get peer cert", "fd error", + "libuv init failed", + "libuv start failed", "rand source is not available, try again", "connection is not ready", + "operation not available", "unknown error" }; |