Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6897767f37d66fc541950edf287c8fb986506075
[simgrid.git] / src / kernel / context / ContextSwapped.cpp
1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/context/context_private.hpp"
7 #include "src/simix/ActorImpl.hpp"
8 #include "src/simix/smx_private.hpp"
9
10 #include "ContextSwapped.hpp"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
13
14 namespace simgrid {
15 namespace kernel {
16 namespace context {
17
18 unsigned long SwappedContext::process_index_;
19
20 SwappedContext* SwappedContext::maestro_context_ = nullptr;
21
22 void SwappedContext::suspend()
23 {
24   /* determine the next context */
25   SwappedContext* next_context;
26   unsigned long int i = process_index_;
27   process_index_++;
28
29   if (i < simix_global->process_to_run.size()) {
30     /* execute the next process */
31     XBT_DEBUG("Run next process");
32     next_context = static_cast<SwappedContext*>(simix_global->process_to_run[i]->context_);
33   } else {
34     /* all processes were run, return to maestro */
35     XBT_DEBUG("No more process to run");
36     next_context = static_cast<SwappedContext*>(get_maestro());
37   }
38   Context::set_current(next_context);
39   this->swap_into(next_context);
40 }
41
42 void SwappedContext::resume()
43 {
44   SwappedContext* old = static_cast<SwappedContext*>(self());
45   Context::set_current(this);
46   old->swap_into(this);
47 }
48
49 void SwappedContext::run_all()
50 {
51   if (simix_global->process_to_run.empty())
52     return;
53   smx_actor_t first_process = simix_global->process_to_run.front();
54   process_index_            = 1;
55   /* execute the first process */
56   static_cast<SwappedContext*>(first_process->context_)->resume();
57 }
58 } // namespace context
59 } // namespace kernel
60 } // namespace simgrid