Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / src / kernel / context / ContextThread.hpp
1 /* Copyright (c) 2009-2015. 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,
26           void_pfn_smxprocess_t cleanup_func,
27           smx_actor_t process, bool maestro =false);
28   ~ThreadContext() override;
29   void stop() override;
30   void suspend() override;
31   void attach_start() override;
32   void attach_stop() override;
33 private:
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;
40 private:
41   static void* wrapper(void *param);
42   static void* maestro_wrapper(void *param);
43 public:
44   void start();
45 };
46
47 class ThreadContextFactory : public ContextFactory {
48 public:
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;
55
56   // Optional methods:
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;
59 };
60
61 }}} // namespace
62
63 #endif