Logo AND Algorithmique Numérique Distribuée

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