2 * Copyright (C) 2005-2013 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, see
17 * <http://www.gnu.org/licenses/>.
23 #include "threads/platform/Condition.h"
25 #include "threads/SystemClock.h"
32 * This is a condition variable along with its predicate. This allows the use of a
33 * condition variable without the spurious returns since the state being monitored
34 * is also part of the condition.
36 * L should implement the Lockable concept
38 * The requirements on P are that it can act as a predicate (that is, I can use
39 * it in an 'while(!predicate){...}' where 'predicate' is of type 'P').
41 template <typename P> class TightConditionVariable
43 ConditionVariable& cond;
47 inline TightConditionVariable(ConditionVariable& cv, P predicate_) : cond(cv), predicate(predicate_) {}
49 template <typename L> inline void wait(L& lock) { while(!predicate) cond.wait(lock); }
50 template <typename L> inline bool wait(L& lock, unsigned long milliseconds)
57 cond.wait(lock,milliseconds /* zero */);
58 return !(!predicate); // eh? I only require the ! operation on P
62 EndTime endTime((unsigned int)milliseconds);
63 for (bool notdone = true; notdone && ret == true;
64 ret = (notdone = (!predicate)) ? ((milliseconds = endTime.MillisLeft()) != 0) : true)
65 cond.wait(lock,milliseconds);
71 inline void notifyAll() { cond.notifyAll(); }
72 inline void notify() { cond.notify(); }