A CIL Tutorial - Department of Computer Science - ETH Zürich
A CIL Tutorial - Department of Computer Science - ETH Zürich
A CIL Tutorial - Department of Computer Science - ETH Zürich
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
CHAPTER 10. ADDING A NEW KIND OF STATEMENT 86<br />
struct pthread_closure {<br />
void *(*fn)(void *);<br />
void *arg;<br />
};<br />
../ciltut-lib/src/tut10.c<br />
static void *tfunc_wrapper(void *arg)<br />
{<br />
struct pthread_closure *c = (struct pthread_closure *)arg;<br />
void *(*fn)(void *) = c->fn;<br />
void *a<br />
= c->arg;<br />
void *res<br />
= NULL;<br />
free(c);<br />
init_CS();<br />
perf_init(gettid());<br />
res = fn(a);<br />
perf_deinit();<br />
}<br />
return res;<br />
With the thread function wrapper set up in tfunc wrapper, we can now set up our version <strong>of</strong><br />
pthread create, which allocates a closure for the thread routine and its argument before making<br />
the call to pthread create orig.<br />
../ciltut-lib/src/tut10.c<br />
int pthread_create(pthread_t *__restrict thread,<br />
__const pthread_attr_t *__restrict attr,<br />
void * (*start_routine)(void *),<br />
void *__restrict arg)<br />
{<br />
struct pthread_closure *c = malloc(size<strong>of</strong>(struct pthread_closure));<br />
int res;<br />
c->fn = start_routine;<br />
c->arg = arg;<br />
res = pthread_create_orig(thread, attr, &tfunc_wrapper, c);<br />
if (res != 0) {<br />
printf("pthread failed\n");<br />
fflush(stdout);<br />
free(c);<br />
}<br />
return res;<br />
}