AJA NTV2 SDK  17.5.0.1242
NTV2 SDK 17.5.0.1242
threadimpl.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
9 #include "ajabase/system/debug.h"
10 
11 
13 {
14  mpThread = pThread;
15  mhThreadHandle = 0;
16  mThreadID = 0;
18  mThreadFunc = NULL;
20  mTerminate = false;
21 }
22 
23 
25 {
26  Stop();
27 }
28 
29 
32 {
33  AJAAutoLock lock(&mLock);
34 
35  // return success if thread is already running
36  if (Active())
37  {
38  return AJA_STATUS_SUCCESS;
39  }
40 
41  // create the thread
42  mTerminate = false;
43  mhThreadHandle = CreateThread(NULL, 0, ThreadProcStatic, this, 0, &mThreadID);
44  if (mhThreadHandle == 0)
45  {
46  mThreadID = 0;
47  return AJA_STATUS_FAIL;
48  }
49 
50  // set the thread priority
52 
53  return AJA_STATUS_SUCCESS;
54 }
55 
56 
57 AJAStatus
58 AJAThreadImpl::Stop(uint32_t timeout)
59 {
60  AJAAutoLock lock(&mLock);
62 
63  // return success if the thread is already stopped
64  if (!Active())
65  {
66  return AJA_STATUS_SUCCESS;
67  }
68 
69  // check for infinite timeout
70  if (timeout == 0xffffffff)
71  {
72  timeout = INFINITE;
73  }
74 
75  // signal thread to stop
76  mTerminate = true;
77 
78  if (timeout > 0)
79  {
80  // wait for thread to stop
81  DWORD returnCode = WaitForSingleObject(mhThreadHandle, (DWORD)timeout);
82  // the wait timed out
83  if (returnCode == WAIT_TIMEOUT)
84  {
85  AJA_REPORT(0, AJA_DebugSeverity_Warning, "AJAThread::Stop timeout waiting to stop");
86  status = AJA_STATUS_TIMEOUT;
87  }
88  else if (returnCode != WAIT_OBJECT_0)
89  {
90  AJA_REPORT(0, AJA_DebugSeverity_Warning, "AJAThread::Stop thread failed to stop");
91  status = AJA_STATUS_FAIL;
92  }
93  else
94  {
95  status = AJA_STATUS_SUCCESS;
96  }
97  }
98 
99  if(AJA_STATUS_SUCCESS == status)
100  {
101  // close thread handle
102  CloseHandle(mhThreadHandle);
103  mhThreadHandle = 0;
104  mThreadID = 0;
105  }
106 
107  return status;
108 }
109 
110 AJAStatus
111 AJAThreadImpl::Kill(uint32_t exitCode)
112 {
113  AJAAutoLock lock(&mLock);
114  AJAStatus status = AJA_STATUS_SUCCESS;
115 
116  // return success if the thread is already stopped
117  if (!Active())
118  {
119  return AJA_STATUS_SUCCESS;
120  }
121  if(TerminateThread(mhThreadHandle, exitCode))
122  return AJA_STATUS_SUCCESS;
123  return AJA_STATUS_FAIL;
124 }
125 
126 bool
128 {
129  // if no handle thread is not active
130  if (mhThreadHandle == 0)
131  {
132  return false;
133  }
134 
135  // if wait succeeds then thread has exited
136  DWORD returnCode = WaitForSingleObject(mhThreadHandle, 0);
137  if (returnCode == WAIT_OBJECT_0)
138  {
139  // the thread has terminated, so clear the handle
140  CloseHandle(mhThreadHandle);
141  mhThreadHandle = 0;
142 
143  return false;
144  }
145 
146  return true;
147 }
148 
149 bool
151 {
152  if(mThreadID == 0)
153  {
154  return false;
155  }
156 
157  if(mThreadID == GetCurrentThreadId())
158  {
159  return true;
160  }
161 
162  return false;
163 }
164 
165 
166 AJAStatus
168 {
169  AJAAutoLock lock(&mLock);
170  bool success = true;
171 
172  // save priority for starts
173  mPriority = threadPriority;
174 
175  // if thread is running dynamically set the priority
176  if (Active())
177  {
178  switch (threadPriority)
179  {
181  success = SetThreadPriority(mhThreadHandle, THREAD_PRIORITY_NORMAL)? true : false;
182  break;
184  success = SetThreadPriority(mhThreadHandle, THREAD_PRIORITY_LOWEST)? true : false;
185  break;
187  success = SetThreadPriority(mhThreadHandle, THREAD_PRIORITY_HIGHEST)? true : false;
188  break;
190  success = SetThreadPriority(mhThreadHandle, THREAD_PRIORITY_TIME_CRITICAL)? true : false;
191  break;
193  success = SetThreadPriority(mhThreadHandle, THREAD_PRIORITY_ABOVE_NORMAL)? true : false;
194  break;
196  default:
197  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJAThread(%p)::SetPriority: bad thread priority %d", mpThread, threadPriority);
198  return AJA_STATUS_RANGE;
199  }
200  }
201 
202  return success? AJA_STATUS_SUCCESS : AJA_STATUS_FAIL;
203 }
204 
205 
206 AJAStatus
208 {
209  if(pTheadPPriority == NULL)
210  {
211  return AJA_STATUS_INITIALIZE;
212  }
213 
214  *pTheadPPriority = mPriority;
215 
216  return AJA_STATUS_SUCCESS;
217 }
218 
219 
220 AJAStatus
222 {
223  return AJA_STATUS_FAIL;
224 }
225 
226 
227 AJAStatus
228 AJAThreadImpl::Attach(AJAThreadFunction* pThreadFunction, void* pUserContext)
229 {
230  // remember the users thread function
231  mThreadFunc = pThreadFunction;
232  mpUserContext = pUserContext;
233 
234  return AJA_STATUS_SUCCESS;
235 }
236 
237 
238 DWORD WINAPI
239 AJAThreadImpl::ThreadProcStatic(void* pThreadImplContext)
240 {
241  // this function is called when the thread starts
242  AJAThreadImpl* pThreadImpl = static_cast<AJAThreadImpl*>(pThreadImplContext);
243  AJA_ASSERT(pThreadImpl != NULL);
244  if (pThreadImpl == NULL)
245  {
246  return 0;
247  }
248 
249  // call the thread worker function
250  try
251  {
252  // if user specified function call it
253  if (pThreadImpl->mThreadFunc != NULL)
254  {
255  (*pThreadImpl->mThreadFunc)(pThreadImpl->mpThread, pThreadImpl->mpUserContext);
256  }
257  // otherwise call the virtual function
258  else
259  {
260  pThreadImpl->mpThread->ThreadRun();
261  }
262  }
263  catch (...)
264  {
265  AJA_REPORT(0, AJA_DebugSeverity_Error, "AJAThread(%p)::ThreadProcStatic exception in thread function", pThreadImpl->mpThread);
266  return 0;
267  }
268 
269  return 1;
270 }
271 
273  // FIXME: This feature is not supported on windows yet.
274  return AJA_STATUS_FAIL;
275 }
276 
278 {
279  return uint64_t(GetCurrentThreadId());
280 }
AJAThreadImpl::mThreadID
DWORD mThreadID
Definition: threadimpl.h:44
AJAThreadImpl::IsCurrentThread
bool IsCurrentThread()
Definition: threadimpl.cpp:406
AJA_ThreadPriority_High
@ AJA_ThreadPriority_High
Definition: thread.h:44
AJAThreadImpl::mPriority
AJAThreadPriority mPriority
Definition: threadimpl.h:49
AJA_DebugSeverity_Warning
@ AJA_DebugSeverity_Warning
Definition: debugshare.h:29
NULL
#define NULL
Definition: ntv2caption608types.h:19
AJAThreadImpl::mpUserContext
void * mpUserContext
Definition: threadimpl.h:51
AJA_STATUS_SUCCESS
@ AJA_STATUS_SUCCESS
Definition: types.h:381
AJAThreadImpl::GetThreadId
static uint64_t GetThreadId()
Definition: threadimpl.cpp:685
AJAThreadImpl::SetThreadName
AJAStatus SetThreadName(const char *name)
Definition: threadimpl.cpp:672
AJAThreadImpl::mpThread
AJAThread * mpThread
Definition: threadimpl.h:42
AJA_DebugSeverity_Error
@ AJA_DebugSeverity_Error
Definition: debugshare.h:28
AJAThreadImpl::mThreadFunc
AJAThreadFunction * mThreadFunc
Definition: threadimpl.h:50
AJAThreadImpl::~AJAThreadImpl
virtual ~AJAThreadImpl()
Definition: threadimpl.cpp:84
AJAThreadPriority
AJAThreadPriority
Definition: thread.h:39
AJAThreadImpl
Definition: threadimpl.h:18
AJA_ThreadPriority_TimeCritical
@ AJA_ThreadPriority_TimeCritical
Definition: thread.h:45
AJAThread
Definition: thread.h:69
AJAThreadImpl::mLock
AJALock mLock
Definition: threadimpl.h:52
AJAStatus
AJAStatus
Definition: types.h:378
AJA_ThreadPriority_Unknown
@ AJA_ThreadPriority_Unknown
Definition: thread.h:41
AJAThreadImpl::Kill
AJAStatus Kill(uint32_t exitCode)
Definition: threadimpl.cpp:332
AJA_STATUS_FAIL
@ AJA_STATUS_FAIL
Definition: types.h:382
AJA_ThreadPriority_Normal
@ AJA_ThreadPriority_Normal
Definition: thread.h:43
AJA_REPORT
#define AJA_REPORT(_index_, _severity_, _format_,...)
Definition: debug.h:117
threadimpl.h
Declares the AJAThreadImpl class.
AJAThread::ThreadRun
virtual AJAStatus ThreadRun()
Definition: thread.cpp:38
AJAThreadImpl::Active
bool Active()
Definition: threadimpl.cpp:386
AJA_STATUS_RANGE
@ AJA_STATUS_RANGE
Definition: types.h:385
AJAAutoLock
Definition: lock.h:91
AJA_STATUS_TIMEOUT
@ AJA_STATUS_TIMEOUT
Definition: types.h:384
AJA_STATUS_INITIALIZE
@ AJA_STATUS_INITIALIZE
Definition: types.h:386
AJAThreadImpl::mTerminate
bool mTerminate
Definition: threadimpl.h:58
AJA_ThreadPriority_AboveNormal
@ AJA_ThreadPriority_AboveNormal
Definition: thread.h:46
AJAThreadImpl::mhThreadHandle
HANDLE mhThreadHandle
Definition: threadimpl.h:43
AJAThreadRealTimePolicy
AJAThreadRealTimePolicy
Definition: thread.h:50
AJA_ThreadPriority_Low
@ AJA_ThreadPriority_Low
Definition: thread.h:42
AJAThreadImpl::ThreadProcStatic
static void * ThreadProcStatic(void *pThreadImplContext)
Definition: threadimpl.cpp:581
AJAThreadImpl::AJAThreadImpl
AJAThreadImpl(AJAThread *pThreadContext)
Definition: threadimpl.cpp:45
true
#define true
Definition: ntv2devicefeatures.h:26
AJAThreadImpl::Stop
AJAStatus Stop(uint32_t timeout=0xffffffff)
Definition: threadimpl.cpp:208
AJAThread::AJAThreadFunction
void AJAThreadFunction(AJAThread *pThread, void *pContext)
Definition: thread.h:22
AJA_ASSERT
#define AJA_ASSERT(_expression_)
Definition: debug.h:113
AJAThreadImpl::Start
AJAStatus Start()
Definition: threadimpl.cpp:116
AJAThreadImpl::SetPriority
AJAStatus SetPriority(AJAThreadPriority threadPriority)
Definition: threadimpl.cpp:425
AJAThreadImpl::Attach
AJAStatus Attach(AJAThreadFunction *pThreadFunction, void *pUserContext)
Definition: threadimpl.cpp:566
AJAThreadImpl::GetPriority
AJAStatus GetPriority(AJAThreadPriority *pThreadPriority)
Definition: threadimpl.cpp:504
debug.h
Declares the AJADebug class.
AJAThreadImpl::SetRealTime
AJAStatus SetRealTime(AJAThreadRealTimePolicy policy, int priority)
Definition: threadimpl.cpp:522