1*523fa7a6SAndroid Build Coastguard Worker /* 2*523fa7a6SAndroid Build Coastguard Worker * Copyright (c) Meta Platforms, Inc. and affiliates. 3*523fa7a6SAndroid Build Coastguard Worker * All rights reserved. 4*523fa7a6SAndroid Build Coastguard Worker * 5*523fa7a6SAndroid Build Coastguard Worker * This source code is licensed under the BSD-style license found in the 6*523fa7a6SAndroid Build Coastguard Worker * LICENSE file in the root directory of this source tree. 7*523fa7a6SAndroid Build Coastguard Worker */ 8*523fa7a6SAndroid Build Coastguard Worker 9*523fa7a6SAndroid Build Coastguard Worker #pragma once 10*523fa7a6SAndroid Build Coastguard Worker 11*523fa7a6SAndroid Build Coastguard Worker namespace executorch::extension::threadpool { 12*523fa7a6SAndroid Build Coastguard Worker 13*523fa7a6SAndroid Build Coastguard Worker // A RAII, thread local (!) guard that enables or disables guard upon 14*523fa7a6SAndroid Build Coastguard Worker // construction, and sets it back to the original value upon destruction. 15*523fa7a6SAndroid Build Coastguard Worker struct NoThreadPoolGuard { 16*523fa7a6SAndroid Build Coastguard Worker static bool is_enabled(); 17*523fa7a6SAndroid Build Coastguard Worker static void set_enabled(bool enabled); 18*523fa7a6SAndroid Build Coastguard Worker NoThreadPoolGuardNoThreadPoolGuard19*523fa7a6SAndroid Build Coastguard Worker NoThreadPoolGuard() : prev_mode_(NoThreadPoolGuard::is_enabled()) { 20*523fa7a6SAndroid Build Coastguard Worker NoThreadPoolGuard::set_enabled(true); 21*523fa7a6SAndroid Build Coastguard Worker } ~NoThreadPoolGuardNoThreadPoolGuard22*523fa7a6SAndroid Build Coastguard Worker ~NoThreadPoolGuard() { 23*523fa7a6SAndroid Build Coastguard Worker NoThreadPoolGuard::set_enabled(prev_mode_); 24*523fa7a6SAndroid Build Coastguard Worker } 25*523fa7a6SAndroid Build Coastguard Worker 26*523fa7a6SAndroid Build Coastguard Worker private: 27*523fa7a6SAndroid Build Coastguard Worker const bool prev_mode_; 28*523fa7a6SAndroid Build Coastguard Worker }; 29*523fa7a6SAndroid Build Coastguard Worker 30*523fa7a6SAndroid Build Coastguard Worker } // namespace executorch::extension::threadpool 31*523fa7a6SAndroid Build Coastguard Worker 32*523fa7a6SAndroid Build Coastguard Worker namespace torch::executorch::threadpool { // DEPRECATED 33*523fa7a6SAndroid Build Coastguard Worker // TODO(T197294990): Remove these deprecated aliases once all users have moved 34*523fa7a6SAndroid Build Coastguard Worker // to the new `::executorch` namespaces. Note that threadpool incorrectly used 35*523fa7a6SAndroid Build Coastguard Worker // the namespace `torch::executorch` instead of `torch::executor`. 36*523fa7a6SAndroid Build Coastguard Worker using ::executorch::extension::threadpool::NoThreadPoolGuard; // DEPRECATED 37*523fa7a6SAndroid Build Coastguard Worker } // namespace torch::executorch::threadpool 38