Thread: How to generalize this while loop?

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    23

    How to generalize this while loop?

    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?
    Last edited by joshuastuden; 10-10-2017 at 02:37 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Use a flag parameter to indicate whether the reader or writer condition should be used.

    Code:
     #define COND(writer_flag) (writer_flag? current_status == SEGMENT_WRITTEN : current_status != SEGMENT_WRITTEN)
    The while condition becomes COND(1) for the writer_aquire_segment() condition and COND(0) for the other one.

    Also you had a couple of syntax errors in what you pasted.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    23
    Thanks. Sorry for the syntax error, I just quickly pasted something and forgot I was in the midst of changing the code before I pasted it.

    The COND macro looks interesting but I must say I am a bit lost on how it works. How do I generalize that whole block of code into an "acquire_segment" function that gets called by reader_acquire_lock and writer_acquire_lock.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You would have to pass in flag, to be used in while(COND(flag)). COND(flag) is generalized. As I said before, the condition is either the 1 or 0 one. However you decide, it comes down to a Boolean variable.

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    23
    Quote Originally Posted by whiteflags View Post
    You would have to pass in flag, to be used in while(COND(flag)). COND(flag) is generalized. As I said before, the condition is either the 1 or 0 one. However you decide, it comes down to a Boolean variable.
    Ah, I THINK I see... so my "acquire_segment" function would have to take an additional parameter "int flag" or something that gets passed to COND. writer_acquire_segment(seg, 1) and reader_acquire_segment(seg, 0) would be how they're called.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Or you could make writer_acquire_segment(seg); call aquire_segment(seg, 1); and reader_acquire_segment(seg); call acquire_segment(seg, 0);

    This way all of the similar code is in acquire_segment() and the outside functions don't even have to change their signatures. From a client programmer perspective it is like nothing changed.
    Last edited by whiteflags; 10-10-2017 at 04:59 PM.

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    23
    Got it. Makes sense. For some reason, I thought there was some way I could play around with the | or something like that, but this macro works perfectly.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while ((current_status != SEGMENT_WRITTEN)
    Well is there a difference between this and say
    while ((current_status == SEGMENT_READ)

    Because if you can make the loops the same syntactically, apart from the value you test against, then parameterising it is a doddle.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  3. Help to Generalize my C Program.
    By clmoore3 in forum C Programming
    Replies: 6
    Last Post: 01-29-2013, 04:31 PM
  4. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  5. Replies: 23
    Last Post: 04-05-2011, 03:40 PM

Tags for this Thread