-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvConductor.cc
402 lines (361 loc) · 10.2 KB
/
AvConductor.cc
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//# Copyright (C) 1995-97 Board of Trustees of the University of Illinois
//#
//# This software, both binary and source, is copyrighted by The
//# Board of Trustees of the University of Illinois. Ownership
//# remains with the University. You should have received a copy
//# of a licensing agreement with this software. See the file
//# "AIPSVIEW_COPYRIGHT", or contact the University at this address:
//#
//# The NCSA AipsView Visualization System
//# National Center for Supercomputing Applications
//# University of Illinois
//# 405 North Mathews Ave.
//# Urbana, IL 61801
//# --------------------------------------------------
//
// $Header: /home/cvs/aips++/code/trial/apps/aipsview/Attic/AvConductor.cc,v 19.0 2003/07/16 05:46:58 aips2adm Exp $
//
// $Log: AvConductor.cc,v $
// Revision 19.0 2003/07/16 05:46:58 aips2adm
// exhale: Base release 19.000.00
//
// Revision 18.0 2002/06/07 21:28:42 aips2adm
// exhale: Base release 18.000.00
//
// Revision 17.0 2001/11/12 19:42:11 aips2adm
// exhale: Base release 17.000.00
//
// Revision 16.0 2001/05/03 01:42:19 aips2adm
// exhale: Base release 16.000.00
//
// Revision 15.0 2000/10/26 17:09:23 aips2adm
// exhale: Base release 15.000.00
//
// Revision 14.0 2000/03/23 16:08:01 aips2adm
// exhale: Base release 14.000.00
//
// Revision 13.0 1999/08/10 18:39:45 aips2adm
// exhale: Base release 13.000.00
//
// Revision 12.0 1999/07/15 00:22:13 aips2adm
// exhale: Base release 12.000.00
//
// Revision 11.0 1998/10/03 06:59:10 aips2adm
// exhale: Base release 11.000.00
//
// Revision 10.0 1998/07/20 17:52:54 aips2adm
// exhale: Base release 10.000.00
//
// Revision 9.0 1997/08/25 21:26:09 aips2adm
// exhale: Base release 09.000.00
//
// Revision 8.0 1997/02/20 03:15:56 aips2adm
// exhale: Base release 08.000.00
//
// Revision 7.4 1997/01/10 22:24:15 hr
// Added addToFront arg to constructor.
//
// Revision 7.3 1996/12/12 06:04:08 droberts
// Final update from monet archive.
//
// Revision 1.4 1996/09/27 16:50:02 hr
// Made conductor items subclass of AvResource. Moved a couple of short
// routines to the include file. Added list locking so calling disconnect
// wouldn't result in removing a currently being used conductor item.
//
// Revision 1.3 1996/09/11 19:09:20 hr
// Added AvConductorItem::remove().
//
// Revision 1.2 1996/07/30 18:40:27 hr
// Added casts to NULL to keep g++ happy.
//
// Revision 1.1 1996/07/11 21:31:21 pixton
// Automated Checkin
//
// Revision 1.7 1996/06/18 18:45:54 pixton
// Copyright Update
//
// Revision 1.6 1996/01/17 16:23:02 hr
// removeCallbacks was having problems with removing the head of the list.
//
// Revision 1.5 1995/09/20 20:03:06 baker
// Release Beta 1.0
//
// Revision 1.5 1995/09/20 01:56:34 baker
// Release Beta 1.0
//
// Revision 1.4 1995/08/03 16:26:55 hr
// (Missed a newline during last checkin).
//
// Revision 1.3 1995/08/03 16:25:08 hr
// Major changes including making callback reason a bit mask rather than
// a single value. Current references to conductor should still work.
//
// Revision 1.2 1995/07/17 21:32:53 hr
// Added destructor.
//
// Revision 1.1 1995/03/16 19:45:22 pixton
// Initial revision
//
//
//---------------------------------------------------------------------------
#include <iostream.h> // For the cerr.
#include "AvConductor.h"
AvConductorItem::AvConductorItem(AvConductorCB callback, const int when,
XtPointer object, XtPointer userData)
{
callback_ = callback;
conductor_ = NULL;
when_ = when;
object_ = object;
userData_ = userData;
next_ = NULL;
marked_ = FALSE;
}
AvConductorItem::~AvConductorItem()
{
// Notify conductor of the destruction. (conductor_ will be NULL if
// AvConductor is destroying this).
if(conductor_ != NULL)
conductor_->callbackDestroyed(this);
}
// Call the callback with current data.
void AvConductorItem::execute()
{
if(callback_ != NULL)
callback_ (object_, conductor_, userData_);
}
Boolean AvConductorItem::matches(AvConductorCB callback, const int when,
const XtPointer object, const XtPointer userData)const
{
return (callback == callback_) && (when == when_) &&
(object == object_) && (userData == userData_);
}
Boolean AvConductorItem::matches(const AvConductorItem *ci)const
{
if(ci == NULL)
return FALSE;
return matches(ci->callback_, ci->when_, ci->object_, ci->userData_);
}
void AvConductorItem::remove()
{
if(conductor_ != NULL)
conductor_->removeCallback(this);
}
void AvConductorItem::markForRemoval(AvConductorItem *i)
{
if(i != NULL)
i->marked_ = TRUE;
}
////////////////////////////////////////////////////////////////////////////
AvConductor::AvConductor ()
{
head = tail = 0;
reason_ = NoReason;
reasons_ = 0;
disableCount_ = 0;
listLocked_ = FALSE;
}
AvConductor::~AvConductor ()
{
invokeCallbacks(Destroyed);
removeCallbacks();
}
// Add callback item to list.
void AvConductor::addCallbackItem(AvConductorItem *ci, const Boolean atFront)
{
if(ci == NULL)
return;
ci->conductor(this);
ci->ref();
ci->lockResource();
// is this the first item?
if (empty ())
head = tail = ci;
else
{ if(atFront)
{
ci->next(head);
head = ci;
}
else
{
tail->next(ci);
tail = ci;
}
}
}
/* Search for item. If needMatch is TRUE, comparison is done by checking
the components. If FALSE, the pointers are checked instead.
On return, prev will point to the predecessor of the node that matched or
NULL if node was at the head.
*/
Boolean AvConductor::findItem(AvConductorItem *ci, const Boolean needMatch,
AvConductorItem **prev)const
{
AvConductorItem *p, *pp;
Boolean found = FALSE;
if((ci == NULL) || (empty()))
return FALSE;
for(pp=NULL, p = head; p != NULL; p = p->next())
{ if( (!ci->markedForRemoval()) && ((needMatch && ci->matches(p)) ||
(!needMatch && (p == ci))))
{ found = TRUE;
break;
}
pp = p;
}
if((prev != NULL) && found)
*prev = pp;
return found;
}
// Remove prev's next item from the list or the head if prev is NULL.
// If the list is locked, just mark it.
void AvConductor::removeNextItem(AvConductorItem *prev)
{
AvConductorItem *ci, *next; // Item to be removed and the one after it.
// If the list is locked, only mark the item.
if(listLocked())
{ if(prev != NULL)
AvConductorItem::markForRemoval(prev->next());
else
AvConductorItem::markForRemoval(head);
return;
}
// Remove from list, updating the pointers.
if(prev != NULL)
{ ci = prev->next();
next = (ci != (AvConductorItem *)NULL) ? ci->next() :
(AvConductorItem *)NULL;
prev->next(next);
}
else // Remove head of list.
{ ci = head;
next = (ci != (AvConductorItem *)NULL) ? ci->next() :
(AvConductorItem *)NULL;
head = next;
}
// If tail was removed, the new tail is the predecessor or NULL
// if the head was the only item on the list.
if(tail == ci)
tail = prev;
// Once removed, ensure it won't try to access conductor again.
if(ci != NULL)
{ ci->conductor(NULL);
ci->unlockResource();
ci->unref();
}
}
// Remove item pointed to by ci.
void AvConductor::removeItem(AvConductorItem *ci)
{
AvConductorItem *prev;
if(findItem(ci, FALSE, &prev))
removeNextItem(prev);
}
/* Called by a conductor item when it is destroyed. Remove item from list.
Conductor items we're destroying can't call this since the pointer to 'this'
is removed before the delete.
*/
void AvConductor::callbackDestroyed(AvConductorItem *ci)
{
// This shouldn't happen since we've ref'd the item.
cerr << "AvConductor::callbackDestroyed called\n";
removeItem(ci);
}
void AvConductor::removeCallback(AvConductorItem *ci)
{
removeItem(ci);
}
// Remove callback whose info matches these args.
void AvConductor::removeCallback(AvConductorCB func, int w,
XtPointer o, XtPointer d)
{
AvConductorItem *ci = new AvConductorItem(func, w, o, d), *prev;
ci->ref();
if(findItem(ci, TRUE, &prev)) // Compare using info, not pointers.
removeNextItem(prev);
ci->unref();
}
// Get rid of entire list.
void AvConductor::removeCallbacks()
{
while(head != NULL)
removeNextItem(NULL);
head = tail = NULL;
}
void AvConductor::addCallback
(AvConductorCB func, int w, XtPointer o, XtPointer d, const Boolean atFront)
{
AvConductorItem * ci = new AvConductorItem(func, w, o, d);
// Is this callback already on the list?
if(findItem(ci, TRUE)) // Ignore if it's already there.
ci->unref();
else
addCallbackItem(ci, atFront);
}
// Call any callbacks which match key or previous bits we've saved.
// If notifyEnabled is FALSE, just remember the key.
void AvConductor::invokeCallbacks (const int key)
{
AvConductorItem * p;
reasons_ |= key;
if(reasons_ == 0)
return; // Nothing to do.
if(!notifyEnabled())
return;
reason_ = reasons_; // For callbackReason.
// If reaon is DISCONNECT, calling the callback will result in the
// conductor item being removed out from under us. The list is locked
// so this won't happen.
lockList();
for (p=head; p; p=p->next())
if (p->keyMatch(reason_))
{ // Execute could result in removing p from list.
p->ref();
p->execute();
p->unref();
}
unlockList();
reason_ = NoReason;
reasons_ = 0;
}
// Increment disable counter. Masks passed to invokeCallbacks will be
// saved until enableNotify reduces the count to 0 or an explicit
// invokeCallbacks is called.
void AvConductor::disableNotify()
{
disableCount_ += 1;
}
// Decrement disableCount. If 0, callbacks will be called unless notify
// is false.
void AvConductor::enableNotify(const Boolean notify)
{ if(disableCount_ > 0)
disableCount_ -= 1;
if((disableCount_ == 0) && notify)
invokeCallbacks(0);
}
// Clears callback mask generated by batched calls to invokeCallbacks.
// This should only be used if it is desired that the callback mask generated
// while notification was disabled be discarded rather than used after
// notification is reenabled.
void AvConductor::clearCallbackMask()
{
reasons_ = 0;
}
// Scan the list removing any callback items that have been marked for removal.
void AvConductor::unlockList()
{
listLocked_ = FALSE;
while( (head != NULL) && head->markedForRemoval())
removeNextItem(NULL); // Remove head.
AvConductorItem *ci = head, *next;
while(ci != NULL)
{ next = ci->next();
if((next != NULL)&&next->markedForRemoval())
removeNextItem(ci);
else
ci = next;
}
}