1 | /*****************************************************************************/ |
---|---|
2 | // Copyright 2006-2008 Adobe Systems Incorporated |
3 | // All Rights Reserved. |
4 | // |
5 | // NOTICE: Adobe permits you to use, modify, and distribute this file in |
6 | // accordance with the terms of the Adobe license agreement accompanying it. |
7 | /*****************************************************************************/ |
8 | |
9 | /* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_abort_sniffer.cpp#1 $ */ |
10 | /* $DateTime: 2012/05/30 13:28:51 $ */ |
11 | /* $Change: 832332 $ */ |
12 | /* $Author: tknoll $ */ |
13 | |
14 | /*****************************************************************************/ |
15 | |
16 | #include "dng_abort_sniffer.h" |
17 | |
18 | #include "dng_mutex.h" |
19 | |
20 | /*****************************************************************************/ |
21 | |
22 | #if qDNGThreadSafe |
23 | |
24 | /*****************************************************************************/ |
25 | |
26 | class dng_priority_manager |
27 | { |
28 | |
29 | private: |
30 | |
31 | dng_mutex fMutex; |
32 | |
33 | dng_condition fCondition; |
34 | |
35 | uint32 fCounter [dng_priority_count]; |
36 | |
37 | public: |
38 | |
39 | dng_priority_manager (); |
40 | |
41 | void Increment (dng_priority priority); |
42 | |
43 | void Decrement (dng_priority priority); |
44 | |
45 | void Wait (dng_priority priority); |
46 | |
47 | private: |
48 | |
49 | dng_priority MinPriority () |
50 | { |
51 | |
52 | // Assumes mutex is locked. |
53 | |
54 | for (uint32 level = dng_priority_maximum; |
55 | level > dng_priority_minimum; |
56 | level--) |
57 | { |
58 | |
59 | if (fCounter [level]) |
60 | { |
61 | return (dng_priority) level; |
62 | } |
63 | |
64 | } |
65 | |
66 | return dng_priority_minimum; |
67 | |
68 | } |
69 | |
70 | }; |
71 | |
72 | /*****************************************************************************/ |
73 | |
74 | dng_priority_manager::dng_priority_manager () |
75 | |
76 | : fMutex ("dng_priority_manager::fMutex") |
77 | , fCondition () |
78 | |
79 | { |
80 | |
81 | for (uint32 level = dng_priority_minimum; |
82 | level <= dng_priority_maximum; |
83 | level++) |
84 | { |
85 | |
86 | fCounter [level] = 0; |
87 | |
88 | } |
89 | |
90 | } |
91 | |
92 | /*****************************************************************************/ |
93 | |
94 | void dng_priority_manager::Increment (dng_priority priority) |
95 | { |
96 | |
97 | dng_lock_mutex lock (&fMutex); |
98 | |
99 | fCounter [priority] += 1; |
100 | |
101 | } |
102 | |
103 | /*****************************************************************************/ |
104 | |
105 | void dng_priority_manager::Decrement (dng_priority priority) |
106 | { |
107 | |
108 | dng_lock_mutex lock (&fMutex); |
109 | |
110 | dng_priority oldMin = MinPriority (); |
111 | |
112 | fCounter [priority] -= 1; |
113 | |
114 | dng_priority newMin = MinPriority (); |
115 | |
116 | if (newMin < oldMin) |
117 | { |
118 | |
119 | fCondition.Broadcast (); |
120 | |
121 | } |
122 | |
123 | } |
124 | |
125 | /*****************************************************************************/ |
126 | |
127 | void dng_priority_manager::Wait (dng_priority priority) |
128 | { |
129 | |
130 | if (priority < dng_priority_maximum) |
131 | { |
132 | |
133 | dng_lock_mutex lock (&fMutex); |
134 | |
135 | while (priority < MinPriority ()) |
136 | { |
137 | |
138 | fCondition.Wait (fMutex); |
139 | |
140 | } |
141 | |
142 | } |
143 | |
144 | } |
145 | |
146 | /*****************************************************************************/ |
147 | |
148 | static dng_priority_manager gPriorityManager; |
149 | |
150 | /*****************************************************************************/ |
151 | |
152 | #endif |
153 | |
154 | /*****************************************************************************/ |
155 | |
156 | dng_set_minimum_priority::dng_set_minimum_priority (dng_priority priority) |
157 | |
158 | : fPriority (priority) |
159 | |
160 | { |
161 | |
162 | #if qDNGThreadSafe |
163 | |
164 | gPriorityManager.Increment (fPriority); |
165 | |
166 | #endif |
167 | |
168 | } |
169 | |
170 | /*****************************************************************************/ |
171 | |
172 | dng_set_minimum_priority::~dng_set_minimum_priority () |
173 | { |
174 | |
175 | #if qDNGThreadSafe |
176 | |
177 | gPriorityManager.Decrement (fPriority); |
178 | |
179 | #endif |
180 | |
181 | } |
182 | |
183 | /*****************************************************************************/ |
184 | |
185 | dng_abort_sniffer::dng_abort_sniffer () |
186 | |
187 | : fPriority (dng_priority_maximum) |
188 | |
189 | { |
190 | |
191 | } |
192 | |
193 | /*****************************************************************************/ |
194 | |
195 | dng_abort_sniffer::~dng_abort_sniffer () |
196 | { |
197 | |
198 | } |
199 | |
200 | /*****************************************************************************/ |
201 | |
202 | void dng_abort_sniffer::SniffForAbort (dng_abort_sniffer *sniffer) |
203 | { |
204 | |
205 | if (sniffer) |
206 | { |
207 | |
208 | #if qDNGThreadSafe |
209 | |
210 | gPriorityManager.Wait (sniffer->Priority ()); |
211 | |
212 | #endif |
213 | |
214 | sniffer->Sniff (); |
215 | |
216 | } |
217 | |
218 | } |
219 | |
220 | /*****************************************************************************/ |
221 | |
222 | void dng_abort_sniffer::StartTask (const char * /* name */, |
223 | real64 /* fract */) |
224 | { |
225 | |
226 | } |
227 | |
228 | /*****************************************************************************/ |
229 | |
230 | void dng_abort_sniffer::EndTask () |
231 | { |
232 | |
233 | } |
234 | |
235 | /*****************************************************************************/ |
236 | |
237 | void dng_abort_sniffer::UpdateProgress (real64 /* fract */) |
238 | { |
239 | |
240 | } |
241 | |
242 | /*****************************************************************************/ |
243 |