nidas v1.2.3
Thread.h
Go to the documentation of this file.
1// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2// vim: set shiftwidth=4 softtabstop=4 expandtab:
3/*
4 ********************************************************************
5 ** NIDAS: NCAR In-situ Data Acquistion Software
6 **
7 ** 2006, Copyright University Corporation for Atmospheric Research
8 **
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** The LICENSE.txt file accompanying this software contains
20 ** a copy of the GNU General Public License. If it is not found,
21 ** write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 **
24 ********************************************************************
25*/
26
27#ifndef NIDAS_UTIL_THREAD_H
28#define NIDAS_UTIL_THREAD_H
29
30#include <csignal>
31
32#include "Exception.h"
33#include "ThreadSupport.h"
34
35#include <string>
36#include <map>
37#include <set>
38#include <atomic>
39
40namespace nidas { namespace util {
41
42class Runnable {
43public:
44
45 virtual ~Runnable() {}
52 virtual int run() = 0;
53
59 virtual void interrupt() = 0;
60
64 virtual bool isInterrupted() const = 0;
65
66protected:
73 void testCancel() const { ::pthread_testcancel(); }
74
80 virtual bool amInterrupted() const;
81};
82
83class Thread : public Runnable {
84
85public:
90 static Thread *currentThread ();
91
92 static pthread_t currentThreadId ();
93
94 static Thread *lookupThread(pthread_t id);
95
100 static const std::string& currentName ()
101 {
102 Thread *thr = currentThread ();
103 if (thr) return thr->getName();
104 return unknownName;
105 }
106
114
115public:
116
125 Thread(const std::string& name, bool detached=false);
126
146 virtual ~Thread();
147
154 virtual void start();
155
166 virtual int join();
167
173 virtual void kill(int sig);
174
180 virtual void cancel();
181
257 virtual void interrupt();
258
262 virtual bool isInterrupted() const
263 {
264 Synchronized sync(_mutex);
265 return _interrupted;
266 }
267
271 virtual bool isRunning() const
272 {
273 Synchronized sync(_mutex);
274 return _running;
275 }
276
280 virtual bool isJoined() const
281 {
282 return getId() == 0;
283 }
284
288 virtual bool isDetached() const
289 {
290 return _detached;
291 }
292
296 bool isCancelEnabled() const;
297
302 bool isCancelDeferred() const;
303
307 const std::string& getName() const throw();
308
313 const std::string& getFullName() throw();
314
319 static std::string getPolicyString(int policy);
320
321 enum SchedPolicy { NU_THREAD_OTHER=SCHED_OTHER,
322 NU_THREAD_FIFO=SCHED_FIFO, NU_THREAD_RR=SCHED_RR};
323
327 bool setRealTimeRoundRobinPriority(int val);
328
332 bool setRealTimeFIFOPriority(int val);
333
338
342 void setThreadScheduler(enum SchedPolicy policy, int priority);
343
354 void blockSignal(int);
355
410 void unblockSignal(int);
411
415 static int test(int argc, char** argv);
416
417protected:
424 void setCancelEnabled(bool val);
425
437 void setCancelDeferred(bool val);
438
439 pthread_t getId() const
440 {
441 Synchronized sync(_mutex);
442 return _id;
443 }
444
445private:
446
447 static void* thr_run(void *me);
448 static void* thr_run_detached(void *me);
449 static void thr_cleanup(void *me);
450 static void thr_cleanup_delete(void *me);
451 static void thr_add_sig(int sig);
452
467 virtual void signalHandler(int /* sig */, siginfo_t* /* si */)
468 {
469 _interrupted = true;
470 }
471
472 static std::string unknownName;
473
474 virtual int pRun ();
475
483 void setThreadSchedulerNolock(enum SchedPolicy policy, int priority);
484
493
494 void makeFullName(); // add thread id to name once we know it
495
503 void registerThread();
504
505 void unregisterThread();
506
510 mutable Mutex _mutex;
511
512 std::string _name;
513
514 std::string _fullname;
515
516 pthread_t _id;
517
519
520 std::atomic<bool> _interrupted;
521
523
525
530
534
536
538
539 static void sigAction(int sig, siginfo_t* si,void* vptr);
540
541 void unsetId()
542 {
543 Synchronized sync(_mutex);
544 _id = 0;
545 }
546
550 Thread& operator=(const Thread& x) = delete;
551 Thread(const Thread& x) = delete;
552
553};
554
558class DetachedThread : public Thread {
559public:
560 DetachedThread(const std::string& name) : Thread(name,true) {}
561};
562
569class ThreadRunnable : public Thread
570{
571public:
572 ThreadRunnable (const std::string& name, Runnable* target = 0) :
573 Thread (name),
574 _target(target)
575 {}
576
577 void interrupt() {
578 if (_target) _target->interrupt();
579 }
580
581 int
583 {
584 if (_target)
585 return _target->run();
586 return NOT_RUNNING;
587 }
588
589private:
591
594
597
598};
599
608{
609public:
610 ThreadJoiner(Thread* thrd);
611
613
614 int run() throw();
615private:
617
620
623};
624
626
627}} // namespace nidas namespace util
628
629#endif
630
A Thread with a constructor that sets detached=true.
Definition Thread.h:558
DetachedThread(const std::string &name)
Definition Thread.h:560
Definition Exception.h:35
A C++ wrapper for a POSIX mutex.
Definition ThreadSupport.h:161
Definition Thread.h:42
virtual bool isInterrupted() const =0
Has the run method been interrupted?
virtual ~Runnable()
Definition Thread.h:45
virtual void interrupt()=0
Interrupt this run method.
void testCancel() const
Check if we have been cancelled.
Definition Thread.h:73
virtual int run()=0
The method which will run in its own thread.
virtual bool amInterrupted() const
Call testCancel, and return true when this thread has been interrupted.
Definition Thread.cc:62
Synchronized is used a simple guard object for critical sections.
Definition ThreadSupport.h:575
In certain situations one needs to "join oneself", which would be a deadlock.
Definition Thread.h:608
int run()
The method which will run in its own thread.
Definition Thread.cc:633
ThreadJoiner & operator=(const ThreadJoiner &)
No assignment.
ThreadJoiner(const ThreadJoiner &)
No copy.
~ThreadJoiner()
Definition Thread.cc:630
ThreadJoiner(Thread *thrd)
Definition Thread.cc:626
Thread * _thread
Definition Thread.h:616
The ThreadRunnable class implements a Thread which uses a Runnable target to supply the run() method.
Definition Thread.h:570
ThreadRunnable & operator=(const ThreadRunnable &)
No assignment.
Runnable * _target
Definition Thread.h:590
ThreadRunnable(const ThreadRunnable &)
No copy.
int run()
The method which will run in its own thread.
Definition Thread.h:582
void interrupt()
Interrupt this thread.
Definition Thread.h:577
ThreadRunnable(const std::string &name, Runnable *target=0)
Definition Thread.h:572
Definition Thread.h:83
std::atomic< bool > _interrupted
Definition Thread.h:520
static void thr_cleanup(void *me)
Definition Thread.cc:264
virtual void kill(int sig)
Send a signal to this thread.
Definition Thread.cc:458
static pthread_t currentThreadId()
Definition Thread.cc:93
static const std::string & currentName()
Convenience routine to return the name for the current thread, or a string indicating that the name o...
Definition Thread.h:100
static void thr_add_sig(int sig)
Definition Thread.cc:216
void blockSignal(int)
Block a signal in this thread.
Definition Thread.cc:181
void registerThread()
Register this current thread with a static registry of threads by id.
Definition Thread.cc:122
void unblockSignal(int)
Install a signal handler and unblock the signal.
Definition Thread.cc:206
bool _running
Definition Thread.h:518
sigset_t _blockedSignals
Definition Thread.h:535
static Thread * lookupThread(pthread_t id)
Definition Thread.cc:107
void setCancelDeferred(bool val)
Set the cancel type for this thread.
Definition Thread.cc:497
virtual void interrupt()
Interrupt this thread.
Definition Thread.cc:563
void setCancelEnabled(bool val)
Set the cancel state for this thread - false means cancel requests are ignored.
Definition Thread.cc:484
virtual void start()
Start the thread running, meaning execute the run method in a separate thread.
Definition Thread.cc:375
bool _cancel_deferred
Definition Thread.h:524
virtual bool isJoined() const
Has this thread been joined?
Definition Thread.h:280
Mutex _mutex
Mutex for accessing _id.
Definition Thread.h:510
SchedPolicy _policy
Definition Thread.h:532
Thread(const std::string &name, bool detached=false)
Constructor for a thread, giving it a name.
Definition Thread.cc:136
virtual ~Thread()
Thread destructor.
Definition Thread.cc:163
int _priority
Definition Thread.h:533
virtual void signalHandler(int, siginfo_t *)
Signal handler function for this thread.
Definition Thread.h:467
virtual int pRun()
Definition Thread.cc:339
const std::string & getName() const
Return the name of this thread.
Definition Thread.cc:511
static Thread * currentThread()
Return the thread object for the current thread.
Definition Thread.cc:100
bool setNonRealTimePriority()
Definition Thread.cc:582
static void thr_cleanup_delete(void *me)
Definition Thread.cc:234
virtual bool isRunning() const
Is this thread running?
Definition Thread.h:271
pthread_t getId() const
Definition Thread.h:439
static void * thr_run_detached(void *me)
Definition Thread.cc:317
runStatus
Values that can be returned by run method.
Definition Thread.h:113
@ NOT_RUNNING
Definition Thread.h:113
@ RUN_CANCELED
Definition Thread.h:113
@ RUN_OK
Definition Thread.h:113
@ RUN_EXCEPTION
Definition Thread.h:113
bool _detached
Definition Thread.h:531
void unsetId()
Definition Thread.h:541
virtual int join()
The calling thread joins this thread, waiting until the thread finishes, which means either that the ...
Definition Thread.cc:432
virtual bool isInterrupted() const
Return true when this thread has been interrupted.
Definition Thread.h:262
Thread(const Thread &x)=delete
static void sigAction(int sig, siginfo_t *si, void *vptr)
Definition Thread.cc:69
const std::string & getFullName()
Return a name with a bunch of descriptive fields, specifying whether it is detached,...
Definition Thread.cc:556
static std::string unknownName
Definition Thread.h:472
SchedPolicy
Definition Thread.h:321
@ NU_THREAD_OTHER
Definition Thread.h:321
@ NU_THREAD_FIFO
Definition Thread.h:322
@ NU_THREAD_RR
Definition Thread.h:322
void unregisterThread()
Definition Thread.cc:129
virtual void cancel()
Cancel this thread.
Definition Thread.cc:473
bool setRealTimeRoundRobinPriority(int val)
Definition Thread.cc:570
std::string _name
Definition Thread.h:512
void makeFullName()
Definition Thread.cc:528
static int test(int argc, char **argv)
a test method.
Definition Thread.cc:648
bool setRealTimeFIFOPriority(int val)
Definition Thread.cc:576
Thread & operator=(const Thread &x)=delete
Copy and assignment not needed so not supported.
pthread_t _id
Definition Thread.h:516
bool isCancelDeferred() const
Return true if the cancel type of this thread is PTHREAD_CANCEL_DEFERRED.
Definition Thread.cc:507
virtual bool isDetached() const
Is this a detached thread.
Definition Thread.h:288
sigset_t _unblockedSignals
Definition Thread.h:537
void setThreadSchedulerNolock()
If the thread is running, set the scheduling policy according to the current policy and priority attr...
Definition Thread.cc:599
void setThreadScheduler(enum SchedPolicy policy, int priority)
Definition Thread.cc:587
static std::string getPolicyString(int policy)
Convenience function to return a string for the given scheduler policy: "Non-RT", "RT:FIFO",...
Definition Thread.cc:517
bool _cancel_enabled
Definition Thread.h:522
bool isCancelEnabled() const
Return true if the cancel state of this thread is PTHREAD_CANCEL_ENABLE.
Definition Thread.cc:494
std::string _fullname
Definition Thread.h:514
Exception * _exception
Exception thrown by run method.
Definition Thread.h:529
static void * thr_run(void *me)
Definition Thread.cc:287
ThreadRunnable DefaultThread
Definition Thread.h:625
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31