1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_SIGNAL_SET_H_
18 #define ART_RUNTIME_SIGNAL_SET_H_
19 
20 #include <signal.h>
21 
22 #include <android-base/logging.h>
23 
24 #if defined(__GLIBC__)
25 #define sigset64_t sigset_t
26 #define sigemptyset64 sigemptyset
27 #define sigaddset64 sigaddset
28 #define pthread_sigmask64 pthread_sigmask
29 #define sigwait64 sigwait
30 #endif
31 
32 namespace art {
33 
34 class SignalSet {
35  public:
SignalSet()36   SignalSet() {
37     if (sigemptyset64(&set_) == -1) {
38       PLOG(FATAL) << "sigemptyset failed";
39     }
40   }
41 
Add(int signal)42   void Add(int signal) {
43     if (sigaddset64(&set_, signal) == -1) {
44       PLOG(FATAL) << "sigaddset " << signal << " failed";
45     }
46   }
47 
Block()48   void Block() {
49     if (pthread_sigmask64(SIG_BLOCK, &set_, nullptr) != 0) {
50       PLOG(FATAL) << "pthread_sigmask failed";
51     }
52   }
53 
Wait()54   int Wait() {
55     // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
56     int signal_number;
57     int rc = TEMP_FAILURE_RETRY(sigwait64(&set_, &signal_number));
58     if (rc != 0) {
59       PLOG(FATAL) << "sigwait failed";
60     }
61     return signal_number;
62   }
63 
64  private:
65   sigset64_t set_;
66 };
67 
68 }  // namespace art
69 
70 #endif  // ART_RUNTIME_SIGNAL_SET_H_
71