aboutsummaryrefslogtreecommitdiff
path: root/src/stable/synchroheap.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/synchroheap.h')
-rw-r--r--src/stable/synchroheap.h151
1 files changed, 151 insertions, 0 deletions
diff --git a/src/stable/synchroheap.h b/src/stable/synchroheap.h
new file mode 100644
index 0000000..4dd898d
--- /dev/null
+++ b/src/stable/synchroheap.h
@@ -0,0 +1,151 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_SYNCHRO_HEAP_H
9#define BU_SYNCHRO_HEAP_H
10
11#include "bu/heap.h"
12#include "bu/mutex.h"
13#include "bu/condition.h"
14
15namespace Bu
16{
17 template<typename item, typename cmpfunc=__basicLTCmp<item>,
18 typename itemalloc=std::allocator<item> >
19 class SynchroHeap
20 {
21 public:
22 SynchroHeap()
23 {
24 }
25
26 virtual ~SynchroHeap()
27 {
28 }
29
30 void enqueue( item i )
31 {
32 imData.lock();
33 hData.enqueue( i );
34 icBlock.signal();
35 imData.unlock();
36 }
37
38 item dequeue( bool bBlock=false )
39 {
40 imData.lock();
41 if( hData.isEmpty() )
42 {
43 imData.unlock();
44
45 if( bBlock )
46 {
47 icBlock.lock();
48
49 while( hData.isEmpty() )
50 icBlock.wait();
51
52 imData.lock();
53 try
54 {
55 item iRet = hData.dequeue();
56 imData.unlock();
57 icBlock.unlock();
58 return iRet;
59 }
60 catch(...)
61 {
62 imData.unlock();
63 icBlock.unlock();
64 throw;
65 }
66 }
67 throw HeapException("Heap empty.");
68 }
69 else
70 {
71 try
72 {
73 item iRet = hData.dequeue();
74 imData.unlock();
75 return iRet;
76 }
77 catch(...)
78 {
79 imData.unlock();
80 throw;
81 }
82 }
83 }
84
85 item dequeue( int iSec, int iUSec )
86 {
87 imData.lock();
88 if( hData.isEmpty() )
89 {
90 imData.unlock();
91
92 icBlock.lock();
93
94 icBlock.wait( iSec, iUSec );
95
96 imData.lock();
97 try
98 {
99 item iRet = hData.dequeue();
100 imData.unlock();
101 icBlock.unlock();
102 return iRet;
103 }
104 catch(...)
105 {
106 imData.unlock();
107 icBlock.unlock();
108 throw;
109 }
110 }
111 else
112 {
113 try
114 {
115 item iRet = hData.dequeue();
116 imData.unlock();
117 return iRet;
118 }
119 catch(...)
120 {
121 imData.unlock();
122 throw;
123 }
124 }
125 }
126
127 bool isEmpty()
128 {
129 imData.lock();
130 bool bRet = hData.isEmpty();
131 imData.unlock();
132 return bRet;
133 }
134
135 int getSize()
136 {
137 imData.lock();
138 int iRet = hData.getSize();
139 imData.unlock();
140 return iRet;
141 }
142
143 private:
144 Heap< item, cmpfunc, itemalloc > hData;
145 Mutex imData;
146 Condition icBlock;
147 };
148};
149
150#endif
151