Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SIMIX_comm_destroy() -> simix::Comm::unref()
[simgrid.git] / src / simix / SynchroComm.cpp
1 /* Copyright (c) 2007-2016. 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/simix/SynchroComm.hpp"
7 #include "src/surf/surf_interface.hpp"
8 #include "src/simix/smx_network_private.h"
9 #include "simgrid/modelchecker.h"
10 #include "src/mc/mc_replay.h"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_network);
13
14 simgrid::simix::Comm::Comm(e_smx_comm_type_t _type) {
15   state = SIMIX_WAITING;
16   this->type = _type;
17   refcount = 1;
18   src_data=NULL;
19   dst_data=NULL;
20
21   XBT_DEBUG("Create communicate synchro %p", this);
22 }
23 void simgrid::simix::Comm::suspend()
24 {
25   /* FIXME: shall we suspend also the timeout synchro? */
26   if (surf_comm)
27     surf_comm->suspend();
28   /* in the other case, the action will be suspended on creation, in SIMIX_comm_start() */
29
30 }
31
32 void simgrid::simix::Comm::resume()
33 {
34   /*FIXME: check what happen with the timeouts */
35   if (surf_comm)
36     surf_comm->resume();
37   /* in the other case, the synchro were not really suspended yet, see SIMIX_comm_suspend() and SIMIX_comm_start() */
38 }
39
40 void simgrid::simix::Comm::cancel()
41 {
42   /* if the synchro is a waiting state means that it is still in a mbox */
43   /* so remove from it and delete it */
44   if (state == SIMIX_WAITING) {
45     SIMIX_mbox_remove(mbox, this);
46     state = SIMIX_CANCELED;
47   }
48   else if (!MC_is_active() /* when running the MC there are no surf actions */
49            && !MC_record_replay_is_active()
50            && (state == SIMIX_READY || state == SIMIX_RUNNING)) {
51
52     surf_comm->cancel();
53   }
54 }
55
56 /**  @brief get the amount remaining from the communication */
57 double simgrid::simix::Comm::remains()
58 {
59   switch (state) {
60
61   case SIMIX_RUNNING:
62     return surf_comm->getRemains();
63     break;
64
65   case SIMIX_WAITING:
66   case SIMIX_READY:
67     return 0; /*FIXME: check what should be returned */
68     break;
69
70   default:
71     return 0; /*FIXME: is this correct? */
72     break;
73   }
74 }
75
76 void simgrid::simix::Comm::unref()
77 {
78   XBT_DEBUG("Destroy synchro %p (refcount: %d), state: %d", this, refcount, (int)state);
79
80   xbt_assert(refcount > 0,
81       "This comm has a negative refcount! You must not call test() or wait() more than once on a given communication.");
82
83   refcount--;
84   if (refcount > 0)
85       return;
86   XBT_DEBUG("Really free communication %p; refcount is now %d", this, refcount);
87
88   cleanupSurf();
89
90   if (detached && state != SIMIX_DONE) {
91     /* the communication has failed and was detached:
92      * we have to free the buffer */
93     if (clean_fun)
94       clean_fun(src_buff);
95     src_buff = NULL;
96   }
97
98   if(mbox)
99     SIMIX_mbox_remove(mbox, this);
100
101   delete this;
102 }
103
104 /** @brief This is part of the cleanup process, probably an internal command */
105 void simgrid::simix::Comm::cleanupSurf()
106 {
107   if (surf_comm){
108     surf_comm->unref();
109     surf_comm = NULL;
110   }
111
112   if (src_timeout){
113     src_timeout->unref();
114     src_timeout = NULL;
115   }
116
117   if (dst_timeout){
118     dst_timeout->unref();
119     dst_timeout = NULL;
120   }
121 }