Garfield++ v2r0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
FunNameStack.cpp
Go to the documentation of this file.
1/*
2Copyright (c) 1999-2003 I. B. Smirnov
3
4Permission to use, copy, modify, distribute and sell this file for any purpose
5is hereby granted without fee, provided that the above copyright notice,
6this permission notice, and notices about any modifications of the original
7text appear in all copies and in supporting documentation.
8The file is provided "as is" without express or implied warranty.
9*/
10
11#include <iostream>
12#include <iomanip>
13#include <cstring>
15
16
17#ifdef USE_BOOST_MULTITHREADING
18NameStack& NameStack::operator=(const NameStack& f) {
19#ifdef USE_TOGETHER_WITH_CLEAN_NEW
20#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
21 MemoriseIgnore::instance().ignore();
22#else
23 s_ignore_keynumberlist = 1; // to avoid report from delete at deletion
24#endif
25#endif
26 if (this != &f) {
27 int n;
28 if (nmode == 1)
29 for (n = 0; n < qname; n++) delete name[n];
30 nmode = f.nmode;
31 qname = f.qname;
32 id = f.id;
33 for (n = 0; n < f.qname; n++) {
34 if (nmode == 0) {
35 name[n] = f.name[n];
36 } else {
37 int l = strlen(f.name[n]) + 1;
38 name[n] = new char[l];
39 strcpy(name[n], f.name[n]);
40 }
41 }
42 }
43#ifdef USE_TOGETHER_WITH_CLEAN_NEW
44#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
45 MemoriseIgnore::instance().not_ignore();
46#else
47 s_ignore_keynumberlist = 0;
48#endif
49#endif
50 return *this;
51}
52#endif
53
54namespace Heed {
55
58
60// static FunNameStack inst; // According to some Internet site this is
61// Meyer's approach,
62// // time of descruction is not determined.
63// // So it can potentially be destructed before it is still in use.
64// return inst;
65#ifdef USE_BOOST_MULTITHREADING
66#ifdef PRINT_BEFORE_LOCK
67 fprintf(stderr, "FunNameStack& FunNameStack::instance()\n");
68#endif
69 boost::mutex locked_object;
70 boost::mutex::scoped_lock scopedLock_object(locked_object);
71#endif
72
73 static FunNameStack* inst = NULL; // According to this site it is
74 // "GoF" approach,
75 // destruction is not performed at all.
76#ifdef USE_TOGETHER_WITH_CLEAN_NEW
77#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
78 MemoriseIgnore::instance().ignore();
79#else
80 s_ignore_keynumberlist = 1; // to avoid report from delete at deletion
81#endif
82#endif
83 if (!inst) inst = new FunNameStack();
84#ifdef USE_TOGETHER_WITH_CLEAN_NEW
85#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
86 MemoriseIgnore::instance().not_ignore();
87#else
88 s_ignore_keynumberlist = 0;
89#endif
90#endif
91 return *inst;
92}
93
95 : // usually called inly from instance()
96 s_init(1),
97 s_act(1),
98 s_print(0),
99 nmode(0) {
100#ifdef USE_BOOST_MULTITHREADING
101
102#ifdef USE_TOGETHER_WITH_CLEAN_NEW
103#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
104 MemoriseIgnore::instance().ignore();
105#else
106 s_ignore_keynumberlist = 1; // to avoid report from delete at deletion
107#endif
108#endif
109 namestack = new std::list<NameStack>;
110 namestack->push_back(NameStack());
111#ifdef USE_TOGETHER_WITH_CLEAN_NEW
112#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
113 MemoriseIgnore::instance().not_ignore();
114#else
115 s_ignore_keynumberlist = 0;
116#endif
117#endif // for ifdef USE_TOGETHER_WITH_CLEAN_NEW
118
119 pthread_t id = pthread_self();
120 namestack->back().id = id;
121#ifdef PRINT_MESSAGE_ABOUT_THREAD_INITIALIZATION
122 mcerr
123 << "-----------------------------------------------------------------\n";
124 mcerr
125 << "-----------------------------------------------------------------\n";
126 mcerr
127 << "-----------------------------------------------------------------\n";
128 mcerr
129 << "-----------------------------------------------------------------\n";
130 mcerr
131 << "-----------------------------------------------------------------\n";
132 mcerr << "FunNameStack::FunNameStack(void) const:\n";
133 mcerr << "thread is initialized:\n";
134 Iprintn(mcerr, id);
135 mcerr
136 << "-----------------------------------------------------------------\n";
137 mcerr
138 << "-----------------------------------------------------------------\n";
139 mcerr
140 << "-----------------------------------------------------------------\n";
141 mcerr
142 << "-----------------------------------------------------------------\n";
143 mcerr
144 << "-----------------------------------------------------------------\n";
145#endif
146#else // for ifdef USE_BOOST_MULTITHREADING
147 qname = 0;
148 for (int n = 0; n < pqname; n++) name[n] = NULL;
149#endif
150}
151
152#ifdef USE_BOOST_MULTITHREADING
153NameStack* FunNameStack::get_thread_stack(void) const {
154 // long nret = 0;
155 pthread_t id = pthread_self();
156 int s_found = 0;
157 std::list<NameStack>::const_iterator it;
158 std::list<NameStack>::const_iterator end = namestack->end();
159 for (it = namestack->begin(); it != end; ++it) {
160 if (pthread_equal((*it).id, id)) {
161 s_found = 1;
162 break;
163 }
164 }
165 if (s_found == 0) {
166// new thread is detected
167/*
168mcerr<<"-----------------------------------------------------------------\n";
169mcerr<<"-----------------------------------------------------------------\n";
170mcerr<<"-----------------------------------------------------------------\n";
171mcerr<<"-----------------------------------------------------------------\n";
172mcerr<<"-----------------------------------------------------------------\n";
173mcerr<<"NameStack* FunNameStack::get_thread_stack(void) const:\n";
174mcerr<<"new thread is detected\n";
175Iprintn(mcerr, id);
176 mcerr<<"-----------------------------------------------------------------\n";
177*/
178#ifdef USE_TOGETHER_WITH_CLEAN_NEW
179#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
180 MemoriseIgnore::instance().ignore();
181#else
182 s_ignore_keynumberlist = 1; // to avoid report from delete at deletion
183#endif
184#endif
185 namestack->push_back(NameStack());
186#ifdef USE_TOGETHER_WITH_CLEAN_NEW
187#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
188 MemoriseIgnore::instance().not_ignore();
189#else
190 s_ignore_keynumberlist = 0; // to avoid report from delete at deletion
191#endif
192#endif
193
194 namestack->back().id = id;
195 }
196 return &(namestack - back());
197}
198NameStack* FunNameStack::get_thread_stack_q(long& nthread,
199 long& qthread) const {
200 nthread = 0;
201 qthread = 0;
202 pthread_t id = pthread_self();
203 int s_found = 0;
204 std::list<NameStack>::const_iterator it;
205 std::list<NameStack>::const_iterator end = namestack->end();
206 for (it = namestack->begin(); it != end; ++it) {
207 if (s_found == 0 && pthread_equal((*it).id, id)) {
208 s_found = 1;
209 nthread = qthread;
210 }
211 qthread++;
212 }
213 if (s_found == 0) {
214// new thread is detected
215/*
216mcerr<<"-----------------------------------------------------------------\n";
217mcerr<<"-----------------------------------------------------------------\n";
218mcerr<<"-----------------------------------------------------------------\n";
219mcerr<<"-----------------------------------------------------------------\n";
220mcerr<<"-----------------------------------------------------------------\n";
221mcerr<<"NameStack* FunNameStack::get_thread_stack(void) const:\n";
222mcerr<<"new thread is detected\n";
223Iprintn(mcerr, id);
224mcerr<<"-----------------------------------------------------------------\n";
225*/
226#ifdef USE_TOGETHER_WITH_CLEAN_NEW
227#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
228 MemoriseIgnore::instance().ignore();
229#else
230 s_ignore_keynumberlist = 1; // to avoid report from delete at deletion
231#endif
232#endif
233 namestack->push_back(NameStack());
234#ifdef USE_TOGETHER_WITH_CLEAN_NEW
235#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
236 MemoriseIgnore::instance().not_ignore();
237#else
238 s_ignore_keynumberlist = 0; // to avoid report from delete at deletion
239#endif
240#endif
241 namestack->back().id = id;
242 nthread = qthread;
243 qthread++;
244 }
245 return &(namestack->back());
246}
247void FunNameStack::remove_thread_stack(void) {
248 pthread_t id = pthread_self();
249 int s_found = 0;
250 std::list<NameStack>::const_iterator it;
251 std::list<NameStack>::const_iterator end = namestack->end();
252 for (it = namestack->begin(); it != end; ++it) {
253 if (pthread_equal((*it).id, id)) {
254 s_found = 1;
255 break;
256 }
257 }
258 if (s_found == 0) {
259 // new thread is detected
260 mcerr << "ERROR in void FunNameStack::remove_thread_stack(void) const:\n";
261 mcerr << "new thread is detected\n";
262 Iprintn(mcerr, id);
263 spexit(mcerr);
264 }
265#ifdef USE_TOGETHER_WITH_CLEAN_NEW
266#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
267 MemoriseIgnore::instance().ignore();
268#else
269 s_ignore_keynumberlist = 1; // to avoid report from delete at deletion
270#endif
271#endif
272 namestack->remove(an);
273#ifdef USE_TOGETHER_WITH_CLEAN_NEW
274#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
275 MemoriseIgnore::instance().not_ignore();
276#else
277 s_ignore_keynumberlist = 0; // to avoid report from delete at deletion
278#endif
279#endif
280}
281#endif
282
283#ifdef USE_BOOST_MULTITHREADING
284std::ostream& FunNameStack::printname(std::ostream& file, NameStack* ns,
285 int n) //
286#else
287std::ostream& FunNameStack::printname(std::ostream& file, int n) //
288#endif
289{
290#ifdef USE_BOOST_MULTITHREADING
291 file << ns->name[n];
292 return file;
293#else
294 file << name[n];
295 return file;
296#endif
297}
298
299void spexit_action(std::ostream& file) {
300 file << "spexit_action: the streams will be now flushed\n";
301 file.flush();
302 mcout.flush();
303 mcerr.flush();
305 if (s_exit_without_core == 1) {
306 file << "spexit_action: the exit(1) function is called\n";
307 exit(1);
308 } else {
309 file << "spexit_action: the abort function is called\n";
310 abort();
311 }
312 } else {
313 file << "spexit_action: an exception is now called\n";
314 throw ExcFromSpexit();
315 }
316}
317
319
321 if (this == &(FunNameStack::instance())) {
322 mcerr << "ERROR in FunNameStack& FunNameStack::operator=(const "
323 "FunNameStack& f)\n";
324 mcerr << "Attempt to copy to operative FunNameStack\n";
325 mcerr << "You don't need and should not initialize main FunNameStack "
326 "directly\n";
327 mcerr << "If you want to change its parameters, use "
328 "FunNameStack::instance()\n";
329 Iprintn(mcout, this);
331 Iprintn(mcout, (*this));
333
334 spexit(mcerr);
335 }
336#ifdef USE_BOOST_MULTITHREADING
337
338#ifdef USE_TOGETHER_WITH_CLEAN_NEW
339#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
340 MemoriseIgnore::instance().ignore();
341#else
342 s_ignore_keynumberlist = 1;
343#endif
344#endif
345 if (namestack) delete namestack;
346 namestack = new std::list<NameStack>(*(f.namestack));
347#ifdef USE_TOGETHER_WITH_CLEAN_NEW
348#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
349 MemoriseIgnore::instance().not_ignore();
350#else
351 s_ignore_keynumberlist = 0;
352#endif
353#endif
354
355#else
356 qname = f.qname;
357#endif
358 s_init = f.s_init;
359 s_act = f.s_act;
360 s_print = f.s_print;
361 nmode = f.nmode;
362#ifdef USE_BOOST_MULTITHREADING
363#else
364 for (int n = 0; n < f.qname; n++) {
365 if (nmode == 0) {
366 name[n] = f.name[n];
367 } else {
368 int l = strlen(f.name[n]) + 1;
369#ifdef USE_TOGETHER_WITH_CLEAN_NEW
370 s_ignore_keynumberlist = 1;
371#endif
372 name[n] = new char[l];
373 strcpy(name[n], f.name[n]);
374#ifdef USE_TOGETHER_WITH_CLEAN_NEW
375 s_ignore_keynumberlist = 0;
376#endif
377 }
378 }
379#endif
380 return *this;
381}
382
383void FunNameStack::set_parameters(int fs_act, int fs_print) {
384 s_act = fs_act;
385 s_print = fs_print; // only to correct this
386}
387
389#ifdef USE_BOOST_MULTITHREADING
390#ifdef USE_TOGETHER_WITH_CLEAN_NEW
391#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
392 MemoriseIgnore::instance().ignore();
393#else
394 s_ignore_keynumberlist = 1;
395#endif
396#endif
397 if (namestack) delete namestack;
398#ifdef USE_TOGETHER_WITH_CLEAN_NEW
399#if defined(MAINTAIN_KEYNUMBER_LIST) && defined(USE_BOOST_MULTITHREADING)
400 MemoriseIgnore::instance().not_ignore();
401#else
402 s_ignore_keynumberlist = 0;
403#endif
404#endif
405#else
406#ifdef USE_TOGETHER_WITH_CLEAN_NEW
407 s_ignore_keynumberlist = 1;
408#endif
409 if (nmode == 1)
410 for (int n = 0; n < qname; n++) delete name[n];
411#ifdef USE_TOGETHER_WITH_CLEAN_NEW
412 s_ignore_keynumberlist = 0;
413#endif
414#endif
415}
416
417void FunNameStack::printput(std::ostream& file) {
418 if (s_print == 1 || s_print == 2) {
419#ifdef USE_BOOST_MULTITHREADING
420 NameStack* ns = get_thread_stack();
421 file << "FunNameStack::put: id=" << ns->id << " qname=" << ns->qname
422 << " last name= \"";
423 printname(file, ns, ns->qname - 1);
424 file << " \"\n";
425#else
426 file << "FunNameStack::put: qname =" << qname << " last name=";
427 printname(file, qname - 1) << '\n';
428#endif
429 } else if (s_print >= 3) {
430#ifdef USE_BOOST_MULTITHREADING
431 NameStack* ns = get_thread_stack();
432 file << "FunNameStack::put: id=" << ns->id << "\n" << (*this);
433#else
434 file << "FunNameStack::put:\n" << (*this);
435#endif
436 }
437}
438void FunNameStack::printdel(std::ostream& file) {
439 if (s_print == 2) {
440#ifdef USE_BOOST_MULTITHREADING
441 NameStack* ns = get_thread_stack();
442 file << "FunNameStack::del: id=" << ns->id << " qname =" << ns->qname
443 << " last name= \"";
444 printname(file, ns, ns->qname - 1);
445 file << " \"\n";
446#else
447 file << "FunNameStack::del: qname =" << qname << " last name=";
448 printname(file, qname - 1) << '\n';
449#endif
450 } else if (s_print == 4) {
451 file << "FunNameStack::del:\n" << (*this);
452 }
453}
454
455std::ostream& operator<<(std::ostream& file, const FunNameStack& f) {
456 if (f.s_act == 1) {
457#ifdef USE_BOOST_MULTITHREADING
458 file << "FunNameStack: s_init=" << f.s_init << '\n';
459 long nret, qret;
460 NameStack* ns = f.get_thread_stack_q(nret, qret);
461 file << " id=" << ns->id << " qname=" << ns->qname << '\n';
462 file << "At the time of scanning there were " << qret << " threads \n"
463 << "registered in FunNameStack system.\n";
464 file << "The current one appeared nth: " << nret << '\n';
465 for (int n = 0; n < ns->qname; n++) {
466 file << std::setw(3) << n << " " << ns->name[n] << " \n";
467 }
468#else
469 file << "FunNameStack: s_init=" << f.s_init << " qname=" << f.qname << '\n';
470 for (int n = 0; n < f.qname; n++) {
471 file << std::setw(3) << n << " " << f.name[n] << " \n";
472 }
473#endif
474 }
475 return file;
476}
477
478std::ostream& operator<<(std::ostream& file, const FunNameWatch& f) {
479 f.hdr(file);
480 return file;
481}
482
483}
#define spexit(stream)
Definition: FunNameStack.h:256
static FunNameStack & instance()
FunNameStack & operator=(const FunNameStack &f)
void set_parameters(int fs_act=1, int fs_print=0)
std::ostream & hdr(std::ostream &file) const
Definition: FunNameStack.h:397
Definition: BGMesh.cpp:5
std::ostream & operator<<(std::ostream &file, const BGMesh &bgm)
Definition: BGMesh.cpp:36
void spexit_action(std::ostream &file)
int s_exit_without_core
int s_throw_exception_in_spexit
#define mcout
Definition: prstream.h:126
#define mcerr
Definition: prstream.h:128
#define Iprintn(file, name)
Definition: prstream.h:205