Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6500b7964011d330b8f398e6f59de108df4ecd82
[simgrid.git] / include / simgrid / s4u / async.hpp
1 /* Copyright (c) 2006-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 #ifndef SIMGRID_S4U_ASYNC_HPP
8 #define SIMGRID_S4U_ASYNC_HPP
9
10 #include <stdlib.h>
11 #include <xbt/base.h>
12 #include <xbt/misc.h>
13
14 #include <simgrid/s4u/forward.hpp>
15
16 SG_BEGIN_DECL();
17 typedef enum {
18   inited, started, finished
19 } e_s4u_async_state_t;
20 SG_END_DECL();
21
22 namespace simgrid {
23 namespace s4u {
24
25 /** @brief Asynchronous Actions
26  *
27  * This class is the ancestor of every asynchronous actions, that is, of actions that do take time in the simulated world.
28  */
29 XBT_PUBLIC_CLASS Async {
30   friend Comm;
31 protected:
32   Async();
33   virtual ~Async();
34   
35 private:
36   struct s_smx_synchro *p_inferior = NULL;
37
38 private:
39   e_s4u_async_state_t p_state = inited;
40 public:
41   /** Starts a previously created async.
42    *
43    * This function is optional: you can call wait() even if you didn't call start()
44    */
45   virtual void start()=0;
46   /** Tests whether the given async is terminated yet */
47   //virtual bool test()=0;
48   /** Blocks until the async is terminated */
49   virtual void wait()=0;
50   /** Blocks until the async is terminated, or until the timeout is elapsed
51    *  Raises: timeout exception.*/
52   virtual void wait(double timeout)=0;
53   /** Cancel that async */
54   //virtual void cancel();
55   /** Retrieve the current state of the async */
56   e_s4u_async_state_t getState() {return p_state;}
57
58 private:
59   double p_remains = 0;
60 public:
61   /** Get the remaining amount of work that this Async entails. When it's 0, it's done. */
62   double getRemains();
63   /** Set the [remaining] amount of work that this Async will entail
64    *
65    * It is forbidden to change the amount of work once the Async is started */
66   void setRemains(double remains);
67
68 private:
69   void *p_userData = NULL;
70 public:
71   /** Put some user data onto the Async */
72   void setUserData(void *data) {p_userData=data;}
73   /** Retrieve the user data of the Async */
74   void *getUserData() { return p_userData; }
75 }; // class
76
77 }}; // Namespace simgrid::s4u
78
79 #endif /* SIMGRID_S4U_ASYNC_HPP */