Flutter Engine
The Flutter Engine
signal_blocker.h
Go to the documentation of this file.
1// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_PLATFORM_SIGNAL_BLOCKER_H_
6#define RUNTIME_PLATFORM_SIGNAL_BLOCKER_H_
7
8#include "platform/assert.h"
9#include "platform/globals.h"
10
11#if defined(DART_HOST_OS_WINDOWS)
12#error Do not include this file on Windows.
13#endif
14
15#include <pthread.h>
16#include <signal.h> // NOLINT
17
18namespace dart {
19
21 public:
22 explicit ThreadSignalBlocker(int sig) {
23 sigset_t signal_mask;
24 sigemptyset(&signal_mask);
25 sigaddset(&signal_mask, sig);
26 // Add sig to signal mask.
27 int r = pthread_sigmask(SIG_BLOCK, &signal_mask, &old);
28 USE(r);
29 ASSERT(r == 0);
30 }
31
32 ThreadSignalBlocker(int sigs_count, const int sigs[]) {
33 sigset_t signal_mask;
34 sigemptyset(&signal_mask);
35 for (int i = 0; i < sigs_count; i++) {
36 sigaddset(&signal_mask, sigs[i]);
37 }
38 // Add sig to signal mask.
39 int r = pthread_sigmask(SIG_BLOCK, &signal_mask, &old);
40 USE(r);
41 ASSERT(r == 0);
42 }
43
45 // Restore signal mask.
46 int r = pthread_sigmask(SIG_SETMASK, &old, nullptr);
47 USE(r);
48 ASSERT(r == 0);
49 }
50
51 private:
52 sigset_t old;
53};
54
55// The definition below is copied from Linux and adapted to avoid lint
56// errors (type long int changed to intptr_t and do/while split on
57// separate lines with body in {}s) and to also block signals.
58#define TEMP_FAILURE_RETRY(expression) \
59 ({ \
60 ThreadSignalBlocker tsb(SIGPROF); \
61 intptr_t __result; \
62 do { \
63 __result = (expression); \
64 } while ((__result == -1L) && (errno == EINTR)); \
65 __result; \
66 })
67
68// This is a version of TEMP_FAILURE_RETRY which does not use the value
69// returned from the expression.
70#define VOID_TEMP_FAILURE_RETRY(expression) \
71 (static_cast<void>(TEMP_FAILURE_RETRY(expression)))
72
73// This macro can be used to insert checks that a call is made, that
74// was expected to not return EINTR, but did it anyway.
75#define NO_RETRY_EXPECTED(expression) \
76 ({ \
77 intptr_t __result = (expression); \
78 if (__result == -1L && errno == EINTR) { \
79 FATAL("Unexpected EINTR errno"); \
80 } \
81 __result; \
82 })
83
84#define VOID_NO_RETRY_EXPECTED(expression) \
85 (static_cast<void>(NO_RETRY_EXPECTED(expression)))
86
87// Define to check in debug mode, if a signal is currently being blocked.
88#define CHECK_IS_BLOCKING(signal) \
89 ({ \
90 sigset_t signal_mask; \
91 int __r = pthread_sigmask(SIG_BLOCK, nullptr, &signal_mask); \
92 USE(__r); \
93 ASSERT(__r == 0); \
94 sigismember(&signal_mask, signal); \
95 })
96
97// Versions of the above, that does not enter a signal blocking scope. Use only
98// when a signal blocking scope is entered manually.
99#define TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(expression) \
100 ({ \
101 intptr_t __result; \
102 ASSERT(CHECK_IS_BLOCKING(SIGPROF)); \
103 do { \
104 __result = (expression); \
105 } while ((__result == -1L) && (errno == EINTR)); \
106 __result; \
107 })
108
109#define VOID_TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(expression) \
110 (static_cast<void>(TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(expression)))
111
112} // namespace dart
113
114#endif // RUNTIME_PLATFORM_SIGNAL_BLOCKER_H_
ThreadSignalBlocker(int sigs_count, const int sigs[])
#define ASSERT(E)
Definition: dart_vm.cc:33
static void USE(T &&)
Definition: globals.h:618