added missing hardy rules and source version
[deb_libcec.git] / src / lib / platform / windows / os-threads.cpp
CommitLineData
dca2d070
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
33#include "../os.h"
34#include "os-threads.h"
35using namespace PLATFORM;
36
37static ConditionArg g_InitializeConditionVariable;
38static ConditionArg g_WakeConditionVariable;
39static ConditionArg g_WakeAllConditionVariable;
40static ConditionMutexArg g_SleepConditionVariableCS;
41
42// check whether vista+ conditions are available at runtime
43static bool CheckVistaConditionFunctions(void)
44{
45 static int iHasVistaConditionFunctions(-1);
46 if (iHasVistaConditionFunctions == -1)
47 {
48 HMODULE handle = GetModuleHandle("Kernel32");
49 if (handle == NULL)
50 {
51 iHasVistaConditionFunctions = 0;
52 }
53 else
54 {
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");
59
60 // 1 when everything is resolved, 0 otherwise
61 iHasVistaConditionFunctions = g_InitializeConditionVariable &&
62 g_WakeConditionVariable &&
63 g_WakeAllConditionVariable &&
64 g_SleepConditionVariableCS ? 1 : 0;
65 }
66 }
67 return iHasVistaConditionFunctions == 1;
68}
69
70CConditionImpl::CConditionImpl(void)
71{
72 m_bOnVista = CheckVistaConditionFunctions();
73 if (m_bOnVista)
74 (*g_InitializeConditionVariable)(m_conditionVista = new CONDITION_VARIABLE);
75 else
76 m_conditionPreVista = ::CreateEvent(NULL, TRUE, FALSE, NULL);
77}
78
79CConditionImpl::~CConditionImpl(void)
80{
81 if (m_bOnVista)
82 delete m_conditionVista;
83 else
84 ::CloseHandle(m_conditionPreVista);
85}
86
87void CConditionImpl::Signal(void)
88{
89 if (m_bOnVista)
90 (*g_WakeConditionVariable)(m_conditionVista);
91 else
92 ::SetEvent(m_conditionPreVista);
93}
94
95void CConditionImpl::Broadcast(void)
96{
97 if (m_bOnVista)
98 (*g_WakeAllConditionVariable)(m_conditionVista);
99 else
100 ::SetEvent(m_conditionPreVista);
101}
102
103bool CConditionImpl::Wait(mutex_t &mutex)
104{
105 if (m_bOnVista)
106 {
107 return ((*g_SleepConditionVariableCS)(m_conditionVista, mutex, INFINITE) ? true : false);
108 }
109 else
110 {
111 ::ResetEvent(m_conditionPreVista);
112 MutexUnlock(mutex);
113 DWORD iWaitReturn = ::WaitForSingleObject(m_conditionPreVista, 1000);
114 MutexLock(mutex);
115 return (iWaitReturn == 0);
116 }
117}
118
119bool CConditionImpl::Wait(mutex_t &mutex, uint32_t iTimeoutMs)
120{
121 if (iTimeoutMs == 0)
122 return Wait(mutex);
123
124 if (m_bOnVista)
125 {
126 return ((*g_SleepConditionVariableCS)(m_conditionVista, mutex, iTimeoutMs) ? true : false);
127 }
128 else
129 {
130 ::ResetEvent(m_conditionPreVista);
131 MutexUnlock(mutex);
132 DWORD iWaitReturn = ::WaitForSingleObject(m_conditionPreVista, iTimeoutMs);
133 MutexLock(mutex);
134 return (iWaitReturn == 0);
135 }
136}