@@ -250,48 +250,30 @@ void CompressionBackend::deactivate() {
250
250
ml.notify_all ();
251
251
}
252
252
253
- // Wait for the threads to drain the compression work list.
253
+ // Wait for the threads to drain the compression work list and do some work yourself .
254
254
while (!_to_compress.is_empty ()) {
255
- // If we have no threads, compress the current one itself.
256
- if (_nr_of_threads == 0 ) {
257
- MutexUnlocker mu (_lock, Mutex::_no_safepoint_check_flag);
258
- thread_loop (true );
259
- } else {
260
- ml.wait ();
261
- }
255
+ do_foreground_work ();
262
256
}
263
257
264
258
_active = false ;
265
259
ml.notify_all ();
266
260
}
267
261
268
- void CompressionBackend::thread_loop (bool single_run) {
269
- // Register if this is a worker thread.
270
- if (!single_run) {
262
+ void CompressionBackend::thread_loop () {
263
+ {
271
264
MonitorLocker ml (_lock, Mutex::_no_safepoint_check_flag);
272
265
_nr_of_threads++;
273
266
}
274
267
275
- while (true ) {
276
- WriteWork* work = get_work ();
277
-
278
- if (work == NULL ) {
279
- assert (!single_run, " Should never happen for single thread" );
280
- MonitorLocker ml (_lock, Mutex::_no_safepoint_check_flag);
281
- _nr_of_threads--;
282
- assert (_nr_of_threads >= 0 , " Too many threads finished" );
283
- ml.notify_all ();
284
-
285
- return ;
286
- } else {
287
- do_compress (work);
288
- finish_work (work);
289
- }
290
-
291
- if (single_run) {
292
- return ;
293
- }
268
+ WriteWork* work;
269
+ while ((work = get_work ()) != NULL ) {
270
+ do_compress (work);
271
+ finish_work (work);
294
272
}
273
+
274
+ MonitorLocker ml (_lock, Mutex::_no_safepoint_check_flag);
275
+ _nr_of_threads--;
276
+ assert (_nr_of_threads >= 0 , " Too many threads finished" );
295
277
}
296
278
297
279
void CompressionBackend::set_error (char const * new_error) {
@@ -363,6 +345,16 @@ void CompressionBackend::free_work_list(WorkList* list) {
363
345
}
364
346
}
365
347
348
+ void CompressionBackend::do_foreground_work () {
349
+ assert (!_to_compress.is_empty (), " Must have work to do" );
350
+ assert (_lock->owned_by_self (), " Must have the lock" );
351
+
352
+ WriteWork* work = _to_compress.remove_first ();
353
+ MutexUnlocker mu (_lock, Mutex::_no_safepoint_check_flag);
354
+ do_compress (work);
355
+ finish_work (work);
356
+ }
357
+
366
358
WriteWork* CompressionBackend::get_work () {
367
359
MonitorLocker ml (_lock, Mutex::_no_safepoint_check_flag);
368
360
@@ -405,9 +397,7 @@ void CompressionBackend::get_new_buffer(char** buffer, size_t* used, size_t* max
405
397
_unused.add_first (work);
406
398
}
407
399
} else if (!_to_compress.is_empty () && (_nr_of_threads == 0 )) {
408
- // If we have no threads, compress the current one itself.
409
- MutexUnlocker mu (_lock, Mutex::_no_safepoint_check_flag);
410
- thread_loop (true );
400
+ do_foreground_work ();
411
401
} else {
412
402
ml.wait ();
413
403
}
0 commit comments