Menu

[r1]: / deferred_manager.cpp  Maximize  Restore  History

Download this file

496 lines (336 with data), 12.1 kB

  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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include <queue>
#include <deferred/deferred_manager_api.h>
#include <deferred/thread_manager_api.h>
#ifdef BOOST_MSVC
# pragma warning(push)
# pragma warning(disable:4251 4275 4231 4660)
#endif
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/tss.hpp>
#include <boost/thread/xtime.hpp>
#ifdef BOOST_MSVC
# pragma warning(pop)
#endif
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <list>
namespace deferred
{
// The following three functions are defined in thread_manger.cpp
// there aren't enough to justify a full header so we just toss them in here
//
void add_dm( DeferredManager * dm );
void remove_dm( DeferredManager * dm );
void rename_dm( const std::string & old_name, const std::string & new_name );
namespace
{// mili micro nano
const int NANOSECONDS_PER_SECOND = 1 * 1000 * 1000 * 1000;
}
struct QCommand
{
SafePtr<Deferred> deferred;
DeferredCommand command;
};
class DeferredManagerImpl : public DeferredManager
{
std::string thread_name;
boost::mutex mutex;
boost::condition condition;
std::queue< QCommand > processing_queue;
bool shutdown_requested;
SafePtr<ThreadManager> thr_mgr; // Hold a safe ref so the manager doesn't get deleted too soon.
static boost::mutex static_mutex;
static int static_instance_count; // used to generate initial thread-names
boost::function0<void> enqueue_hook;
typedef std::list< boost::function1<void, DeferredManager *> > CleanupCallbackList;
CleanupCallbackList cleanup_callbacks;
typedef boost::mutex::scoped_lock Lock;
SafePtr<CallbackData> _req_shutdown(); // helper method
public:
DeferredManagerImpl();
virtual ~DeferredManagerImpl();
virtual std::string & getThreadName()
{
return thread_name;
}
virtual void setThreadName( const std::string & new_name );
virtual void queueCommand( const boost::function0< void > & cmd );
virtual SafePtr<Deferred> queueDeferredCommand( const DeferredCommand & dc );
virtual void queueCompletedDeferred( SafePtr<Deferred> d, SafePtr<CallbackData> result );
virtual void addEnqueueHook( const boost::function0<void> & cmd );
virtual int queueLength();
virtual void executeQueuedOperations(bool throw_on_failure = false);
virtual void executeNextOperation(bool throw_on_failure = false);
// Returns false if the timeout occurs prior to executing any operations
//
virtual bool waitForAnyOperation( long sec=0, long usec=0 );
virtual bool waitForCompletionOf( SafePtr<Deferred> d,
long sec=0, long usec=0 );
// Adds a function throwing ShutdownRequested to the end of the processing queue
//
virtual void requestShutdown();
void wake( SafePtr<Deferred> d );
void addDestructorCallback( const boost::function1<void, DeferredManager *> & cmd );
// returns true if a shutdown has been requested
//
virtual bool shutdownRequested() { return shutdown_requested; }
void execute_one( bool throw_on_failure );
};
namespace
{
boost::thread_specific_ptr< SafePtr<DeferredManagerImpl> > ts_mgr;
void do_nothing()
{}
};
TSDM_Singleton g_deferred_manager;
SafePtr<DeferredManager> TSDM_Singleton::operator ->()
{
return *this;
}
TSDM_Singleton::operator SafePtr<DeferredManager> ()
{
SafePtr<DeferredManagerImpl> * p = ts_mgr.get();
if (!p)
{
p = new SafePtr<DeferredManagerImpl>();
*p = new DeferredManagerImpl();
ts_mgr.reset(p);
}
return *p;
}
//----------------------------------------------------------------------------------------------
// define storage for static vars
boost::mutex DeferredManagerImpl::static_mutex;
int DeferredManagerImpl::static_instance_count;
DeferredManagerImpl::DeferredManagerImpl() : shutdown_requested(false)
{
Lock guard( static_mutex );
thread_name = "DeferredThread:" + boost::lexical_cast<std::string>( ++static_instance_count );
enqueue_hook = do_nothing;
thr_mgr = &(*thread_manager);
add_dm( this );
}
DeferredManagerImpl::~DeferredManagerImpl()
{
try
{
CleanupCallbackList::iterator i;
for( i = cleanup_callbacks.begin(); i != cleanup_callbacks.end(); i++ )
i->operator()( this );
}
catch(...)
{}
remove_dm( this );
}
void DeferredManagerImpl::setThreadName( const std::string & new_name )
{
Lock guard( mutex );
rename_dm( thread_name, new_name );
thread_name = new_name;
}
namespace
{
SafePtr<CallbackData> execute_command( const boost::function0< void > & cmd )
{
cmd();
return 0;
}
}
void DeferredManagerImpl::queueCommand( const boost::function0< void > & cmd )
{
{
Lock guard( mutex );
processing_queue.push( QCommand() );
processing_queue.back().command = boost::bind( execute_command, cmd );
condition.notify_one();
}
enqueue_hook();
}
SafePtr<Deferred> DeferredManagerImpl::queueDeferredCommand( const DeferredCommand & dc )
{
SafePtr<Deferred> ret;
{
Lock guard( mutex );
processing_queue.push( QCommand() );
ret = createDeferred();
processing_queue.back().deferred = ret;
processing_queue.back().command = dc;
condition.notify_one();
}
enqueue_hook();
return ret;
}
namespace
{
SafePtr<CallbackData> execute_deferred( SafePtr<Deferred> & d, SafePtr<CallbackData> & result )
{
d->execute( result );
return d;
}
}
void DeferredManagerImpl::queueCompletedDeferred( SafePtr<Deferred> d, SafePtr<CallbackData> result )
{
{
Lock guard( mutex );
processing_queue.push( QCommand() );
processing_queue.back().command = boost::bind( execute_deferred, d, result );
condition.notify_one();
}
enqueue_hook();
}
void DeferredManagerImpl::addEnqueueHook( const boost::function0<void> & cmd )
{
enqueue_hook = cmd;
}
int DeferredManagerImpl::queueLength()
{
Lock guard( mutex );
return (int) processing_queue.size();
}
void DeferredManagerImpl::execute_one( bool throw_on_failure )
{
SafePtr<Deferred> deferred;
DeferredCommand command;
SafePtr<CallbackData> result;
{ // isolate the lock here so that we can execute the command without holding the lock
Lock guard(mutex);
if( processing_queue.size() == 0 )
return;
deferred = processing_queue.front().deferred;
command = processing_queue.front().command;
processing_queue.pop();
}
if( deferred )
{
try
{
result = command();
}
catch( Failure & f )
{
result = f.clone();
}
deferred->execute( result );
}
else
{
// the result with either be 0 (for case of executing a normal command)
// or will be a valid SafePtr<Deferred> ( for a deferred command )
//
try
{
result = command();
if( result )
{
deferred = static_pointer_cast<Deferred>( result );
if( deferred->status() == Deferred::FAILURE )
result = static_pointer_cast<Failure>( deferred->endResult() );
}
}
catch( Failure & f )
{
result = f.clone();
}
}
SafePtr<Failure> err = dynamic_pointer_cast<Failure>(result);
if( throw_on_failure && err )
err->throw_self();
}
void DeferredManagerImpl::executeQueuedOperations( bool throw_on_failure )
{
while( processing_queue.size() != 0 )
execute_one( throw_on_failure );
}
void DeferredManagerImpl::executeNextOperation( bool throw_on_failure )
{
if( processing_queue.size() != 0 )
execute_one( throw_on_failure );
}
bool DeferredManagerImpl::waitForAnyOperation( long sec, long usec )
{
Lock guard( mutex );
if (processing_queue.size() > 0)
return true;
if (sec == 0 && usec == 0)
{
condition.wait( guard );
return false;
}
else
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += sec;
xt.nsec += usec * 1000;
while(xt.nsec >= NANOSECONDS_PER_SECOND)
{
++xt.sec;
xt.nsec -= NANOSECONDS_PER_SECOND;
}
return condition.timed_wait( guard, xt );
}
}
bool DeferredManagerImpl::waitForCompletionOf( SafePtr<Deferred> d,
long sec, long usec )
{
boost::xtime xt;
bool use_xt = false;
if (d->isComplete())
return true;
if ( sec != 0 || usec != 0 )
{
use_xt = true;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += sec;
xt.nsec += usec * 1000;
while(xt.nsec >= NANOSECONDS_PER_SECOND)
{
++xt.sec;
xt.nsec -= NANOSECONDS_PER_SECOND;
}
}
d->addCompleteCallback( boost::bind( &DeferredManagerImpl::wake, this, _1 ) );
while( true )
{
if( d->isComplete() )
return true;
while( processing_queue.size() != 0 )
{
execute_one( false );
if( d->isComplete() )
return true;
}
if( use_xt )
{
Lock guard( mutex );
if (!condition.timed_wait( guard, xt ) )
return false;
}
else
{
Lock guard( mutex );
condition.wait( guard );
}
}
}
SafePtr<CallbackData> DeferredManagerImpl::_req_shutdown()
{
shutdown_requested = true;
throw ShutdownRequested();
}
void DeferredManagerImpl::requestShutdown()
{
queueCommand( boost::bind(&DeferredManagerImpl::_req_shutdown, this) );
}
void DeferredManagerImpl::wake( SafePtr<Deferred> d )
{
Lock guard( mutex );
condition.notify_one();
}
void DeferredManagerImpl::addDestructorCallback( const boost::function1<void, DeferredManager *> & cmd )
{
Lock guard( mutex );
cleanup_callbacks.push_back( cmd );
}
}// end namespace deferred