Joshua Peraza | b427af26 | 2020-04-13 21:54:42 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
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" |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 11 | // TODO(crbug.com/1010217) Remove once no #includers are getting base/macros.h |
| 12 | // by including this header. |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 13 | #include "base/macros.h" |
| 14 | #include "build/build_config.h" |
| 15 | |
| 16 | namespace base { |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 17 | |
| 18 | // ScopedClearLastError stores and resets the value of thread local error codes |
| 19 | // (errno, GetLastError()), and restores them in the destructor. This is useful |
| 20 | // to avoid side effects on these values in instrumentation functions that |
| 21 | // interact with the OS. |
| 22 | |
| 23 | // Common implementation of ScopedClearLastError for all platforms. Use |
| 24 | // ScopedClearLastError instead. |
| 25 | class BASE_EXPORT ScopedClearLastErrorBase { |
| 26 | public: |
| 27 | ScopedClearLastErrorBase() : last_errno_(errno) { errno = 0; } |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 28 | ScopedClearLastErrorBase(const ScopedClearLastErrorBase&) = delete; |
| 29 | ScopedClearLastErrorBase& operator=(const ScopedClearLastErrorBase&) = delete; |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 30 | ~ScopedClearLastErrorBase() { errno = last_errno_; } |
| 31 | |
| 32 | private: |
| 33 | const int last_errno_; |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | #if defined(OS_WIN) |
| 37 | |
| 38 | // Windows specific implementation of ScopedClearLastError. |
| 39 | class BASE_EXPORT ScopedClearLastError : public ScopedClearLastErrorBase { |
| 40 | public: |
| 41 | ScopedClearLastError(); |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 42 | ScopedClearLastError(const ScopedClearLastError&) = delete; |
| 43 | ScopedClearLastError& operator=(const ScopedClearLastError&) = delete; |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 44 | ~ScopedClearLastError(); |
| 45 | |
| 46 | private: |
Joshua Peraza | b427af26 | 2020-04-13 21:54:42 | [diff] [blame] | 47 | const unsigned long last_system_error_; |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 51 | |
| 52 | using ScopedClearLastError = ScopedClearLastErrorBase; |
| 53 | |
| 54 | #endif // defined(OS_WIN) |
| 55 | |
Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 56 | } // namespace base |
| 57 | |
| 58 | #endif // BASE_SCOPED_CLEAR_LAST_ERROR_H_ |