closure.hh
/* closure.hh - Cooperative multitasking
* Copyright 2008 Bas Wijnen <wijnen@debian.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef SHEVEK_CLOSURE_HH
#define SHEVEK_CLOSURE_HH
#include <pthread.h>
#include "refbase.hh"
namespace shevek
{
class closure : public refbase
{
static closure *current;
enum state_t { EMPTY, BLOCKING, RUNNING } state;
sigc::slot0 <void> callback, function;
pthread_t thread;
int blocking_pipe[2];
int waking_pipe[2];
void do_write (int *fds);
void do_read (int *fds);
static void *start_wrapper (void *me);
closure ();
public:
static Glib::RefPtr <closure> create ()
{ return Glib::RefPtr <closure> (new closure ()); }
bool empty () const { return state == EMPTY; }
void set_function (sigc::slot0 <void> func, bool run = true,
sigc::slot0 <void> cb = sigc::slot0 <void> ());
~closure ();
static void block ();
void wake ();
};
}
#endif
shevek::refbase
Base class for classes which want reference counting through Glib::RefPtr.
Definition: refbase.hh:27
shevek::closure::~closure
~closure()
Destructor.
shevek
Definition: args.hh:52
shevek::closure::wake
void wake()
Continue running the closure.
shevek::closure
Block and resume without blocking the main loop.
Definition: closure.hh:36
shevek::closure::create
static Glib::RefPtr< closure > create()
Create a new closure.
Definition: closure.hh:52
shevek::closure::empty
bool empty() const
Check if the closure is empty.
Definition: closure.hh:58
shevek::closure::set_function
void set_function(sigc::slot0< void > func, bool run=true, sigc::slot0< void > cb=sigc::slot0< void >())
Set running function on an empty closure.
shevek::closure::block
static void block()
Sleep, returning control to the caller until awoken.