Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
modernize a few simcall_execution_* calls
[simgrid.git] / include / simgrid / s4u / Activity.hpp
1 /* Copyright (c) 2006-2019. 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 #ifndef SIMGRID_S4U_ACTIVITY_HPP
7 #define SIMGRID_S4U_ACTIVITY_HPP
8
9 #include <simgrid/forward.h>
10 #include <xbt/signal.hpp>
11
12 namespace simgrid {
13 namespace s4u {
14
15 /** @brief Activities
16  *
17  * This class is the ancestor of every activities that an actor can undertake.
18  * That is, activities are all the things that do take time to the actor in the simulated world.
19  *
20  * They are somewhat linked but not identical to simgrid::kernel::resource::Action,
21  * that are stuff occurring on a resource:
22  *
23  * - A sequential execution activity encompasses 2 actions: one for the exec itself,
24  *   and a time-limited sleep used as timeout detector.
25  * - A point-to-point communication activity encompasses 3 actions: one for the comm itself
26  *   (which spans on all links of the path), and one infinite sleep used as failure detector
27  *   on both sender and receiver hosts.
28  * - Synchronization activities may possibly be connected to no action.
29  */
30 class XBT_PUBLIC Activity {
31   friend simgrid::s4u::Comm;
32   friend XBT_PUBLIC void intrusive_ptr_release(Comm * c);
33   friend XBT_PUBLIC void intrusive_ptr_add_ref(Comm * c);
34   friend simgrid::s4u::Exec;
35   friend XBT_PUBLIC void intrusive_ptr_release(Exec * e);
36   friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec * e);
37   friend simgrid::s4u::Io;
38   friend XBT_PUBLIC void intrusive_ptr_release(Io* i);
39   friend XBT_PUBLIC void intrusive_ptr_add_ref(Io* i);
40
41 protected:
42   Activity()  = default;
43   virtual ~Activity() = default;
44
45 public:
46 #ifndef DOXYGEN
47   Activity(Activity const&) = delete;
48   Activity& operator=(Activity const&) = delete;
49 #endif
50
51   enum class State { INITED = 0, STARTED, CANCELED, ERRORED, FINISHED };
52
53   /** Starts a previously created activity.
54    *
55    * This function is optional: you can call wait() even if you didn't call start()
56    */
57   virtual Activity* start() = 0;
58   /** Tests whether the given activity is terminated yet. This is a pure function. */
59   //virtual bool test()=0;
60   /** Blocks until the activity is terminated */
61   virtual Activity* wait() = 0;
62   /** Blocks until the activity is terminated, or until the timeout is elapsed
63    *  Raises: timeout exception.*/
64   virtual Activity* wait_for(double timeout) = 0;
65   /** Blocks until the activity is terminated, or until the time limit is reached
66    * Raises: timeout exception. */
67   void wait_until(double time_limit);
68
69   /** Cancel that activity */
70   virtual Activity* cancel() = 0;
71   /** Retrieve the current state of the activity */
72   Activity::State get_state() { return state_; }
73   /** Returns whether this activity is completed */
74   virtual bool test() = 0;
75
76   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
77   virtual double get_remaining();
78
79   /** Set the [remaining] amount of work that this Activity will entail
80    *
81    * It is forbidden to change the amount of work once the Activity is started */
82   Activity* set_remaining(double remains);
83
84   /** Put some user data onto the Activity */
85   Activity* set_user_data(void* data)
86   {
87     user_data_ = data;
88     return this;
89   }
90   /** Retrieve the user data of the Activity */
91   void* get_user_data() { return user_data_; }
92
93   simgrid::kernel::activity::ActivityImplPtr get_impl() { return pimpl_; }
94
95 #ifndef DOXYGEN
96   XBT_ATTRIB_DEPRECATED_v324("Please use Activity::wait_for()") virtual void wait(double timeout) = 0;
97   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::get_state()") Activity::State getState() { return state_; }
98   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::get_remaining()") double getRemains() { return get_remaining(); }
99   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::set_remaining()") Activity* setRemains(double remains)
100   {
101     return set_remaining(remains);
102   }
103   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::set_user_data()") Activity* setUserData(void* data)
104   {
105     return set_user_data(data);
106   }
107   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::get_user_data()") void* getUserData() { return user_data_; }
108 #endif
109
110 private:
111   simgrid::kernel::activity::ActivityImplPtr pimpl_ = nullptr;
112   Activity::State state_                            = Activity::State::INITED;
113   double remains_                                   = 0;
114   void* user_data_                                  = nullptr;
115 }; // class
116
117 }}; // Namespace simgrid::s4u
118
119 #endif /* SIMGRID_S4U_ACTIVITY_HPP */