libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
thread.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "precompiled.hpp"
31 #include "macros.hpp"
32 #include "thread.hpp"
33 #include "err.hpp"
34 #include "platform.hpp"
35 
36 #ifdef ZMQ_HAVE_WINDOWS
37 
38 extern "C"
39 {
40 #if defined _WIN32_WCE
41  static DWORD thread_routine (LPVOID arg_)
42 #else
43  static unsigned int __stdcall thread_routine (void *arg_)
44 #endif
45  {
46  zmq::thread_t *self = (zmq::thread_t*) arg_;
47  self->tfn (self->arg);
48  return 0;
49  }
50 }
51 
52 void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
53 {
54  tfn = tfn_;
55  arg = arg_;
56 #if defined _WIN32_WCE
57  descriptor = (HANDLE) CreateThread (NULL, 0,
58  &::thread_routine, this, 0 , NULL);
59 #else
60  descriptor = (HANDLE) _beginthreadex (NULL, 0,
61  &::thread_routine, this, 0 , NULL);
62 #endif
63  win_assert (descriptor != NULL);
64 }
65 
66 void zmq::thread_t::stop ()
67 {
68  DWORD rc = WaitForSingleObject (descriptor, INFINITE);
69  win_assert (rc != WAIT_FAILED);
70  BOOL rc2 = CloseHandle (descriptor);
71  win_assert (rc2 != 0);
72 }
73 
74 void zmq::thread_t::setSchedulingParameters(int priority_, int schedulingPolicy_)
75 {
76  // not implemented
77  LIBZMQ_UNUSED (priority_);
78  LIBZMQ_UNUSED (schedulingPolicy_);
79 }
80 
81 #else
82 
83 #include <signal.h>
84 #include <unistd.h>
85 
86 extern "C"
87 {
88  static void *thread_routine (void *arg_)
89  {
90 #if !defined ZMQ_HAVE_OPENVMS && !defined ZMQ_HAVE_ANDROID
91  // Following code will guarantee more predictable latencies as it'll
92  // disallow any signal handling in the I/O thread.
93  sigset_t signal_set;
94  int rc = sigfillset (&signal_set);
95  errno_assert (rc == 0);
96  rc = pthread_sigmask (SIG_BLOCK, &signal_set, NULL);
97  posix_assert (rc);
98 #endif
99 
100  zmq::thread_t *self = (zmq::thread_t*) arg_;
101  self->tfn (self->arg);
102  return NULL;
103  }
104 }
105 
106 void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
107 {
108  tfn = tfn_;
109  arg = arg_;
110  int rc = pthread_create (&descriptor, NULL, thread_routine, this);
111  posix_assert (rc);
112 }
113 
115 {
116  int rc = pthread_join (descriptor, NULL);
117  posix_assert (rc);
118 }
119 
120 void zmq::thread_t::setSchedulingParameters(int priority_, int schedulingPolicy_)
121 {
122 #if defined _POSIX_THREAD_PRIORITY_SCHEDULING && _POSIX_THREAD_PRIORITY_SCHEDULING >= 0
123  int policy = 0;
124  struct sched_param param;
125 
126 #if _POSIX_THREAD_PRIORITY_SCHEDULING == 0 && defined _SC_THREAD_PRIORITY_SCHEDULING
127  if (sysconf(_SC_THREAD_PRIORITY_SCHEDULING) < 0) {
128  return;
129  }
130 #endif
131  int rc = pthread_getschedparam(descriptor, &policy, &param);
132  posix_assert (rc);
133 
134  if(priority_ != -1)
135  {
136  param.sched_priority = priority_;
137  }
138 
139  if(schedulingPolicy_ != -1)
140  {
141  policy = schedulingPolicy_;
142  }
143 
144 #ifdef __NetBSD__
145  if(policy == SCHED_OTHER) param.sched_priority = -1;
146 #endif
147 
148  rc = pthread_setschedparam(descriptor, policy, &param);
149  posix_assert (rc);
150 #else
151 
152  LIBZMQ_UNUSED (priority_);
153  LIBZMQ_UNUSED (schedulingPolicy_);
154 #endif
155 }
156 
157 #endif
thread_fn * tfn
Definition: thread.hpp:76
#define posix_assert(x)
Definition: err.hpp:139
pthread_t descriptor
Definition: thread.hpp:84
void start(thread_fn *tfn_, void *arg_)
Definition: thread.cpp:106
void stop()
Definition: thread.cpp:114
static void * thread_routine(void *arg_)
Definition: thread.cpp:88
void setSchedulingParameters(int priority_, int schedulingPolicy_)
Definition: thread.cpp:120
#define LIBZMQ_UNUSED(object)
Definition: macros.hpp:6
void * arg
Definition: thread.hpp:77
#define errno_assert(x)
Definition: err.hpp:129
void( thread_fn)(void *)
Definition: thread.hpp:44