1 | // |
---|
2 | // Thread.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Thread.cpp#4 $ |
---|
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.h> |
---|
38 | #include <Poco/Mutex.h> |
---|
39 | #include <Poco/Exception.h> |
---|
40 | #include <Poco/ThreadLocal.h> |
---|
41 | #include <sstream> |
---|
42 | |
---|
43 | |
---|
44 | #if defined(POCO_OS_FAMILY_WINDOWS) |
---|
45 | #include "Thread_WIN32.hpp" |
---|
46 | #else |
---|
47 | #include "Thread_POSIX.hpp" |
---|
48 | #endif |
---|
49 | |
---|
50 | |
---|
51 | namespace Poco { |
---|
52 | |
---|
53 | |
---|
54 | Thread::Thread(): |
---|
55 | _id(uniqueId()), |
---|
56 | _name(makeName()), |
---|
57 | _pTLS(0) |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | Thread::Thread(const std::string& name): |
---|
63 | _id(uniqueId()), |
---|
64 | _name(name), |
---|
65 | _pTLS(0) |
---|
66 | { |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | Thread::~Thread() |
---|
71 | { |
---|
72 | delete _pTLS; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | void Thread::setPriority(Priority prio) |
---|
77 | { |
---|
78 | setPriorityImpl(prio); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | Thread::Priority Thread::getPriority() const |
---|
83 | { |
---|
84 | return Priority(getPriorityImpl()); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | void Thread::start(Runnable& target) |
---|
89 | { |
---|
90 | startImpl(target); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | void Thread::start(Callable target, void* pData) |
---|
95 | { |
---|
96 | startImpl(target, pData); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void Thread::join() |
---|
101 | { |
---|
102 | joinImpl(); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | void Thread::join(long milliseconds) |
---|
107 | { |
---|
108 | if (!joinImpl(milliseconds)) |
---|
109 | throw TimeoutException(); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | bool Thread::tryJoin(long milliseconds) |
---|
114 | { |
---|
115 | return joinImpl(milliseconds); |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | ThreadLocalStorage& Thread::tls() |
---|
120 | { |
---|
121 | if (!_pTLS) |
---|
122 | _pTLS = new ThreadLocalStorage; |
---|
123 | return *_pTLS; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | void Thread::clearTLS() |
---|
128 | { |
---|
129 | if (_pTLS) |
---|
130 | { |
---|
131 | delete _pTLS; |
---|
132 | _pTLS = 0; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | std::string Thread::makeName() |
---|
138 | { |
---|
139 | std::ostringstream name; |
---|
140 | name << '#' << _id; |
---|
141 | return name.str(); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | int Thread::uniqueId() |
---|
146 | { |
---|
147 | static FastMutex mtx; |
---|
148 | FastMutex::ScopedLock lock(mtx); |
---|
149 | |
---|
150 | static unsigned count = 0; |
---|
151 | ++count; |
---|
152 | return count; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | void Thread::setName(const std::string& name) |
---|
157 | { |
---|
158 | FastMutex::ScopedLock lock(_mutex); |
---|
159 | |
---|
160 | _name = name; |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | } // namespace Poco |
---|