Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_SCOPED_CLEAR_LAST_ERROR_H_ |
| 6 | #define BASE_SCOPED_CLEAR_LAST_ERROR_H_ |
| 7 | |
| 8 | #include <errno.h> |
| 9 | |
| 10 | #include "base/base_export.h" |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 11 | #include "build/build_config.h" |
| 12 | |
| 13 | namespace base { |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 14 | |
| 15 | // ScopedClearLastError stores and resets the value of thread local error codes |
| 16 | // (errno, GetLastError()), and restores them in the destructor. This is useful |
| 17 | // to avoid side effects on these values in instrumentation functions that |
| 18 | // interact with the OS. |
| 19 | |
| 20 | // Common implementation of ScopedClearLastError for all platforms. Use |
| 21 | // ScopedClearLastError instead. |
| 22 | class BASE_EXPORT ScopedClearLastErrorBase { |
| 23 | public: |
| 24 | ScopedClearLastErrorBase() : last_errno_(errno) { errno = 0; } |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 25 | ScopedClearLastErrorBase(const ScopedClearLastErrorBase&) = delete; |
| 26 | ScopedClearLastErrorBase& operator=(const ScopedClearLastErrorBase&) = delete; |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 27 | ~ScopedClearLastErrorBase() { errno = last_errno_; } |
| 28 | |
| 29 | private: |
| 30 | const int last_errno_; |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 31 | }; |
| 32 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 33 | #if BUILDFLAG(IS_WIN) |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 34 | |
| 35 | // Windows specific implementation of ScopedClearLastError. |
| 36 | class BASE_EXPORT ScopedClearLastError : public ScopedClearLastErrorBase { |
| 37 | public: |
| 38 | ScopedClearLastError(); |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 39 | ScopedClearLastError(const ScopedClearLastError&) = delete; |
| 40 | ScopedClearLastError& operator=(const ScopedClearLastError&) = delete; |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 41 | ~ScopedClearLastError(); |
| 42 | |
| 43 | private: |
Joshua Peraza | b427af26 | 2020-04-13 21:54:42 | [diff] [blame] | 44 | const unsigned long last_system_error_; |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 45 | }; |
| 46 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 47 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 48 | |
| 49 | using ScopedClearLastError = ScopedClearLastErrorBase; |
| 50 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 51 | #endif // BUILDFLAG(IS_WIN) |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 52 | |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 53 | } // namespace base |
| 54 | |
| 55 | #endif // BASE_SCOPED_CLEAR_LAST_ERROR_H_ |