/* * ActionManager.cpp * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "ActionManager.h" #include #include #include #include namespace Mad { namespace Common { ActionManager ActionManager::actionManager; void ActionManager::doInit() { // TODO Error handling pipe(notifyPipe); fcntl(notifyPipe[0], F_SETFL, fcntl(notifyPipe[0], F_GETFL) | O_NONBLOCK); fcntl(notifyPipe[1], F_SETFL, fcntl(notifyPipe[1], F_GETFL) | O_NONBLOCK); Net::FdManager::get()->registerFd(notifyPipe[0], sigc::hide(sigc::mem_fun(this, &ActionManager::run)), POLLIN); } void ActionManager::doDeinit() { Net::FdManager::get()->unregisterFd(notifyPipe[0]); close(notifyPipe[0]); close(notifyPipe[1]); } void ActionManager::run() { // Empty pipe char buf[16]; while(read(notifyPipe[0], buf, sizeof(buf)) > 0) {} while(true) { sigset_t set, oldset; sigfillset(&set); sigprocmask(SIG_SETMASK, &set, &oldset); if(actions.empty()) { sigprocmask(SIG_SETMASK, &oldset, 0); return; } sigc::slot action = actions.front(); actions.pop(); sigprocmask(SIG_SETMASK, &oldset, 0); action(); } } void ActionManager::add(const sigc::slot &action) { sigset_t set, oldset; sigfillset(&set); sigprocmask(SIG_SETMASK, &set, &oldset); actions.push(action); write(notifyPipe[1], "", 1); sigprocmask(SIG_SETMASK, &oldset, 0); } } }