Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ThreadContext: wrapper and maestro_wrapper are nearly the same. Merge.
[simgrid.git] / src / kernel / context / ContextThread.hpp
1 /* Copyright (c) 2009-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* \file ThreadContext.hpp Context switching with native threads */
8
9 #ifndef SIMGRID_SIMIX_THREAD_CONTEXT_HPP
10 #define SIMGRID_SIMIX_THREAD_CONTEXT_HPP
11
12 #include <simgrid/simix.hpp>
13
14
15 namespace simgrid {
16 namespace kernel {
17 namespace context {
18
19 class ThreadContext;
20 class ThreadContextFactory;
21
22 class ThreadContext : public AttachContext {
23 public:
24   friend ThreadContextFactory;
25   ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process, bool maestro);
26   ~ThreadContext() override;
27   void stop() override;
28   void suspend() override;
29   void attach_start() override;
30   void attach_stop() override;
31 private:
32   /** A portable thread */
33   xbt_os_thread_t thread_ = nullptr;
34   /** Semaphore used to schedule/yield the process */
35   xbt_os_sem_t begin_ = nullptr;
36   /** Semaphore used to schedule/unschedule */
37   xbt_os_sem_t end_ = nullptr;
38   bool is_maestro_;
39
40   static void* wrapper(void *param);
41 public:
42   void start();
43   void yield();
44 };
45
46 class ThreadContextFactory : public ContextFactory {
47 public:
48   ThreadContextFactory();
49   ~ThreadContextFactory() override;
50   ThreadContext* create_context(std::function<void()> code,
51     void_pfn_smxprocess_t cleanup_func,  smx_actor_t process) override;
52   void run_all() override;
53   ThreadContext* self() override;
54
55   // Optional methods:
56   ThreadContext* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process) override;
57   ThreadContext* create_maestro(std::function<void()> code, smx_actor_t process) override;
58 };
59
60 }}} // namespace
61
62 #endif