~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/src/Thread_POSIX.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Thread_POSIX.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/src/Thread_POSIX.cpp#1 $
 
5
//
 
6
// Library: Foundation
 
7
// Package: Threading
 
8
// Module:  Thread
 
9
//
 
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
11
// and Contributors.
 
12
//
 
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:
 
19
// 
 
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.
 
26
// 
 
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.
 
34
//
 
35
 
 
36
 
 
37
#include "Poco/Thread_POSIX.h"
 
38
#include "Poco/Exception.h"
 
39
#include "Poco/ErrorHandler.h"
 
40
#include <signal.h>
 
41
 
 
42
 
 
43
//
 
44
// Block SIGPIPE in main thread.
 
45
//
 
46
#if defined(POCO_OS_FAMILY_UNIX)
 
47
namespace
 
48
{
 
49
        class SignalBlocker
 
50
        {
 
51
        public:
 
52
                SignalBlocker()
 
53
                {
 
54
                        sigset_t sset;
 
55
                        sigemptyset(&sset);
 
56
                        sigaddset(&sset, SIGPIPE); 
 
57
                        pthread_sigmask(SIG_BLOCK, &sset, 0);
 
58
                }
 
59
                ~SignalBlocker()
 
60
                {
 
61
                }
 
62
        };
 
63
        
 
64
        static SignalBlocker signalBlocker;
 
65
}
 
66
#endif
 
67
 
 
68
 
 
69
namespace Poco {
 
70
 
 
71
 
 
72
pthread_key_t ThreadImpl::_currentKey;
 
73
bool ThreadImpl::_haveCurrentKey = false;
 
74
 
 
75
 
 
76
ThreadImpl::ThreadImpl(): 
 
77
        _pTarget(0), 
 
78
        _thread(0), 
 
79
        _prio(PRIO_NORMAL_IMPL)
 
80
{
 
81
        if (!_haveCurrentKey)
 
82
        {
 
83
                if (pthread_key_create(&_currentKey, NULL))
 
84
                        throw SystemException("cannot allocate thread context key");
 
85
                _haveCurrentKey = true;
 
86
        }
 
87
}
 
88
 
 
89
                        
 
90
ThreadImpl::~ThreadImpl()
 
91
{
 
92
        if (_pTarget)
 
93
                pthread_detach(_thread);
 
94
}
 
95
 
 
96
 
 
97
void ThreadImpl::setPriorityImpl(int prio)
 
98
{
 
99
        if (prio != _prio)
 
100
        {
 
101
                _prio = prio;
 
102
                if (_pTarget)
 
103
                {
 
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");
 
108
                }
 
109
        }
 
110
}
 
111
 
 
112
 
 
113
void ThreadImpl::startImpl(Runnable& target)
 
114
{
 
115
        if (_pTarget) throw SystemException("thread already running");
 
116
 
 
117
        _pTarget = &target;
 
118
        if (pthread_create(&_thread, NULL, entry, this))
 
119
        {
 
120
                _pTarget = 0;
 
121
                throw SystemException("cannot start thread");
 
122
        }
 
123
 
 
124
        if (_prio != PRIO_NORMAL_IMPL)
 
125
        {
 
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");
 
130
        }
 
131
}
 
132
 
 
133
 
 
134
void ThreadImpl::joinImpl()
 
135
{
 
136
        if (!_pTarget) return;
 
137
        void* result;
 
138
        if (pthread_join(_thread, &result))
 
139
                throw SystemException("cannot join thread"); 
 
140
        _pTarget = 0;
 
141
}
 
142
 
 
143
 
 
144
bool ThreadImpl::isRunningImpl() const
 
145
{
 
146
        return _pTarget != 0;
 
147
}
 
148
 
 
149
 
 
150
ThreadImpl* ThreadImpl::currentImpl()
 
151
{
 
152
        if (_haveCurrentKey)
 
153
                return (ThreadImpl*) pthread_getspecific(_currentKey);
 
154
        else
 
155
                return 0;
 
156
}
 
157
 
 
158
 
 
159
void* ThreadImpl::entry(void* pThread)
 
160
{
 
161
        pthread_setspecific(_currentKey, pThread);
 
162
 
 
163
#if defined(POCO_OS_FAMILY_UNIX)
 
164
        sigset_t sset;
 
165
        sigemptyset(&sset);
 
166
        sigaddset(&sset, SIGQUIT);
 
167
        sigaddset(&sset, SIGTERM);
 
168
        sigaddset(&sset, SIGPIPE); 
 
169
        pthread_sigmask(SIG_BLOCK, &sset, 0);
 
170
#endif
 
171
 
 
172
        try
 
173
        {
 
174
                reinterpret_cast<ThreadImpl*>(pThread)->_pTarget->run();
 
175
        }
 
176
        catch (Exception& exc)
 
177
        {
 
178
                ErrorHandler::handle(exc);
 
179
        }
 
180
        catch (std::exception& exc)
 
181
        {
 
182
                ErrorHandler::handle(exc);
 
183
        }
 
184
        catch (...)
 
185
        {
 
186
                ErrorHandler::handle();
 
187
        }
 
188
        return 0;
 
189
}
 
190
 
 
191
 
 
192
int ThreadImpl::mapPrio(int prio)
 
193
{
 
194
#if defined(__VMS) || defined(__digital__)
 
195
        static const int pmin = PRI_OTHER_MIN;
 
196
        static const int pmax = PRI_OTHER_MAX;
 
197
#else
 
198
        static const int pmin = sched_get_priority_min(SCHED_OTHER);
 
199
        static const int pmax = sched_get_priority_max(SCHED_OTHER);
 
200
#endif
 
201
 
 
202
        switch (prio)
 
203
        {
 
204
        case PRIO_LOWEST_IMPL:
 
205
                return pmin;
 
206
        case PRIO_LOW_IMPL:
 
207
                return pmin + (pmax - pmin)/4;
 
208
        case PRIO_NORMAL_IMPL:
 
209
                return pmin + (pmax - pmin)/2;
 
210
        case PRIO_HIGH_IMPL:
 
211
                return pmin + 3*(pmax - pmin)/4;
 
212
        case PRIO_HIGHEST_IMPL:
 
213
                return pmax;
 
214
        default:
 
215
                poco_bugcheck_msg("invalid thread priority");
 
216
        }
 
217
        return -1; // just to satisfy compiler - we'll never get here anyway
 
218
}
 
219
 
 
220
 
 
221
} // namespace Poco