File: waitable.h

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (214 lines) | stat: -rw-r--r-- 3,874 bytes parent folder | download | duplicates (2)
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
205
206
207
208
209
210
211
212
213
214
/*
   FALCON - Falcon advanced simple text evaluator.
   FILE: mt.h

   Multithreading abstraction layer
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Tue, 08 Apr 2008 20:10:47 +0200

   -------------------------------------------------------------------
   (C) Copyright 2008: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Multithreading abstraction layer.

   Mutexes, condition variables and threads. Very basic things.
*/

#ifndef FLC_WAITABLE_H
#define FLC_WAITABLE_H

#include <falcon/setup.h>
#include <falcon/types.h>
#include <falcon/basealloc.h>
#include <falcon/genericlist.h>
#include <falcon/error.h>
#include <falcon/mt.h>

#include <systhread.h>

namespace Falcon {

class Item;

namespace Ext {


/** Waiter class
   This class waits for something to happen...
*/
class Waitable: public BaseAlloc
{
   friend class WaitableProvider;
   friend class ThreadProvider;
   void *m_sysData;


protected:
   Waitable():
      m_refCount( 1 )
   {
      WaitableProvider::init( this );
   }

   virtual ~Waitable() { WaitableProvider::destroy( this ); }

   /** Signal to the first interested thread that we may be acquired. */
   void signal() { WaitableProvider::signal( this ); }

   /** Signal to all the interested thread that we may be acquired. */
   void broadcast() { WaitableProvider::broadcast( this ); }

   /** Useful for subclasses */
   mutable Mutex m_mtx;
   mutable int m_refCount;

   virtual bool acquireInternal()=0;

public:

   virtual bool acquire() = 0;
   virtual void release() = 0;

   void incref();
   void decref();
};


class Grant: public Waitable
{
   int m_count;

protected:
   virtual bool acquireInternal();

public:
   Grant( int count = 1 );
   virtual ~Grant();

   virtual bool acquire();
   virtual void release();
};

class Barrier: public Waitable
{
   bool m_bOpen;

protected:
   virtual bool acquireInternal();

public:
   Barrier( bool bOpen = false );
   virtual ~Barrier();

   virtual bool acquire();
   virtual void release();

   void open();
   void close();
};


/** Windows like events. */
class Event: public Waitable
{
   bool m_bSignaled;
   bool m_bAutoReset;
   bool m_bHeld;

protected:
   virtual bool acquireInternal();

public:
   Event( bool bAutoReset = true );
   virtual ~Event();
   virtual bool acquire();
   virtual void release();

   virtual void set();
   virtual void reset();
};

/** Synchronized queue. */
class SyncQueue: public Waitable
{
   List m_items;
   bool m_bHeld;

protected:
   virtual bool acquireInternal();

public:
   SyncQueue();
   virtual ~SyncQueue();

   virtual bool acquire();
   virtual void release();

   virtual void pushFront( void *data );
   virtual void pushBack( void *data );
   virtual bool popFront( void *&data );
   virtual bool popBack( void *&data );
   virtual bool empty() const;
   virtual uint32 size() const;
};

/** Counter (semaphore). */
class SyncCounter: public Waitable
{
   int m_count;

protected:
   virtual bool acquireInternal();

public:
   SyncCounter( int init = 0 );
   virtual ~SyncCounter();

   virtual bool acquire();
   virtual void release();
   void post( int count = 1 );
};


/** Thread status. */
class ThreadStatus: public Waitable
{

protected:
   bool m_bTerminated;
   bool m_bDetached;
   bool m_bStarted;
   int m_acquiredCount;

protected:
   virtual bool acquireInternal();

public:
   ThreadStatus();
   virtual ~ThreadStatus();

   virtual bool acquire();
   virtual void release();

   bool isTerminated() const;
   bool isDetached() const;

   /** Turns the status into started if not started nor detached. */
   bool startable();

   bool detach();
   /** Turn the status in terminated, removing the started status. */
   bool terminated();
};

}
}

#endif

/* end of mt.h */