void Peer::LoopData::down() { VLOG(3) << this << " Down() on Loop" << " LoopRefCnt: " << refCount_ << " -> " << refCount_ - 1 ; assert(refCount_); --refCount_; if (destroying_ && !refCount_) { LOG(INFO) << this << " walking uv_loop before stopping it" ; uv_walk(prepare_.loop, walkAndDumpHandlesCb< DEBUG >, this); CloseHandle closeHandle = { this, NULL }; uv_walk(prepare_.loop, walkAndCloseHandlesCb, &closeHandle); } }
// shutdown shuts down the Node's event loop and cleans up resources. void shutdown() { uv_async_init(m_uv_loop.get(), &m_async, [](uv_async_t* handle) { auto self = (Node*)(handle->data); auto timer = self->m_timer.get(); uv_timer_stop(timer); uv_close((uv_handle_t*)timer, [](uv_handle_t* handle) { auto self = (Node*)(handle->data); auto tcp = self->m_tcp.get(); auto loop = self->m_uv_loop.get(); self->m_peer_registry = nullptr; uv_close((uv_handle_t*)tcp, [](uv_handle_t* handle) { auto self = (Node*)(handle->data); auto loop = self->m_uv_loop.get(); uv_walk(loop, [](uv_handle_t* handle, void* arg) { if (uv_is_closing(handle) == 0) { uv_close(handle, [](uv_handle_t* h){}); } }, nullptr); }); }); }); m_async.data = this; uv_async_send(&m_async); }
void lws_libuv_destroyloop(struct lws_context *context, int tsi) { struct lws_context_per_thread *pt = &context->pt[tsi]; int m; if (!(context->options & LWS_SERVER_OPTION_LIBUV)) return; if (!pt->io_loop_uv) return; if (context->use_ev_sigint) uv_signal_stop(&pt->w_sigint.uv_watcher); for (m = 0; m < ARRAY_SIZE(sigs); m++) uv_signal_stop(&pt->signals[m]); if (!pt->ev_loop_foreign) { uv_stop(pt->io_loop_uv); uv_walk(pt->io_loop_uv, lws_uv_walk_cb, NULL); while (uv_run(pt->io_loop_uv, UV_RUN_NOWAIT)); m = uv_loop_close(pt->io_loop_uv); if (m == UV_EBUSY) lwsl_debug("%s: uv_loop_close: UV_EBUSY\n", __func__); lws_free(pt->io_loop_uv); } }
/* * Class: com_iwebpp_libuvpp_handles_LoopHandle * Method: _close_all * Signature: (J)V */ extern "C" JNIEXPORT void JNICALL Java_com_iwebpp_libuvpp_handles_LoopHandle__1close_1all (JNIEnv *env, jobject that, jlong ptr) { assert(ptr); uv_loop_t* loop = reinterpret_cast<uv_loop_t*>(ptr); uv_walk(loop, _close_all_cb, NULL); }
int EventBase_eventCount(struct EventBase* eventBase) { int eventCount = 0; struct EventBase_pvt* ctx = Identity_cast((struct EventBase_pvt*) eventBase); uv_walk(ctx->loop, countCallback, &eventCount); return eventCount; }
void TCPClient::closeinl() { if (isclosed_) { return; } StopReconnect(); uv_walk(&loop_, CloseWalkCB, this); LOG(INFO) << "client(" << this << ")close"; }
void EventServer::stop() { add_async([this]() { CHECK_EQ(std::this_thread::get_id(), thread_id_); uv_walk(&uv_loop_, EventServer::uv_walk_cb, nullptr); timer_handles_.clear(); poll_handles_.clear(); async_handle_ = nullptr; }); }
static int loop_gc(lua_State *L) { uv_loop_t* loop = luv_loop(L); // Call uv_close on every active handle uv_walk(loop, walk_cb, NULL); // Run the event loop until all handles are successfully closed while (uv_loop_close(loop)) { uv_run(loop, UV_RUN_DEFAULT); } return 0; }
void lws_libuv_destroyloop(struct lws_context *context, int tsi) { struct lws_context_per_thread *pt = &context->pt[tsi]; // struct lws_context *ctx; int m, budget = 100, ns; if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV)) return; if (!pt->io_loop_uv) return; lwsl_notice("%s: closing signals + timers context %p\n", __func__, context); if (context->use_ev_sigint) { uv_signal_stop(&pt->w_sigint.uv_watcher); ns = ARRAY_SIZE(sigs); if (lws_check_opt(context->options, LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN)) ns = 2; for (m = 0; m < ns; m++) { uv_signal_stop(&pt->signals[m]); uv_close((uv_handle_t *)&pt->signals[m], lws_uv_close_cb); } } uv_timer_stop(&pt->uv_timeout_watcher); uv_close((uv_handle_t *)&pt->uv_timeout_watcher, lws_uv_close_cb); uv_idle_stop(&pt->uv_idle); uv_close((uv_handle_t *)&pt->uv_idle, lws_uv_close_cb); if (pt->ev_loop_foreign) return; while (budget-- && uv_run(pt->io_loop_uv, UV_RUN_NOWAIT)) ; lwsl_notice("%s: closing all loop handles context %p\n", __func__, context); uv_stop(pt->io_loop_uv); uv_walk(pt->io_loop_uv, lws_uv_walk_cb, NULL); while (uv_run(pt->io_loop_uv, UV_RUN_NOWAIT)) ; #if UV_VERSION_MAJOR > 0 m = uv_loop_close(pt->io_loop_uv); if (m == UV_EBUSY) lwsl_err("%s: uv_loop_close: UV_EBUSY\n", __func__); #endif lws_free(pt->io_loop_uv); }
DLLEXPORT void uv_atexit_hook() { uv_loop_t* loop = jl_global_event_loop(); struct uv_shutdown_queue queue = {NULL, NULL}; uv_walk(loop, jl_uv_exitcleanup_walk, &queue); struct uv_shutdown_queue_item *item = queue.first; while (item) { uv_handle_t *handle = item->h; switch(handle->type) { case UV_TTY: case UV_UDP: //#ifndef __WIN32__ // unix only supports shutdown on TCP and NAMED_PIPE // but uv_shutdown doesn't seem to be particularly reliable, so we'll avoid it in general uv_close(handle,NULL); break; //#endif case UV_TCP: case UV_NAMED_PIPE: if (uv_is_writable((uv_stream_t*)handle)) { // uv_shutdown returns an error if not writable uv_shutdown_t *req = malloc(sizeof(uv_shutdown_t)); int err = uv_shutdown(req, (uv_stream_t*)handle, jl_shutdown_uv_cb); if (err != 0) { printf("shutdown err: %s\n", uv_strerror(uv_last_error(jl_global_event_loop())));} } else { uv_close(handle,NULL); } break; case UV_POLL: case UV_TIMER: case UV_PREPARE: case UV_CHECK: case UV_IDLE: case UV_ASYNC: case UV_SIGNAL: case UV_PROCESS: case UV_FS_EVENT: case UV_FS_POLL: uv_close(handle,NULL); //do we want to use jl_close_uv? break; case UV_HANDLE: case UV_STREAM: case UV_UNKNOWN_HANDLE: case UV_HANDLE_TYPE_MAX: case UV_RAW_FD: case UV_RAW_HANDLE: default: assert(0); } item = item->next; } uv_run(loop); //let libuv spin until everything has finished closing }
int Start(int argc, char** argv) { // Initialize debug print. InitDebugSettings(); // Create environtment. Environment* env = Environment::GetEnv(); // Parse command line arguemnts. if (!env->ParseCommandLineArgument(argc, argv)) { DLOG("ParseCommandLineArgument failed"); return 1; } // Set event loop. env->set_loop(uv_default_loop()); // Initalize JerryScript engine. if (!InitJerry(env)) { DLOG("InitJerry failed"); return 1; } // Start IoT.js if (!StartIoTjs(env)) { DLOG("StartIoTJs failed"); return 1; } // close uv loop. //uv_stop(env->loop()); uv_walk(env->loop(), UvWalkToCloseCallback, NULL); uv_run(env->loop(), UV_RUN_DEFAULT); int res = uv_loop_close(env->loop()); IOTJS_ASSERT(res == 0); // Release JerryScript engine. ReleaseJerry(); // Release environment. Environment::Release(); // Release debug print setting. ReleaseDebugSettings(); return 0; }
void GarbageCollector::runAsync() { std::vector<ScopedPointer*> deletable; { Mutex::ScopedLock lock(_mutex); if (!_tid) { _tid = uv_thread_self(); } if (!_ready.empty() || !_pending.empty()) { TraceL << "Deleting: " << "\n\tReady: " << _ready.size() << "\n\tPending: " << _pending.size() << "\n\tFinalize: " << _finalize << std::endl; // Delete waiting pointers deletable = _ready; _ready.clear(); // Swap pending pointers to the ready queue _ready.swap(_pending); } } // Delete pointers util::clearVector(deletable); // Handle finalization if (_finalize) { Mutex::ScopedLock lock(_mutex); if (_ready.empty() && _pending.empty()) { // Stop and close the timer handle. // This should cause the loop to return after // uv_close has been called on the timer handle. uv_timer_stop(_handle.ptr<uv_timer_t>()); //_handle.close(); TraceL << "Finalization complete: " << _handle.loop()->active_handles << std::endl; #ifdef _DEBUG // Print active handles, there should only be 1 left uv_walk(_handle.loop(), onPrintHandle, nullptr); //assert(_handle.loop()->active_handles <= 1); #endif } } }
static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf) { if (cnt <= 0) { uv_read_stop(stream); return; } int *interrupted = stream->data; for (int i = 0; i < cnt; i++) { if (buf->base[i] == CTRL_C) { (*interrupted)++; } } uv_loop_t write_loop; uv_loop_init(&write_loop); uv_tty_t out; uv_tty_init(&write_loop, &out, fileno(stdout), 0); uv_write_t req; uv_buf_t b = { .base = buf->base, #ifdef WIN32 .len = (ULONG)cnt #else .len = (size_t)cnt #endif }; uv_write(&req, STRUCT_CAST(uv_stream_t, &out), &b, 1, NULL); uv_run(&write_loop, UV_RUN_DEFAULT); uv_close(STRUCT_CAST(uv_handle_t, &out), NULL); uv_run(&write_loop, UV_RUN_DEFAULT); if (uv_loop_close(&write_loop)) { abort(); } free(buf->base); if (*interrupted >= 2) { uv_walk(uv_default_loop(), walk_cb, NULL); } else if (*interrupted == 1) { fprintf(stderr, "interrupt received, press again to exit\n"); } }
static int walk_final(void* vparam) { walk_param_t* param = (walk_param_t*)vparam; TUV_ASSERT(seen_timer_handle == 1); /* Loop is finished, walk_cb should not see our timer handle. */ seen_timer_handle = 0; uv_walk(param->loop, walk_cb, magic_cookie); TUV_ASSERT(seen_timer_handle == 0); TUV_ASSERT(0 == uv_loop_close(param->loop)); // cleanup tuv param free(param); // jump to next test run_tests_continue(); return 0; }
/* * Class: com_iwebpp_libuvpp_handles_LoopHandle * Method: _list * Signature: (J)[Ljava/lang/String */ extern "C" JNIEXPORT jobjectArray JNICALL Java_com_iwebpp_libuvpp_handles_LoopHandle__1list (JNIEnv *env, jobject that, jlong ptr) { assert(ptr); assert(_string_cid); uv_loop_t* loop = reinterpret_cast<uv_loop_t*>(ptr); std::vector<const char*> bag; uv_walk(loop, _list_cb, &bag); jsize size = static_cast<jsize>(bag.size()); jobjectArray handles = env->NewObjectArray(size, _string_cid, 0); OOMN(env, handles); for (int i=0; i < size; i++) { jstring s = env->NewStringUTF(bag[i]); OOMN(env, s); env->SetObjectArrayElement(handles, i, s); env->DeleteLocalRef(s); } return handles; }
void Application::finalize() { DebugLS(this) << "Finalizing" << std::endl; #ifdef _DEBUG // Print active handles uv_walk(loop, Application::onPrintHandle, nullptr); #endif // Shutdown the garbage collector to free memory GarbageCollector::destroy(); // Run until handles are closed run(); assert(loop->active_handles == 0); //assert(loop->active_reqs == 0); DebugLS(this) << "Finalization complete" << std::endl; }
int Start(int argc, char** argv) { InitDebugSettings(); // Initalize JerryScript engine. if (!InitJerry()) { DLOG("InitJerry failed"); return 1; } // Create environtment. Environment* env = Environment::GetEnv(); // Init environment with argument and uv loop. env->Init(argc, argv, uv_default_loop()); // Start IoT.js if (!StartIoTjs(env)) { DLOG("StartIoTJs failed"); return 1; } // close uv loop. //uv_stop(env->loop()); uv_walk(env->loop(), UvWalkToCloseCallback, NULL); uv_run(env->loop(), UV_RUN_DEFAULT); int res = uv_loop_close(env->loop()); IOTJS_ASSERT(res == 0); // Release JerryScript engine. ReleaseJerry(); ReleaseDebugSettings(); return 0; }
void close_loop(uv_loop_t *loop) { uv_walk(loop, close_walk_cb, NULL); uv_run(loop, UV_RUN_DEFAULT); uv_loop_close(loop); }
DLLEXPORT void uv_atexit_hook() { #if defined(JL_GC_MARKSWEEP) && defined(GC_FINAL_STATS) jl_print_gc_stats(JL_STDERR); #endif if (jl_base_module) { jl_value_t *f = jl_get_global(jl_base_module, jl_symbol("_atexit")); if (f!=NULL && jl_is_function(f)) { jl_apply((jl_function_t*)f, NULL, 0); } } uv_loop_t* loop = jl_global_event_loop(); struct uv_shutdown_queue queue = {NULL, NULL}; uv_walk(loop, jl_uv_exitcleanup_walk, &queue); // close stdout and stderr last, since we like being // able to show stuff (incl. printf's) jl_uv_exitcleanup_add((uv_handle_t*)jl_uv_stdout, &queue); jl_uv_exitcleanup_add((uv_handle_t*)jl_uv_stderr, &queue); struct uv_shutdown_queue_item *item = queue.first; while (item) { uv_handle_t *handle = item->h; if (uv_is_closing(handle)) { item = item->next; continue; } switch(handle->type) { case UV_TTY: case UV_UDP: //#ifndef __WIN32__ // unix only supports shutdown on TCP and NAMED_PIPE // but uv_shutdown doesn't seem to be particularly reliable, so we'll avoid it in general jl_close_uv(handle); break; //#endif case UV_TCP: case UV_NAMED_PIPE: if (uv_is_writable((uv_stream_t*)handle)) { // uv_shutdown returns an error if not writable uv_shutdown_t *req = malloc(sizeof(uv_shutdown_t)); int err = uv_shutdown(req, (uv_stream_t*)handle, jl_shutdown_uv_cb); if (err != 0) { printf("shutdown err: %s\n", uv_strerror(uv_last_error(jl_global_event_loop()))); jl_close_uv(handle); } } else { jl_close_uv(handle); } break; case UV_POLL: case UV_TIMER: case UV_PREPARE: case UV_CHECK: case UV_IDLE: case UV_ASYNC: case UV_SIGNAL: case UV_PROCESS: case UV_FS_EVENT: case UV_FS_POLL: jl_close_uv(handle); break; case UV_HANDLE: case UV_STREAM: case UV_UNKNOWN_HANDLE: case UV_HANDLE_TYPE_MAX: case UV_RAW_FD: case UV_RAW_HANDLE: default: assert(0); } item = item->next; } uv_run(loop,UV_RUN_DEFAULT); //let libuv spin until everything has finished closing }
static void loop_close(uv_loop_t *loop) { uv_walk(loop, walk_close_cb, NULL); uv_run(loop, UV_RUN_DEFAULT); uv_loop_close(loop); }
LWS_VISIBLE void lws_close_all_handles_in_loop(uv_loop_t *loop) { uv_walk(loop, lws_uv_walk_cb, NULL); }
extern "C" void rust_uv_walk(uv_loop_t* loop, uv_walk_cb cb, void* arg) { uv_walk(loop, cb, arg); }
DLLEXPORT void uv_atexit_hook() { #if defined(JL_GC_MARKSWEEP) && defined(GC_FINAL_STATS) jl_print_gc_stats(JL_STDERR); #endif if (jl_base_module) { jl_value_t *f = jl_get_global(jl_base_module, jl_symbol("_atexit")); if (f!=NULL && jl_is_function(f)) { jl_apply((jl_function_t*)f, NULL, 0); } } uv_loop_t* loop = jl_global_event_loop(); struct uv_shutdown_queue queue = {NULL, NULL}; uv_walk(loop, jl_uv_exitcleanup_walk, &queue); // close stdout and stderr last, since we like being // able to show stuff (incl. printf's) jl_uv_exitcleanup_add((uv_handle_t*)jl_uv_stdout, &queue); jl_uv_exitcleanup_add((uv_handle_t*)jl_uv_stderr, &queue); struct uv_shutdown_queue_item *item = queue.first; while (item) { uv_handle_t *handle = item->h; if (uv_is_closing(handle)) { item = item->next; continue; } switch(handle->type) { case UV_TTY: case UV_UDP: //#ifndef _OS_WINDOWS_ // unix only supports shutdown on TCP and NAMED_PIPE // but uv_shutdown doesn't seem to be particularly reliable, so we'll avoid it in general jl_close_uv(handle); break; //#endif case UV_TCP: case UV_NAMED_PIPE: // These will be shut down in jl_close_uv. jl_close_uv(handle); break; //Don't close these directly, but rather let the GC take care of it case UV_POLL: uv_poll_stop((uv_poll_t*)handle); handle->data = NULL; uv_unref(handle); break; case UV_TIMER: uv_timer_stop((uv_timer_t*)handle); handle->data = NULL; uv_unref(handle); break; case UV_IDLE: uv_idle_stop((uv_idle_t*)handle); case UV_ASYNC: handle->data = NULL; uv_unref(handle); break; case UV_FS_EVENT: handle->data = NULL; uv_unref(handle); break; case UV_FS_POLL: uv_fs_poll_stop((uv_fs_poll_t*)handle); handle->data = NULL; uv_unref(handle); break; case UV_PREPARE: case UV_CHECK: case UV_SIGNAL: case UV_PROCESS: jl_close_uv(handle); break; case UV_HANDLE: case UV_STREAM: case UV_UNKNOWN_HANDLE: case UV_HANDLE_TYPE_MAX: case UV_RAW_FD: case UV_RAW_HANDLE: default: assert(0); } item = item->next; } uv_run(loop,UV_RUN_DEFAULT); //let libuv spin until everything has finished closing }
static void timer_cb(uv_timer_t* handle) { TUV_ASSERT(handle == &timer); uv_walk(handle->loop, walk_cb, magic_cookie); uv_close((uv_handle_t*)handle, NULL); }
static void gc_mark_uv_state(uv_loop_t *loop) { uv_walk(loop,gc_mark_uv_handle,0); }