Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [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 | // This provides a wrapper around system calls which may be interrupted by a | ||||
6 | // signal and return EINTR. See man 7 signal. | ||||
[email protected] | c143c72 | 2013-01-23 00:21:39 | [diff] [blame] | 7 | // To prevent long-lasting loops (which would likely be a bug, such as a signal |
8 | // that should be masked) to go unnoticed, there is a limit after which the | ||||
9 | // caller will nonetheless see an EINTR in Debug builds. | ||||
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 10 | // |
Sergey Ulanov | 34b6bbd | 2017-08-25 21:15:01 | [diff] [blame] | 11 | // On Windows and Fuchsia, this wrapper macro does nothing because there are no |
12 | // signals. | ||||
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 13 | // |
14 | // Don't wrap close calls in HANDLE_EINTR. Use IGNORE_EINTR if the return | ||||
15 | // value of close is significant. See http://crbug.com/269623. | ||||
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 16 | |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 17 | #ifndef BASE_POSIX_EINTR_WRAPPER_H_ |
18 | #define BASE_POSIX_EINTR_WRAPPER_H_ | ||||
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 19 | |
20 | #include "build/build_config.h" | ||||
21 | |||||
Xiaohan Wang | 1a8409ee | 2022-01-15 14:56:41 | [diff] [blame] | 22 | #if BUILDFLAG(IS_POSIX) |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 23 | |
24 | #include <errno.h> | ||||
25 | |||||
[email protected] | c143c72 | 2013-01-23 00:21:39 | [diff] [blame] | 26 | #if defined(NDEBUG) |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 27 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 28 | #define HANDLE_EINTR(x) \ |
29 | ({ \ | ||||
30 | decltype(x) eintr_wrapper_result; \ | ||||
31 | do { \ | ||||
32 | eintr_wrapper_result = (x); \ | ||||
33 | } while (eintr_wrapper_result == -1 && errno == EINTR); \ | ||||
34 | eintr_wrapper_result; \ | ||||
35 | }) | ||||
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 36 | |
37 | #else | ||||
38 | |||||
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 39 | #define HANDLE_EINTR(x) \ |
40 | ({ \ | ||||
41 | int eintr_wrapper_counter = 0; \ | ||||
42 | decltype(x) eintr_wrapper_result; \ | ||||
43 | do { \ | ||||
44 | eintr_wrapper_result = (x); \ | ||||
45 | } while (eintr_wrapper_result == -1 && errno == EINTR && \ | ||||
46 | eintr_wrapper_counter++ < 100); \ | ||||
47 | eintr_wrapper_result; \ | ||||
48 | }) | ||||
[email protected] | c143c72 | 2013-01-23 00:21:39 | [diff] [blame] | 49 | |
50 | #endif // NDEBUG | ||||
51 | |||||
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 52 | #define IGNORE_EINTR(x) \ |
53 | ({ \ | ||||
54 | decltype(x) eintr_wrapper_result; \ | ||||
55 | do { \ | ||||
56 | eintr_wrapper_result = (x); \ | ||||
57 | if (eintr_wrapper_result == -1 && errno == EINTR) { \ | ||||
58 | eintr_wrapper_result = 0; \ | ||||
59 | } \ | ||||
60 | } while (0); \ | ||||
61 | eintr_wrapper_result; \ | ||||
62 | }) | ||||
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 63 | |
Xiaohan Wang | 1a8409ee | 2022-01-15 14:56:41 | [diff] [blame] | 64 | #else // !BUILDFLAG(IS_POSIX) |
[email protected] | c143c72 | 2013-01-23 00:21:39 | [diff] [blame] | 65 | |
[email protected] | becdaa3d | 2012-03-20 18:14:40 | [diff] [blame] | 66 | #define HANDLE_EINTR(x) (x) |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 67 | #define IGNORE_EINTR(x) (x) |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 68 | |
Xiaohan Wang | 1a8409ee | 2022-01-15 14:56:41 | [diff] [blame] | 69 | #endif // !BUILDFLAG(IS_POSIX) |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 70 | |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 71 | #endif // BASE_POSIX_EINTR_WRAPPER_H_ |