Permalink
Browse files

wavy::add_signal and pthread_signal set SIG_IGN

  • Loading branch information...
1 parent 6c6ad22 commit bf4d8ddb935065622af5ac2c764d21cc53d823e8 @frsyuki committed Apr 9, 2010
Showing with 56 additions and 17 deletions.
  1. +44 −8 mp/signal.h
  2. +1 −1 mp/wavy.hmpl
  3. +7 −2 mpsrc/wavy_signal.h
  4. +4 −6 test/signal.cc
View
@@ -92,34 +92,70 @@ class scoped_sigprocmask {
};
+class scoped_signal {
+public:
+ scoped_signal(int signo, void (*handler)(int)) :
+ m_signo(signo)
+ {
+ struct sigaction act;
+ memset(&act, 0, sizeof(act));
+ act.sa_handler = handler;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = SA_RESTART;
+ if(sigaction(m_signo, &act, &m_save) < 0) {
+ throw system_error(errno, "failed to set signal handler");
+ }
+ }
+
+ ~scoped_signal()
+ {
+ sigaction(m_signo, &m_save, NULL);
+ }
+
+ //FIXME
+ //scoped_signal(int signo, function<void (int)> func);
+
+private:
+ int m_signo;
+ struct sigaction m_save;
+
+private:
+ scoped_signal();
+ scoped_signal(const scoped_signal&);
+};
+
+
class pthread_signal : public pthread_thread {
public:
- typedef function<bool (int signo)> handler_t;
+ typedef function<bool ()> handler_t;
- pthread_signal(const sigset& set, handler_t handler) :
- m_mask(set)
+ pthread_signal(int signo, handler_t handler) :
+ m_signal(signo, SIG_IGN),
+ m_sigmask(sigset().add(signo))
{
- run(mp::bind(&pthread_signal::thread_main, set, handler));
+ run(mp::bind(&pthread_signal::thread_main, signo, handler));
}
~pthread_signal() { }
public:
- static void thread_main(sigset set, handler_t handler)
+ static void thread_main(int signo, handler_t handler)
{
- int signo;
+ sigset set;
+ set.add(signo);
while(true) {
if(sigwait(set.get(), &signo) != 0) {
signo = -1;
}
- if(!handler(signo)) {
+ if(!handler()) {
return;
}
}
}
private:
- scoped_sigprocmask m_mask;
+ scoped_signal m_signal;
+ scoped_sigprocmask m_sigmask;
private:
pthread_signal();
View
@@ -108,7 +108,7 @@ public:
void remove_timer(int ident);
- typedef function<bool (int signo)> signal_callback_t;
+ typedef function<bool ()> signal_callback_t;
int add_signal(int signo, signal_callback_t callback);
View
@@ -19,6 +19,7 @@
#define WAVY_SIGNAL_H__
#include "wavy_loop.h"
+#include "mp/signal.h"
#include <signal.h>
namespace mp {
@@ -60,20 +61,24 @@ class signal_handler : kernel_signal, public basic_handler {
signal_callback_t callback) :
kernel_signal(kern, signo),
basic_handler(signal_ident(), this),
- m_signo(signo), m_callback(callback)
+ m_signo(signo), m_callback(callback),
+ m_signal(signo, SIG_IGN),
+ m_sigmask(sigset().add(signo))
{ }
~signal_handler() { }
bool operator() (event& e)
{
read_signal(e);
- return m_callback(m_signo);
+ return m_callback();
}
private:
int m_signo;
signal_callback_t m_callback;
+ scoped_signal m_signal;
+ scoped_sigprocmask m_sigmask;
};
View
@@ -8,7 +8,7 @@
using namespace mp::placeholders;
-bool signal_handler(int signo, int* count, mp::wavy::loop* lo)
+bool signal_handler(int* count, mp::wavy::loop* lo)
{
std::cout << "signal" << std::endl;
@@ -22,15 +22,13 @@ bool signal_handler(int signo, int* count, mp::wavy::loop* lo)
int main(void)
{
- signal(SIGUSR1, SIG_IGN); // kqueue compatibility
- mp::scoped_sigprocmask mask( // epoll compatibility
- mp::sigset().add(SIGUSR1));
-
mp::wavy::loop lo;
int count = 0;
+
+ // add signal handler before starting any other threads.
lo.add_signal(SIGUSR1, mp::bind(
- &signal_handler, _1, &count, &lo));
+ &signal_handler, &count, &lo));
lo.start(3);

0 comments on commit bf4d8dd

Please sign in to comment.