1 /* Copyright (c) 2009-2015. 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>
19 class ThreadContextFactory;
21 class ThreadContext : public AttachContext {
23 friend ThreadContextFactory;
24 ThreadContext(std::function<void()> code,
25 void_pfn_smxprocess_t cleanup_func,
26 smx_process_t process, bool maestro =false);
29 void suspend() override;
30 void attach_start() override;
31 void attach_stop() override;
33 /** A portable thread */
34 xbt_os_thread_t thread_ = nullptr;
35 /** Semaphore used to schedule/yield the process */
36 xbt_os_sem_t begin_ = nullptr;
37 /** Semaphore used to schedule/unschedule */
38 xbt_os_sem_t end_ = nullptr;
40 static void* wrapper(void *param);
41 static void* maestro_wrapper(void *param);
46 class ThreadContextFactory : public ContextFactory {
48 ThreadContextFactory();
49 ~ThreadContextFactory();
50 virtual ThreadContext* create_context(std::function<void()> code,
51 void_pfn_smxprocess_t cleanup_func, smx_process_t process) override;
52 void run_all() override;
53 ThreadContext* self() override;
56 ThreadContext* attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process) override;
57 ThreadContext* create_maestro(std::function<void()> code, smx_process_t process) override;