1457005c557b8762475db3220ce5a747d629f975bElliott Hughes/*
2457005c557b8762475db3220ce5a747d629f975bElliott Hughes * Copyright (C) 2012 The Android Open Source Project
3457005c557b8762475db3220ce5a747d629f975bElliott Hughes *
4457005c557b8762475db3220ce5a747d629f975bElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5457005c557b8762475db3220ce5a747d629f975bElliott Hughes * you may not use this file except in compliance with the License.
6457005c557b8762475db3220ce5a747d629f975bElliott Hughes * You may obtain a copy of the License at
7457005c557b8762475db3220ce5a747d629f975bElliott Hughes *
8457005c557b8762475db3220ce5a747d629f975bElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9457005c557b8762475db3220ce5a747d629f975bElliott Hughes *
10457005c557b8762475db3220ce5a747d629f975bElliott Hughes * Unless required by applicable law or agreed to in writing, software
11457005c557b8762475db3220ce5a747d629f975bElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12457005c557b8762475db3220ce5a747d629f975bElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13457005c557b8762475db3220ce5a747d629f975bElliott Hughes * See the License for the specific language governing permissions and
14457005c557b8762475db3220ce5a747d629f975bElliott Hughes * limitations under the License.
15457005c557b8762475db3220ce5a747d629f975bElliott Hughes */
16457005c557b8762475db3220ce5a747d629f975bElliott Hughes
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_SIGNAL_SET_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_SIGNAL_SET_H_
19457005c557b8762475db3220ce5a747d629f975bElliott Hughes
20457005c557b8762475db3220ce5a747d629f975bElliott Hughes#include <signal.h>
21457005c557b8762475db3220ce5a747d629f975bElliott Hughes
2207ed66b5ae659c452cbe1ab20c3dbf1d6f546461Elliott Hughes#include "base/logging.h"
23457005c557b8762475db3220ce5a747d629f975bElliott Hughes
24457005c557b8762475db3220ce5a747d629f975bElliott Hughesnamespace art {
25457005c557b8762475db3220ce5a747d629f975bElliott Hughes
26457005c557b8762475db3220ce5a747d629f975bElliott Hughesclass SignalSet {
27457005c557b8762475db3220ce5a747d629f975bElliott Hughes public:
28457005c557b8762475db3220ce5a747d629f975bElliott Hughes  SignalSet() {
29457005c557b8762475db3220ce5a747d629f975bElliott Hughes    if (sigemptyset(&set_) == -1) {
30457005c557b8762475db3220ce5a747d629f975bElliott Hughes      PLOG(FATAL) << "sigemptyset failed";
31457005c557b8762475db3220ce5a747d629f975bElliott Hughes    }
32457005c557b8762475db3220ce5a747d629f975bElliott Hughes  }
33457005c557b8762475db3220ce5a747d629f975bElliott Hughes
34457005c557b8762475db3220ce5a747d629f975bElliott Hughes  void Add(int signal) {
35457005c557b8762475db3220ce5a747d629f975bElliott Hughes    if (sigaddset(&set_, signal) == -1) {
36457005c557b8762475db3220ce5a747d629f975bElliott Hughes      PLOG(FATAL) << "sigaddset " << signal << " failed";
37457005c557b8762475db3220ce5a747d629f975bElliott Hughes    }
38457005c557b8762475db3220ce5a747d629f975bElliott Hughes  }
39457005c557b8762475db3220ce5a747d629f975bElliott Hughes
40457005c557b8762475db3220ce5a747d629f975bElliott Hughes  void Block() {
415fe1026cca5df935bd55ab1ee6892eeae02819c4Vladimir Marko    if (pthread_sigmask(SIG_BLOCK, &set_, nullptr) != 0) {
425fe1026cca5df935bd55ab1ee6892eeae02819c4Vladimir Marko      PLOG(FATAL) << "pthread_sigmask failed";
43457005c557b8762475db3220ce5a747d629f975bElliott Hughes    }
44457005c557b8762475db3220ce5a747d629f975bElliott Hughes  }
45457005c557b8762475db3220ce5a747d629f975bElliott Hughes
46457005c557b8762475db3220ce5a747d629f975bElliott Hughes  int Wait() {
47457005c557b8762475db3220ce5a747d629f975bElliott Hughes    // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
48457005c557b8762475db3220ce5a747d629f975bElliott Hughes    int signal_number;
49457005c557b8762475db3220ce5a747d629f975bElliott Hughes    int rc = TEMP_FAILURE_RETRY(sigwait(&set_, &signal_number));
50457005c557b8762475db3220ce5a747d629f975bElliott Hughes    if (rc != 0) {
51457005c557b8762475db3220ce5a747d629f975bElliott Hughes      PLOG(FATAL) << "sigwait failed";
52457005c557b8762475db3220ce5a747d629f975bElliott Hughes    }
53457005c557b8762475db3220ce5a747d629f975bElliott Hughes    return signal_number;
54457005c557b8762475db3220ce5a747d629f975bElliott Hughes  }
55457005c557b8762475db3220ce5a747d629f975bElliott Hughes
56457005c557b8762475db3220ce5a747d629f975bElliott Hughes private:
57457005c557b8762475db3220ce5a747d629f975bElliott Hughes  sigset_t set_;
58457005c557b8762475db3220ce5a747d629f975bElliott Hughes};
59457005c557b8762475db3220ce5a747d629f975bElliott Hughes
60457005c557b8762475db3220ce5a747d629f975bElliott Hughes}  // namespace art
61457005c557b8762475db3220ce5a747d629f975bElliott Hughes
62fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_SIGNAL_SET_H_
63