Remove LiveTV menu.
[vuplus_xbmc] / xbmc / threads / Condition.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
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, or (at your option)
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 XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #pragma once
22
23 #include "threads/platform/Condition.h"
24
25 #include "threads/SystemClock.h"
26 #include <stdio.h>
27
28 namespace XbmcThreads
29 {
30
31   /**
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.
35    *
36    * L should implement the Lockable concept
37    *
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').
40    */
41   template <typename P> class TightConditionVariable
42   {
43     ConditionVariable& cond;
44     P predicate;
45
46   public:
47     inline TightConditionVariable(ConditionVariable& cv, P predicate_) : cond(cv), predicate(predicate_) {}
48
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)
51     {
52       bool ret = true;
53       if (!predicate)
54       {
55         if (!milliseconds)
56         {
57           cond.wait(lock,milliseconds /* zero */);
58           return !(!predicate); // eh? I only require the ! operation on P
59         }
60         else
61         {
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);
66         }
67       }
68       return ret;
69     }
70
71     inline void notifyAll() { cond.notifyAll(); }
72     inline void notify() { cond.notify(); }
73   };
74 }
75