changeset 305:02071f609de5

Add a cleanup handler to counter a possible dead lock
author Sebastien Decugis <sdecugis@nict.go.jp>
date Thu, 08 Jan 2009 14:50:21 +0900
parents a9eb3f7fc273
children b9b74d6ac29e
files waaad/peer-events.c
diffstat 1 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/waaad/peer-events.c	Thu Jan 08 14:19:10 2009 +0900
+++ b/waaad/peer-events.c	Thu Jan 08 14:50:21 2009 +0900
@@ -45,6 +45,16 @@
 
 EVENTSTR_DEFINITION;
 
+static void cleanup_mutex(void * mtx)
+{
+	/* We have been canceled  */
+	TRACE_DEBUG(INFO, "Thread cancelled, freeing the mutex");
+	
+	CHECK_FCT_DO( pthread_mutex_unlock((pthread_mutex_t *)mtx), /* continue */);
+	
+	return;
+}
+
 /* Send an event to a peer from inside; the peer lock must be held. */
 int _peer_events_send_nl(_peer_t * peer, _pevent_t event, void * data)
 {
@@ -90,10 +100,13 @@
 	
 	CHECK_POSIX(ret);
 	
+	pthread_cleanup_push(cleanup_mutex, &peer->p_lock);
+
 	/* Create and queue the event */
 	CHECK_FCT_DO(  ret = _peer_events_send_nl(peer, event, data), /* continue */  );
 	
 	/* unlock */
+	pthread_cleanup_pop(0);
 	CHECK_POSIX(  pthread_mutex_unlock(&peer->p_lock)  );
 	
 	return ret;
"Welcome to our mercurial repository"