Has anyone ever encountered that when starting the program under Valgrind, messages like "Possible data race" indicate the function of blocking / unblocking mutexes? The mane seems to say clearly that Valgrind works great with Posix threads, but he clearly doesn’t like the pthread_mutex_lock / unlock functions, or am I missing something?
Possible data race during write of size 4 at 0x64BC34 by thread #1 ==11521== Locks held: none ==11521== at 0x3A60A1: pthread_mutex_lock (in /lib/libthr.so.3) ==11521== by 0x5C48E: pthread_mutex_lock (in /usr/local/lib/valgrind /vgpreload_helgrind-x86-freebsd.so) ==11521== by 0x8052651: md_mutex_operation (md_pthread.c:92) ==11521== by 0x804D9ED: md_prt (md_funcs.c:49) ==11521== by 0x804DFD6: md_freepthr_create (md_funcs.c:268) ==11521== by 0x804B031: main (ml_shd.c:374) ==11521== ==11521== This conflicts with a previous write of size 4 by thread #2 ==11521== Locks held: none ==11521== at 0x3A686A: ??? (in /lib/libthr.so.3) ==11521== by 0x5C88E: pthread_mutex_unlock (in /usr/local/lib/valgrind/vgpreload_helgrind-x86-freebsd.so) ==11521== by 0x805278F: md_mutex_operation (md_pthread.c:118) ==11521== by 0x804DAA4: md_prt (md_funcs.c:68) ==11521== by 0x8054EEA: md_freepthr_func (md_pthread.c:1193) ==11521== by 0x5F245: ??? (in /usr/local/lib/valgrind/vgpreload_helgrind-x86-freebsd.so) ==11521== by 0x39F4B9: ??? (in /lib/libthr.so.3) ==11521== ==11521== Address 0x64BC34 is 52 bytes inside a block of size 60 alloc'd ==11521== at 0x5B6FA: calloc (in /usr/local/lib/valgrind/vgpreload_helgrind-x86-freebsd.so) ==11521== by 0x3A596C: ??? (in /lib/libthr.so.3) ==11521== by 0x5EF68: pthread_mutex_init (in /usr/local/lib/valgrind/vgpreload_helgrind-x86-freebsd.so) ==11521== by 0x804C4D2: md_conninit (md_cfg.c:189) ==11521== by 0x804A19E: main (ml_shd.c:62) ==11521== ==11521== (action on error) vgdb me ... ==11521== Continuing ...
Update:
#define MD_PRT(dlevel,fmt,...) \ md_prt(dlevel,"%s (%s:%d): " fmt,\ pname,__func__,__LINE__,##__VA_ARGS__) void md_prt(uint32_t dlevel,const char *fmt, ...) { va_list vp; md_mutex_operation(&(p_conn->log_mutex),MD_LOCK); va_start(vp,fmt); vsyslog(LOG_DEBUG,fmt,vp); va_end(vp); md_mutex_operation(&(p_conn->log_mutex),MD_UNLOCK); return; } int md_mutex_operation(p_mutex, operation) pthread_mutex_t *p_mutex; int operation; { int res = 0; time_t sec; struct timespec tim; tim.tv_nsec = 0; #ifdef ML_DEBUG char str[20]; #endif switch(operation){ case MD_LOCK: res = pthread_mutex_lock(p_mutex); #ifdef ML_DEBUG if(res) MD_PRT(MD_PRT_THREAD,"Error %d of locking mutex (%s, pthread %u)", res,str,pthread_self()); #endif break; case MD_TRYLOCK: res = pthread_mutex_trylock(p_mutex); #ifdef ML_DEBUG if(res) MD_PRT(MD_PRT_THREAD,"Error %d of trying lock mutex (%s, pthread %u)", res,str,pthread_self()); #endif break; case MD_TIMLOCK: time(&sec); tim.tv_sec = sec + MD_LOCK_TIMEOUT; res = pthread_mutex_timedlock(p_mutex,&tim); #ifdef ML_DEBUG if(res) MD_PRT(MD_PRT_THREAD,"Error %d of time locking mutex (%s, pthread %u)", res,str,pthread_self()); #endif break; case MD_UNLOCK: res = pthread_mutex_unlock(p_mutex); #ifdef ML_DEBUG if(res) MD_PRT(MD_PRT_THREAD,"Error %d of unlocking mutex (%s, pthread %u)", res,str,pthread_self()); #endif break; default : break; } return(res); }