File: pmutils.h

package info (click to toggle)
t38modem 0.8.0%2B20050304-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 436 kB
  • ctags: 662
  • sloc: cpp: 6,364; makefile: 78; sh: 52
file content (280 lines) | stat: -rw-r--r-- 7,648 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * pmutils.h
 *
 * T38FAX Pseudo Modem
 *
 * Copyright (c) 2001-2005 Vyacheslav Frolov
 *
 * Open H323 Project
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Open H323 Library.
 *
 * The Initial Developer of the Original Code is Vyacheslav Frolov
 *
 * Contributor(s): Equivalence Pty ltd
 *
 * $Log: pmutils.h,v $
 * Revision 1.15  2005/02/04 10:18:49  vfrolov
 * Fixed warnings for No Trace build
 *
 * Revision 1.14  2005/02/03 11:32:12  vfrolov
 * Fixed MSVC compile warnings
 *
 * Revision 1.13  2004/10/20 14:15:09  vfrolov
 * Added reset of signal counter to WaitDataReady()
 *
 * Revision 1.12  2004/07/07 07:53:58  vfrolov
 * Moved ptlib.h including to *.cxx for precompiling
 * Fixed compiler warning
 *
 * Revision 1.11  2004/03/09 17:23:19  vfrolov
 * Added PROCESS_PER_THREAD ifdef
 *
 * Revision 1.10  2003/12/04 13:22:35  vfrolov
 * Removed ambiguous isEof()
 * Improved memory usage in DataStream
 * Fixed myPTRACE
 *
 * Revision 1.9  2003/01/08 16:37:29  vfrolov
 * Changed class DataStream:
 *   members moved to private section and added isEof()
 *   added threshold and isFull()
 *
 * Revision 1.8  2002/12/30 12:49:39  vfrolov
 * Added tracing thread's CPU usage (Linux only)
 *
 * Revision 1.7  2002/12/20 10:13:01  vfrolov
 * Implemented tracing with PID of thread (for LinuxThreads)
 *   or ID of thread (for other POSIX Threads)
 *
 * Revision 1.6  2002/04/27 10:12:21  vfrolov
 * If defined MYPTRACE_LEVEL=N then myPTRACE() will output the trace with level N
 *
 * Revision 1.5  2002/03/07 07:30:44  vfrolov
 * Fixed endless recursive call SignalChildStop(). Possible there is
 * a bug in gcc version 2.95.4 20010902 (Debian prerelease).
 * Markus Storm reported the promlem.
 *
 * Revision 1.4  2002/03/01 08:17:28  vfrolov
 * Added Copyright header
 * Removed virtual modifiers
 *
 * Revision 1.3  2002/02/11 08:35:12  vfrolov
 * myPTRACE() outputs trace to cout only if defined COUT_TRACE
 *
 * Revision 1.2  2002/01/10 06:10:03  craigs
 * Added MPL header
 *
 * Revision 1.1  2002/01/01 23:06:54  craigs
 * Initial version
 *
 */

#ifndef _PMUTILS_H
#define _PMUTILS_H

///////////////////////////////////////////////////////////////
class ModemThread : public PThread
{
    PCLASSINFO(ModemThread, PThread);
  public:
  /**@name Construction */
  //@{
    ModemThread();
  //@}

  /**@name Operations */
  //@{
    void SignalDataReady() { dataReadySyncPoint.Signal(); }
    void SignalChildStop();
    virtual void SignalStop();
  //@}

  protected:
    virtual void Main() = 0;
    void WaitDataReady();

    volatile BOOL stop;		// *this was requested to stop
    volatile BOOL childstop;	// there is a child that was requested to stop
    PSyncPoint dataReadySyncPoint;
};
///////////////////////////////////////////////////////////////
class ModemThreadChild : public ModemThread
{
    PCLASSINFO(ModemThreadChild, ModemThread);
  public:
  /**@name Construction */
  //@{
    ModemThreadChild(ModemThread &_parent);
  //@}

  /**@name Operations */
  //@{
    virtual void SignalStop();
  //@}

  protected:
    ModemThread &parent;
};
///////////////////////////////////////////////////////////////
PQUEUE(_PBYTEArrayQ, PBYTEArray);

class PBYTEArrayQ : public _PBYTEArrayQ
{
    PCLASSINFO(PBYTEArrayQ, _PBYTEArrayQ);
  public:
    PBYTEArrayQ() : count(0) {}
    ~PBYTEArrayQ() { Clean(); }

    virtual void Enqueue(PBYTEArray *buf) {
      PWaitAndSignal mutexWait(Mutex);
      count += buf->GetSize();
      _PBYTEArrayQ::Enqueue(buf);
    }

    virtual PBYTEArray *Dequeue() {
      PWaitAndSignal mutexWait(Mutex);
      PBYTEArray *buf = _PBYTEArrayQ::Dequeue();
      if( buf ) count -= buf->GetSize();
      return buf;
    }

    PINDEX GetCount() const { return count; }

    void Clean() {
      PBYTEArray *buf;
      while( (buf = Dequeue()) != NULL ) {
        delete buf;
      }
    }
  protected:
    PINDEX count;
    PMutex Mutex;
};
///////////////////////////////////////////////////////////////
class ChunkStream : public PObject
{
    PCLASSINFO(ChunkStream, PObject);
  public:
    ChunkStream() : first(0), last(0) {}

    int write(const void *pBuf, PINDEX count);
    int read(void *pBuf, PINDEX count);

  private:
    BYTE data[256];
    PINDEX first;
    PINDEX last;
};

PQUEUE(ChunkStreamQ, ChunkStream);
///////////////////////////////////////////////////////////////
class DataStream : public PObject
{
    PCLASSINFO(DataStream, PObject);
  public:
    DataStream(PINDEX _threshold = 0)
      : firstBuf(NULL), lastBuf(NULL), busy(0),
        threshold(_threshold), eof(FALSE), diag(0) {}
    ~DataStream() { DataStream::Clean(); }

    int PutData(const void *pBuf, PINDEX count);
    int GetData(void *pBuf, PINDEX count);
    void PutEof() { eof = TRUE; }
    int GetDiag() const { return diag; }
    DataStream &SetDiag(int _diag) { diag = _diag; return *this; }
    BOOL isFull() const { return threshold && threshold < busy; }
    virtual void Clean();

  private:
    ChunkStream *firstBuf;
    ChunkStreamQ bufQ;
    ChunkStream *lastBuf;	// if not NULL then it should be in bufQ or firstBuf
    PINDEX busy;

    PINDEX threshold;
    BOOL eof;
    int diag;
};
///////////////////////////////////////////////////////////////
PQUEUE(_DataStreamQ, DataStream);

class DataStreamQ : public _DataStreamQ
{
    PCLASSINFO(DataStreamQ, _DataStreamQ);
  public:
    DataStreamQ() {}
    ~DataStreamQ() { Clean(); }

    virtual void Enqueue(DataStream *buf) {
      PWaitAndSignal mutexWait(Mutex);
      _DataStreamQ::Enqueue(buf);
    }

    virtual DataStream *Dequeue() {
      PWaitAndSignal mutexWait(Mutex);
      return _DataStreamQ::Dequeue();
    }

    void Clean() {
      DataStream *buf;
      while( (buf = Dequeue()) != NULL ) {
        delete buf;
      }
    }
  protected:
    PMutex Mutex;
};
///////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// warning C4127: conditional expression is constant
#pragma warning(disable:4127)
#endif // _MSC_VER

#ifdef COUT_TRACE
#define _myPTRACE(level, args) do { \
  PTRACE(level, args); \
  cout << PThread::Current()->GetThreadName() << ": " << args << endl; \
} while(0)
#define myCanTrace(level) TRUE
#define myPTRACE_PARAM(param) param
#else
#define _myPTRACE(level, args) do { PTRACE(level, args); } while(0)
#define myCanTrace(level) PTrace::CanTrace(level)
#define myPTRACE_PARAM(param) PTRACE_PARAM(param)
#endif // COUT_TRACE

#ifdef MYPTRACE_LEVEL
#define myPTRACE(level, args) _myPTRACE(MYPTRACE_LEVEL, args)
#else
#define myPTRACE(level, args) _myPTRACE(level, args)
#endif // MYPTRACE_LEVEL

#define PRTHEX(data) " {\n" << setprecision(2) << hex << setfill('0') << data << dec << setfill(' ') << " }"
///////////////////////////////////////////////////////////////
extern void RenameCurrentThread(const PString &newname);

#ifdef PROCESS_PER_THREAD
extern const PString GetThreadTimes(const char *head = "", const char *tail = "");
#else
inline const PString GetThreadTimes(const char *head = "", const char *tail = "");

inline const PString GetThreadTimes(const char *, const char *)
{
  return "";
}
#endif
///////////////////////////////////////////////////////////////

#endif  // _PMUTILS_H