thread.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "precompiled.hpp"
  25. #include "macros.hpp"
  26. #include "thread.hpp"
  27. #include "err.hpp"
  28. #ifdef ZMQ_HAVE_WINDOWS
  29. #include <winnt.h>
  30. #endif
  31. bool zmq::thread_t::get_started () const
  32. {
  33. return _started;
  34. }
  35. #ifdef ZMQ_HAVE_WINDOWS
  36. extern "C" {
  37. #if defined _WIN32_WCE
  38. static DWORD thread_routine (LPVOID arg_)
  39. #else
  40. static unsigned int __stdcall thread_routine (void *arg_)
  41. #endif
  42. {
  43. zmq::thread_t *self = static_cast<zmq::thread_t *> (arg_);
  44. self->applyThreadName ();
  45. self->_tfn (self->_arg);
  46. return 0;
  47. }
  48. }
  49. void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
  50. {
  51. _tfn = tfn_;
  52. _arg = arg_;
  53. if (name_)
  54. strncpy (_name, name_, sizeof (_name) - 1);
  55. #if defined _WIN32_WCE
  56. _descriptor =
  57. (HANDLE) CreateThread (NULL, 0, &::thread_routine, this, 0, &_thread_id);
  58. #else
  59. _descriptor = (HANDLE) _beginthreadex (NULL, 0, &::thread_routine, this, 0,
  60. &_thread_id);
  61. #endif
  62. win_assert (_descriptor != NULL);
  63. _started = true;
  64. }
  65. bool zmq::thread_t::is_current_thread () const
  66. {
  67. return GetCurrentThreadId () == _thread_id;
  68. }
  69. void zmq::thread_t::stop ()
  70. {
  71. if (_started) {
  72. const DWORD rc = WaitForSingleObject (_descriptor, INFINITE);
  73. win_assert (rc != WAIT_FAILED);
  74. const BOOL rc2 = CloseHandle (_descriptor);
  75. win_assert (rc2 != 0);
  76. }
  77. }
  78. void zmq::thread_t::setSchedulingParameters (
  79. int priority_, int scheduling_policy_, const std::set<int> &affinity_cpus_)
  80. {
  81. // not implemented
  82. LIBZMQ_UNUSED (priority_);
  83. LIBZMQ_UNUSED (scheduling_policy_);
  84. LIBZMQ_UNUSED (affinity_cpus_);
  85. }
  86. void zmq::thread_t::
  87. applySchedulingParameters () // to be called in secondary thread context
  88. {
  89. // not implemented
  90. }
  91. namespace
  92. {
  93. #pragma pack(push, 8)
  94. struct thread_info_t
  95. {
  96. DWORD _type;
  97. LPCSTR _name;
  98. DWORD _thread_id;
  99. DWORD _flags;
  100. };
  101. #pragma pack(pop)
  102. }
  103. struct MY_EXCEPTION_REGISTRATION_RECORD
  104. {
  105. typedef EXCEPTION_DISPOSITION (NTAPI *HandlerFunctionType) (
  106. EXCEPTION_RECORD *, void *, CONTEXT *, void *);
  107. MY_EXCEPTION_REGISTRATION_RECORD *Next;
  108. HandlerFunctionType Handler;
  109. };
  110. static EXCEPTION_DISPOSITION NTAPI continue_execution (EXCEPTION_RECORD *rec,
  111. void *frame,
  112. CONTEXT *ctx,
  113. void *disp)
  114. {
  115. return ExceptionContinueExecution;
  116. }
  117. void zmq::thread_t::
  118. applyThreadName () // to be called in secondary thread context
  119. {
  120. if (!_name[0] || !IsDebuggerPresent ())
  121. return;
  122. thread_info_t thread_info;
  123. thread_info._type = 0x1000;
  124. thread_info._name = _name;
  125. thread_info._thread_id = -1;
  126. thread_info._flags = 0;
  127. NT_TIB *tib = ((NT_TIB *) NtCurrentTeb ());
  128. MY_EXCEPTION_REGISTRATION_RECORD rec;
  129. rec.Next = (MY_EXCEPTION_REGISTRATION_RECORD *) tib->ExceptionList;
  130. rec.Handler = continue_execution;
  131. // push our handler, raise, and finally pop our handler
  132. tib->ExceptionList = (_EXCEPTION_REGISTRATION_RECORD *) &rec;
  133. const DWORD MS_VC_EXCEPTION = 0x406D1388;
  134. RaiseException (MS_VC_EXCEPTION, 0,
  135. sizeof (thread_info) / sizeof (ULONG_PTR),
  136. (ULONG_PTR *) &thread_info);
  137. tib->ExceptionList =
  138. (_EXCEPTION_REGISTRATION_RECORD
  139. *) (((MY_EXCEPTION_REGISTRATION_RECORD *) tib->ExceptionList)->Next);
  140. }
  141. #elif defined ZMQ_HAVE_VXWORKS
  142. extern "C" {
  143. static void *thread_routine (void *arg_)
  144. {
  145. zmq::thread_t *self = (zmq::thread_t *) arg_;
  146. self->applySchedulingParameters ();
  147. self->_tfn (self->_arg);
  148. return NULL;
  149. }
  150. }
  151. void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
  152. {
  153. LIBZMQ_UNUSED (name_);
  154. _tfn = tfn_;
  155. _arg = arg_;
  156. _descriptor = taskSpawn (NULL, DEFAULT_PRIORITY, DEFAULT_OPTIONS,
  157. DEFAULT_STACK_SIZE, (FUNCPTR) thread_routine,
  158. (int) this, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  159. if (_descriptor != NULL || _descriptor > 0)
  160. _started = true;
  161. }
  162. void zmq::thread_t::stop ()
  163. {
  164. if (_started)
  165. while ((_descriptor != NULL || _descriptor > 0)
  166. && taskIdVerify (_descriptor) == 0) {
  167. }
  168. }
  169. bool zmq::thread_t::is_current_thread () const
  170. {
  171. return taskIdSelf () == _descriptor;
  172. }
  173. void zmq::thread_t::setSchedulingParameters (
  174. int priority_, int schedulingPolicy_, const std::set<int> &affinity_cpus_)
  175. {
  176. _thread_priority = priority_;
  177. _thread_sched_policy = schedulingPolicy_;
  178. _thread_affinity_cpus = affinity_cpus_;
  179. }
  180. void zmq::thread_t::
  181. applySchedulingParameters () // to be called in secondary thread context
  182. {
  183. int priority =
  184. (_thread_priority >= 0 ? _thread_priority : DEFAULT_PRIORITY);
  185. priority = (priority < UCHAR_MAX ? priority : DEFAULT_PRIORITY);
  186. if (_descriptor != NULL || _descriptor > 0) {
  187. taskPrioritySet (_descriptor, priority);
  188. }
  189. }
  190. void zmq::thread_t::
  191. applyThreadName () // to be called in secondary thread context
  192. {
  193. // not implemented
  194. }
  195. #else
  196. #include <signal.h>
  197. #include <unistd.h>
  198. #include <sys/time.h>
  199. #include <sys/resource.h>
  200. extern "C" {
  201. static void *thread_routine (void *arg_)
  202. {
  203. #if !defined ZMQ_HAVE_OPENVMS && !defined ZMQ_HAVE_ANDROID
  204. // Following code will guarantee more predictable latencies as it'll
  205. // disallow any signal handling in the I/O thread.
  206. sigset_t signal_set;
  207. int rc = sigfillset (&signal_set);
  208. errno_assert (rc == 0);
  209. rc = pthread_sigmask (SIG_BLOCK, &signal_set, NULL);
  210. posix_assert (rc);
  211. #endif
  212. zmq::thread_t *self = (zmq::thread_t *) arg_;
  213. self->applySchedulingParameters ();
  214. self->applyThreadName ();
  215. self->_tfn (self->_arg);
  216. return NULL;
  217. }
  218. }
  219. void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
  220. {
  221. _tfn = tfn_;
  222. _arg = arg_;
  223. if (name_)
  224. strncpy (_name, name_, sizeof (_name) - 1);
  225. int rc = pthread_create (&_descriptor, NULL, thread_routine, this);
  226. posix_assert (rc);
  227. _started = true;
  228. }
  229. void zmq::thread_t::stop ()
  230. {
  231. if (_started) {
  232. int rc = pthread_join (_descriptor, NULL);
  233. posix_assert (rc);
  234. }
  235. }
  236. bool zmq::thread_t::is_current_thread () const
  237. {
  238. return bool(pthread_equal (pthread_self (), _descriptor));
  239. }
  240. void zmq::thread_t::setSchedulingParameters (
  241. int priority_, int scheduling_policy_, const std::set<int> &affinity_cpus_)
  242. {
  243. _thread_priority = priority_;
  244. _thread_sched_policy = scheduling_policy_;
  245. _thread_affinity_cpus = affinity_cpus_;
  246. }
  247. void zmq::thread_t::
  248. applySchedulingParameters () // to be called in secondary thread context
  249. {
  250. #if defined _POSIX_THREAD_PRIORITY_SCHEDULING \
  251. && _POSIX_THREAD_PRIORITY_SCHEDULING >= 0
  252. int policy = 0;
  253. struct sched_param param;
  254. #if _POSIX_THREAD_PRIORITY_SCHEDULING == 0 \
  255. && defined _SC_THREAD_PRIORITY_SCHEDULING
  256. if (sysconf (_SC_THREAD_PRIORITY_SCHEDULING) < 0) {
  257. return;
  258. }
  259. #endif
  260. int rc = pthread_getschedparam (pthread_self (), &policy, &param);
  261. posix_assert (rc);
  262. if (_thread_sched_policy != ZMQ_THREAD_SCHED_POLICY_DFLT) {
  263. policy = _thread_sched_policy;
  264. }
  265. /* Quoting docs:
  266. "Linux allows the static priority range 1 to 99 for the SCHED_FIFO and
  267. SCHED_RR policies, and the priority 0 for the remaining policies."
  268. Other policies may use the "nice value" in place of the priority:
  269. */
  270. bool use_nice_instead_priority =
  271. (policy != SCHED_FIFO) && (policy != SCHED_RR);
  272. if (_thread_priority != ZMQ_THREAD_PRIORITY_DFLT) {
  273. if (use_nice_instead_priority)
  274. param.sched_priority =
  275. 0; // this is the only supported priority for most scheduling policies
  276. else
  277. param.sched_priority =
  278. _thread_priority; // user should provide a value between 1 and 99
  279. }
  280. #ifdef __NetBSD__
  281. if (policy == SCHED_OTHER)
  282. param.sched_priority = -1;
  283. #endif
  284. rc = pthread_setschedparam (pthread_self (), policy, &param);
  285. #if defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
  286. // If this feature is unavailable at run-time, don't abort.
  287. if (rc == ENOSYS)
  288. return;
  289. #endif
  290. posix_assert (rc);
  291. #if !defined ZMQ_HAVE_VXWORKS
  292. if (use_nice_instead_priority
  293. && _thread_priority != ZMQ_THREAD_PRIORITY_DFLT) {
  294. // assume the user wants to decrease the thread's nice value
  295. // i.e., increase the chance of this thread being scheduled: try setting that to
  296. // maximum priority.
  297. rc = nice (-20);
  298. errno_assert (rc != -1);
  299. // IMPORTANT: EPERM is typically returned for unprivileged processes: that's because
  300. // CAP_SYS_NICE capability is required or RLIMIT_NICE resource limit should be changed to avoid EPERM!
  301. }
  302. #endif
  303. #ifdef ZMQ_HAVE_PTHREAD_SET_AFFINITY
  304. if (!_thread_affinity_cpus.empty ()) {
  305. cpu_set_t cpuset;
  306. CPU_ZERO (&cpuset);
  307. for (std::set<int>::const_iterator it = _thread_affinity_cpus.begin (),
  308. end = _thread_affinity_cpus.end ();
  309. it != end; it++) {
  310. CPU_SET ((int) (*it), &cpuset);
  311. }
  312. rc =
  313. pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
  314. posix_assert (rc);
  315. }
  316. #endif
  317. #endif
  318. }
  319. void zmq::thread_t::
  320. applyThreadName () // to be called in secondary thread context
  321. {
  322. /* The thread name is a cosmetic string, added to ease debugging of
  323. * multi-threaded applications. It is not a big issue if this value
  324. * can not be set for any reason (such as Permission denied in some
  325. * cases where the application changes its EUID, etc.) The value of
  326. * "int rc" is retained where available, to help debuggers stepping
  327. * through code to see its value - but otherwise it is ignored.
  328. */
  329. if (!_name[0])
  330. return;
  331. /* Fails with permission denied on Android 5/6 */
  332. #if defined(ZMQ_HAVE_ANDROID)
  333. return;
  334. #endif
  335. #if defined(ZMQ_HAVE_PTHREAD_SETNAME_1)
  336. int rc = pthread_setname_np (_name);
  337. if (rc)
  338. return;
  339. #elif defined(ZMQ_HAVE_PTHREAD_SETNAME_2)
  340. int rc = pthread_setname_np (pthread_self (), _name);
  341. if (rc)
  342. return;
  343. #elif defined(ZMQ_HAVE_PTHREAD_SETNAME_3)
  344. int rc = pthread_setname_np (pthread_self (), _name, NULL);
  345. if (rc)
  346. return;
  347. #elif defined(ZMQ_HAVE_PTHREAD_SET_NAME)
  348. pthread_set_name_np (pthread_self (), _name);
  349. #endif
  350. }
  351. #endif