ThreadPriority.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //-----------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2006-2021 Basler AG
  4. // http://www.baslerweb.com
  5. //-----------------------------------------------------------------------------
  6. /*!
  7. \file
  8. \brief Declaration of methods to control the thread priority
  9. */
  10. #ifndef __THREADPRIORITY_H__
  11. #define __THREADPRIORITY_H__
  12. #if _MSC_VER > 1000
  13. #pragma once
  14. #endif // _MSC_VER > 1000
  15. #include <pylon/Platform.h>
  16. #ifdef _MSC_VER
  17. # pragma pack(push, PYLON_PACKING)
  18. #endif /* _MSC_VER */
  19. #include "PylonBase.h"
  20. namespace Pylon
  21. {
  22. #if defined(PYLON_WIN_BUILD)
  23. /** \brief Typedef for a Win32 thread handle
  24. \ingroup PYLON_PUBLIC */
  25. typedef HANDLE THREADHANDLE;
  26. #elif defined(PYLON_HAS_POSIX_THREADS)
  27. /** \brief Typedef for a pthreads thread handle */
  28. typedef pthread_t THREADHANDLE;
  29. #else
  30. # error Platform not supported.
  31. #endif
  32. /**
  33. \brief Queries the range of allowed thread priorities
  34. \ingroup PYLON_PUBLIC
  35. */
  36. void PYLONBASE_API GetRTThreadPriorityCapabilities( int& prioMin, int& prioMax );
  37. /**
  38. \brief Indicates the current thread priority of a thread
  39. \ingroup PYLON_PUBLIC
  40. */
  41. int PYLONBASE_API GetRTThreadPriority( THREADHANDLE thread );
  42. /**
  43. \brief Allows to set the realtime thread priority of a thread
  44. \ingroup PYLON_PUBLIC
  45. \if windows
  46. This method allows to raise a thread's priority into the range
  47. of "realtime" thread priorities. The methods can control each thread
  48. independently. This means that it it not necessary to raise the process to the
  49. "realtime" priority class, which would affect all thread of the process.
  50. If the process is not in the realtime priority class and if the process has the
  51. SE_INC_BASE_PRIORITY_NAME privilege, values from 15 to 31 can be set. If the
  52. privilege is not granted, only 15 can be set.
  53. If the process is in the realtime priority class,
  54. values from 16 to 31 can be set. No special permissions are required.
  55. To grant the privilege, use the Windows policy editor to enable the
  56. SE_INC_BASE_PRIORITY_NAME right (User Rights Assignment / Increase scheduling policy).
  57. Typically, all members of the Administrators group have the privilege enabled.
  58. Caution: Do not change the process priority class after setting the realtime thread priority.
  59. \endif
  60. \if linux
  61. Values greater than 0 set the thread's static priority and imply realtime scheduling
  62. (SCHED_RR scheduling policy). When setting the value 0, non-realtime scheduling is used.
  63. The thread's dynamic priority depends on the process' dynamic priority (nice scheduling).
  64. Setting realtime priorities requires certain permissions. The pylon install guide described
  65. how to grant an application the permissions to set realtime thread priorities.
  66. \endif
  67. \if osx
  68. Values greater than 0 set the thread's static priority and imply realtime scheduling
  69. (THREAD_TIME_CONSTRAINT_POLICY scheduling policy). When setting the value 0, non-realtime
  70. scheduling is used and the thread's static priority is set to the highest possible depends
  71. on the process dynamic priority (nice scheduling).
  72. \endif
  73. Typically a thread that receives image data should be set to realtime thread priorities
  74. to reduce jitter and delays. Be aware that such a realtime thread shouldn't perform time
  75. consuming tasks (like image processing). A realtime thread that is continuously working can
  76. cause the whole operating system to be blocked!
  77. */
  78. void PYLONBASE_API SetRTThreadPriority( THREADHANDLE thread, int priority );
  79. /**
  80. \brief Get current running thread handle.
  81. This wrapper method return the handle of the current running thread.
  82. */
  83. inline THREADHANDLE GetCurrentThreadHandle()
  84. {
  85. #if defined(PYLON_WIN_BUILD)
  86. return ::GetCurrentThread();
  87. #elif defined(PYLON_HAS_POSIX_THREADS)
  88. return pthread_self();
  89. #else
  90. # error Platform not supported.
  91. #endif
  92. }
  93. /**
  94. \brief Get current running thread id.
  95. This wrapper method return the id of the current running thread.
  96. */
  97. inline int GetCurrentThreadIdentifier()
  98. {
  99. #if defined(PYLON_WIN_BUILD)
  100. return static_cast<int>(::GetCurrentThreadId());
  101. #elif defined(PYLON_LINUX_BUILD)
  102. return static_cast<int>(pthread_self());
  103. #elif defined(PYLON_HAS_POSIX_THREADS)
  104. return reinterpret_cast<long int>(pthread_self());
  105. #else
  106. # error Platform not supported.
  107. #endif
  108. }
  109. }
  110. #ifdef _MSC_VER
  111. # pragma pack(pop)
  112. #endif /* _MSC_VER */
  113. #endif //__THREADPRIORITY_H__