File: thread.h

package info (click to toggle)
openhpi 3.8.0-2.3
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 31,888 kB
  • sloc: ansic: 225,326; cpp: 63,687; java: 16,472; cs: 15,161; python: 11,884; sh: 11,508; makefile: 4,945; perl: 1,529; xml: 36; asm: 13
file content (181 lines) | stat: -rw-r--r-- 3,264 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/** 
 * @file    thread.h
 *
 * The file includes classes for thread handling:\n
 * cThread\n
 * cThreadLock\n
 * cThreadLockAuto\n
 * cThreadLockRw\n
 * cThreadCond\n
 * 
 * @author  Thomas Kanngieser <thomas.kanngieser@fci.com>
 * @author  Lars Wetzel <larswetzel@users.sourceforge.net> (documentation only)
 * @version 1.0
 * @date    2004
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  This
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Copyright (c) 2004 by FORCE Computers.  
 */
#ifndef __THREAD_H__
#define __THREAD_H__


#include <pthread.h>
#include <unistd.h>

/// states of a thread
enum tTheadState
{
  eTsUnknown,
  eTsSuspend,
  eTsRun,
  eTsExit
};


/**
 * @class cThread
 * 
 * abstract class to support threads
 **/
class cThread
{
protected:
  /// thread structure
  pthread_t   m_thread;
  bool        m_main;  //!< true => main thread
  /// state of the thread
  tTheadState m_state;

  static void *Thread( void *param );

public:
  cThread();
  cThread( const pthread_t &thread, bool main_thread, tTheadState state );
  virtual ~cThread();

  // get the current thread class
  static cThread *GetThread();

  // start thread
  virtual bool Start();

  // wait for thread termination
  virtual bool Wait( void *&rv );

  /// return if the thread is running
  bool IsRunning() { return m_state == eTsRun; }
  /// return if it is the main thread
  bool IsMain()  { return m_main; }

protected:
  /// abstract run method to be implemented by childs
  virtual void *Run() = 0;
  virtual void Exit( void *rv );
};


/**
 * @class cThreadLock
 * 
 * Supports the locking
 **/
class cThreadLock
{
protected:
  /// mutex lock variable
  pthread_mutex_t m_lock;

public:
  cThreadLock();
  virtual ~cThreadLock();

  virtual void Lock();
  virtual void Unlock();

  virtual bool TryLock();
};

/**
 * @class cThreadLockAuto
 * 
 * The lock is set at calling the constructor and unlock 
 * in the destructor
 **/
class cThreadLockAuto
{
  /// holding the lock
  cThreadLock &m_lock;

public:
  /// Constructor with lock address which is set
  cThreadLockAuto( cThreadLock &lock )
    : m_lock( lock )
  {
    m_lock.Lock();
  }
  /// Destructor with unlock
  ~cThreadLockAuto()
  {
    m_lock.Unlock();
  }
};


/**
 * @class cThreadLockRw
 * 
 * Using read and write locks 
 **/
class cThreadLockRw
{
protected:
  /// holding the read/write lock
  pthread_rwlock_t m_rwlock;

public:
  cThreadLockRw();
  virtual ~cThreadLockRw();

  virtual void ReadLock();
  virtual void ReadUnlock();
  virtual bool TryReadLock();

  virtual void WriteLock();
  virtual void WriteUnlock();
  virtual bool TryWriteLock();

  // true => no lock held
  bool CheckLock();
};

/**
 * @class cThreadCond
 * 
 * holding the thread conditions
 **/
class cThreadCond : public cThreadLock
{
protected:
  /// conditions
  pthread_cond_t m_cond;

public:
  cThreadCond();
  virtual ~cThreadCond();

  // call Lock before Signal
  virtual void Signal();

  // call Lock before Wait
  virtual void Wait();
};


#endif