blob: 854c43a67cba848f418cb4a3ad901393590b1d7b [file] [log] [blame]
vapier@chromium.orgf6b9ffa2012-03-21 03:14:40 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
agl@chromium.orgd263ad72009-05-02 06:37:31 +09002// 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.
jln@chromium.org62b9e142013-01-23 09:21:39 +09007// 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.
agl@chromium.orgd263ad72009-05-02 06:37:31 +090010//
11// On Windows, this wrapper macro does nothing.
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +090012//
13// Don't wrap close calls in HANDLE_EINTR. Use IGNORE_EINTR if the return
14// value of close is significant. See http://crbug.com/269623.
agl@chromium.orgd263ad72009-05-02 06:37:31 +090015
brettw@chromium.orgb1788fb2012-11-15 05:54:35 +090016#ifndef BASE_POSIX_EINTR_WRAPPER_H_
17#define BASE_POSIX_EINTR_WRAPPER_H_
agl@chromium.orgd263ad72009-05-02 06:37:31 +090018
19#include "build/build_config.h"
20
21#if defined(OS_POSIX)
22
23#include <errno.h>
24
jln@chromium.org62b9e142013-01-23 09:21:39 +090025#if defined(NDEBUG)
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +090026
agl@chromium.orgd263ad72009-05-02 06:37:31 +090027#define HANDLE_EINTR(x) ({ \
jln@chromium.org62b9e142013-01-23 09:21:39 +090028 typeof(x) eintr_wrapper_result; \
agl@chromium.orgd263ad72009-05-02 06:37:31 +090029 do { \
jln@chromium.org62b9e142013-01-23 09:21:39 +090030 eintr_wrapper_result = (x); \
31 } while (eintr_wrapper_result == -1 && errno == EINTR); \
32 eintr_wrapper_result; \
agl@chromium.orgd263ad72009-05-02 06:37:31 +090033})
34
35#else
36
jln@chromium.org62b9e142013-01-23 09:21:39 +090037#define HANDLE_EINTR(x) ({ \
38 int eintr_wrapper_counter = 0; \
39 typeof(x) eintr_wrapper_result; \
40 do { \
41 eintr_wrapper_result = (x); \
42 } while (eintr_wrapper_result == -1 && errno == EINTR && \
43 eintr_wrapper_counter++ < 100); \
44 eintr_wrapper_result; \
45})
46
47#endif // NDEBUG
48
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +090049#define IGNORE_EINTR(x) ({ \
50 typeof(x) eintr_wrapper_result; \
51 do { \
52 eintr_wrapper_result = (x); \
53 if (eintr_wrapper_result == -1 && errno == EINTR) { \
54 eintr_wrapper_result = 0; \
55 } \
56 } while (0); \
57 eintr_wrapper_result; \
58})
59
jln@chromium.org62b9e142013-01-23 09:21:39 +090060#else
61
vapier@chromium.orgf6b9ffa2012-03-21 03:14:40 +090062#define HANDLE_EINTR(x) (x)
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +090063#define IGNORE_EINTR(x) (x)
agl@chromium.orgd263ad72009-05-02 06:37:31 +090064
65#endif // OS_POSIX
66
brettw@chromium.orgb1788fb2012-11-15 05:54:35 +090067#endif // BASE_POSIX_EINTR_WRAPPER_H_