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 #include "base/macros.h"
25 
26 #if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
27 #define sigset64_t sigset_t
28 #define sigemptyset64 sigemptyset
29 #define sigaddset64 sigaddset
30 #define pthread_sigmask64 pthread_sigmask
31 #define sigwait64 sigwait
32 #endif
33 
34 namespace art HIDDEN {
35 
36 class SignalSet {
37  public:
SignalSet()38   SignalSet() {
39     if (sigemptyset64(&set_) == -1) {
40       PLOG(FATAL) << "sigemptyset failed";
41     }
42   }
43 
Add(int signal)44   void Add(int signal) {
45     if (sigaddset64(&set_, signal) == -1) {
46       PLOG(FATAL) << "sigaddset " << signal << " failed";
47     }
48   }
49 
Block()50   void Block() {
51     if (pthread_sigmask64(SIG_BLOCK, &set_, nullptr) != 0) {
52       PLOG(FATAL) << "pthread_sigmask failed";
53     }
54   }
55 
Wait()56   int Wait() {
57     // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
58     int signal_number;
59     int rc = TEMP_FAILURE_RETRY(sigwait64(&set_, &signal_number));
60     if (rc != 0) {
61       PLOG(FATAL) << "sigwait failed";
62     }
63     return signal_number;
64   }
65 
66  private:
67   sigset64_t set_;
68 };
69 
70 }  // namespace art
71 
72 #endif  // ART_RUNTIME_SIGNAL_SET_H_
73