libshevek
closure.hh
1 /* closure.hh - Cooperative multitasking
2  * Copyright 2008 Bas Wijnen <wijnen@debian.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SHEVEK_CLOSURE_HH
19 #define SHEVEK_CLOSURE_HH
20 
21 #include <pthread.h>
22 #include "refbase.hh"
23 
24 namespace shevek
25 {
27 
36  class closure : public refbase
37  {
38  static closure *current;
39  enum state_t { EMPTY, BLOCKING, RUNNING } state;
40  sigc::slot0 <void> callback, function;
41  pthread_t thread;
42  int blocking_pipe[2];
43  int waking_pipe[2];
44  void do_write (int *fds);
45  void do_read (int *fds);
46  static void *start_wrapper (void *me);
47  closure ();
48  public:
50 
52  static Glib::RefPtr <closure> create ()
53  { return Glib::RefPtr <closure> (new closure ()); }
55 
58  bool empty () const { return state == EMPTY; }
60 
64  void set_function (sigc::slot0 <void> func, bool run = true,
65  sigc::slot0 <void> cb = sigc::slot0 <void> ());
67  ~closure ();
69 
75  static void block ();
77 
81  void wake ();
82  };
83 }
84 
85 #endif
Base class for classes which want reference counting through Glib::RefPtr.
Definition: refbase.hh:27
~closure()
Destructor.
Definition: args.hh:52
void wake()
Continue running the closure.
Block and resume without blocking the main loop.
Definition: closure.hh:36
static Glib::RefPtr< closure > create()
Create a new closure.
Definition: closure.hh:52
bool empty() const
Check if the closure is empty.
Definition: closure.hh:58
void set_function(sigc::slot0< void > func, bool run=true, sigc::slot0< void > cb=sigc::slot0< void >())
Set running function on an empty closure.
static void block()
Sleep, returning control to the caller until awoken.