forked from hehol/t38modem
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pmutils.cxx
276 lines (242 loc) · 5.94 KB
/
pmutils.cxx
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
/*
* pmutils.cxx
*
* T38FAX Pseudo Modem
*
* Copyright (c) 2001-2006 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.cxx,v $
* Revision 1.13 2006-12-11 10:27:35 vfrolov
* Disabled renaming thread if no PTRACING
*
* Revision 1.13 2006/12/11 10:27:35 vfrolov
* Disabled renaming thread if no PTRACING
*
* Revision 1.12 2005/02/03 11:32:12 vfrolov
* Fixed MSVC compile warnings
*
* Revision 1.11 2004/10/20 14:15:09 vfrolov
* Added reset of signal counter to WaitDataReady()
*
* Revision 1.10 2004/07/06 16:07:24 vfrolov
* Included ptlib.h for precompiling
*
* Revision 1.9 2004/03/09 17:23:11 vfrolov
* Added PROCESS_PER_THREAD ifdef
*
* Revision 1.8 2004/02/17 13:23:14 vfrolov
* Fixed MSVC compile errors
*
* Revision 1.7 2003/12/04 13:22:28 vfrolov
* Removed ambiguous isEof()
* Improved memory usage in DataStream
* Fixed myPTRACE
*
* Revision 1.6 2003/01/08 16:37:25 vfrolov
* Changed class DataStream:
* members moved to private section and added isEof()
* added threshold and isFull()
*
* Revision 1.5 2002/12/30 12:49:36 vfrolov
* Added tracing thread's CPU usage (Linux only)
*
* Revision 1.4 2002/12/20 10:12:57 vfrolov
* Implemented tracing with PID of thread (for LinuxThreads)
* or ID of thread (for other POSIX Threads)
*
* Revision 1.3 2002/03/07 07:55:18 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.
* Added Copyright header.
*
* 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
*
*/
#include <ptlib.h>
#include "pmutils.h"
#define new PNEW
///////////////////////////////////////////////////////////////
ModemThread::ModemThread()
: PThread(30000,
NoAutoDeleteThread,
NormalPriority),
stop(FALSE),
childstop(FALSE)
{
}
void ModemThread::SignalChildStop() {
childstop = TRUE;
SignalDataReady();
}
void ModemThread::SignalStop() {
stop = TRUE;
SignalDataReady();
}
void ModemThread::WaitDataReady()
{
//do {
dataReadySyncPoint.Wait();
//} while(!dataReadySyncPoint.WillBlock());
}
///////////////////////////////////////////////////////////////
ModemThreadChild::ModemThreadChild(ModemThread &_parent)
: parent(_parent)
{
}
void ModemThreadChild::SignalStop()
{
ModemThread::SignalStop();
parent.SignalChildStop();
}
///////////////////////////////////////////////////////////////
int ChunkStream::write(const void *pBuf, PINDEX count)
{
int len = sizeof(data) - last;
if (!len)
return -1;
if (len > count)
len = count;
memcpy(data + last, pBuf, len);
last += len;
return len;
}
int ChunkStream::read(void *pBuf, PINDEX count)
{
if (sizeof(data) == first)
return -1;
int len = last - first;
if (len > count)
len = count;
memcpy(pBuf, data + first, len);
first += len;
return len;
}
///////////////////////////////////////////////////////////////
int DataStream::PutData(const void *_pBuf, PINDEX count)
{
if (eof)
return -1;
int done = 0;
const BYTE *pBuf = (const BYTE *)_pBuf;
while (count) {
if (!lastBuf) {
lastBuf = new ChunkStream();
bufQ.Enqueue(lastBuf);
}
int len = lastBuf->write(pBuf, count);
if (len < 0) {
lastBuf = NULL;
} else {
pBuf += len;
count -= len;
done += len;
}
}
busy += done;
return done;
}
int DataStream::GetData(void *_pBuf, PINDEX count)
{
if (!busy) {
if (eof)
return -1;
else
return 0;
}
int done = 0;
BYTE *pBuf = (BYTE *)_pBuf;
while (count) {
if (!firstBuf) {
firstBuf = bufQ.Dequeue();
if (!firstBuf) {
lastBuf = NULL;
break;
}
}
int len = firstBuf->read(pBuf, count);
if (len < 0) {
delete firstBuf;
firstBuf = NULL;
} else {
if (!len)
break;
pBuf += len;
count -= len;
done += len;
}
}
busy -= done;
return done;
}
void DataStream::Clean()
{
ChunkStream *buf;
while ((buf = bufQ.Dequeue()) != NULL)
delete buf;
if (firstBuf)
delete firstBuf;
firstBuf = lastBuf = NULL;
busy = 0;
eof = FALSE;
diag = 0;
}
///////////////////////////////////////////////////////////////
#if PTRACING
void RenameCurrentThread(const PString &newname)
{
PString oldname = PThread::Current()->GetThreadName();
PThread::Current()->SetThreadName(PString(newname)
#if defined(PROCESS_PER_THREAD)
+ ":" + PString((unsigned)getpid())
#else
#if defined(P_PTHREADS)
+ ":" + PString((unsigned)pthread_self())
#else
+ ":%0x"
#endif
#endif
);
myPTRACE(2, "T38Modem\tRenameCurrentThread old ThreadName=" << oldname);
}
#endif /* PTRACING */
///////////////////////////////////////////////////////////////
#ifdef PROCESS_PER_THREAD
#include <sys/times.h>
static double clktck = sysconf(_SC_CLK_TCK);
const PString GetThreadTimes(const char *head, const char *tail)
{
struct tms t;
if (clktck && times(&t) != -1) {
return psprintf("%suser=%.3f, system=%.3f%s",
head,
t.tms_utime/clktck,
t.tms_stime/clktck,
tail);
}
return "";
}
#endif
///////////////////////////////////////////////////////////////