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
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
CHAPTER 6. OVERRIDING FUNCTIONS 49<br />
../ciltut-lib/src/tut6.c<br />
int pthread_mutex_lock(pthread_mutex_t *m)<br />
{<br />
int res;<br />
if (!pthread_mutex_lock_orig)<br />
pthread_mutex_lock_orig = checked_dlsym(RTLD_NEXT, "pthread_mutex_lock");<br />
res = pthread_mutex_lock_orig(m);<br />
if (enable_lock_tracking) {<br />
printf("thread: %d - pthread_mutex_lock(%p)\n", gettid(), m);<br />
fflush(stdout);<br />
}<br />
return res;<br />
}<br />
int pthread_mutex_unlock(pthread_mutex_t *m)<br />
{<br />
int res;<br />
if (!pthread_mutex_unlock_orig)<br />
pthread_mutex_unlock_orig = checked_dlsym(RTLD_NEXT, "pthread_mutex_unlock");<br />
if (enable_lock_tracking) {<br />
printf("thread: %d - pthread_mutex_unlock(%p)\n", gettid(), m);<br />
fflush(stdout);<br />
}<br />
res = pthread_mutex_unlock_orig(m);<br />
return res;<br />
}<br />
void toggle_lock_tracking()<br />
{<br />
enable_lock_tracking = !enable_lock_tracking;<br />
}<br />
6.4 test/tut6.c<br />
In this test, we simply declare a lock at global scope, then acquire and release it in the main function.<br />
We do this simply to test that the wrapper functions we wrote in ciltut-lib/src/tut6.c are<br />
correctly run whenever we call the lock and unlock functions.