@@ -99,27 +99,37 @@ void Engine::begin_pub_transaction()
99
99
// Only one publisher has to do this
100
100
std::unique_lock<sg4::Mutex> lock (*pub_mutex_);
101
101
if (not pub_transaction_in_progress_) {
102
- XBT_DEBUG (" Publish Transaction %u started by %s" , pub_transaction_id_, sg4::Actor::self ()->get_cname ());
103
102
pub_transaction_in_progress_ = true ;
104
103
if (not pub_barrier_) { // This is the first transaction.
105
104
if (not publishers_.empty ()) { // Assume all publishers have opened the stream and create a barrier
106
105
XBT_DEBUG (" Create a barrier for %zu publishers" , publishers_.size ());
107
106
pub_barrier_ = sg4::Barrier::create (publishers_.size ());
108
107
first_pub_transaction_started_->notify_all ();
109
108
}
109
+
110
110
} else {
111
111
// Wait for the completion of the Publish activities from the previous transaction
112
112
XBT_DEBUG (" Wait for the completion of %u publish activities from the previous transaction" ,
113
113
pub_transaction_.size ());
114
114
pub_transaction_.wait_all ();
115
115
XBT_DEBUG (" All on-flight publish activities are completed. Proceed with the current transaction." );
116
- pub_transaction_.clear ();
117
116
pub_transaction_id_++;
117
+ XBT_DEBUG (" %u sub activities pending" , sub_transaction_.size ());
118
118
if (pub_transaction_id_ >= sub_transaction_id_) {
119
+ pub_transaction_.clear ();
119
120
// We may have subscribers waiting for a transaction to be over. Notify them
120
- first_pub_transaction_completed_ ->notify_all ();
121
+ pub_transaction_completed_ ->notify_all ();
121
122
}
122
123
}
124
+ XBT_DEBUG (" Publish Transaction %u started by %s" , pub_transaction_id_, sg4::Actor::self ()->get_cname ());
125
+ }
126
+ if (type_ == Type::Staging) {
127
+ XBT_DEBUG (" Maybe I should wait: %zu subscribers and %u <= %u" , get_num_subscribers (), pub_transaction_id_, sub_transaction_id_ -1 );
128
+ while (get_num_subscribers () == 0 || pub_transaction_id_ < sub_transaction_id_ -1 ) {
129
+ XBT_DEBUG (" Wait" );
130
+ sub_transaction_started_->wait (lock);
131
+ }
132
+
123
133
}
124
134
}
125
135
@@ -151,7 +161,7 @@ void Engine::pub_close()
151
161
if (type_ == Type::File) {
152
162
if (get_num_subscribers () > 0 && pub_transaction_id_ >= sub_transaction_id_) {
153
163
// We may have subscribers waiting for a transaction to be over. Notify them
154
- first_pub_transaction_completed_ ->notify_all ();
164
+ pub_transaction_completed_ ->notify_all ();
155
165
}
156
166
}
157
167
}
@@ -183,39 +193,37 @@ void Engine::begin_sub_transaction()
183
193
// We have publishers on that stream, wait for them to complete a transaction first
184
194
if (type_ == Type::File && get_num_publishers () > 0 ) {
185
195
while (pub_transaction_id_ < sub_transaction_id_)
186
- first_pub_transaction_completed_ ->wait (lock);
196
+ pub_transaction_completed_ ->wait (lock);
187
197
}
188
-
189
198
if (not sub_transaction_in_progress_) {
199
+ if (type_ == Type::Staging && pub_transaction_id_ == sub_transaction_id_ -1 )
200
+ sub_transaction_started_->notify_all ();
190
201
XBT_DEBUG (" Subscribe Transaction %u started by %s" , sub_transaction_id_, sg4::Actor::self ()->get_cname ());
191
202
sub_transaction_in_progress_ = true ;
192
203
if (not sub_barrier_) { // This is the first transaction.
193
204
if (not subscribers_.empty ()) { // Assume all subscribers have opened the stream and create a barrier
194
205
XBT_DEBUG (" Create a barrier for %zu subscribers" , subscribers_.size ());
195
206
sub_barrier_ = sg4::Barrier::create (subscribers_.size ());
196
207
}
197
- } else {
198
- // wait for the completion of the Subscribe activities from the previous transaction
199
- XBT_DEBUG (" Wait for the completion of %u subscribe activities from the previous transaction" ,
200
- sub_transaction_.size ());
201
- sub_transaction_.wait_all ();
202
- XBT_DEBUG (" All on-flight subscribe activities are completed. Proceed with the current transaction." );
203
- sub_transaction_.clear ();
204
208
}
205
209
}
206
210
}
207
211
208
212
void Engine::end_sub_transaction ()
209
213
{
210
214
if (sub_barrier_ && sub_barrier_->wait ()) { // I'm the last subscriber entering the barrier
211
- XBT_DEBUG (" Start the %d subscribe activities for the transaction" , sub_transaction_.size ());
215
+ XBT_DEBUG (" Wait for the %d subscribe activities for the transaction" , sub_transaction_.size ());
212
216
for (unsigned int i = 0 ; i < sub_transaction_.size (); i++)
213
- sub_transaction_.at (i)->resume ();
214
-
217
+ sub_transaction_.at (i)->resume ()->wait ();
218
+ XBT_DEBUG (" All on-flight subscribe activities are completed. Proceed with the current transaction." );
219
+ sub_transaction_.clear ();
215
220
// Mark this transaction as over
216
221
sub_transaction_in_progress_ = false ;
217
222
sub_transaction_id_++;
218
223
}
224
+ // Prevent subscribers to start a new transaction before this one is really over
225
+ if (sub_barrier_)
226
+ sub_barrier_->wait ();
219
227
}
220
228
221
229
void Engine::sub_close ()
@@ -225,12 +233,6 @@ void Engine::sub_close()
225
233
if (not sub_closing_) {
226
234
// I'm the first to close
227
235
sub_closing_ = true ;
228
- XBT_DEBUG (" Wait for the completion of %u subscribe activities from the previous transaction" ,
229
- sub_transaction_.size ());
230
- sub_transaction_.wait_all ();
231
- sub_transaction_.clear ();
232
- XBT_DEBUG (" last subscribe transaction is over" );
233
- sub_transaction_id_++;
234
236
}
235
237
rm_subscriber (self);
236
238
0 commit comments