Updated MxTasking example

This commit is contained in:
Michael Mueller
2022-07-11 12:00:09 +02:00
parent 0c1f727871
commit e5b58e1eb6
6 changed files with 82 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
build "core init timer app/hello_mxtask"
create_boot_directory
install_config {
<config>
<parent-provides>
@@ -12,9 +13,7 @@ install_config {
<service name="IRQ"/>
<service name="IO_MEM"/>
<service name="IO_PORT"/>
<service name="CAP"/>
<service name="RM"/>
<service name="SIGNAL"/>
</parent-provides>
<default-route>
<any-service><parent/><any-child/></any-service>
@@ -28,16 +27,19 @@ install_config {
</route>
</start>
<start name="hello_mxtask">
<resource name="RAM" quantum="10M"/>
<resource name="RAM" quantum="8000M"/>
<route>
<service name="Timer"> <child name="timer"/> </service>
<any-service><parent/><any-child/></any-service>
<any-service> <parent/> <any-child/> </any-service>
</route>
<config>
<libc stdout="/dev/log" stderr="/dev/log"/>
</config>
</start>
</config>
}
set boot_modules {
core init timer vfs.lib.so ld.lib.so libm.lib.so libc.lib.so stdcxx.lib.so mxtasking.lib.so hello_mxtask
core init timer vfs.lib.so ld.lib.so libm.lib.so libc.lib.so stdcxx.lib.so hello_mxtask
}
build_boot_image $boot_modules
append qemu_args "-nographic -m 64"

View File

@@ -22,31 +22,46 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */
#include <base/component.h>
#include <mx/tasking/runtime.h>
#include <libc/component.h>
//#include <iostream>
#include <mx/tasking/runtime.h>
//#include <mx/system/environment.h>
#include <mx/system/topology.h>
class HelloWorldTask : public mx::tasking::TaskInterface
{
public:
constexpr HelloWorldTask() = default;
~HelloWorldTask() override = default;
mx::tasking::TaskResult execute(const std::uint16_t /*core_id*/, const std::uint16_t /*channel_id*/) override
mx::tasking::TaskResult execute(const std::uint16_t core_id, const std::uint16_t channel_id) override
{
std::cout << "Hello World" << std::endl;
//std::cout << "Hello World" << std::endl;
Genode::log("Hello world");
// Stop MxTasking runtime after this task.
return mx::tasking::TaskResult::make_stop();
}
};
void Component::construct(Genode::Env &env)
void Libc::Component::construct(Libc::Env &env)
{
// Define which cores will be used (1 core here).
const auto cores = mx::util::core_set::build(1);
mx::system::Environment::env = env;
{ // Scope for the MxTasking runtime.
Genode::log("Starting MxTasking ...");
mx::system::Environment::set_env(&env);
Libc::with_libc([&] () { // Scope for the MxTasking runtime.
//mx::system::Environment::env = &env;
Genode::log("Initialized system environment for MxTasking");
Genode::log("Running on core ", mx::system::topology::core_id());
const auto cores = mx::util::core_set::build(1);
// Create a runtime for the given cores.
mx::tasking::runtime_guard _{cores};
@@ -57,8 +72,15 @@ void Component::construct(Genode::Env &env)
// Annotate the task to run on the first core.
hello_world_task->annotate(cores.front());
Genode::log("Created new task.");
auto *task2 = mx::tasking::runtime::new_task<HelloWorldTask>(cores.front());
task2->annotate(cores.front());
// Schedule the task.
Genode::log("Spawning new task.");
mx::tasking::runtime::spawn(*hello_world_task);
}
mx::tasking::runtime::spawn(*task2);
Genode::log("Task spawned.");
});
}

View File

@@ -1,4 +1,5 @@
TARGET = hello_mxtask
SRC_CC = main.cc
LIBS += base libm libc stdcxx mxtasking
CXXFLAGS += -Wno-error
LIBS += base libc stdcxx mxtasking
CC_OPT += -Wno-error -fno-aligned-new
CC_CXX_WARN_STRICT =

View File

@@ -1,9 +1,12 @@
#include <thread>
#include <base/component.h>
#include <libc/component.h>
#include <base/log.h>
#include <chrono>
#include <memory>
#include <cstdint>
#include <cstdlib>
#include <vector>
#include <unistd.h>
namespace Posix_playground {
class Chrono_thread;
@@ -19,12 +22,19 @@ class Posix_playground::Chrono_thread {
void execute()
{
std::chrono::time_point<std::chrono::steady_clock> start;
std::chrono::time_point<std::chrono::steady_clock> end;
while (true) {
Genode::log("Pong from Thread ", _id);
auto start = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::milliseconds(_id * 1000)));
auto end = std::chrono::steady_clock::now();
Genode::log("Thread ", _id, " woke up after ", std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count());
if (&this->_id == nullptr) {
Genode::log("WTF? this is a nullptr!");
return;
}
Genode::log("Pong from Thread ", this->_id);
start = std::chrono::steady_clock::now();
sleep(this->_id);
end = std::chrono::steady_clock::now();
Genode::log("Thread ", _id, " woke up after ", std::chrono::duration_cast<std::chrono::seconds>(end - start).count());
}
}
};
@@ -34,16 +44,32 @@ int main(void) {
Genode::log("Let's start some threads");
std::vector<Posix_playground::Chrono_thread*> thread_objs(4);
std::vector<Posix_playground::Chrono_thread*> thread_objs(5);
std::vector<std::thread*> thread_list(4);
for (std::uint16_t i = 1; i < 4; i++) {
thread_objs[i] = new Posix_playground::Chrono_thread(i);
auto thread = new std::thread([&]
Genode::log("Let's use aligned memory for threads objects.");
for (int i = 0; i < 3; i++) {
Posix_playground::Chrono_thread *thread_obj;
if(posix_memalign((void**)(&thread_obj), 64, sizeof(Posix_playground::Chrono_thread))) {
Genode::error("Could not allocate thread object ", i);
continue;
}
Genode::log("Thread object ", (i+1), " is at address ", (void*)(thread_obj));
thread_objs[i] = new (thread_obj) Posix_playground::Chrono_thread((std::uint16_t)(i+1));
auto thread = new std::thread([thread_objs, i]
{ thread_objs[i]->execute(); });
thread_list.push_back(thread);
thread_list[i] = thread;
//thread->join();
}
for (auto thread : thread_list) {
thread->join();
}
while(true);
return 0;
}
}

View File

@@ -1,5 +1,5 @@
TARGET = posix_playground
SRC_CC = main.cc
LIBS += base posix libm libc stdcxx
CXXFLAGS += -Wno-error
CC_OPT += -Wno-error -Wno-permissive -fpermissive

View File

@@ -48,7 +48,7 @@ class Thread_test::Tester
private:
Env &_env;
Heap _heap{_env.ram(), _env.rm()};
Range_allocator _heap; //{_env.ram(), _env.rm()};
Thread_list _threads{};
public:
@@ -68,7 +68,7 @@ public:
for (unsigned i = 1; i < space.total(); i++)
{
Affinity::Location location = env.cpu().affinity_space().location_of_index(i);
Test_thread *thread = new (_heap) Test_thread(env, (uint16_t)i, location);
Test_thread *thread = new (_heap.alloc_aligned(sizeof(Test_thread), 64)) Test_thread(env, (uint16_t)i, location);
thread->start();
_threads.insert(&thread->_list_element);