4
// $Id: //poco/1.2/Foundation/src/Thread_POSIX.cpp#1 $
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/Thread_POSIX.h"
38
#include "Poco/Exception.h"
39
#include "Poco/ErrorHandler.h"
44
// Block SIGPIPE in main thread.
46
#if defined(POCO_OS_FAMILY_UNIX)
56
sigaddset(&sset, SIGPIPE);
57
pthread_sigmask(SIG_BLOCK, &sset, 0);
64
static SignalBlocker signalBlocker;
72
pthread_key_t ThreadImpl::_currentKey;
73
bool ThreadImpl::_haveCurrentKey = false;
76
ThreadImpl::ThreadImpl():
79
_prio(PRIO_NORMAL_IMPL)
83
if (pthread_key_create(&_currentKey, NULL))
84
throw SystemException("cannot allocate thread context key");
85
_haveCurrentKey = true;
90
ThreadImpl::~ThreadImpl()
93
pthread_detach(_thread);
97
void ThreadImpl::setPriorityImpl(int prio)
104
struct sched_param par;
105
par.sched_priority = mapPrio(_prio);
106
if (pthread_setschedparam(_thread, SCHED_OTHER, &par))
107
throw SystemException("cannot set thread priority");
113
void ThreadImpl::startImpl(Runnable& target)
115
if (_pTarget) throw SystemException("thread already running");
118
if (pthread_create(&_thread, NULL, entry, this))
121
throw SystemException("cannot start thread");
124
if (_prio != PRIO_NORMAL_IMPL)
126
struct sched_param par;
127
par.sched_priority = mapPrio(_prio);
128
if (pthread_setschedparam(_thread, SCHED_OTHER, &par))
129
throw SystemException("cannot set thread priority");
134
void ThreadImpl::joinImpl()
136
if (!_pTarget) return;
138
if (pthread_join(_thread, &result))
139
throw SystemException("cannot join thread");
144
bool ThreadImpl::isRunningImpl() const
146
return _pTarget != 0;
150
ThreadImpl* ThreadImpl::currentImpl()
153
return (ThreadImpl*) pthread_getspecific(_currentKey);
159
void* ThreadImpl::entry(void* pThread)
161
pthread_setspecific(_currentKey, pThread);
163
#if defined(POCO_OS_FAMILY_UNIX)
166
sigaddset(&sset, SIGQUIT);
167
sigaddset(&sset, SIGTERM);
168
sigaddset(&sset, SIGPIPE);
169
pthread_sigmask(SIG_BLOCK, &sset, 0);
174
reinterpret_cast<ThreadImpl*>(pThread)->_pTarget->run();
176
catch (Exception& exc)
178
ErrorHandler::handle(exc);
180
catch (std::exception& exc)
182
ErrorHandler::handle(exc);
186
ErrorHandler::handle();
192
int ThreadImpl::mapPrio(int prio)
194
#if defined(__VMS) || defined(__digital__)
195
static const int pmin = PRI_OTHER_MIN;
196
static const int pmax = PRI_OTHER_MAX;
198
static const int pmin = sched_get_priority_min(SCHED_OTHER);
199
static const int pmax = sched_get_priority_max(SCHED_OTHER);
204
case PRIO_LOWEST_IMPL:
207
return pmin + (pmax - pmin)/4;
208
case PRIO_NORMAL_IMPL:
209
return pmin + (pmax - pmin)/2;
211
return pmin + 3*(pmax - pmin)/4;
212
case PRIO_HIGHEST_IMPL:
215
poco_bugcheck_msg("invalid thread priority");
217
return -1; // just to satisfy compiler - we'll never get here anyway