From 1a1032a3351b42d4a8a5f5c3554be65ef53764b2 Mon Sep 17 00:00:00 2001
From: Florian Schmaus <flow@cs.fau.de>
Date: Fri, 19 Mar 2021 19:56:51 +0100
Subject: [PATCH] Provider worker ID as argument in "new worker" hook

---
 emper/ContextManager.cpp                           | 5 +++--
 emper/MemoryManager.hpp                            | 5 +++--
 emper/Runtime.cpp                                  | 2 +-
 emper/Runtime.hpp                                  | 6 ++++--
 emper/Scheduler.cpp                                | 4 ++--
 emper/Scheduler.hpp                                | 2 +-
 emper/strategies/AbstractWorkStealingScheduler.cpp | 2 +-
 emper/strategies/laws/LawsScheduler.cpp            | 6 ++----
 8 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/emper/ContextManager.cpp b/emper/ContextManager.cpp
index baab2fdf..a25687af 100644
--- a/emper/ContextManager.cpp
+++ b/emper/ContextManager.cpp
@@ -1,5 +1,5 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
-// Copyright © 2020 Florian Schmaus
+// Copyright © 2020-2021 Florian Schmaus
 #include "ContextManager.hpp"
 
 #include <cassert>	// for assert
@@ -9,12 +9,13 @@
 #include "Debug.hpp"			 // for LOGD
 #include "Dispatcher.hpp"	 // for Dispatcher
 #include "Runtime.hpp"		 // for Runtime
+#include "emper-common.h"
 #include "emper-config.h"	 // // IWYU pragma: keep
 
 class Fiber;
 
 ContextManager::ContextManager(Runtime& runtime) : MemoryManager(runtime), runtime(runtime) {
-	auto newWorkerHook = [this]() {
+	auto newWorkerHook = [this](ATTR_UNUSED workerid_t workerId) {
 		for (unsigned int i = 0; i < CONTEXT_MANAGER_FIRST_LAYER_QUEUE_SIZE * 2; ++i) {
 			auto* context = new Context(this->runtime.dispatcher.getDispatchLoop());
 			putFreeContext(context);
diff --git a/emper/MemoryManager.hpp b/emper/MemoryManager.hpp
index d9572eb7..17b533db 100644
--- a/emper/MemoryManager.hpp
+++ b/emper/MemoryManager.hpp
@@ -1,9 +1,10 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
-// Copyright © 2020 Florian Schmaus
+// Copyright © 2020-2021 Florian Schmaus
 #pragma once
 
 #include "Common.hpp"
 #include "Runtime.hpp"
+#include "emper-common.h"
 #include "lib/adt/BoundedBumpArray.hpp"
 #include "lib/adt/WsClQueue.hpp"
 
@@ -82,6 +83,6 @@ MemoryManager<T, WS_QUEUE_SIZE, WORKER_EXCLUSIVE_QUEUE_SIZE>::MemoryManager(Runt
 		: workerCount(runtime.getWorkerCount()) {
 	queues = new adt::WsClQueue<void*, WS_QUEUE_SIZE>*[workerCount];
 
-	auto newWorkerHook = [this]() { queues[Runtime::getWorkerId()] = &queue; };
+	auto newWorkerHook = [this](workerid_t workerId) { queues[workerId] = &queue; };
 	runtime.addNewWorkerHook(newWorkerHook);
 }
diff --git a/emper/Runtime.cpp b/emper/Runtime.cpp
index 282c3354..43ba9be7 100644
--- a/emper/Runtime.cpp
+++ b/emper/Runtime.cpp
@@ -217,7 +217,7 @@ auto Runtime::workerLoop(Worker* worker) -> void* {
 		DIE_MSG_ERRNO("pthread_setcanceltype() failed");
 	}
 
-	for (const auto& f : newWorkerHooks) f();
+	for (const auto& f : newWorkerHooks) f(worker->workerId);
 
 	workerLatch.count_down_and_wait();
 
diff --git a/emper/Runtime.hpp b/emper/Runtime.hpp
index efc1ea47..24a8c52a 100644
--- a/emper/Runtime.hpp
+++ b/emper/Runtime.hpp
@@ -48,7 +48,7 @@ class Runtime : public Logger<LogSubsystem::RUNTI> {
 
 	const workerid_t workerCount;
 
-	std::vector<std::function<void(void)>> newWorkerHooks;
+	std::vector<std::function<void(workerid_t)>> newWorkerHooks;
 
 	Latch workerLatch;
 
@@ -80,7 +80,9 @@ class Runtime : public Logger<LogSubsystem::RUNTI> {
 	static auto getDefaultWorkerCount() -> workerid_t;
 
  protected:
-	void addNewWorkerHook(const std::function<void(void)>& hook) { newWorkerHooks.push_back(hook); };
+	void addNewWorkerHook(const std::function<void(workerid_t)>& hook) {
+		newWorkerHooks.push_back(hook);
+	};
 
 	template <CallerEnvironment callerEnvironment = CallerEnvironment::EMPER>
 	inline void wakeupSleepingWorkers() {
diff --git a/emper/Scheduler.cpp b/emper/Scheduler.cpp
index 8db2ea0a..eb4dde0b 100644
--- a/emper/Scheduler.cpp
+++ b/emper/Scheduler.cpp
@@ -1,5 +1,5 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
-// Copyright © 2020 Florian Schmaus
+// Copyright © 2020-2021 Florian Schmaus
 #include "Scheduler.hpp"
 
 #include "CallerEnvironment.hpp"
@@ -7,7 +7,7 @@
 
 Scheduler::Scheduler(Runtime& runtime) : runtime(runtime) {}
 
-void Scheduler::addNewWorkerHook(const std::function<void(void)>& hook) {
+void Scheduler::addNewWorkerHook(const std::function<void(workerid_t)>& hook) {
 	runtime.addNewWorkerHook(hook);
 }
 
diff --git a/emper/Scheduler.hpp b/emper/Scheduler.hpp
index 9263f1e7..6ea42bc8 100644
--- a/emper/Scheduler.hpp
+++ b/emper/Scheduler.hpp
@@ -27,7 +27,7 @@ class Scheduler : public Logger<LogSubsystem::SCHED> {
 
 	virtual ~Scheduler() = default;
 
-	void addNewWorkerHook(const std::function<void(void)>& hook);
+	void addNewWorkerHook(const std::function<void(workerid_t)>& hook);
 
 	static inline auto getAffinityBuffer(Fiber& fiber) -> workeraffinity_t* {
 		return fiber.getAffinityBuffer();
diff --git a/emper/strategies/AbstractWorkStealingScheduler.cpp b/emper/strategies/AbstractWorkStealingScheduler.cpp
index a811f66b..94281a84 100644
--- a/emper/strategies/AbstractWorkStealingScheduler.cpp
+++ b/emper/strategies/AbstractWorkStealingScheduler.cpp
@@ -24,7 +24,7 @@ AbstractWorkStealingScheduler::AbstractWorkStealingScheduler(
 	const workerid_t workerCount = runtime.getWorkerCount();
 	queues = new AbstractWorkStealingScheduler::WsQueue<QUEUE_SIZE>*[workerCount];
 
-	auto newWorkerHook = [this]() { queues[Runtime::getWorkerId()] = &queue; };
+	auto newWorkerHook = [this](workerid_t workerId) { queues[workerId] = &queue; };
 	addNewWorkerHook(newWorkerHook);
 }
 
diff --git a/emper/strategies/laws/LawsScheduler.cpp b/emper/strategies/laws/LawsScheduler.cpp
index 3e72b841..3432f8e4 100644
--- a/emper/strategies/laws/LawsScheduler.cpp
+++ b/emper/strategies/laws/LawsScheduler.cpp
@@ -9,6 +9,7 @@
 #include "LawsStrategy.hpp"	 // IWYU pragma: keep
 #include "NextFiberResult.hpp"
 #include "Runtime.hpp"
+#include "emper-common.h"
 
 #define EMPER_OVERFLOW_QUEUE
 
@@ -19,10 +20,7 @@ LawsScheduler::LawsScheduler(Runtime& runtime, LawsStrategy& lawsStrategy)
 	const workerid_t workerCount = runtime.getWorkerCount();
 	priorityQueues = new LawsScheduler::LawsMpscQueue*[workerCount];
 
-	auto newWorkerHook = [this]() {
-		workerid_t workerId = Runtime::getWorkerId();
-		priorityQueues[workerId] = &priorityQueue;
-	};
+	auto newWorkerHook = [this](workerid_t workerId) { priorityQueues[workerId] = &priorityQueue; };
 	addNewWorkerHook(newWorkerHook);
 }
 
-- 
GitLab