thread.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifndef __ZMQ_THREAD_HPP_INCLUDED__
  25. #define __ZMQ_THREAD_HPP_INCLUDED__
  26. #if defined ZMQ_HAVE_VXWORKS
  27. #include <vxWorks.h>
  28. #include <taskLib.h>
  29. #elif !defined ZMQ_HAVE_WINDOWS
  30. #include <pthread.h>
  31. #endif
  32. #include <set>
  33. #include <cstring>
  34. namespace zmq
  35. {
  36. typedef void (thread_fn) (void *);
  37. // Class encapsulating OS thread. Thread initiation/termination is done
  38. // using special functions rather than in constructor/destructor so that
  39. // thread isn't created during object construction by accident, causing
  40. // newly created thread to access half-initialised object. Same applies
  41. // to the destruction process: Thread should be terminated before object
  42. // destruction begins, otherwise it can access half-destructed object.
  43. class thread_t
  44. {
  45. public:
  46. thread_t () :
  47. _tfn (NULL),
  48. _arg (NULL),
  49. _started (false),
  50. _thread_priority (ZMQ_THREAD_PRIORITY_DFLT),
  51. _thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_DFLT)
  52. {
  53. memset (_name, 0, sizeof (_name));
  54. }
  55. #ifdef ZMQ_HAVE_VXWORKS
  56. ~thread_t ()
  57. {
  58. if (descriptor != NULL || descriptor > 0) {
  59. taskDelete (descriptor);
  60. }
  61. }
  62. #endif
  63. // Creates OS thread. 'tfn' is main thread function. It'll be passed
  64. // 'arg' as an argument.
  65. // Name is 16 characters max including terminating NUL. Thread naming is
  66. // implemented only for pthread, and windows when a debugger is attached.
  67. void start (thread_fn *tfn_, void *arg_, const char *name_);
  68. // Returns whether the thread was started, i.e. start was called.
  69. bool get_started () const;
  70. // Returns whether the executing thread is the thread represented by the
  71. // thread object.
  72. bool is_current_thread () const;
  73. // Waits for thread termination.
  74. void stop ();
  75. // Sets the thread scheduling parameters. Only implemented for
  76. // pthread. Has no effect on other platforms.
  77. void setSchedulingParameters (int priority_,
  78. int scheduling_policy_,
  79. const std::set<int> &affinity_cpus_);
  80. // These are internal members. They should be private, however then
  81. // they would not be accessible from the main C routine of the thread.
  82. void applySchedulingParameters ();
  83. void applyThreadName ();
  84. thread_fn *_tfn;
  85. void *_arg;
  86. char _name[16];
  87. private:
  88. bool _started;
  89. #ifdef ZMQ_HAVE_WINDOWS
  90. HANDLE _descriptor;
  91. #if defined _WIN32_WCE
  92. DWORD _thread_id;
  93. #else
  94. unsigned int _thread_id;
  95. #endif
  96. #elif defined ZMQ_HAVE_VXWORKS
  97. int _descriptor;
  98. enum
  99. {
  100. DEFAULT_PRIORITY = 100,
  101. DEFAULT_OPTIONS = 0,
  102. DEFAULT_STACK_SIZE = 4000
  103. };
  104. #else
  105. pthread_t _descriptor;
  106. #endif
  107. // Thread scheduling parameters.
  108. int _thread_priority;
  109. int _thread_sched_policy;
  110. std::set<int> _thread_affinity_cpus;
  111. ZMQ_NON_COPYABLE_NOR_MOVABLE (thread_t)
  112. };
  113. }
  114. #endif