-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProCon.h
More file actions
605 lines (520 loc) · 16.7 KB
/
ProCon.h
File metadata and controls
605 lines (520 loc) · 16.7 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#ifndef _PRO_CON_H
#define _PRO_CON_H
//
// Copyright (c) 2002 by Ted T. Yuan.
//
// Permission is granted to use this code without restriction as long as this copyright notice appears in all source files.
//
#include <boost/thread/condition.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/shared_ptr.hpp>
#include <deque>
#include <vector>
#include <iostream>
#ifndef SPACE_YIN // either you define the space name
#define SPACE_YIN yin // or I define it with a default name "yin"
#endif
// in most cases, you do not want to define HAS_PUTTABLETAKABLE,
// it forces you to define Takable, Puttable in your code
#ifdef HAS_PUTTABLETAKABLE
#define _HAS_PUTTABLETAKABLE
#endif
// namespace is 'yin', the yin classes are usually sub-classed
// by applications, the sub-classes that contain application logic
// are to be in the 'yang' namespace
namespace SPACE_YIN {
/////////////////////////////////////////
// forward declarations...
class Latch;
class Gate;
// sync'd implementation of the corresponding std container classes
template < typename _Tp, typename _queueTp >
class Channel; // intend to be a linear queue, FIFO
template < typename _Tp > struct Pool;
template < typename _Tp >
struct Channels; // a pool of Channel objects
// access syntax api of the above implementation,
// _ChannelTp can be Channel or Channels
// TODO - _ChannelTp should be defined in a trait class...
template < typename _Tp, typename _ChannelTp > struct Takable;
template < typename _Tp, typename _ChannelTp > struct Puttable;
// producer-consumer idiom implemetation templates
template < typename _Tp, typename _ChannelTp > class Producer;
template < typename _Tp, typename _ChannelTp > class Consumer;
// Production has producers and consumers which are connected through queue
template < typename _Tp, typename _ChannelTp,
typename _ProducerTp, typename _ConsumerTp >
class Production;
// Consuming is a pool of consumers to consume a static queue
template < typename _Tp, typename _ChannelTp,
typename _ConsumerTp > class Consuming;
/////////////////////////////////////////
// typedefs and convenient functions
typedef boost::mutex Mutex;
typedef boost::condition Condition;
typedef boost::mutex::scoped_lock Lock;
inline static void sleep(
int secs, int msecs = 0, int usecs = 0, int nsecs = 0)
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
if(nsecs > 1000)
{
usecs += nsecs / 1000;
nsecs = nsecs % 1000;
}
if(usecs > 1000)
{
msecs += usecs / 1000;
usecs = usecs % 1000;
}
if(msecs > 1000)
{
secs += msecs / 1000;
msecs = msecs % 1000;
}
xt.sec += secs;
xt.nsec += (long)msecs * 1000000L + usecs * 1000L + nsecs;
boost::thread::sleep(xt);
}
// if you want logging you need to define WITH_LOGGING,
// then call Logger::log("any text"); once in your main thread
// before using the classes in this file. It assures
// Logger::instance() to run properly (static Mutex s_mu is created un-interruptedly).
#ifdef WITH_LOGGING
#define _WITH_LOGGING
#endif
// debug classes...
struct Logger
{
#ifdef _WITH_LOGGING
// call this once at start of application (main thread) to make sure s_mu is proper
inline static Mutex & instance()
{
static Mutex s_mu;
return(s_mu);
}
#endif
inline static void log(const char * buf)
{
#ifdef _WITH_LOGGING
// can not protect if an outside thread that does not know the mutex and accesses std::cout directly
Lock lock(instance());
std::cout << buf << std::endl << std::flush;
#endif
}
inline static void beeper(int interval, char * msg)
{
if(msg) {
SPACE_YIN::sleep(interval);
log(msg);
}
}
};
class pc_exception : public std::runtime_error
{
public:
explicit pc_exception(const std::string& msg)
: std::runtime_error(msg) {}
};
/////////////////////////////////////////
// implementation
// _queueTp has to have Container traits
template < typename _Tp, typename _queueTp = std::deque<_Tp> >
class Channel : private _queueTp
{
private:
size_t maxSize_;
Mutex monitor_;
Condition bufferNotFull_, bufferNotEmpty_;
volatile bool bMayStop_;
public:
explicit Channel(size_t limit = (size_t)-1) : maxSize_(limit < 1 ? 1 : limit), bMayStop_(false) {}
Channel(_queueTp& queue)
: _queueTp(queue), maxSize_(queue.max_size()) {}
// for consumer thread...
_Tp poll(long msecs = -1) // ignore msecs for now
{
Lock lk(monitor_);
while (!bMayStop_ && 0 == ((_queueTp *)this)->size())
{
bufferNotEmpty_.wait(lk);
}
// outside caller intentionally calls for stop, last resort?
if(bMayStop_ && 0 == ((_queueTp *)this)->size())
throw pc_exception("consumer to end");
// pop back
_Tp item = pop();
bufferNotFull_.notify_one();
return item;
}
// for producer thread...
bool offer(_Tp item, long msecs = -1) // ignore msecs for now
{
Lock lk(monitor_);
while (maxSize_ == ((_queueTp *)this)->size())
{
bufferNotFull_.wait(lk);
}
// push front
push(item);
bufferNotEmpty_.notify_one();
return true;
}
virtual void mayStop(bool bMayStop = true)
{
Lock lk(monitor_);
bMayStop_ = bMayStop;
if(bMayStop) // if outside says may stop, wake up the waiting threads...
{
bufferNotEmpty_.notify_all();
}
}
// for outside callers only, calling this on Linux 7.2 within
// the above poll() or offer() will deadlock
typename _queueTp::size_type size()
{
Lock lk(monitor_);
return ((_queueTp *)this)->size();
}
protected:
virtual _Tp pop() // retrieve from end
{
_Tp item = ((_queueTp *)this)->back();
((_queueTp *)this)->pop_back();
return item;
}
virtual void push(const _Tp item) // insert at head...
{
((_queueTp *)this)->insert(begin(), item);
}
};
// Pool provides thread-safe access to std::vector<_Tp>
template < typename _Tp >
struct Pool : public Channel< _Tp, std::vector<_Tp> > {
Pool(size_t limit = (size_t)-1) : Channel< _Tp, std::vector<_Tp> >(limit) {}
Pool(std::vector<_Tp>& queue)
: Channel< _Tp, std::vector<_Tp> >(queue) {}
};
// Channels is intended to be a thread-safe vector of thread-safe queues...
template < typename _Tp >
struct Channels : public Pool< Channel<_Tp> >
{
Channels(size_t size = (size_t)-1) : Pool< Channel<_Tp> >(size) {}
_Tp poll(long msecs = -1) // ignore msecs for now
{
Channel<_Tp> channel = ((Pool< Channel<_Tp> >*)this)->poll(msecs);
return channel.poll(msecs);
}
bool offer(_Tp item, long msecs = -1) // ignore msecs for now
{
Channel<_Tp> channel = poll(msecs);
return channel.offer(item, msecs);
}
};
// taker API on top of thread-safe _ChannelTp
template < typename _Tp, typename _ChannelTp = Channel<_Tp> >
struct Takable // thread-safe storage iterator
{
_ChannelTp& channel_;
Takable(_ChannelTp& chan) : channel_(chan) {}
virtual _Tp take(long msecs = -1)
{
try {
return channel_.poll(msecs);
} catch (pc_exception& pce) { throw pce;
} catch (boost::lock_error& err) { Logger::log(err.what()); throw (int)err.what();
} catch (std::exception& err) { Logger::log(err.what()); throw (int)err.what();
} catch (...) { Logger::log("you threw take error"); throw -1; }
}
};
// inserter API on top of thread-safe _ChannelTp
template < typename _Tp, typename _ChannelTp = Channel<_Tp> >
struct Puttable // thread-safe storage inserter
{
_ChannelTp& channel_;
Puttable(_ChannelTp& chan) : channel_(chan) {}
virtual void put(_Tp item, long msecs = -1)
{
try {
channel_.offer(item, msecs);
} catch (boost::lock_error& err) { throw (int)err.what();
} catch (std::exception& err) { Logger::log(err.what()); throw (int)err.what();
} catch (...) { Logger::log("you threw put error"); throw -1; }
}
};
// Locking mechanisms
class Latch
{
protected:
volatile bool state_;
#define latched_ state_
Mutex monitor_;
Condition cond_;
public:
explicit Latch(bool state = false) : latched_(state) {}
bool attempt(long msecs)
{
return false; // function holder, not implemented yet
}
void acquire()
{
Lock lk(monitor_);
while (!latched_)
cond_.wait(lk);
}
// enables all current and future acquires to pass
void release(bool bRelease = true)
{
Lock lk(monitor_);
latched_ = bRelease;
cond_.notify_all();
}
#undef latched_
};
// use object pointers of type _Tp to go around STL's value semantic...
template < typename _Tp, typename _ChannelTp = Channel<_Tp> >
class Producer
{
Puttable<_Tp, _ChannelTp> * ptr_holder_;
boost::shared_ptr< Puttable<_Tp, _ChannelTp> > holder_;
protected:
Puttable<_Tp, _ChannelTp>& channel_;
Latch& latch_;
volatile bool bMayStop_;
public:
Producer(Puttable<_Tp, _ChannelTp>& channel, Latch& lh)
: channel_(channel), latch_(lh), bMayStop_(true) {}
Producer(_ChannelTp& channel, Latch& lh)
: ptr_holder_( new Puttable<_Tp, _ChannelTp>(channel) ),
holder_(ptr_holder_), channel_(*holder_),
latch_(lh), bMayStop_(true) {}
void operator()()
{
latch_.acquire();
started();
// bMayStop_ is a suggest to stop from outside, overwritable cancel() is internal condition...
for(;!(bMayStop_ && cancel());)
{
try
{
_Tp o = produce();
channel_.put(o);
} catch (pc_exception) {
break;
} catch (...) { // you threw
Logger::log("Caught unknown exception in Producer::produce");
break;
}
boost::thread::yield(); // need breath...
}
done();
}
virtual void mayStop(bool bMayStop = true)
{ channel_.channel_.mayStop(bMayStop_ = bMayStop); }
protected: // overwritable by derived classes...
virtual _Tp produce() { // value semantic, _Tp may need to have a copy constructor...
Logger::beeper(1, "producer not implemented");
throw -1 ;
}
virtual bool cancel() { return false; } // defualt implementation is never to cancel
virtual void started() {
Logger::log("producer started");
}
virtual void done() {
Logger::log("producer done");
}
};
template < typename _Tp, typename _ChannelTp = Channel<_Tp> >
class Consumer
{
Takable<_Tp, _ChannelTp> * ptr_holder_;
boost::shared_ptr< Takable<_Tp, _ChannelTp> > holder_;
protected:
Takable<_Tp, _ChannelTp>& channel_;
Latch& latch_;
long checkEmptyWithinMSec_;
volatile bool bMayStop_;
public:
Consumer(Takable<_Tp, _ChannelTp>& channel, Latch& lh)
: channel_(channel), checkEmptyWithinMSec_(-1),
latch_(lh), bMayStop_(true) {}
Consumer(_ChannelTp& channel, Latch& lh)
: ptr_holder_( new Takable<_Tp, _ChannelTp>(channel) ),
holder_(ptr_holder_), channel_(*holder_),
checkEmptyWithinMSec_(-1), latch_(lh), bMayStop_(true) {}
void operator()()
{
latch_.acquire();
started();
// bMayStop_ is a suggest to stop from outside, overwritable cancel() is internal condition...
for(;!(bMayStop_ && cancel());)
{
try {
// wait for within_msec milli-seconds to make sure no new tasks coming in...
_Tp item = channel_.take(checkEmptyWithinMSec_ < 0 ? -1 : checkEmptyWithinMSec_);
consume(item);
} catch (pc_exception) { // thrown by mayStop() in _ChannelTp
break;
} catch (...) { // you threw
Logger::log("Caught unknown exception in Consumer::consume");
break;
}
boost::thread::yield(); // need breath...
}
done();
}
virtual void mayStop(bool bMayStop = true)
{ channel_.channel_.mayStop(bMayStop_ = bMayStop); }
protected: // overwritable by derived classes...
virtual void consume(_Tp x) {
Logger::log("consumer not implemented");
}
virtual bool cancel() { return false; } // default implementation is never to cancel
virtual void started() { // defualt implementation does nothing
Logger::log("consumer started");
}
virtual void done() { // defualt implementation does nothing
Logger::log("consumer done");
}
};
// thread-safe, sustainable production-line logic that chains producers with consumers
template < typename _Tp,
typename _ChannelTp = Channel<_Tp>,
typename _ProducerTp = Producer<_Tp, _ChannelTp>,
typename _ConsumerTp = Consumer<_Tp, _ChannelTp> >
class Production
{
private:
size_t queueLen_, nProducers_, nConsumers_;
bool syncStart_, bHidePuttableTakable_;
public:
// bHideTakable indicates derived classes of Producer and Consumer
// do not have custom implementation of Puttable and Takable interfaces
explicit Production(size_t np = 1, size_t nc = 1,
bool sc = true, size_t ql = (size_t)-1, bool bHideTakable = false)
: nProducers_(np), nConsumers_(nc), syncStart_(sc),
queueLen_(ql), bHidePuttableTakable_(bHideTakable) {}
void operator()()
{
_ChannelTp chan(queueLen_);
Latch theLatch, noLatch(true);
#if defined(_HAS_PUTTABLETAKABLE)
Puttable<_Tp, _ChannelTp> puttable(chan);
Takable<_Tp, _ChannelTp> takable(chan);
_ProducerTp* producer = bHidePuttableTakable_
? new _ProducerTp(chan, noLatch)
: new _ProducerTp(puttable, noLatch) ;
_ConsumerTp* consumer = bHidePuttableTakable_
? new _ConsumerTp(chan, syncStart_ ? theLatch : noLatch)
: new _ConsumerTp(takable, syncStart_ ? theLatch : noLatch);
#else
_ProducerTp* producer =
new _ProducerTp(chan, noLatch);
_ConsumerTp* consumer =
new _ConsumerTp(chan, syncStart_ ? theLatch : noLatch);
#endif
std::auto_ptr<_ProducerTp> prodClean(producer);
std::auto_ptr<_ConsumerTp> consClean(consumer);
consumer->mayStop(false);
pcModelCreated(*producer, *consumer);
try {
boost::thread_group pthreads, cthreads;
size_t i;
for (i = 0; i < nProducers_; ++i)
pthreads.create_thread(*producer);
for (i = 0; i < nConsumers_; ++i)
cthreads.create_thread(*consumer);
theLatch.release();
beforeJoin();
pthreads.join_all();
// wait for tasks to be processed by consumers,
// then tell them to stop...not really needed?
while(chan.size() > 0) sleep(0, 10);
consumer->mayStop(true); // producers are done, consumers *may* stop...
cthreads.join_all();
afterJoin();
} catch (boost::lock_error& err) {
Logger::log(err.what());
} catch (std::exception& err) {
Logger::log(err.what());
} catch (...) {
Logger::log("caught...");
}
}
// sub-classes overwritables ...
// monitoring...
virtual void pcModelCreated(_ProducerTp& producer, _ConsumerTp& consumer) {}
virtual void beforeJoin() {
Logger::log("production before join");
}
virtual void afterJoin() {
Logger::log("production after join");
}
};
template < typename _Tp,
typename _ChannelTp = Channel<_Tp>,
typename _ConsumerTp = Consumer<_Tp, _ChannelTp> >
class Consuming
{
protected:
size_t nConsumers_;
bool syncStart_, bHidePuttableTakable_;
_ChannelTp& channel_;
public:
// bHideTakable indicates derived classes of Consumer
// do not have custom implementation of Takable interfaces
Consuming(_ChannelTp& channel, size_t nc = 1,
bool sc = true, bool bHideTakable = false) //, Gate * gate = NULL)
: channel_(channel), nConsumers_(nc), syncStart_(sc),
bHidePuttableTakable_(bHideTakable) //, channelReadGate(gate)
{}
void operator()()
{
Latch theLatch, noLatch(true);
#if defined(_HAS_PUTTABLETAKABLE)
Takable<_Tp, _ChannelTp> takable(channel_); // valid in this function scope
// consumer is created here, and it is shared by all consumer threads...
// in a sense the single consumer is the model for a group of threaded live consumers
_ConsumerTp* consumer = bHidePuttableTakable_
? new _ConsumerTp(channel_, syncStart_ ? theLatch : noLatch)
: new _ConsumerTp(takable, syncStart_ ? theLatch : noLatch);
#else
_ConsumerTp* consumer = new _ConsumerTp(channel_, syncStart_ ? theLatch : noLatch);
#endif
std::auto_ptr<_ConsumerTp> consClean(consumer);
consumer->mayStop(true);
consumerModelCreated(*consumer);
boost::thread_group threads;
try {
for (size_t i = 0; i < nConsumers_; ++i)
threads.create_thread(*consumer);
} catch (boost::lock_error& err) {
Logger::log(err.what());
} catch (std::exception& err) {
Logger::log(err.what());
} catch (...) {
Logger::log("caught...");
}
theLatch.release();
beforeJoin();
threads.join_all();
afterJoin();
}
// sub-classes overwritables ...
// monitoring...
virtual void consumerModelCreated(_ConsumerTp& consumer) {}
virtual void beforeJoin() {
Logger::log("Consuming before join");
}
virtual void afterJoin() {
Logger::log("Consuming after join");
}
// if you set consumer->mayStop(false) in consumerModelCreated, like in ThreadPool
// this is the only way to stop the pool later by an application...
virtual void mayStop(bool bMayStop = true)
{ channel_.mayStop(bMayStop); }
};
} // namespace yin
#endif