#ifndef BOUNDED_COLLECTIVE_HPP
#define BOUNDED_COLLECTIVE_HPP

#include <queue>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <cstdint>

struct Task{
	int frame_id;
	int row_id;
};

class BoundedCollective {
	private:
		std::queue<Task> task_queue;
		int max_queue_size;
		bool stop_flag;
		
		std::mutex q_mtx;
		std::condition_variable cv_not_full;
		std::condition_variable cv_not_empty;
		
		int target_rows;
		int rows_completed;
		std::mutex barrier_mtx;
		std::condition_variable cv_frame_done;
		
		std::vector<std::thread> workers;
		
		uint8t_t* video_data;
		int W,H,C;
		
		void worker_loop(int id);
		
	public:
		BoundedCollective(int num_threads, int max_size, uint8_t* video	, int width , int height , int channels);
		~BoundedCollective();

		void push_task(const Task& t);
		void wait_frame_done(int total_rows);
};

#endif
