From commits-return-96559-archive-asf-public=cust-asf.ponee.io@mxnet.incubator.apache.org Thu Aug 8 20:28:08 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id EA768180642 for ; Thu, 8 Aug 2019 22:28:07 +0200 (CEST) Received: (qmail 99936 invoked by uid 500); 8 Aug 2019 20:28:07 -0000 Mailing-List: contact commits-help@mxnet.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@mxnet.incubator.apache.org Delivered-To: mailing list commits@mxnet.incubator.apache.org Received: (qmail 99927 invoked by uid 99); 8 Aug 2019 20:28:07 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 08 Aug 2019 20:28:07 +0000 From: GitBox To: commits@mxnet.apache.org Subject: [GitHub] [incubator-mxnet] mseth10 commented on a change in pull request #15762: Refactor LibraryInitializer so it's thread safe. Fixes random sporadical concurrency crashes. Message-ID: <156529608726.22375.8932996929125565476.gitbox@gitbox.apache.org> Date: Thu, 08 Aug 2019 20:28:07 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit mseth10 commented on a change in pull request #15762: Refactor LibraryInitializer so it's thread safe. Fixes random sporadical concurrency crashes. URL: https://github.com/apache/incubator-mxnet/pull/15762#discussion_r312228337 ########## File path: src/initialize.cc ########## @@ -41,65 +67,188 @@ static void SegfaultLogger(int sig) { } #endif -class LibraryInitializer { - public: - LibraryInitializer() { - dmlc::InitLogging("mxnet"); -#if MXNET_USE_SIGNAL_HANDLER && DMLC_LOG_STACK_TRACE - struct sigaction sa; - sigaction(SIGSEGV, nullptr, &sa); - if (sa.sa_handler == nullptr) { - signal(SIGSEGV, SegfaultLogger); +// pthread_atfork handlers, delegated to LibraryInitializer members. + +void pthread_atfork_prepare() { + LibraryInitializer* library_initializer = LibraryInitializer::Get(); + library_initializer->atfork_prepare(); +} + +void pthread_atfork_parent() { + LibraryInitializer* library_initializer = LibraryInitializer::Get(); + library_initializer->atfork_parent(); +} + +void pthread_atfork_child() { + LibraryInitializer* library_initializer = LibraryInitializer::Get(); + library_initializer->atfork_child(); +} + +// LibraryInitializer member functions + +LibraryInitializer::LibraryInitializer() + : original_pid_(common::current_process_id()), + mp_worker_nthreads_(dmlc::GetEnv("MXNET_MP_WORKER_NTHREADS", 1)), + cpu_worker_nthreads_(dmlc::GetEnv("MXNET_CPU_WORKER_NTHREADS", 1)), + mp_cv_num_threads_(dmlc::GetEnv("MXNET_MP_OPENCV_NUM_THREADS", 0)) { + dmlc::InitLogging("mxnet"); + engine::OpenMP::Get(); // force OpenMP initialization + install_signal_handlers(); + install_pthread_atfork_handlers(); +} + +LibraryInitializer::~LibraryInitializer() { + close_open_libs(); +} + +bool LibraryInitializer::lib_is_loaded(const std::string& path) const { + return loaded_libs.count(path) > 0; +} + +/*! + * \brief Loads the dynamic shared library file + * \param path library file location + * \return handle a pointer for the loaded library, throws dmlc::error if library can't be loaded + */ +void* LibraryInitializer::lib_load(const char* path) { + void *handle = nullptr; + // check if library was already loaded + if (!lib_is_loaded(path)) { + // if not, load it +#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__) + handle = LoadLibrary(path); + if (!handle) { + char *err_msg = nullptr; + win_err(&err_msg); + LOG(FATAL) << "Error loading library: '" << path << "'\n" << err_msg; + LocalFree(err_msg); + return nullptr; } -#endif +#else + handle = dlopen(path, RTLD_LAZY); + if (!handle) { + LOG(FATAL) << "Error loading library: '" << path << "'\n" << dlerror(); + return nullptr; + } +#endif // _WIN32 or _WIN64 or __WINDOWS__ + // then store the pointer to the library + loaded_libs[path] = handle; + } else { + loaded_libs.at(path); Review comment: assign this to handle? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services