1 // include/Yield.hh
2 // This file is part of libpbe; see http://svn.chezphil.org/libpbe/
3 // (C) 2008 Philip Endecott
4 
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef libpbe_Yield_hh
20 #define libpbe_Yield_hh
21 
22 // The file implements a Futex-like class which simply calls sched_yield().
23 
24 #include <sched.h>
25 
26 #include "Exception.hh"
27 #include "compiler_magic.hh"
28 
29 namespace pbe {
30 
31 
32 struct Yield {
33 
Yieldpbe::Yield34   Yield(PBE_UNUSED_ARG(int& val)) {}
~Yieldpbe::Yield35   ~Yield() {}
36 
waitpbe::Yield37   void wait(PBE_UNUSED_ARG(int expected)) {
38     int r = ::sched_yield();
39     if (r==-1) {
40       throw_ErrnoException("futex(FUTEX_WAIT)");
41     }
42   }
43 
timed_waitpbe::Yield44   bool timed_wait(PBE_UNUSED_ARG(int expected), const timespec& timeout) {
45   // TODO detect timeout
46     wait(expected);
47     return true;
48   }
49 
timed_waitpbe::Yield50   bool timed_wait(PBE_UNUSED_ARG(int expected), float timeout) {
51   // TODO detect timeout
52     wait(expected);
53     return true;
54   }
55 
56   // Wake up to n waiters; returns the number woken.
wakepbe::Yield57   int wake(int n=1) {
58     // Hmm, how much does the return value matter?
59     return n;
60   }
61 };
62 
63 
64 };
65 
66 
67 #endif
68 
69