8278351: Add function to retrieve worker_id from any context

Reviewed-by: eosterlund, kbarrett, ayang
This commit is contained in:
Per Liden 2021-12-15 10:09:02 +00:00
parent 758fe9bed3
commit 7adf7f3353
5 changed files with 25 additions and 26 deletions

View File

@ -821,7 +821,7 @@ inline DiscoveredList* ReferenceProcessor::get_discovered_list(ReferenceType rt)
if (_discovery_is_mt) {
// During a multi-threaded discovery phase,
// each thread saves to its "own" list.
id = WorkerThread::current()->id();
id = WorkerThread::worker_id();
} else {
// single-threaded discovery, we save in round-robin
// fashion to each of the lists.

View File

@ -59,8 +59,9 @@ void WorkerTaskDispatcher::worker_run_task() {
// Wait for the coordinator to dispatch a task.
_start_semaphore.wait();
// Get worker id.
// Get and set worker id.
const uint worker_id = Atomic::fetch_and_add(&_started, 1u);
WorkerThread::set_worker_id(worker_id);
// Run task.
GCIdMark gc_id_mark(_task->gc_id());
@ -91,18 +92,20 @@ void WorkerThreads::initialize_workers() {
}
}
WorkerThread* WorkerThreads::create_worker(uint id) {
WorkerThread* WorkerThreads::create_worker(uint name_suffix) {
if (is_init_completed() && InjectGCWorkerCreationFailure) {
return NULL;
}
WorkerThread* const worker = new WorkerThread(_name, id, &_dispatcher);
WorkerThread* const worker = new WorkerThread(_name, name_suffix, &_dispatcher);
if (!os::create_thread(worker, os::gc_thread)) {
delete worker;
return NULL;
}
on_create_worker(worker);
os::start_thread(worker);
return worker;
@ -146,10 +149,11 @@ void WorkerThreads::run_task(WorkerTask* task, uint num_workers) {
run_task(task);
}
WorkerThread::WorkerThread(const char* name_prefix, uint id, WorkerTaskDispatcher* dispatcher) :
_dispatcher(dispatcher),
_id(id) {
set_name("%s#%d", name_prefix, id);
THREAD_LOCAL uint WorkerThread::_worker_id = UINT_MAX;
WorkerThread::WorkerThread(const char* name_prefix, uint name_suffix, WorkerTaskDispatcher* dispatcher) :
_dispatcher(dispatcher) {
set_name("%s#%u", name_prefix, name_suffix);
}
void WorkerThread::run() {

View File

@ -91,8 +91,10 @@ private:
uint _active_workers;
WorkerTaskDispatcher _dispatcher;
WorkerThread* create_worker(uint name_suffix);
protected:
virtual WorkerThread* create_worker(uint id);
virtual void on_create_worker(WorkerThread* worker) {}
public:
WorkerThreads(const char* name, uint max_workers);
@ -117,23 +119,19 @@ public:
};
class WorkerThread : public NamedThread {
friend class WorkerTaskDispatcher;
private:
static THREAD_LOCAL uint _worker_id;
WorkerTaskDispatcher* const _dispatcher;
const uint _id;
static void set_worker_id(uint worker_id) { _worker_id = worker_id; }
public:
static WorkerThread* current() {
return WorkerThread::cast(Thread::current());
}
static uint worker_id() { return _worker_id; }
static WorkerThread* cast(Thread* t) {
assert(t->is_Worker_thread(), "incorrect cast to WorkerThread");
return static_cast<WorkerThread*>(t);
}
WorkerThread(const char* name_prefix, uint id, WorkerTaskDispatcher* dispatcher);
uint id() const { return _id; }
WorkerThread(const char* name_prefix, uint which, WorkerTaskDispatcher* dispatcher);
bool is_Worker_thread() const override { return true; }
const char* type_name() const override { return "WorkerThread"; }

View File

@ -71,11 +71,9 @@ ShenandoahPushWorkerScope::~ShenandoahPushWorkerScope() {
assert(nworkers == _old_workers, "Must be able to restore");
}
WorkerThread* ShenandoahWorkerThreads::create_worker(uint id) {
WorkerThread* worker = WorkerThreads::create_worker(id);
void ShenandoahWorkerThreads::on_create_worker(WorkerThread* worker) {
ShenandoahThreadLocalData::create(worker);
if (_initialize_gclab) {
ShenandoahThreadLocalData::initialize_gclab(worker);
}
return worker;
}

View File

@ -60,9 +60,8 @@ public:
WorkerThreads(name, workers), _initialize_gclab(false) {
}
// Create a GC worker.
// We need to initialize gclab for dynamic allocated workers
WorkerThread* create_worker(uint id);
void on_create_worker(WorkerThread* worker);
void set_initialize_gclab() { assert(!_initialize_gclab, "Can only enable once"); _initialize_gclab = true; }
};