thread_win.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Platform Check
10 *****************************************************************************/
11 
12 #include <ecl/config/ecl.hpp>
13 #if defined(ECL_IS_WIN32)
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <iostream>
20 #include "../../include/ecl/threads/thread_win.hpp"
21 
22 /*****************************************************************************
23 ** Namespaces
24 *****************************************************************************/
25 
26 namespace ecl {
27 
28 /*****************************************************************************
29 * Thread Class Methods
30 *****************************************************************************/
31 
32 Thread::Thread(VoidFunction function, const Priority &priority, const long &stack_size) :
33  thread_handle(NULL),
34  thread_task(NULL),
35  has_started(false),
36  join_requested(false)
37 {
38  start(function, priority, stack_size);
39 }
40 
41 Error Thread::start(VoidFunction function, const Priority &priority, const long &stack_size)
42 {
43  // stack_size is ignored
44 
45  if ( has_started ) {
46  ecl_debug_throw(StandardException(LOC,BusyError,"The thread has already been started."));
47  return Error(BusyError); // if in release mode, gracefully fall back to return values.
48  } else {
49  has_started = true;
50  }
51 
52  NullaryFreeFunction<void> nullary_function_object = generateFunctionObject(function);
53  thread_task = new threads::ThreadTask< NullaryFreeFunction<void> >(nullary_function_object, priority);
54 
55  DWORD threadid;
56 
57  thread_handle = CreateThread(NULL,
58  0,
59  (LPTHREAD_START_ROUTINE)threads::ThreadTask< NullaryFreeFunction<void> >::EntryPoint,
60  thread_task,
61  0,
62  &threadid);
63 
64  if (!thread_handle) {
65  ecl_debug_throw(StandardException(LOC, UnknownError, "Failed to create thread."));
66  return Error(UnknownError);
67  }
68 
69  BOOL bResult = FALSE;
70 
71  if (priority >= RealTimePriority1) {
72  bResult = SetThreadPriority(thread_handle, THREAD_PRIORITY_TIME_CRITICAL);
73  }
74 
75  switch (priority) {
76  case CriticalPriority:
77  bResult = SetThreadPriority(thread_handle, HIGH_PRIORITY_CLASS);
78  break;
79  case HighPriority:
80  bResult = SetThreadPriority(thread_handle, THREAD_PRIORITY_ABOVE_NORMAL);
81  break;
82  case LowPriority:
83  bResult = SetThreadPriority(thread_handle, THREAD_PRIORITY_BELOW_NORMAL);
84  break;
85  case BackgroundPriority:
86  bResult = SetThreadPriority(thread_handle, THREAD_PRIORITY_IDLE);
87  break;
88  default:
89  break;
90  }
91 
92  if (!bResult) {
93  ecl_debug_throw(threads::throwPriorityException(LOC));
94  }
95 
96  return Error(NoError);
97 }
98 
99 Thread::~Thread() {
100  cancel();
101 }
102 
103 void Thread::cancel() {
104  if (thread_handle) {
105  unsigned long exitcode;
106  if (::GetExitCodeThread(thread_handle, &exitcode)) {
107  if (exitcode == 0x0103) {
108  // unsafe termination.
109  ::TerminateThread(thread_handle, exitcode);
110  }
111  }
112  ::CloseHandle(thread_handle);
113  thread_handle = NULL;
114  }
115  if (thread_task) {
116  delete thread_task;
117  thread_task = NULL;
118  }
119  has_started = false;
120  join_requested = false;
121 }
122 
123 void Thread::join() {
124  join_requested = true;
125 
126  if (thread_handle) {
127  WaitForSingleObject(thread_handle, INFINITE);
128  }
129 }
130 
131 void Thread::initialise(const long &stack_size) {
132  // stack_size is ignored
133 }
134 
135 }; // namespace ecl
136 
137 #endif /* ECL_IS_WIN32 */
ecl::HighPriority
@ HighPriority
Definition: priority_common.hpp:48
ecl::LowPriority
@ LowPriority
Definition: priority_common.hpp:46
ecl::VoidFunction
void(* VoidFunction)()
ecl::CriticalPriority
@ CriticalPriority
Definition: priority_common.hpp:49
ecl::RealTimePriority1
@ RealTimePriority1
Definition: priority_common.hpp:50
ecl::BackgroundPriority
@ BackgroundPriority
Definition: priority_common.hpp:45
ecl
Embedded control libraries.
ecl::Priority
Priority
Shared abstraction of the scheduling priorities.
Definition: priority_common.hpp:36
generateFunctionObject
NullaryFreeFunction< R > generateFunctionObject(R(*function)())
ecl_debug_throw
#define ecl_debug_throw(exception)


ecl_threads
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:43