pytorch/caffe2/utils/threadpool/thread_pool_guard.h
Akshit Khurana ecd8e4c1d5 Add guard to run on current thread (#52361)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/52361

Test Plan:
buck build //xplat/caffe2:aten_test_test_thread_pool_guard
./aten_test_test_thread_pool_guard

Reviewed By: kimishpatel

Differential Revision: D26429540

fbshipit-source-id: 16e4a56d4bf9b73b1ea1ff88d7dc6730e0b1e029
2021-03-03 11:43:40 -08:00

24 lines
567 B
C++

#pragma once
#include <c10/macros/Macros.h>
namespace caffe2 {
// A RAII, thread local (!) guard that enables or disables grad mode upon
// construction, and sets it back to the original value upon destruction.
struct TORCH_API _NoPThreadPoolGuard {
static bool is_enabled();
static void set_enabled(bool enabled);
_NoPThreadPoolGuard(): prev_mode_(_NoPThreadPoolGuard::is_enabled()) {
_NoPThreadPoolGuard::set_enabled(true);
}
~_NoPThreadPoolGuard() {
_NoPThreadPoolGuard::set_enabled(prev_mode_);
}
private:
bool prev_mode_;
};
}