2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
34 #include "os-threads.h"
35 using namespace PLATFORM
;
37 static ConditionArg g_InitializeConditionVariable
;
38 static ConditionArg g_WakeConditionVariable
;
39 static ConditionArg g_WakeAllConditionVariable
;
40 static ConditionMutexArg g_SleepConditionVariableCS
;
42 // check whether vista+ conditions are available at runtime
43 static bool CheckVistaConditionFunctions(void)
45 static int iHasVistaConditionFunctions(-1);
46 if (iHasVistaConditionFunctions
== -1)
48 HMODULE handle
= GetModuleHandle("Kernel32");
51 iHasVistaConditionFunctions
= 0;
55 g_InitializeConditionVariable
= (ConditionArg
) GetProcAddress(handle
,"InitializeConditionVariable");
56 g_WakeConditionVariable
= (ConditionArg
) GetProcAddress(handle
,"WakeConditionVariable");
57 g_WakeAllConditionVariable
= (ConditionArg
) GetProcAddress(handle
,"WakeAllConditionVariable");
58 g_SleepConditionVariableCS
= (ConditionMutexArg
)GetProcAddress(handle
,"SleepConditionVariableCS");
60 // 1 when everything is resolved, 0 otherwise
61 iHasVistaConditionFunctions
= g_InitializeConditionVariable
&&
62 g_WakeConditionVariable
&&
63 g_WakeAllConditionVariable
&&
64 g_SleepConditionVariableCS
? 1 : 0;
67 return iHasVistaConditionFunctions
== 1;
70 CConditionImpl::CConditionImpl(void)
72 m_bOnVista
= CheckVistaConditionFunctions();
74 (*g_InitializeConditionVariable
)(m_conditionVista
= new CONDITION_VARIABLE
);
76 m_conditionPreVista
= ::CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
79 CConditionImpl::~CConditionImpl(void)
82 delete m_conditionVista
;
84 ::CloseHandle(m_conditionPreVista
);
87 void CConditionImpl::Signal(void)
90 (*g_WakeConditionVariable
)(m_conditionVista
);
92 ::SetEvent(m_conditionPreVista
);
95 void CConditionImpl::Broadcast(void)
98 (*g_WakeAllConditionVariable
)(m_conditionVista
);
100 ::SetEvent(m_conditionPreVista
);
103 bool CConditionImpl::Wait(mutex_t
&mutex
)
107 return ((*g_SleepConditionVariableCS
)(m_conditionVista
, mutex
, INFINITE
) ? true : false);
111 ::ResetEvent(m_conditionPreVista
);
113 DWORD iWaitReturn
= ::WaitForSingleObject(m_conditionPreVista
, 1000);
115 return (iWaitReturn
== 0);
119 bool CConditionImpl::Wait(mutex_t
&mutex
, uint32_t iTimeoutMs
)
126 return ((*g_SleepConditionVariableCS
)(m_conditionVista
, mutex
, iTimeoutMs
) ? true : false);
130 ::ResetEvent(m_conditionPreVista
);
132 DWORD iWaitReturn
= ::WaitForSingleObject(m_conditionPreVista
, iTimeoutMs
);
134 return (iWaitReturn
== 0);