1 | //
|
---|
2 | // thread.cc
|
---|
3 | //
|
---|
4 | // Copyright (C) 1997 Limit Point Systems, Inc.
|
---|
5 | //
|
---|
6 | // Author: Edward Seidl <seidl@janed.com>
|
---|
7 | // Maintainer: LPS
|
---|
8 | //
|
---|
9 | // This file is part of the SC Toolkit.
|
---|
10 | //
|
---|
11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
12 | // it under the terms of the GNU Library General Public License as published by
|
---|
13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
14 | // any later version.
|
---|
15 | //
|
---|
16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU Library General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU Library General Public License
|
---|
22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | //
|
---|
25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
26 | //
|
---|
27 |
|
---|
28 | #ifdef __GNUC__
|
---|
29 | #pragma implementation
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <util/keyval/keyval.h>
|
---|
33 | #include <util/group/thread.h>
|
---|
34 | #include <util/misc/formio.h>
|
---|
35 | #include <util/misc/exenv.h>
|
---|
36 |
|
---|
37 | // debug includes
|
---|
38 | #include <sys/types.h>
|
---|
39 | #include <unistd.h>
|
---|
40 |
|
---|
41 | #include <scconfig.h>
|
---|
42 | #ifdef HAVE_PTHREAD
|
---|
43 | # include <util/group/thpthd.h>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | using namespace std;
|
---|
47 | using namespace sc;
|
---|
48 |
|
---|
49 | // This has C linkage.
|
---|
50 | void *
|
---|
51 | Thread__run_Thread_run(void* vth)
|
---|
52 | {
|
---|
53 | return Thread::run_Thread_run(vth);
|
---|
54 | }
|
---|
55 |
|
---|
56 | namespace sc {
|
---|
57 |
|
---|
58 | /////////////////////////////////////////////////////////////////////////////
|
---|
59 |
|
---|
60 | ThreadLock::ThreadLock()
|
---|
61 | {
|
---|
62 | }
|
---|
63 |
|
---|
64 | ThreadLock::~ThreadLock()
|
---|
65 | {
|
---|
66 | }
|
---|
67 |
|
---|
68 | /////////////////////////////////////////////////////////////////////////////
|
---|
69 |
|
---|
70 | Thread::Thread()
|
---|
71 | {
|
---|
72 | }
|
---|
73 |
|
---|
74 | Thread::~Thread()
|
---|
75 | {
|
---|
76 | }
|
---|
77 |
|
---|
78 | void *
|
---|
79 | Thread::run_Thread_run(void* vth)
|
---|
80 | {
|
---|
81 | if (vth) ((Thread*)vth)->run();
|
---|
82 | return 0;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /////////////////////////////////////////////////////////////////////////////
|
---|
86 | // ThreadGrp members
|
---|
87 |
|
---|
88 | static ClassDesc ThreadGrp_cd(typeid(ThreadGrp),"ThreadGrp",1,
|
---|
89 | "public DescribedClass");
|
---|
90 |
|
---|
91 | ThreadGrp::ThreadGrp() : threads_(0), nthread_(1)
|
---|
92 | {
|
---|
93 | threads_ = new Thread*[nthread_];
|
---|
94 | for (int i = 0; i<nthread_; i++) threads_[i] = 0;
|
---|
95 | }
|
---|
96 |
|
---|
97 | ThreadGrp::ThreadGrp(const ThreadGrp &tg, int nthread)
|
---|
98 | {
|
---|
99 | if (nthread == -1) nthread_ = tg.nthread_;
|
---|
100 | else nthread_ = nthread;
|
---|
101 | threads_ = new Thread*[nthread_];
|
---|
102 | for (int i = 0; i<nthread_; i++) threads_[i] = 0;
|
---|
103 | }
|
---|
104 |
|
---|
105 | ThreadGrp::ThreadGrp(const Ref<KeyVal>& keyval)
|
---|
106 | {
|
---|
107 | int defaultnum = ExEnv::nproc();
|
---|
108 | if (defaultnum == 0) defaultnum = 1;
|
---|
109 | KeyValValueint num(defaultnum);
|
---|
110 | nthread_ = keyval->intvalue("num_threads",num);
|
---|
111 |
|
---|
112 | threads_ = new Thread*[nthread_];
|
---|
113 | for (int i=0; i<nthread_; i++) threads_[i] = 0;
|
---|
114 | }
|
---|
115 |
|
---|
116 | ThreadGrp::~ThreadGrp()
|
---|
117 | {
|
---|
118 | if (nthread_) {
|
---|
119 | delete[] threads_;
|
---|
120 | nthread_=0;
|
---|
121 | threads_=0;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | void
|
---|
126 | ThreadGrp::delete_threads()
|
---|
127 | {
|
---|
128 | for (int i=0; i<nthread_; i++) {
|
---|
129 | delete threads_[i];
|
---|
130 | threads_[i] = 0;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | void
|
---|
135 | ThreadGrp::add_thread(int i, Thread*t)
|
---|
136 | {
|
---|
137 | if (i >= nthread_) {
|
---|
138 | ExEnv::err0() << indent
|
---|
139 | << "ThreadGrp::add_thread: trying to add too many threads"
|
---|
140 | << endl;
|
---|
141 | } else {
|
---|
142 | threads_[i] = t;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | void
|
---|
147 | ThreadGrp::add_thread(int i, Thread*t, int priority)
|
---|
148 | {
|
---|
149 | add_thread(i,t);
|
---|
150 | }
|
---|
151 |
|
---|
152 | static Ref<ThreadGrp> default_threadgrp;
|
---|
153 |
|
---|
154 | void
|
---|
155 | ThreadGrp::set_default_threadgrp(const Ref<ThreadGrp>& grp)
|
---|
156 | {
|
---|
157 | default_threadgrp = grp;
|
---|
158 | }
|
---|
159 |
|
---|
160 | ThreadGrp*
|
---|
161 | ThreadGrp::get_default_threadgrp()
|
---|
162 | {
|
---|
163 | if (default_threadgrp.null()) {
|
---|
164 | #ifdef HAVE_PTHREAD
|
---|
165 | default_threadgrp = new PthreadThreadGrp;
|
---|
166 | #else
|
---|
167 | default_threadgrp = new ProcThreadGrp;
|
---|
168 | #endif
|
---|
169 | }
|
---|
170 |
|
---|
171 | return default_threadgrp;
|
---|
172 | }
|
---|
173 |
|
---|
174 | ThreadGrp*
|
---|
175 | ThreadGrp::initial_threadgrp(int& argc, char ** argv)
|
---|
176 | {
|
---|
177 | ThreadGrp *grp = 0;
|
---|
178 | char * keyval_string = 0;
|
---|
179 |
|
---|
180 | // see if a thread group is given on the command line
|
---|
181 | if (argc && argv) {
|
---|
182 | for (int i=0; i < argc; i++) {
|
---|
183 | if (argv[i] && !strcmp(argv[i], "-threadgrp")) {
|
---|
184 | char *threadgrp_string = argv[i];
|
---|
185 | i++;
|
---|
186 | if (i >= argc) {
|
---|
187 | ExEnv::errn() << "-threadgrp must be following by an argument"
|
---|
188 | << endl;
|
---|
189 | abort();
|
---|
190 | }
|
---|
191 | keyval_string = argv[i];
|
---|
192 | // move the threadgrp arguments to the end of argv
|
---|
193 | int j;
|
---|
194 | for (j=i+1; j<argc; j++) {
|
---|
195 | argv[j-2] = argv[j];
|
---|
196 | }
|
---|
197 | argv[j++] = threadgrp_string;
|
---|
198 | argv[j++] = keyval_string;
|
---|
199 | // decrement argc to hide the last two arguments
|
---|
200 | argc -= 2;
|
---|
201 | break;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (!keyval_string) {
|
---|
207 | // find out if the environment gives the containing thread group
|
---|
208 | keyval_string = getenv("THREADGRP");
|
---|
209 | if (keyval_string) {
|
---|
210 | if (!strncmp("THREADGRP=", keyval_string, 11)) {
|
---|
211 | keyval_string = strchr(keyval_string, '=');
|
---|
212 | }
|
---|
213 | if (*keyval_string == '=') keyval_string++;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | // if keyval input for a thread group was found, then
|
---|
218 | // create it.
|
---|
219 | if (keyval_string) {
|
---|
220 | if (keyval_string[0] == '\0') return 0;
|
---|
221 | Ref<ParsedKeyVal> strkv = new ParsedKeyVal();
|
---|
222 | strkv->parse_string(keyval_string);
|
---|
223 | Ref<DescribedClass> dc = strkv->describedclassvalue();
|
---|
224 | grp = dynamic_cast<ThreadGrp*>(dc.pointer());
|
---|
225 | if (dc.null()) {
|
---|
226 | ExEnv::errn() << "initial_threadgrp: couldn't find a ThreadGrp in "
|
---|
227 | << keyval_string << endl;
|
---|
228 | abort();
|
---|
229 | } else if (!grp) {
|
---|
230 | ExEnv::errn() << "initial_threadgrp: wanted ThreadGrp but got "
|
---|
231 | << dc->class_name() << endl;
|
---|
232 | abort();
|
---|
233 | }
|
---|
234 | // prevent an accidental delete
|
---|
235 | grp->reference();
|
---|
236 | strkv = 0;
|
---|
237 | dc = 0;
|
---|
238 | // accidental delete not a problem anymore since all smart pointers
|
---|
239 | // to grp are dead
|
---|
240 | grp->dereference();
|
---|
241 | return grp;
|
---|
242 | }
|
---|
243 |
|
---|
244 | return 0;
|
---|
245 | }
|
---|
246 |
|
---|
247 | ThreadGrp*
|
---|
248 | ThreadGrp::clone(int nthread)
|
---|
249 | {
|
---|
250 | ExEnv::errn() << "ThreadGrp::clone not supported for " << class_name()
|
---|
251 | << endl;
|
---|
252 | abort();
|
---|
253 | return 0;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /////////////////////////////////////////////////////////////////////////////
|
---|
257 | // ProcThreadLock class
|
---|
258 |
|
---|
259 | class ProcThreadLock : public ThreadLock {
|
---|
260 | public:
|
---|
261 | ProcThreadLock() {}
|
---|
262 | ~ProcThreadLock() {}
|
---|
263 |
|
---|
264 | void lock() {}
|
---|
265 | void unlock() {}
|
---|
266 | };
|
---|
267 |
|
---|
268 | /////////////////////////////////////////////////////////////////////////////
|
---|
269 | // ProcThreadGrp members
|
---|
270 |
|
---|
271 | static ClassDesc ProcThreadGrp_cd(
|
---|
272 | typeid(ProcThreadGrp),"ProcThreadGrp",1,"public ThreadGrp",
|
---|
273 | 0, create<ProcThreadGrp>, 0);
|
---|
274 |
|
---|
275 | ProcThreadGrp::ProcThreadGrp()
|
---|
276 | : ThreadGrp()
|
---|
277 | {
|
---|
278 | }
|
---|
279 |
|
---|
280 | ProcThreadGrp::ProcThreadGrp(const Ref<KeyVal>& keyval)
|
---|
281 | : ThreadGrp(keyval)
|
---|
282 | {
|
---|
283 | if (nthread_ > 1) {
|
---|
284 | delete[] threads_;
|
---|
285 | nthread_ = 1;
|
---|
286 | threads_ = new Thread*[nthread_];
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | ProcThreadGrp::~ProcThreadGrp()
|
---|
291 | {
|
---|
292 | }
|
---|
293 |
|
---|
294 | int
|
---|
295 | ProcThreadGrp::start_threads()
|
---|
296 | {
|
---|
297 | if (threads_[0]) threads_[0]->run();
|
---|
298 | return 0;
|
---|
299 | }
|
---|
300 |
|
---|
301 | int
|
---|
302 | ProcThreadGrp::wait_threads()
|
---|
303 | {
|
---|
304 | return 0;
|
---|
305 | }
|
---|
306 |
|
---|
307 | Ref<ThreadLock>
|
---|
308 | ProcThreadGrp::new_lock()
|
---|
309 | {
|
---|
310 | return new ProcThreadLock;
|
---|
311 | }
|
---|
312 |
|
---|
313 | ThreadGrp*
|
---|
314 | ProcThreadGrp::clone(int nthread)
|
---|
315 | {
|
---|
316 | return new ProcThreadGrp;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /////////////////////////////////////////////////////////////////////////////
|
---|
320 |
|
---|
321 | }
|
---|
322 |
|
---|
323 | // Local Variables:
|
---|
324 | // mode: c++
|
---|
325 | // c-file-style: "ETS"
|
---|
326 | // End:
|
---|