I have two blocks of code that I feel can be generalized. I have a reader_acquire_segment function and a writer_acquire_segment function and I feel that the bulk of the code could be generalized to an "acquire_segment" function with different parameters passed in. Here's the relevant code:
Code:
#define SEGMENT_INITIALIZED 1
#define SEGMENT_READ 2
#define SEGMENT_WRITTEN 3
void
reader_acquire_segment(shared_segment_t *seg)
{
lock_segment(seg);
int current_status = read_segment_status(seg);
while ((current_status != SEGMENT_WRITTEN) {
wait_segment(seg);
current_status = read_segment_status(seg);
}
}
void
writer_acquire_segment(shared_segment_t *seg)
{
lock_segment(seg);
int current_status = read_segment_status(seg);
while (current_status == SEGMENT_WRITTEN) {
wait_segment(seg);
current_status = read_segment_status(seg); }
}
I feel like I could just have writer_acquire_segment call a generic acquire_segment with the appropriate parameters, and have reader_acquire_segment also call acquire_segment with the appropriate parameters, but I am little confused how to generalize the while condition. Any suggestions?