Re: Loops in GCC preprocessor needed

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

 



There is a few things to know about the C preprocessor :
   - No recursion.
   - No arithmetic operation.
   - One counter only incrementing on evaluation.
   - If an argument of a macro is an other macro then you have to call an
other macro with this argument to get it's evaluation.
   - Doesn't handle un-paired expression.

This because Macro is something that was required but never wished by the C
designers.

That being said, what you want I not totally undo-able except for one thing
: the counter.

User defined preprocessor counter are possible (see
boost/preprocessor/arithmetic/...) but requires #include as operation and
you can't have an include in a macro.

So the only other option is to define a weak global unsigned long.

Here is how you could have what you want :

macro_tool.h:--------------------------------------------------------------------------------------------------------

__attribute__((weak))
unsigned long __ITERATOR__ = 0;

#define LOOP_THREADS(end, body)    \
 APP ## end(body)                              // ## is a preprocessor
operator that paste two tokens together

#define APP1(val) __ITERATOR__ = 1; val
#define APP2(val) __ITERATOR__ = 2; val APP1(val)
....
#define APPN(val) __ITERATOR__ = N; val APP(N - 1)(val) // use a small
python script to produces all thoses lines. Here N is 5

// the python scrip :
// for x in range(0, 5):
//    print("#define APP{0}(val) __couter__ = {0}; val APP{1}(val)".format(x
+ 1, x))

main.c:---------------------------------------------------------------------------------------------------------------

#include <pthread.h>
#include "macro_tool.h"

void *routine(void *arg)
{
   (void)arg;
   return 0;
}

int  main()
{
   pthread_t ths[5];

   LOOP_THREAD(5, pthread_create(&ths[100 - __ITERATOR__], 0, routine, 0););
   LOOP_THREAD(5, pthread_join(th[100 - __ITERATOR__], 0););
}

expanded
result:-------------------------------------------------------------------------------------

__attribute__((weak))
unsigned long __ITERATOR__ = 0;

void *routine(void *arg)
{
   (void)arg;
   return 0;
}

int main()
{
   pthread_t ths[5];

   __ITERATOR__ = 5;
   pthread_create(&ths[100 - __ITERATOR__], 0, routine, 0);
   __ITERATOR__ = 4;
   pthread_create(&ths[100 - __ITERATOR__], 0, routine, 0);
   __ITERATOR__ = 3;
   pthread_create(&ths[100 - __ITERATOR__], 0, routine, 0);
   __ITERATOR__ = 2;
   pthread_create(&ths[100 - __ITERATOR__], 0, routine, 0);
   __ITERATOR__ = 1;
   pthread_create(&ths[100 - __ITERATOR__], 0, routine, 0);

    __ITERATOR__ = 5;
    pthread_join(th[100 - __ITERATOR__], 0);
    ...
    __ITERATOR__ = 1;
    pthread_join(th[100 - __ITERATOR__], 0);
   return 0;
}



--
View this message in context: http://gcc.1065356.n5.nabble.com/Loops-in-GCC-preprocessor-needed-tp1230045p1230122.html
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