| 1 |
|
| 2 |
// Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without modification, |
| 6 |
// are permitted provided that the following conditions are met: |
| 7 |
// |
| 8 |
// 1. Redistributions of source code must retain the above copyright notice, |
| 9 |
// this list of conditions, and the following disclaimer. |
| 10 |
// |
| 11 |
// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
// this list of conditions, and the following disclaimer in the documentation |
| 13 |
// and/or other materials provided with the distribution. |
| 14 |
// |
| 15 |
// 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 |
// may be used to endorse or promote products derived from this software without |
| 17 |
// specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 |
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 |
// THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 |
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 |
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 |
// SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 |
// OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 |
// ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 |
// |
| 32 |
|
| 33 |
#ifndef XSTHREAD_H |
| 34 |
#define XSTHREAD_H |
| 35 |
|
| 36 |
#include "xstime.h" |
| 37 |
#if defined(XSENS_DEBUG) && defined(_MSC_VER) |
| 38 |
#pragma warning (disable: 4985) |
| 39 |
#include <intrin.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifndef __GNUC__ |
| 43 |
#pragma warning(disable: 4127) |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef __cplusplus |
| 47 |
extern "C" { |
| 48 |
#endif |
| 49 |
|
| 50 |
#ifdef XSENS_WINDOWS |
| 51 |
#ifndef WINVER // Allow use of features specific to Windows XP or later. |
| 52 |
#define WINVER 0x0502 // Change this to the appropriate value to target other versions of Windows. |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. |
| 56 |
#define _WIN32_WINNT 0x0502 // Change this to the appropriate value to target other versions of Windows. |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later. |
| 60 |
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later. |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifndef _WIN32_IE // Allow use of features specific to IE 6.0 or later. |
| 64 |
#define _WIN32_IE 0x0600 // Change this to the appropriate value to target other versions of IE. |
| 65 |
#endif |
| 66 |
|
| 67 |
#include <windows.h> |
| 68 |
|
| 69 |
/*! \addtogroup enums Global enumerations |
| 70 |
@{ |
| 71 |
*/ |
| 72 |
/*! \brief Thread priorities for xsSetThreadPriority() and xsGetThreadPriority() |
| 73 |
*/ |
| 74 |
enum XsThreadPriority { |
| 75 |
XS_THREAD_PRIORITY_LOWEST = THREAD_PRIORITY_IDLE, |
| 76 |
XS_THREAD_PRIORITY_LOWER = THREAD_PRIORITY_LOWEST, |
| 77 |
XS_THREAD_PRIORITY_LOW = THREAD_PRIORITY_BELOW_NORMAL, |
| 78 |
XS_THREAD_PRIORITY_NORMAL = THREAD_PRIORITY_NORMAL, |
| 79 |
XS_THREAD_PRIORITY_HIGH = THREAD_PRIORITY_ABOVE_NORMAL, |
| 80 |
XS_THREAD_PRIORITY_HIGHER = THREAD_PRIORITY_HIGHEST, |
| 81 |
XS_THREAD_PRIORITY_HIGHEST = THREAD_PRIORITY_TIME_CRITICAL |
| 82 |
}; |
| 83 |
/*! @} */ |
| 84 |
|
| 85 |
// The components of the type of a thread function |
| 86 |
#define XSENS_THREAD_RETURN DWORD |
| 87 |
#define XSENS_THREAD_TYPE WINAPI |
| 88 |
#define XSENS_THREAD_PARAM LPVOID |
| 89 |
|
| 90 |
#define XSENS_INVALID_THREAD INVALID_HANDLE_VALUE |
| 91 |
|
| 92 |
/*! \brief Release the remainder of the timeslice so other operations can run. |
| 93 |
On Windows this is done using Sleep(0), since this is the most reliable method. |
| 94 |
SwitchToThread can cause delays since it does not allow the thread to resume on a different core. |
| 95 |
Sleep http://msdn.microsoft.com/en-us/library/ms686298%28v=vs.85%29.aspx |
| 96 |
SwitchToThread http://msdn.microsoft.com/en-us/library/ms686352%28v=vs.85%29.aspx |
| 97 |
*/ |
| 98 |
#define xsYield() Sleep(0) |
| 99 |
|
| 100 |
//! A handle for a thread |
| 101 |
typedef HANDLE XsThread; |
| 102 |
#ifdef __cplusplus |
| 103 |
typedef ::DWORD XsThreadId; |
| 104 |
#else |
| 105 |
typedef DWORD XsThreadId; |
| 106 |
#endif |
| 107 |
|
| 108 |
//! Start a function as a thread |
| 109 |
#define xsStartThread(func,param,pid) CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) func,param,0,pid) |
| 110 |
|
| 111 |
XSTYPES_DLL_API void xsNameThisThread(const char* threadName); |
| 112 |
|
| 113 |
#define xsGetCurrentThreadId() GetCurrentThreadId() |
| 114 |
#define xsSuspendThread(thrd) SuspendThread(thrd) |
| 115 |
#define xsResumeThread(thrd) ResumeThread(thrd) |
| 116 |
#define xsSetThreadPriority(thrd,prio) SetThreadPriority(thrd,prio) |
| 117 |
#define xsGetThreadPriority(thrd) GetThreadPriority(thrd) |
| 118 |
|
| 119 |
#else |
| 120 |
#include <pthread.h> |
| 121 |
#include <semaphore.h> |
| 122 |
#include <errno.h> |
| 123 |
|
| 124 |
/*! \addtogroup enums Global enumerations |
| 125 |
@{ |
| 126 |
*/ |
| 127 |
/*! \brief Thread priorities for xsSetThreadPriority() and xsGetThreadPriority() |
| 128 |
*/ |
| 129 |
enum XsThreadPriority { |
| 130 |
XS_THREAD_PRIORITY_LOWEST = 0, //THREAD_PRIORITY_IDLE, |
| 131 |
XS_THREAD_PRIORITY_LOWER = 1, //THREAD_PRIORITY_LOWEST, |
| 132 |
XS_THREAD_PRIORITY_LOW = 2, //THREAD_PRIORITY_BELOW_NORMAL, |
| 133 |
XS_THREAD_PRIORITY_NORMAL = 3, //THREAD_PRIORITY_NORMAL, |
| 134 |
XS_THREAD_PRIORITY_HIGH = 4, //THREAD_PRIORITY_ABOVE_NORMAL, |
| 135 |
XS_THREAD_PRIORITY_HIGHER = 5, //THREAD_PRIORITY_HIGHEST, |
| 136 |
XS_THREAD_PRIORITY_HIGHEST = 6 //THREAD_PRIORITY_TIME_CRITICAL |
| 137 |
}; |
| 138 |
/*! @} */ |
| 139 |
|
| 140 |
// The components of the type of a thread function |
| 141 |
#define XSENS_THREAD_RETURN void* // DWORD |
| 142 |
#define XSENS_THREAD_TYPE // WINAPI |
| 143 |
#define XSENS_THREAD_PARAM void* // LPVOID |
| 144 |
|
| 145 |
#define XSENS_INVALID_THREAD 0 // INVALID_HANDLE_VALUE |
| 146 |
|
| 147 |
//! Release the remainder of the timeslice so other operations can run. |
| 148 |
#define xsYield() sched_yield() |
| 149 |
|
| 150 |
void xsNameThisThread(const char* threadName); |
| 151 |
//! A handle for a thread |
| 152 |
typedef pthread_t XsThread; |
| 153 |
typedef pthread_t XsThreadId; |
| 154 |
|
| 155 |
//! Start a function as a thread |
| 156 |
pthread_t xsStartThread(void *(func)(void *), void *param, void *pid); |
| 157 |
#define xsGetCurrentThreadId() pthread_self() |
| 158 |
#define xsSuspendThread(thrd) |
| 159 |
#define xsResumeThread(thrd) |
| 160 |
#define xsSetThreadPriority(thrd,prio) |
| 161 |
|
| 162 |
#endif // _WIN32 |
| 163 |
|
| 164 |
#if !defined(XSENS_USE_POSIX_LOCKING) |
| 165 |
#if defined(XSENS_WINDOWS) |
| 166 |
#define XSENS_USE_POSIX_LOCKING 0 |
| 167 |
#else |
| 168 |
#define XSENS_USE_POSIX_LOCKING 1 |
| 169 |
#endif |
| 170 |
#endif |
| 171 |
|
| 172 |
#ifndef __GNUC__ |
| 173 |
#pragma warning(default: 4127) |
| 174 |
#endif |
| 175 |
|
| 176 |
#ifdef __cplusplus |
| 177 |
} // extern "C" |
| 178 |
#endif |
| 179 |
|
| 180 |
typedef enum XsThreadPriority XsThreadPriority; |
| 181 |
|
| 182 |
#endif |