source: soft/giet_vm/applications/ocean/null_macros/c.m4.null.POSIX @ 582

Last change on this file since 582 was 581, checked in by laurent, 9 years ago

Adding ocean application, some mathematics functions and distributed locks

File size: 2.9 KB
Line 
1divert(-1)
2define(NEWPROC,) dnl
3
4define(BARRIER, `{
5   long int cycle;
6   int cancel, temp, error;
7   
8
9   error = pthread_mutex_lock(&($1).mutex);
10   if (error != 0) {
11      printf("Error while trying to get lock in barrier.\n");
12      exit(-1);
13   }
14
15   cycle = ($1).cycle;
16   if (++($1).counter != ($2)) {
17      pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel);
18      while (cycle == ($1).cycle) {
19         error = pthread_cond_wait(&($1).cv, &($1).mutex);
20         if (error != 0) {
21            break;
22         }
23      }
24      pthread_setcancelstate(cancel, &temp);
25   } else {
26      ($1).cycle = !($1).cycle;
27      ($1).counter = 0;
28      error = pthread_cond_broadcast(&($1).cv);
29   }
30   pthread_mutex_unlock(&($1).mutex);
31}')
32
33define(BARDEC, `
34struct {
35   pthread_mutex_t mutex;
36   pthread_cond_t cv;
37   long int counter;
38   long int cycle;
39} ($1);
40')
41
42define(BARINIT, `{
43   long int error;
44
45   error = pthread_mutex_init(&($1).mutex, NULL);
46   if (error != 0) {
47      printf("Error while initializing barrier.\n");
48      exit(-1);
49   }
50
51   error = pthread_cond_init(&($1).cv, NULL);
52   if (error != 0) {
53      printf("Error while initializing barrier.\n");
54      pthread_mutex_destroy(&($1).mutex);
55      exit(-1);
56   }
57
58   ($1).counter = 0;
59   ($1).cycle = 0;
60}')
61
62define(LOCKDEC, `pthread_mutex_t ($1);')
63define(LOCKINIT, `{pthread_mutex_init(&($1), NULL);}')
64define(LOCK, `{
65    pthread_mutex_lock(&($1));
66    }')
67define(UNLOCK, `{pthread_mutex_unlock(&($1));}')
68
69define(WAIT_FOR_END, `{
70   long int i, error;
71   for (i = 0; i < ($1) - 1; i++) {
72      error = pthread_join(PThreadTable[i], NULL);
73      if (error != 0) {
74         printf("Error in pthread_join().\n");
75         exit(-1);
76      }
77   }
78}')
79
80define(CREATE, `{
81   long i, error;
82
83   for (i = 0; i < ($2) - 1; i++) {
84      error = pthread_create(&PThreadTable[i], NULL, (void * (*)(void *))($1), &ids_procs[i+1]);
85      if (error != 0) {
86         printf("Error in pthread_create().\n");
87         exit(-1);
88      }
89   }
90   $1(&ids_procs[0]);
91}')
92
93define(MAIN_INITENV, `{;}')
94define(MAIN_END, `{exit(0);}')
95
96define(MAIN_ENV,`
97#include <pthread.h>
98#include <sys/time.h>
99#include <sys/fcntl.h>
100#include <sys/resource.h>
101#include <unistd.h>
102#include <stdlib.h>
103#include <stdio.h>
104#include <time.h>
105#define MAX_THREADS 512
106pthread_t PThreadTable[MAX_THREADS];
107')
108
109define(ENV, ` ')
110define(EXTERN_ENV, `
111#include <stdio.h>
112#include <math.h>
113#include <time.h>
114#include <pthread.h>
115#include <sys/time.h>
116#include <sys/stat.h>
117#include <sys/resource.h>
118#include <unistd.h>
119#include <stdlib.h>
120#include <stdio.h>
121extern pthread_t PThreadTable[];
122')
123
124define(G_MALLOC, `malloc($1);')
125
126define(CLOCK, `{
127   struct timeval   FullTime;
128
129   gettimeofday(&FullTime, NULL);
130   ($1) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
131}')
132
133define(START_PHASE, `{
134    CLOCK(*gp[$1].step_start)
135}')
136
137define(END_PHASE, `{
138    unsigned int t;
139    CLOCK(t)
140    gp[$1].steps_time[($2)-1] += (double)t - *gp[$1].step_start;
141}')
142
143divert(0)
Note: See TracBrowser for help on using the repository browser.