Home | History | Annotate | Download | only in posix
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      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.
      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.
     10 //
     11 // On Windows, this wrapper macro does nothing.
     12 
     13 #ifndef BASE_POSIX_EINTR_WRAPPER_H_
     14 #define BASE_POSIX_EINTR_WRAPPER_H_
     15 
     16 #include "build/build_config.h"
     17 
     18 #if defined(OS_POSIX)
     19 
     20 #include <errno.h>
     21 
     22 #if defined(NDEBUG)
     23 #define HANDLE_EINTR(x) ({ \
     24   typeof(x) eintr_wrapper_result; \
     25   do { \
     26     eintr_wrapper_result = (x); \
     27   } while (eintr_wrapper_result == -1 && errno == EINTR); \
     28   eintr_wrapper_result; \
     29 })
     30 
     31 #else
     32 
     33 #define HANDLE_EINTR(x) ({ \
     34   int eintr_wrapper_counter = 0; \
     35   typeof(x) eintr_wrapper_result; \
     36   do { \
     37     eintr_wrapper_result = (x); \
     38   } while (eintr_wrapper_result == -1 && errno == EINTR && \
     39            eintr_wrapper_counter++ < 100); \
     40   eintr_wrapper_result; \
     41 })
     42 
     43 #endif  // NDEBUG
     44 
     45 #else
     46 
     47 #define HANDLE_EINTR(x) (x)
     48 
     49 #endif  // OS_POSIX
     50 
     51 #endif  // BASE_POSIX_EINTR_WRAPPER_H_
     52