blob: c272514f61e7cf9c32cb833b401e613aac590d8d [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "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() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070041 if (sigprocmask(SIG_BLOCK, &set_, nullptr) == -1) {
Elliott Hughes457005c2012-04-16 13:54:25 -070042 PLOG(FATAL) << "sigprocmask failed";
43 }
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_