blob: 53613236fa7009493b00e6b18298e42f04dcef0f [file] [log] [blame]
Elliott Hughes457005c2012-04-16 13:54:25 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_SIGNAL_SET_H_
18#define ART_RUNTIME_SIGNAL_SET_H_
Elliott Hughes457005c2012-04-16 13:54:25 -070019
20#include <signal.h>
21
Andreas Gampe57943812017-12-06 21:39:13 -080022#include <android-base/logging.h>
Elliott Hughes457005c2012-04-16 13:54:25 -070023
24namespace art {
25
26class SignalSet {
27 public:
28 SignalSet() {
29 if (sigemptyset(&set_) == -1) {
30 PLOG(FATAL) << "sigemptyset failed";
31 }
32 }
33
34 void Add(int signal) {
35 if (sigaddset(&set_, signal) == -1) {
36 PLOG(FATAL) << "sigaddset " << signal << " failed";
37 }
38 }
39
40 void Block() {
Vladimir Marko5fe10262016-06-21 10:38:23 +010041 if (pthread_sigmask(SIG_BLOCK, &set_, nullptr) != 0) {
42 PLOG(FATAL) << "pthread_sigmask failed";
Elliott Hughes457005c2012-04-16 13:54:25 -070043 }
44 }
45
46 int Wait() {
47 // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
48 int signal_number;
49 int rc = TEMP_FAILURE_RETRY(sigwait(&set_, &signal_number));
50 if (rc != 0) {
51 PLOG(FATAL) << "sigwait failed";
52 }
53 return signal_number;
54 }
55
56 private:
57 sigset_t set_;
58};
59
60} // namespace art
61
Brian Carlstromfc0e3212013-07-17 14:40:12 -070062#endif // ART_RUNTIME_SIGNAL_SET_H_