1 /* 2 * 3 * Copyright 2019 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #ifndef GRPC_CORE_LIB_IOMGR_POLLER_EVENTMANAGER_LIBUV_H 20 #define GRPC_CORE_LIB_IOMGR_POLLER_EVENTMANAGER_LIBUV_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <string> 25 #include <vector> 26 27 #include "src/core/lib/gprpp/atomic.h" 28 #include "src/core/lib/gprpp/sync.h" 29 #include "src/core/lib/gprpp/thd.h" 30 31 namespace grpc { 32 namespace experimental { 33 34 class LibuvEventManager { 35 public: 36 class Options { 37 public: 38 Options(); 39 explicit Options(int num_workers); 40 num_workers()41 int num_workers() const { return num_workers_; } set_num_workers(int num)42 void set_num_workers(int num) { num_workers_ = num; } 43 thread_name_prefix()44 const std::string& thread_name_prefix() const { 45 return thread_name_prefix_; 46 } set_thread_name_prefix(const std::string & name)47 void set_thread_name_prefix(const std::string& name) { 48 thread_name_prefix_ = name; 49 } 50 51 private: 52 // Number of worker threads to create at startup. If less than 0, uses the 53 // default value of 32. 54 int num_workers_; 55 // Name prefix used for worker. 56 std::string thread_name_prefix_; 57 }; 58 59 explicit LibuvEventManager(const Options& options); 60 virtual ~LibuvEventManager(); 61 62 void Shutdown(); 63 void ShutdownRef(); 64 void ShutdownUnref(); 65 66 private: 67 // Function run by the worker threads. 68 void RunWorkerLoop(); 69 70 // Whether the EventManager has been shut down. 71 bool ShouldStop(); 72 73 const Options options_; 74 // Whether the EventManager workers should be stopped. 75 grpc_core::Atomic<bool> should_stop_{false}; 76 // A refcount preventing the EventManager from shutdown. 77 grpc_core::Atomic<int> shutdown_refcount_{0}; 78 // Worker threads of the EventManager. 79 std::vector<grpc_core::Thread> workers_; 80 // Mutex and condition variable used for shutdown. 81 grpc_core::Mutex shutdown_mu_; 82 grpc_core::CondVar shutdown_cv_; 83 }; 84 85 } // namespace experimental 86 } // namespace grpc 87 88 #endif /* GRPC_CORE_LIB_IOMGR_POLLER_EVENTMANAGER_LIBUV_H */ 89