1/*
2 * Copyright (c) 2018, 2019, 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 */
23
24#include "precompiled.hpp"
25#include "gc/shared/workgroup.hpp"
26#include "gc/z/zRuntimeWorkers.hpp"
27#include "gc/z/zThread.hpp"
28#include "runtime/mutexLocker.hpp"
29
30class ZRuntimeWorkersInitializeTask : public AbstractGangTask {
31private:
32 const uint _nworkers;
33 uint _started;
34 Monitor _monitor;
35
36public:
37 ZRuntimeWorkersInitializeTask(uint nworkers) :
38 AbstractGangTask("ZRuntimeWorkersInitializeTask"),
39 _nworkers(nworkers),
40 _started(0),
41 _monitor(Monitor::leaf,
42 "ZRuntimeWorkersInitialize",
43 false /* allow_vm_block */,
44 Monitor::_safepoint_check_never) {}
45
46 virtual void work(uint worker_id) {
47 // Register as runtime worker
48 ZThread::set_runtime_worker();
49
50 // Wait for all threads to start
51 MonitorLocker ml(&_monitor, Monitor::_no_safepoint_check_flag);
52 if (++_started == _nworkers) {
53 // All threads started
54 ml.notify_all();
55 } else {
56 while (_started != _nworkers) {
57 ml.wait();
58 }
59 }
60 }
61};
62
63ZRuntimeWorkers::ZRuntimeWorkers() :
64 _workers("RuntimeWorker",
65 nworkers(),
66 false /* are_GC_task_threads */,
67 false /* are_ConcurrentGC_threads */) {
68
69 log_info(gc, init)("Runtime Workers: %u parallel", nworkers());
70
71 // Initialize worker threads
72 _workers.initialize_workers();
73 _workers.update_active_workers(nworkers());
74 if (_workers.active_workers() != nworkers()) {
75 vm_exit_during_initialization("Failed to create ZRuntimeWorkers");
76 }
77
78 // Execute task to register threads as runtime workers. This also
79 // helps reduce latency in early safepoints, which otherwise would
80 // have to take on any warmup costs.
81 ZRuntimeWorkersInitializeTask task(nworkers());
82 _workers.run_task(&task);
83}
84
85uint ZRuntimeWorkers::nworkers() const {
86 return ParallelGCThreads;
87}
88
89WorkGang* ZRuntimeWorkers::workers() {
90 return &_workers;
91}
92
93void ZRuntimeWorkers::threads_do(ThreadClosure* tc) const {
94 _workers.threads_do(tc);
95}
96
97void ZRuntimeWorkers::print_threads_on(outputStream* st) const {
98 _workers.print_worker_threads_on(st);
99}
100