nidas  v1.2-1520
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 
39 namespace nidas { namespace util {
40 
41 class Runnable {
42 public:
43 
44  virtual ~Runnable() {}
49  virtual int run() throw(Exception) = 0;
50 
56  virtual void interrupt() = 0;
57 
61  virtual bool isInterrupted() const = 0;
62 
63 protected:
70  void testCancel() const { ::pthread_testcancel(); }
71 
77  virtual bool amInterrupted() const;
78 };
79 
80 class Thread : public Runnable {
81 
82 public:
87  static Thread *currentThread ();
88 
89  static pthread_t currentThreadId ();
90 
91  static Thread *lookupThread(pthread_t id);
92 
97  static const std::string& currentName ()
98  {
99  Thread *thr = currentThread ();
100  if (thr) return thr->getName();
101  return unknownName;
102  }
103 
110  enum runStatus { RUN_CANCELED = -1, RUN_OK = 0, NOT_RUNNING = 1, RUN_EXCEPTION = 2 };
111 
112 public:
113 
122  Thread(const std::string& name,bool detached=false);
123 
128  Thread(const Thread& x);
129 
149  virtual ~Thread();
150 
155  virtual void start() throw(Exception);
156 
165  virtual int join() throw(Exception);
166 
170  virtual void kill(int sig) throw(Exception);
171 
175  virtual void cancel() throw(Exception);
176 
252  virtual void interrupt();
253 
257  virtual bool isInterrupted() const
258  {
259  Synchronized sync(_mutex);
260  return _interrupted;
261  }
262 
266  virtual bool isRunning() const
267  {
268  Synchronized sync(_mutex);
269  return _running;
270  }
271 
275  virtual bool isJoined() const
276  {
277  return getId() == 0;
278  }
279 
283  virtual bool isDetached() const
284  {
285  return _detached;
286  }
287 
291  bool isCancelEnabled() const;
292 
297  bool isCancelDeferred() const;
298 
302  const std::string& getName() const throw();
303 
308  const std::string& getFullName() throw();
309 
314  static std::string getPolicyString(int policy);
315 
316  enum SchedPolicy { NU_THREAD_OTHER=SCHED_OTHER,
317  NU_THREAD_FIFO=SCHED_FIFO, NU_THREAD_RR=SCHED_RR};
318 
319  bool setRealTimeRoundRobinPriority(int val) throw(Exception);
320  bool setRealTimeFIFOPriority(int val) throw(Exception);
321  bool setNonRealTimePriority() throw(Exception);
322 
323  void setThreadScheduler(enum SchedPolicy policy, int priority) throw(Exception);
324 
335  void blockSignal(int);
336 
391  void unblockSignal(int);
392 
396  static int test(int argc, char** argv);
397 
398 protected:
405  void setCancelEnabled(bool val);
406 
418  void setCancelDeferred(bool val);
419 
420  pthread_t getId() const
421  {
422  Synchronized sync(_mutex);
423  return _id;
424  }
425 
426 private:
427 
428  static void* thr_run(void *me);
429  static void* thr_run_detached(void *me);
430  static void thr_cleanup(void *me);
431  static void thr_cleanup_delete(void *me);
432  static void thr_add_sig(int sig);
433 
448  virtual void signalHandler(int /* sig */, siginfo_t* /* si */)
449  {
450  _interrupted = true;
451  }
452 
453  static std::string unknownName;
454 
455  virtual int pRun ();
456 
457  void setThreadSchedulerNolock(enum SchedPolicy policy, int priority) throw(Exception);
458 
459  void makeFullName(); // add thread id to name once we know it
460 
468  void registerThread();
469 
470  void unregisterThread();
471 
475  mutable Mutex _mutex;
476 
477  std::string _name;
478 
479  std::string _fullname;
480 
481  pthread_t _id;
482 
483  bool _running;
484 
486 
488 
490 
491  pthread_attr_t _thread_attr;
492 
497 
498  bool _detached;
499 
500  sigset_t _blockedSignals;
501 
503 
504  static void sigAction(int sig, siginfo_t* si,void* vptr);
505 
506  void unsetId()
507  {
508  Synchronized sync(_mutex);
509  _id = 0;
510  }
511 
517  Thread& operator=(const Thread& x);
518 
519 };
520 
524 class DetachedThread : public Thread {
525 public:
526  DetachedThread(const std::string& name) : Thread(name,true) {}
527 };
528 
535 class ThreadRunnable : public Thread
536 {
537 public:
538  ThreadRunnable (const std::string& name, Runnable* target = 0) :
539  Thread (name),
540  _target(target)
541  {}
542 
543  void interrupt() {
544  if (_target) _target->interrupt();
545  }
546 
547  int
548  run () throw(Exception)
549  {
550  if (_target)
551  return _target->run();
552  return NOT_RUNNING;
553  }
554 
555 private:
557 
560 
563 
564 };
565 
574 {
575 public:
576  ThreadJoiner(Thread* thrd);
577 
578  ~ThreadJoiner();
579 
580  int run() throw();
581 private:
583 
585  ThreadJoiner(const ThreadJoiner&);
586 
589 };
590 
592 
593 }} // namespace nidas namespace util
594 
595 #endif
596 
static void thr_cleanup_delete(void *me)
Definition: Thread.cc:264
Synchronized is used a simple guard object for critical sections.
Definition: ThreadSupport.h:544
void setThreadSchedulerNolock(enum SchedPolicy policy, int priority)
Definition: Thread.cc:629
bool _cancel_enabled
Definition: Thread.h:487
sigset_t _unblockedSignals
Definition: Thread.h:502
ThreadRunnable & operator=(const ThreadRunnable &)
No assignment.
void interrupt()
Interrupt this thread.
Definition: Thread.h:543
virtual int join()
The calling thread joins this thread, waiting until the thread finishes, which means either that the ...
Definition: Thread.cc:469
static void thr_cleanup(void *me)
Definition: Thread.cc:294
Definition: Thread.h:316
const std::string & getFullName()
Return a name with a bunch of descriptive fields, specifying whether it is detached, the real-time priority, etc.
Definition: Thread.cc:593
pthread_attr_t _thread_attr
Definition: Thread.h:491
virtual void kill(int sig)
Send a signal to this thread.
Definition: Thread.cc:495
bool setRealTimeFIFOPriority(int val)
Definition: Thread.cc:613
int run()
The method which will run in its own thread.
Definition: Thread.cc:666
Mutex _mutex
Mutex for accessing _id.
Definition: Thread.h:475
static int test(int argc, char **argv)
a test method.
Definition: Thread.cc:681
void registerThread()
Register this current thread with a static registry of threads by id.
Definition: Thread.cc:127
bool _cancel_deferred
Definition: Thread.h:489
const std::string & getName() const
Return the name of this thread.
Definition: Thread.cc:548
DetachedThread(const std::string &name)
Definition: Thread.h:526
static void * thr_run_detached(void *me)
Definition: Thread.cc:347
Definition: Thread.h:41
The ThreadRunnable class implements a Thread which uses a Runnable target to supply the run() method...
Definition: Thread.h:535
Runnable * _target
Definition: Thread.h:556
pthread_t _id
Definition: Thread.h:481
void unregisterThread()
Definition: Thread.cc:134
void setCancelEnabled(bool val)
Set the cancel state for this thread - false means cancel requests are ignored.
Definition: Thread.cc:521
static pthread_t currentThreadId()
Definition: Thread.cc:98
pthread_t getId() const
Definition: Thread.h:420
Thread * _thread
Definition: Thread.h:582
virtual ~Runnable()
Definition: Thread.h:44
ThreadRunnable DefaultThread
Definition: Thread.h:591
virtual void start()
Start the thread running, meaning execute the run method in a separate thread.
Definition: Thread.cc:406
ThreadJoiner & operator=(const ThreadJoiner &)
No assignment.
Definition: Thread.h:110
void setCancelDeferred(bool val)
Set the cancel type for this thread.
Definition: Thread.cc:534
void blockSignal(int)
Block a signal in this thread.
Definition: Thread.cc:211
int run()
The method which will run in its own thread.
Definition: Thread.h:548
bool _interrupted
Definition: Thread.h:485
std::string _name
Definition: Thread.h:477
Exception * _exception
Exception thrown by run method.
Definition: Thread.h:496
static std::string unknownName
Definition: Thread.h:453
virtual bool isInterrupted() const
Return true when this thread has been interrupted.
Definition: Thread.h:257
bool _detached
Definition: Thread.h:498
Definition: Exception.h:35
static void thr_add_sig(int sig)
Definition: Thread.cc:246
virtual bool isInterrupted() const =0
Has the run method been interrupted?
Definition: Thread.h:80
std::string _fullname
Definition: Thread.h:479
bool setRealTimeRoundRobinPriority(int val)
Definition: Thread.cc:607
virtual bool isJoined() const
Has this thread been joined?
Definition: Thread.h:275
sigset_t _blockedSignals
Definition: Thread.h:500
static Thread * lookupThread(pthread_t id)
Definition: Thread.cc:112
virtual int pRun()
Definition: Thread.cc:369
SchedPolicy
Definition: Thread.h:316
virtual void signalHandler(int, siginfo_t *)
Signal handler function for this thread.
Definition: Thread.h:448
static std::string getPolicyString(int policy)
Convenience function to return a string for the given scheduler policy: &quot;Non-RT&quot;, &quot;RT:FIFO&quot;...
Definition: Thread.cc:554
bool isCancelEnabled() const
Return true if the cancel state of this thread is PTHREAD_CANCEL_ENABLE.
Definition: Thread.cc:531
void makeFullName()
Definition: Thread.cc:565
~ThreadJoiner()
Definition: Thread.cc:663
runStatus
Values that can be returned by run method.
Definition: Thread.h:110
virtual int run()=0
The method which will run in its own thread.
bool isCancelDeferred() const
Return true if the cancel type of this thread is PTHREAD_CANCEL_DEFERRED.
Definition: Thread.cc:544
A Thread with a constructor that sets detached=true.
Definition: Thread.h:524
ThreadJoiner(Thread *thrd)
Definition: Thread.cc:659
Definition: Thread.h:110
virtual void interrupt()=0
Interrupt this run method.
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:97
ThreadRunnable(const std::string &name, Runnable *target=0)
Definition: Thread.h:538
static Thread * currentThread()
Return the thread object for the current thread.
Definition: Thread.cc:105
void testCancel() const
Check if we have been cancelled.
Definition: Thread.h:70
void setThreadScheduler(enum SchedPolicy policy, int priority)
Definition: Thread.cc:624
static void sigAction(int sig, siginfo_t *si, void *vptr)
Definition: Thread.cc:74
bool _running
Definition: Thread.h:483
Definition: Thread.h:110
bool setNonRealTimePriority()
Definition: Thread.cc:619
Definition: Thread.h:317
virtual bool amInterrupted() const
Call testCancel, and return true when this thread has been interrupted.
Definition: Thread.cc:67
virtual void interrupt()
Interrupt this thread.
Definition: Thread.cc:600
In certain situations one needs to &quot;join oneself&quot;, which would be a deadlock.
Definition: Thread.h:573
virtual bool isDetached() const
Is this a detached thread.
Definition: Thread.h:283
void unblockSignal(int)
Install a signal handler and unblock the signal.
Definition: Thread.cc:236
Definition: Thread.h:110
static void * thr_run(void *me)
Definition: Thread.cc:317
Thread & operator=(const Thread &x)
Assignment operator not supported.
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154
virtual bool isRunning() const
Is this thread running?
Definition: Thread.h:266
void unsetId()
Definition: Thread.h:506
virtual ~Thread()
Thread destructor.
Definition: Thread.cc:192
Thread(const std::string &name, bool detached=false)
Constructor for a thread, giving it a name.
Definition: Thread.cc:141
Definition: Thread.h:317
virtual void cancel()
Cancel this thread.
Definition: Thread.cc:510