blob: aad0534f30762abbfe34eaab6fa32b6d9fca062a [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]157c61b2009-05-01 21:37:312// 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]c143c722013-01-23 00:21:397// 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]157c61b2009-05-01 21:37:3110//
Sergey Ulanov34b6bbd2017-08-25 21:15:0111// On Windows and Fuchsia, this wrapper macro does nothing because there are no
12// signals.
[email protected]d89eec82013-12-03 14:10:5913//
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]157c61b2009-05-01 21:37:3116
[email protected]2025d002012-11-14 20:54:3517#ifndef BASE_POSIX_EINTR_WRAPPER_H_
18#define BASE_POSIX_EINTR_WRAPPER_H_
[email protected]157c61b2009-05-01 21:37:3119
20#include "build/build_config.h"
21
Xiaohan Wang1a8409ee2022-01-15 14:56:4122#if BUILDFLAG(IS_POSIX)
[email protected]157c61b2009-05-01 21:37:3123
24#include <errno.h>
25
[email protected]c143c722013-01-23 00:21:3926#if defined(NDEBUG)
[email protected]d89eec82013-12-03 14:10:5927
Peter Kasting134ef9af2024-12-28 02:30:0928#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]157c61b2009-05-01 21:37:3136
37#else
38
Peter Kasting134ef9af2024-12-28 02:30:0939#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]c143c722013-01-23 00:21:3949
50#endif // NDEBUG
51
Peter Kasting134ef9af2024-12-28 02:30:0952#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]d89eec82013-12-03 14:10:5963
Xiaohan Wang1a8409ee2022-01-15 14:56:4164#else // !BUILDFLAG(IS_POSIX)
[email protected]c143c722013-01-23 00:21:3965
[email protected]becdaa3d2012-03-20 18:14:4066#define HANDLE_EINTR(x) (x)
[email protected]d89eec82013-12-03 14:10:5967#define IGNORE_EINTR(x) (x)
[email protected]157c61b2009-05-01 21:37:3168
Xiaohan Wang1a8409ee2022-01-15 14:56:4169#endif // !BUILDFLAG(IS_POSIX)
[email protected]157c61b2009-05-01 21:37:3170
[email protected]2025d002012-11-14 20:54:3571#endif // BASE_POSIX_EINTR_WRAPPER_H_