2 * Copyright (C) 2005-2011 Team XBMC
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, or (at your option)
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.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 #include "threads/platform/Condition.h"
26 #include "threads/SystemClock.h"
33 * This is a condition variable along with its predicate. This allows the use of a
34 * condition variable without the spurious returns since the state being monitored
35 * is also part of the condition.
37 * L should implement the Lockable concept
39 * The requirements on P are that it can act as a predicate (that is, I can use
40 * it in an 'while(!predicate){...}' where 'predicate' is of type 'P').
42 template <typename P> class TightConditionVariable
44 ConditionVariable& cond;
48 inline TightConditionVariable(ConditionVariable& cv, P predicate_) : cond(cv), predicate(predicate_) {}
50 template <typename L> inline void wait(L& lock) { while(!predicate) cond.wait(lock); }
51 template <typename L> inline bool wait(L& lock, unsigned long milliseconds)
58 cond.wait(lock,milliseconds /* zero */);
59 return !(!predicate); // eh? I only require the ! operation on P
63 EndTime endTime((unsigned int)milliseconds);
64 for (bool notdone = true; notdone && ret == true;
65 ret = (notdone = (!predicate)) ? ((milliseconds = endTime.millisLeft()) != 0) : true)
66 cond.wait(lock,milliseconds);
72 inline void notifyAll() { cond.notifyAll(); }
73 inline void notify() { cond.notify(); }