// Example 3.1. See page 11 of the FG tutorial. #include "FG.h" #include using namespace std; // Stage function prototypes. int func1(FG_stage, FG_stage_params); int func2(FG_stage, FG_stage_params); int func3(FG_stage, FG_stage_params); int main() { // Declare the pipeline variables. int buffer_size = sizeof(int); // Declare the threads, and store them in a NULL-terminated array. FG_pipeline_thread_helper thread1 = new FG_pipeline_thread_helper_info(), thread2 = new FG_pipeline_thread_helper_info(), thread3 = new FG_pipeline_thread_helper_info(); FG_pipeline_thread_helper threads[] = {thread1, thread2, thread3, NULL}; // Declare the stages. FG_pipeline_stage_helper stage1 = new FG_pipeline_stage_helper_info(thread1), stage2 = new FG_pipeline_stage_helper_info(thread2), stage3 = new FG_pipeline_stage_helper_info(thread3); // Assign a function to each stage. stage1->set_func(func1, NULL); stage2->set_func(func2, NULL); stage3->set_func(func3, NULL); // Store the stages in a NULL-terminated array. FG_pipeline_stage_helper stages[] = {stage1, stage2, stage3, NULL}; // Instantiate the pipeline. FG_pipeline my_pipeline = FG_pipeline_info::create(stages, threads); // Set the pipeline variables. my_pipeline->set_buff_size(buffer_size); // Make stage 2 be the only caboose setter. stage2->make_caboose_setter(); // Fix the pipeline, which also runs it, and store the return code. int error = my_pipeline->fix(); // Display the return code. cout << "FINISHED PIPELINE WITH ERROR CODE " << error << endl; // Dismantle the pipeline. my_pipeline->dismantle(); return 0; } // func1, run by stage 1. int func1(FG_stage my_stage, FG_stage_params my_params) { // Accept a thumbnail from the preceding stage (the source). FG_pipeline_thumbnail thumb = my_stage->accept_thumbnail(); // Store a single int value in the buffer. int *buff = (int *) thumb->get_address(); *buff = 2 * thumb->get_round(); // Convey the thumbnail to the next stage (stage 2). my_stage->convey_thumbnail(thumb); return 0; } // func2, run by stage 2. int func2(FG_stage my_stage, FG_stage_params my_params) { // Accept a thumbnail from the preceding stage (stage 1). FG_pipeline_thumbnail thumb = my_stage->accept_thumbnail(); // Change the int value in the buffer. int *buff = (int *) thumb->get_address(); *buff += 20; // Once we store a value greater than 30 into this buffer, declare // it to be the caboose. if (*buff > 30) my_stage->set_caboose(thumb); // Convey the thumbnail to the next stage (stage 3). my_stage->convey_thumbnail(thumb); return 0; } // func3, run by stage 3. int func3(FG_stage my_stage, FG_stage_params my_params) { // Accept a thumbnail from the preceding stage (stage 1). FG_pipeline_thumbnail thumb = my_stage->accept_thumbnail(); // Print the value that is in the buffer. int *buff = (int *) thumb->get_address(); cout << "result: " << *buff << endl; // Convey the thumbnail to the next stage (the sink). my_stage->convey_thumbnail(thumb); return 0; }