Blame view

src/helpers/CThreadDeque.hh 4.48 KB
fbe3c2bb   Benjamin Renard   First commit
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* -*- Base: 10 ; Mode: C++ -*- */
/*------------------------------------------------------------------------
				     **	
  				FOST project
				     **
--------------------------------------------------------------------------
--------------------------------------------------------------------------
   				FILE LOG
		$Revision: 1.3 $    $Date: 2012-06-15 13:04:37 $
--------------------------------------------------------------------------
CREATION
	F.CASIMIR

SUMMARY

DESCRIPTION

	The main function performs the following actions :
	<ul>
	<li>
	</ul>

------------------------------------------------------------------------*/

//=============================================================================
// 
// History of code
//
// creation 
//
// modification 
//=============================================================================

#ifndef _CTHREADDEQUE_HH_
#define _CTHREADDEQUE_HH_ 1

#include <deque>
#include <semaphore.h>

/**

*/

//=============================================================================
// Class CThreadDeque
//=============================================================================

/**
* @brief thread  salfe std::deque of object
*
* @details  manage a deque of any object
*/
template< class Object>
class CThreadDeque
{

public:
  typedef std::deque<Object>  ObjectList;

  /**
     Constructor (default one)
  */
  CThreadDeque() {
    pthread_mutex_init( & _objectListMutex, NULL);
    sem_init( & _objectSem, 0, 0);
  }

  /**
     Destructor
  */
  virtual ~CThreadDeque() {
    pthread_mutex_destroy( & _objectListMutex);  
    sem_destroy( & _objectSem);
  }

  // Get Methods
  
  // Set Methods
  
  // Other Methods
  /**
   * @brief add an Object and  Post SEM.
    */
  void push( Object object) { 
    Lock sync( & _objectListMutex);
    _objectList.push_back(object);
    sem_post( & _objectSem);
  }

  /**
   * @brief Wait for SEM being posted and pop an Object
    */
  Object pop() {
    sem_wait( & _objectSem);
    return internalPop();
  }

  /**
   * Wait wait second for SEM being posted.
   */
  Object pop_timedwait( int wait, Object null) {
    Object object = null;
    struct timespec ts;

    if (clock_gettime(CLOCK_REALTIME, &ts) != -1) {
      ts.tv_sec += wait;      
      if ( sem_timedwait( & _objectSem, &ts) != -1 ) {
	object = internalPop();
      }
    }

    return object;
  }

  Object tryToPop( Object null) {
    Object object = null;
    Lock lock( & _objectListMutex);
    if ( ! _objectList.empty() )
      {
	if ( sem_trywait( & _objectSem) == 0 ) {
	  object = _objectList.front();
	  _objectList.pop_front();
	} 
      }
    return object;
  }

  void remove( Object object) {
    Lock lock( & _objectListMutex);
    typename ObjectList::iterator it = _objectList.begin();
    for (; it != _objectList.end(); ++it) {
      if ( (*it) == object ) {
	_objectList.erase(it);
	break;
      }
    }
  }

  void flush(ObjectList& objectList) {
    Lock lock( & _objectListMutex);
    while ( ! _objectList.empty() )
      {
	sem_wait( & _objectSem);
	objectList.push_back( _objectList.front());
	_objectList.pop_front();
      }
  }

  bool empty() {
    Lock lock( & _objectListMutex);
    return _objectList.empty();
  }


private:
  
  class Lock {
  public:
    Lock(pthread_mutex_t* mutex) : _mutex(mutex) { pthread_mutex_lock( _mutex); }
    ~Lock() { pthread_mutex_unlock( _mutex); }
  private:
    pthread_mutex_t* _mutex;
  };

  Object internalPop() {
    Object object = NULL;
    Lock sync( & _objectListMutex);
    if ( ! _objectList.empty() )
      {
	object = _objectList.front();
	_objectList.pop_front();
      } 
    return object;
  }

  /**
     Copy Constructor (normally should not be used)
  */
  CThreadDeque(const CThreadDeque& copied) {}

  pthread_mutex_t    _objectListMutex;
  sem_t              _objectSem;

  ObjectList       _objectList;
};

//=============================================================================
// Inline Methods for Class CThreadDeque
//=============================================================================

//-----------------------------------------------------------------------------
// Get Methods

//-----------------------------------------------------------------------------
// Set Methods

//-----------------------------------------------------------------------------
// Other Methods

//=============================================================================
// End of Class CThreadDeque
//=============================================================================

#endif // _CTHREADDEQUE_HH_