Macros for function definitions/calls

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Is it possible to split macro variable arguments into separate tokens?  I
would like to write a macro which expands into two function definitions, one
which is wrapped in a pthread_mutex... forcing mutual exclusion on a given
function.  Note, I haven't tried compiling this yet so there may be minor
errors, I wanted to see if I could get the right preprocessor output
first... Here's the first attempt:


   #define ATOMIC(ret, NAME, ...)                                    \
       static pthread_mutex_t NAME##_mutex=PTHREAD_MUTEX_INITIALIZER;\
       ret NAME(__VA_ARGS__){                                        \
           ret retVal;                                               \
           while(pthread_mutex_lock(&NAME ## _mutex));               \
               fprintf(stderr, " !! Error Entering: %s\n",#NAME);    \
           retVal = NAME ## _lock(__VA_ARGS__);                      \
           pthread_mutex_unlock(&NAME ## _mutex);                    \
           return retVal;                                            \
       }                                                             \
       ret NAME ## _lock(__VA_ARGS__)                                      


So that the usage:


   ATOMIC(int, testFunc, int arg){
       return 15;
   }

Produces:

   static pthread_mutex_t testFunc_mutex = PTHREAD_MUTEX_INITIALIZER; 
   int testFunc(int arg){ 
       int retVal; 
       while(pthread_mutex_lock(&testFunc_mutex)) 
           fprintf(stderr, " !! Error Entering: %s\n","testFunc"); 
       retVal = testFunc_lock(int arg); 
       pthread_mutex_unlock(&testFunc_mutex); 
       return retVal; 
   } 
   int testFunc_lock(int arg){
       return 15;
   }


The problem is that I'm not sure if there is a way to drop the type from the
argument list when calling the 'generated' function.  Does anyone know if
this is possible?  Or have a good/better way to do what I'm attempting? 
Basically to allow a bunch of functions to be changed between non-atomic and
atomic easily (with minimal manual effort). 


Thanks,

Bryan.
-- 
View this message in context: http://www.nabble.com/Macros-for-function-definitions-calls-tf3965655.html#a11255528
Sent from the gcc - Help mailing list archive at Nabble.com.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux