@@ -236,11 +236,24 @@ PipeSource::PipeSource() {}
236236Status PipeSource::Initialize (Pipe* pipe) {
237237 if (pipe_) return Status::Invalid (" Pipe:" + pipe->PipeName () + " has multiple sinks" );
238238 pipe_ = pipe;
239+ backpressure_source_.AddController (pipe);
239240 return Status::OK ();
240241}
241242
242- void PipeSource::Pause (int32_t counter) { pipe_->Pause (this , counter); }
243- void PipeSource::Resume (int32_t counter) { pipe_->Resume (this , counter); }
243+ void PipeSource::Pause (int32_t counter) {
244+ auto lock = mutex_.Lock ();
245+ if (backpressure_counter < counter) {
246+ backpressure_counter = counter;
247+ backpressure_source_.Pause ();
248+ }
249+ }
250+ void PipeSource::Resume (int32_t counter) {
251+ auto lock = mutex_.Lock ();
252+ if (backpressure_counter < counter) {
253+ backpressure_counter = counter;
254+ backpressure_source_.Resume ();
255+ }
256+ }
244257Status PipeSource::StopProducing () {
245258 if (pipe_) return pipe_->StopProducing (this );
246259 // stopped before initialization
@@ -264,67 +277,20 @@ Pipe::Pipe(ExecPlan* plan, std::string pipe_name,
264277 std::unique_ptr<BackpressureControl> ctrl,
265278 std::function<Status()> stopProducing, Ordering ordering, bool pause_on_any,
266279 bool stop_on_any)
267- : plan_(plan),
280+ : BackpressureCombiner(std::move(ctrl), pause_on_any),
281+ plan_(plan),
268282 ordering_(ordering),
269283 pipe_name_(pipe_name),
270- ctrl_(std::move(ctrl)),
271284 stopProducing_(stopProducing),
272- pause_on_any_(pause_on_any),
273285 stop_on_any_(stop_on_any) {}
274286
275287const Ordering& Pipe::ordering () const { return ordering_; }
276288
277- void Pipe::Pause (PipeSource* output, int counter) {
278- auto lock = mutex_.Lock ();
279- auto & state = state_[output];
280- if (state.backpressure_counter < counter) {
281- state.backpressure_counter = counter;
282- if (!state.paused && !state.stopped ) {
283- state.paused = true ;
284- size_t paused_count = ++paused_count_;
285- if (pause_on_any_) {
286- if (paused_count == 1 ) {
287- ctrl_->Pause ();
288- }
289- } else {
290- if (paused_count == CountSources () - stopped_count_) {
291- ctrl_->Pause ();
292- }
293- }
294- }
295- }
296- }
297-
298- void Pipe::Resume (PipeSource* output, int counter) {
299- auto lock = mutex_.Lock ();
300- auto & state = state_[output];
301- if (state.backpressure_counter < counter) {
302- state.backpressure_counter = counter;
303- DoResume (state);
304- }
305- }
306-
307- void Pipe::DoResume (SourceState& state) {
308- if (state.paused && !state.stopped ) {
309- state.paused = false ;
310- size_t paused_count = --paused_count_;
311- if (pause_on_any_) {
312- if (paused_count == 0 ) {
313- ctrl_->Resume ();
314- }
315- } else {
316- if (paused_count == CountSources () - stopped_count_ - 1 ) {
317- ctrl_->Resume ();
318- }
319- }
320- }
321- }
322-
323289Status Pipe::StopProducing (PipeSource* output) {
324290 auto lock = mutex_.Lock ();
325291 auto & state = state_[output];
326292 DCHECK (!state.stopped );
327- DoResume (state );
293+ BackpressureCombiner::Stop ( );
328294 state.stopped = true ;
329295 size_t stopped_count = ++stopped_count_;
330296 if (stop_on_any_) {
0 commit comments