TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

tsp_mtgprof.c

00001 /* 
00002  * pthread_create wrapper for gprof compatibility
00003  * Provided by Samuel Hocevar at http://sam.zoy.org/doc/programming/gprof.html
00004  *
00005  * needed headers: <pthread.h>
00006  *                 <sys/time.h>
00007  */
00008 
00009 
00010 #include "tsp_mtgprof.h"
00011 #ifndef VXWORKS
00012 /* Same prototype as pthread_create; use some #define magic to
00013  * transparently replace it in other files */
00014 int gprof_pthread_create(pthread_t * thread, pthread_attr_t * attr,
00015                          void * (*start_routine)(void *), void * arg)
00016 {
00017     wrapper_t wrapper_data;
00018     int i_return;
00019 
00020     /* Initialize the wrapper structure */
00021     wrapper_data.start_routine = start_routine;
00022     wrapper_data.arg = arg;
00023     getitimer(ITIMER_PROF, &wrapper_data.itimer);
00024     pthread_cond_init(&wrapper_data.wait, NULL);
00025     pthread_mutex_init(&wrapper_data.lock, NULL);
00026     pthread_mutex_lock(&wrapper_data.lock);
00027 
00028     /* The real pthread_create call */
00029     i_return = pthread_create(thread, attr, &wrapper_routine,
00030                                             &wrapper_data);
00031 
00032     /* If the thread was successfully spawned, wait for the data
00033      * to be released */
00034     if(i_return == 0)
00035     {
00036         pthread_cond_wait(&wrapper_data.wait, &wrapper_data.lock);
00037     }
00038 
00039     pthread_mutex_unlock(&wrapper_data.lock);
00040     pthread_mutex_destroy(&wrapper_data.lock);
00041     pthread_cond_destroy(&wrapper_data.wait);
00042 
00043     return i_return;
00044 }
00045 
00046 /* The wrapper function in charge for setting the itimer value */
00047 static void * wrapper_routine(void * data)
00048 {
00049     /* Put user data in thread-local variables */
00050     void * (*start_routine)(void *) = ((wrapper_t*)data)->start_routine;
00051     void * arg = ((wrapper_t*)data)->arg;
00052 
00053     /* Set the profile timer value */
00054     setitimer(ITIMER_PROF, &((wrapper_t*)data)->itimer, NULL);
00055 
00056     /* Tell the calling thread that we don't need its data anymore */
00057     pthread_mutex_lock(&((wrapper_t*)data)->lock);
00058     pthread_cond_signal(&((wrapper_t*)data)->wait);
00059     pthread_mutex_unlock(&((wrapper_t*)data)->lock);
00060 
00061     /* Call the real function */
00062     return start_routine(arg);
00063 }
00064 #endif /* VXWORKS */
Framework Home Page.

Beware !! TSP wave is coming...