blob: 6d4b2a1f9d1e309df2f121061681a751f4840782 [file] [log] [blame]
define(EXTERN_ENV,
`
#include <time.h>
extern pthread_t _M4_threads[MAX_THREADS];
extern pthread_mutexattr_t _M4_normalMutexAttr;
extern int _M4_numThreads;
')
define(MAIN_ENV,
`
#ifdef _XOPEN_SOURCE
#undef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef __USE_XOPEN2K
#define __USE_XOPEN2K
#endif
#ifndef __USE_UNIX98
#define __USE_UNIX98
#endif
#include <pthread.h>
#include <time.h>
#define MAX_THREADS 128
pthread_t _M4_threadsTable[MAX_THREADS];
int _M4_threadsTableAllocated[MAX_THREADS];
pthread_mutexattr_t _M4_normalMutexAttr;
int _M4_numThreads = MAX_THREADS;
')
define(MAIN_INITENV, `
pthread_mutexattr_init( &_M4_normalMutexAttr);
// pthread_mutexattr_settype( &_M4_normalMutexAttr, PTHREAD_MUTEX_NORMAL);
_M4_numThreads = $3;
{
int _M4_i;
for ( _M4_i = 0; _M4_i < MAX_THREADS; _M4_i++) {
_M4_threadsTableAllocated[_M4_i] = 0;
}
}
')
define(MAIN_END, `')
define(CREATE_WITH_ARG, `
{
int _M4_i;
for ( _M4_i = 0; _M4_i < MAX_THREADS; _M4_i++) {
if ( _M4_threadsTableAllocated[_M4_i] == 0) break;
}
pthread_create(&_M4_threadsTable[_M4_i],NULL,(void *(*)(void *))$1,(void *)$2);
_M4_threadsTableAllocated[_M4_i] = 1;
}
')
define(CREATE, `CREATE_WITH_ARG($1,NULL);')
define(SELF, `( long)pthread_self()')
define(BARDEC, `pthread_barrier_t $1;')
define(BARINIT, `pthread_barrier_init(&($1),NULL,_M4_numThreads);')
define(BARRIER, `pthread_barrier_wait(&($1))')
define(LOCKDEC, `pthread_mutex_t $1;')
define(LOCKINIT, `pthread_mutex_init(&($1), &_M4_normalMutexAttr);')
define(LOCK, `pthread_mutex_lock(&($1));')
define(UNLOCK, `pthread_mutex_unlock(&($1));')
define(LOCKRDEC, `pthread_mutex_t $1;')
define(LOCKRINIT, `pthread_mutex_init(&($1), NULL);')
define(LOCKR, `pthread_mutex_lock(&($1));')
define(UNLOCKR, `pthread_mutex_unlock(&($1));')
define(CVDEC, `pthread_cond_t $1;')
define(CVINIT, `pthread_cond_init(&$1,NULL);')
define(CVWAIT, `pthread_cond_wait(&$1,&$2);')
define(CVWAITREL, `pthread_cond_wait(&$1,&$2); pthread_mutex_unlock(&$2);')
define(CVSIGNALALL, `pthread_cond_broadcast(&$1);')
define(CVSIGNALONE, `pthread_cond_signal(&$1);')
define(ACQUIRE, `pthread_mutex_lock(&($1));')
define(RELEASE, `pthread_mutex_unlock(&($1));')
define(ALOCKDEC, `pthread_mutex_t ($1[$2]);')
define(ALOCKINIT, `{
int _M4_loop_j;
for(_M4_loop_j=0; _M4_loop_j < $2; _M4_loop_j++){
pthread_mutex_init((pthread_mutex_t*)&($1[_M4_loop_j]), NULL);
}
}')
define(ALOCK, `pthread_mutex_lock((pthread_mutex_t*)&($1[$2]));')
define(AULOCK, `pthread_mutex_unlock((pthread_mutex_t*)&($1[$2]));')
define(AACQUIRE, `pthread_mutex_lock(&($1[$2]));')
define(ARELEASE, `pthread_mutex_unlock(&($1[$2]));')
define(WAIT_FOR_END, `
{
int _M4_i;
void *_M4_ret;
for ( _M4_i = 0; _M4_i < MAX_THREADS;_M4_i++) {
if ( _M4_threadsTableAllocated[_M4_i] == 0) break;
pthread_join( _M4_threadsTable[_M4_i], &_M4_ret);
}
}
')
define(JOIN, `{pthread_join( _M4_threadsTable[($1)], NULL );}')
define(CLOCK, `{long time(); ($1) = time(0);}')
define(GET_PID, `$1 = pthread_self();')
define(AUG_ON, `')
define(AUG_OFF, `')
define(G_MALLOC, `malloc($1);')
define(MALLOC, `malloc($1);')