blob: d4aec86d1b74da6bae459c0718905e694f54ce14 [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
17#ifndef ART_SRC_SIGNAL_SET_H_
18#define ART_SRC_SIGNAL_SET_H_
19
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() {
41 if (sigprocmask(SIG_BLOCK, &set_, NULL) == -1) {
42 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
62#endif // ART_SRC_SIGNAL_SET_H_