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