1 /* Copyright (c) 2009-2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* \file ThreadContext.hpp Context switching with native threads */
9 #ifndef SIMGRID_SIMIX_THREAD_CONTEXT_HPP
10 #define SIMGRID_SIMIX_THREAD_CONTEXT_HPP
12 #include <simgrid/simix.hpp>
20 class ThreadContextFactory;
22 class ThreadContext : public AttachContext {
24 friend ThreadContextFactory;
25 ThreadContext(std::function<void()> code,
26 void_pfn_smxprocess_t cleanup_func,
27 smx_actor_t process, bool maestro =false);
28 ~ThreadContext() override;
30 void suspend() override;
31 void attach_start() override;
32 void attach_stop() override;
34 /** A portable thread */
35 xbt_os_thread_t thread_ = nullptr;
36 /** Semaphore used to schedule/yield the process */
37 xbt_os_sem_t begin_ = nullptr;
38 /** Semaphore used to schedule/unschedule */
39 xbt_os_sem_t end_ = nullptr;
41 static void* wrapper(void *param);
42 static void* maestro_wrapper(void *param);
47 class ThreadContextFactory : public ContextFactory {
49 ThreadContextFactory();
50 ~ThreadContextFactory() override;
51 ThreadContext* create_context(std::function<void()> code,
52 void_pfn_smxprocess_t cleanup_func, smx_actor_t process) override;
53 void run_all() override;
54 ThreadContext* self() override;
57 ThreadContext* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process) override;
58 ThreadContext* create_maestro(std::function<void()> code, smx_actor_t process) override;