1 /*
2 * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
411 }
412
413 bool SubTasksDone::valid() {
414 return _tasks != NULL;
415 }
416
417 void SubTasksDone::clear() {
418 for (uint i = 0; i < _n_tasks; i++) {
419 _tasks[i] = 0;
420 }
421 _threads_completed = 0;
422 #ifdef ASSERT
423 _claimed = 0;
424 #endif
425 }
426
427 bool SubTasksDone::is_task_claimed(uint t) {
428 assert(t < _n_tasks, "bad task id.");
429 uint old = _tasks[t];
430 if (old == 0) {
431 old = Atomic::cmpxchg(1, &_tasks[t], 0);
432 }
433 assert(_tasks[t] == 1, "What else?");
434 bool res = old != 0;
435 #ifdef ASSERT
436 if (!res) {
437 assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
438 Atomic::inc((volatile jint*) &_claimed);
439 }
440 #endif
441 return res;
442 }
443
444 void SubTasksDone::all_tasks_completed(uint n_threads) {
445 jint observed = _threads_completed;
446 jint old;
447 do {
448 old = observed;
449 observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
450 } while (observed != old);
451 // If this was the last thread checking in, clear the tasks.
452 uint adjusted_thread_count = (n_threads == 0 ? 1 : n_threads);
453 if (observed + 1 == (jint)adjusted_thread_count) {
454 clear();
455 }
456 }
457
458
459 SubTasksDone::~SubTasksDone() {
460 if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks);
461 }
462
463 // *** SequentialSubTasksDone
464
465 void SequentialSubTasksDone::clear() {
466 _n_tasks = _n_claimed = 0;
467 _n_threads = _n_completed = 0;
468 }
469
470 bool SequentialSubTasksDone::valid() {
471 return _n_threads > 0;
472 }
473
474 bool SequentialSubTasksDone::is_task_claimed(uint& t) {
475 t = _n_claimed;
476 while (t < _n_tasks) {
477 jint res = Atomic::cmpxchg(t+1, &_n_claimed, t);
478 if (res == (jint)t) {
479 return false;
480 }
481 t = res;
482 }
483 return true;
484 }
485
486 bool SequentialSubTasksDone::all_tasks_completed() {
487 uint complete = _n_completed;
488 while (true) {
489 uint res = Atomic::cmpxchg(complete+1, &_n_completed, complete);
490 if (res == complete) {
491 break;
492 }
493 complete = res;
494 }
495 if (complete+1 == _n_threads) {
496 clear();
497 return true;
498 }
|
1 /*
2 * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
411 }
412
413 bool SubTasksDone::valid() {
414 return _tasks != NULL;
415 }
416
417 void SubTasksDone::clear() {
418 for (uint i = 0; i < _n_tasks; i++) {
419 _tasks[i] = 0;
420 }
421 _threads_completed = 0;
422 #ifdef ASSERT
423 _claimed = 0;
424 #endif
425 }
426
427 bool SubTasksDone::is_task_claimed(uint t) {
428 assert(t < _n_tasks, "bad task id.");
429 uint old = _tasks[t];
430 if (old == 0) {
431 old = Atomic::cmpxchg(1u, &_tasks[t], 0u);
432 }
433 assert(_tasks[t] == 1, "What else?");
434 bool res = old != 0;
435 #ifdef ASSERT
436 if (!res) {
437 assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
438 Atomic::inc((volatile jint*) &_claimed);
439 }
440 #endif
441 return res;
442 }
443
444 void SubTasksDone::all_tasks_completed(uint n_threads) {
445 uint observed = _threads_completed;
446 uint old;
447 do {
448 old = observed;
449 observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
450 } while (observed != old);
451 // If this was the last thread checking in, clear the tasks.
452 uint adjusted_thread_count = (n_threads == 0 ? 1 : n_threads);
453 if (observed + 1 == adjusted_thread_count) {
454 clear();
455 }
456 }
457
458
459 SubTasksDone::~SubTasksDone() {
460 if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks);
461 }
462
463 // *** SequentialSubTasksDone
464
465 void SequentialSubTasksDone::clear() {
466 _n_tasks = _n_claimed = 0;
467 _n_threads = _n_completed = 0;
468 }
469
470 bool SequentialSubTasksDone::valid() {
471 return _n_threads > 0;
472 }
473
474 bool SequentialSubTasksDone::is_task_claimed(uint& t) {
475 t = _n_claimed;
476 while (t < _n_tasks) {
477 uint res = Atomic::cmpxchg(t+1, &_n_claimed, t);
478 if (res == t) {
479 return false;
480 }
481 t = res;
482 }
483 return true;
484 }
485
486 bool SequentialSubTasksDone::all_tasks_completed() {
487 uint complete = _n_completed;
488 while (true) {
489 uint res = Atomic::cmpxchg(complete+1, &_n_completed, complete);
490 if (res == complete) {
491 break;
492 }
493 complete = res;
494 }
495 if (complete+1 == _n_threads) {
496 clear();
497 return true;
498 }
|