changeset 306:b9b74d6ac29e

Cleanup on cleanup functions
author Sebastien Decugis <sdecugis@nict.go.jp>
date Thu, 08 Jan 2009 15:36:06 +0900
parents 02071f609de5
children a4592b91368c
files waaad/dispatch.c waaad/peer-events.c waaad/peer-expire.c waaad/peer-in.c waaad/peer-internal.h waaad/peer-out.c waaad/peer-psm.c waaad/peer-sctp.c waaad/peer-secini.c waaad/peer-server.c waaad/peer-struct.c waaad/routing.c waaad/utils.c waaad/utils.h
diffstat 14 files changed, 110 insertions(+), 106 deletions(-) [+]
line wrap: on
line diff
--- a/waaad/dispatch.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/dispatch.c	Thu Jan 08 15:36:06 2009 +0900
@@ -178,7 +178,7 @@
 		
 		/* Now we'll call the dispatch handlers, lock the appropriate rdlock */
 		CHECK_POSIX_DO( pthread_rwlock_rdlock(&disp_dictlock), goto error );
-		pthread_cleanup_push( (void *)pthread_rwlock_unlock, &disp_dictlock );
+		pthread_cleanup_push( cleanup_rwlock, &disp_dictlock );
 		
 		/* Start with calling all ANY callbacks */
 		for (li = any_handlers.next; (li != &any_handlers); li = li->next) {
--- a/waaad/peer-events.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-events.c	Thu Jan 08 15:36:06 2009 +0900
@@ -45,16 +45,6 @@
 
 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)
 {
--- a/waaad/peer-expire.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-expire.c	Thu Jan 08 15:36:06 2009 +0900
@@ -94,16 +94,6 @@
 	return 0;
 }
 
-/* Cancellation cleanup handler for the expiry thread */
-static void _cleanup (void * arg)
-{
-	/* Unlock the lists mutex */
-	CHECK_POSIX_DO(  pthread_mutex_unlock( &_peers_lists_mtx ),  /* continue */  );
-	
-	/* End of cleanup handler */
-	return;
-}
-
 /* The code of the thread to send an EXPIRE event / delete the peer when the timeout timer is reached. */
 static void * _expire_th (void * arg)
 {
@@ -113,7 +103,7 @@
 	
 	/* Lock the lists lock. It will be released on cancellation */
 	CHECK_POSIX_DO(  pthread_mutex_lock( &_peers_lists_mtx ),  goto error  );
-	pthread_cleanup_push(_cleanup, NULL);
+	pthread_cleanup_push( cleanup_mutex, &_peers_lists_mtx);
 	
 	/* loop forever, the thread will be canceled */
 	while (1) {
--- a/waaad/peer-in.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-in.c	Thu Jan 08 15:36:06 2009 +0900
@@ -42,28 +42,6 @@
 #include "waaad-internal.h"
 #include "peer-internal.h"	
 
-
-static void cleanup_buffer(void * buf)
-{
-	/* We have been canceled while the buffer was being parsed, so free it */
-	TRACE_DEBUG(INFO, "Thread cancelled, received data is lost -- does it happen?");
-	
-	free(buf);
-	
-	return;
-}
-
-static void cleanup_msg(void * msg)
-{
-	/* We have been cancelled before the message could be queued, free it (what else can we do? */
-	TRACE_DEBUG(INFO, "Thread cancelled, message is lost -- does it happen?");
-	
-	msg_free(msg, 1);
-	
-	return;
-}
-	
-
 /* The code of the p_in_th thread */
 /* When this thread is created, the peer must have its p_sec_session and p_secmod already set */
 static void * _peer_in_th(void * arg)
--- a/waaad/peer-internal.h	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-internal.h	Thu Jan 08 15:36:06 2009 +0900
@@ -438,6 +438,7 @@
 void _peer_struct_addrs_reset( _peer_t * peer );
 void _peer_struct_secmod_reset(_peer_t * peer);
 int peer_dump_hash(int debug_level);
+void cleanup_peer(void * peer);
 
 
 int _peer_failover(_peer_t * peer, int freeloc);
--- a/waaad/peer-out.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-out.c	Thu Jan 08 15:36:06 2009 +0900
@@ -85,7 +85,7 @@
 			return 0; /* is this OK with the cancelation handler??? */
 		}  );
 
-	pthread_cleanup_push( free, buf );
+	pthread_cleanup_push( cleanup_buffer, buf );
 
 	/* Now send the message using the appropriate security module */
 	ASSERT( peer && peer->p_secmod && peer->p_secmod->sec_send_protect );
--- a/waaad/peer-psm.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-psm.c	Thu Jan 08 15:36:06 2009 +0900
@@ -54,12 +54,6 @@
 static int _psm_do_watchdog( _peer_t * peer, _pevent_t event, void * ev_data );
 
 
-static void _cleanup_msg( void * msg )
-{
-	CHECK_FCT_DO( msg_free( msg, 1 ), /* continue */ );
-	return;
-}
-
 /* Election */
 static int _psm_election( _peer_t * peer )
 {
@@ -257,7 +251,7 @@
 	peer->p_flags |= PEERFL_RESPONDER;
 	
 	/* Push two cancelation handlers to free the event data in case of we are cancelled */
-	pthread_cleanup_push( free, inc );
+	pthread_cleanup_push( cleanup_buffer, inc );
 	pthread_cleanup_push( (void (*)(void *))_peer_struct_destroy, &inc->peer );
 	
 	/* The inc->peer object already contains all parsed data from the received CER, copy this data into the main peer structure */
@@ -315,7 +309,7 @@
 
 	/* Create a CEA message */
 	CHECK_FCT_DO( _peer_cea_create( peer, &cea, eteid, hbhid ), goto error );
-	pthread_cleanup_push( _cleanup_msg, cea );
+	pthread_cleanup_push( cleanup_msg, cea );
 	
 	/* At this point, if peer->p_sec_hdl is NULL, it means we have no common security, otherwise it points to our most prioritary common module */
 	if (peer->p_sec_hdl == NULL) {
@@ -417,14 +411,14 @@
 	ASSERT( inc && VALIDATE_PEER(inc->peer) );
 	
 	/* Push two cancelation handlers to free the event data in case of we are cancelled */
-	pthread_cleanup_push( free, inc );
+	pthread_cleanup_push( cleanup_buffer, inc );
 	pthread_cleanup_push( (void (*)(void *))_peer_struct_destroy, &inc->peer );
 	
 	/* The inc->peer object already contains all parsed data from the received CER, copy this data into the main peer structure */
 	
 	/* Create a CEA message */
 	CHECK_FCT_DO( _peer_cea_create( inc->peer, &cea, inc->ete, inc->hbh ), goto error );
-	pthread_cleanup_push( _cleanup_msg, cea );
+	pthread_cleanup_push( cleanup_msg, cea );
 	
 	/* Now set the error code */
 	CHECK_FCT_DO( msg_rescode_set( cea, "DIAMETER_UNABLE_TO_COMPLY", NULL, NULL, 0 ), goto error );
@@ -511,7 +505,7 @@
 	
 	/* Retrieve the CEA from the incoming queue */
 	CHECK_FCT_DO(  meq_tryget(peer->p_in_q, &cea),  return 1  );
-	pthread_cleanup_push( _cleanup_msg, cea );
+	pthread_cleanup_push( cleanup_msg, cea );
 	
 	TRACE_DEBUG(FULL, "Received this message from '%s':", peer->p_diamid);
 	msg_dump_walk(FULL, cea);
@@ -587,7 +581,7 @@
 	
 	/* Retrieve the new message from the incoming queue */
 	CHECK_FCT_DO(  meq_tryget(peer->p_in_q, &msg),  return 1  );
-	pthread_cleanup_push( _cleanup_msg, msg );
+	pthread_cleanup_push( cleanup_msg, msg );
 	
 	TRACE_DEBUG(FULL, "Received this message from '%s':", peer->p_diamid);
 	msg_dump_walk(FULL, msg);
--- a/waaad/peer-sctp.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-sctp.c	Thu Jan 08 15:36:06 2009 +0900
@@ -958,13 +958,6 @@
 	return sendmsg(*(int*)(sess->conn), &mhdr, 0);
 }
 
-/* cleanup for next function */
-static void cleanup_freeindirect(void * loc)
-{
-	free( *(void **)loc );
-	return;
-}
-
 /*
  * FUNCTION:	_peer_sctp_recv
  *
@@ -1014,7 +1007,7 @@
 	CHECK_MALLOC_DO(  *data = malloc(sz),  { errno = ENOMEM; return -1;  }  );
 	
 	/* We free this buffer if we are cancelled during this call */
-	pthread_cleanup_push( cleanup_freeindirect, data );
+	pthread_cleanup_push( cleanup_buffer_i, data );
 
 nextmsg:	
 	memset(&mhdr, 0, sizeof(mhdr));
--- a/waaad/peer-secini.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-secini.c	Thu Jan 08 15:36:06 2009 +0900
@@ -128,7 +128,7 @@
 	
 	memcpy(newmsg, header, sizeof(header));
 	while (received < *length) {
-		pthread_cleanup_push(free, newmsg); /* In case we are canceled, clean the partialy built buffer */
+		pthread_cleanup_push(cleanup_buffer, newmsg); /* In case we are canceled, clean the partialy built buffer */
 		ret = (*session->cbs.tcp.recv_data) (session, newmsg + received, (*length) - received);
 		pthread_cleanup_pop(0);
 	
--- a/waaad/peer-server.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-server.c	Thu Jan 08 15:36:06 2009 +0900
@@ -80,35 +80,6 @@
 static _peer_servinfo_t * _psi = NULL;
 static int _psi_cnt = 0;
 
-/* Cancelation cleanup handler for cleaning a receiver thread */
-static void cleanup_peer (void * arg)
-{
-	_peer_t * dum = _P(arg);
-
-	TRACE_DEBUG(FULL, "Cleaning up receiver thread for dummy peer %p", arg);
-	
-	CHECK_FCT_DO(  _peer_struct_destroy(&dum),  );
-	
-	/* End of cleanup handler */
-	return;
-}
-
-static void cleanup_lock (void * arg)
-{
-	CHECK_POSIX_DO(  pthread_mutex_unlock(arg),  /* nothing */ );
-	return;
-}
-
-static void cleanup_pmsg (void * arg)
-{
-	msg_t ** pmsg = (msg_t **)arg;
-	if (pmsg && *pmsg) {
-		CHECK_FCT_DO(  msg_free(*pmsg, 1),  /* nothing */ );
-	}
-	return;
-}
-
-
 /* The code of a receiver thread, to handle an incoming connection, before the CER is received */
 static void * _peer_handle_client_th(void * arg)
 {
@@ -141,7 +112,7 @@
 	
 	/* Lock the peer to wait for incoming events */
 	CHECK_POSIX_DO(  pthread_mutex_lock(&dummy->p_lock),  goto error  );
-	pthread_cleanup_push(cleanup_lock, &dummy->p_lock);
+	pthread_cleanup_push(cleanup_mutex, &dummy->p_lock);
 	
 	/* Create the receiver thread */
 	CHECK_FCT_DO(  _peer_in_start(dummy),  goto error  );
@@ -151,7 +122,7 @@
 	delay.tv_sec += INCNX_TIMEOUT;
 	
 	/* Stack the cleanup handler, in case we are canceled */
-	pthread_cleanup_push(cleanup_pmsg, &received);
+	pthread_cleanup_push(cleanup_msg_i, &received);
 			
 	
 	/* Now wait for an event to occur. Such events include: connection broken, message received. 
@@ -249,7 +220,7 @@
 		/* Did we find the peer? */
 		switch (ret) {
 			case 0:
-				pthread_cleanup_push(cleanup_lock, H_LOCK( dummy->p_hash ));
+				pthread_cleanup_push(cleanup_mutex, H_LOCK( dummy->p_hash ));
 				{
 					/* the peer already exists, send it an event, the PSM will deal with incoming connection depending on its state */
 					_ev_incoming_t * ev;
@@ -257,7 +228,7 @@
 
 					/* Lock the peer */
 					CHECK_POSIX_DO( pthread_mutex_lock( &ref->p_lock ), 
-							{ cleanup_lock(H_LOCK( dummy->p_hash )); goto error; } );
+							{ pthread_mutex_unlock(H_LOCK( dummy->p_hash )); goto error; } );
 					
 					/* Check its state */
 					if ( ref->p_state == STATE_DISABLED ) {
@@ -274,7 +245,7 @@
 						CHECK_FCT_DO(  _peer_psm_start( ref, 2),
 							{ 
 								CHECK_POSIX_DO( pthread_mutex_unlock( &ref->p_lock ), /* ignore */ );
-								cleanup_lock(H_LOCK( dummy->p_hash )); 
+								CHECK_POSIX_DO( pthread_mutex_unlock(H_LOCK( dummy->p_hash )), /* ignore */ ); 
 								goto error; 
 							} );
 					}
@@ -283,7 +254,7 @@
 					CHECK_MALLOC_DO( ev = (_ev_incoming_t *) malloc(sizeof(_ev_incoming_t)),
 						{ 
 							CHECK_POSIX_DO( pthread_mutex_unlock( &ref->p_lock ), /* ignore */ );
-							cleanup_lock(H_LOCK( dummy->p_hash )); 
+							CHECK_POSIX_DO( pthread_mutex_unlock(H_LOCK( dummy->p_hash )), /* ignore */ ); 
 							goto error; 
 						} );
 
@@ -296,7 +267,7 @@
 						{ 
 							free(ev);
 							CHECK_POSIX_DO( pthread_mutex_unlock( &ref->p_lock ), /* ignore */ );
-							cleanup_lock(H_LOCK( dummy->p_hash )); 
+							CHECK_POSIX_DO( pthread_mutex_unlock(H_LOCK( dummy->p_hash )), /* ignore */ ); 
 							goto error; 
 						} );
 
@@ -305,14 +276,14 @@
 					dummy = NULL;
 						
 					CHECK_POSIX_DO( pthread_mutex_unlock( &ref->p_lock ), 
-							{ cleanup_lock(H_LOCK( dummy->p_hash )); goto error; } );
+							{ pthread_mutex_unlock(H_LOCK( dummy->p_hash )); goto error; } );
 						
 				}
 				pthread_cleanup_pop(1);
 				break;
 			
 			case ENOENT:
-				pthread_cleanup_push(cleanup_lock, H_LOCK( dummy->p_hash ));
+				pthread_cleanup_push(cleanup_mutex, H_LOCK( dummy->p_hash ));
 				/* This is a new peer, we must decide if we can handle it, then create a PSM for it. */
 				/*
 				   CERs received from unknown peers MAY be silently discarded, or a CEA
--- a/waaad/peer-struct.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/peer-struct.c	Thu Jan 08 15:36:06 2009 +0900
@@ -373,6 +373,18 @@
 	return 0;
 }
 
+void cleanup_peer ( void * peer )
+{
+	_peer_t * dum = _P(peer);
+
+	TRACE_ENTRY("%p", peer);
+	
+	CHECK_FCT_DO(  _peer_struct_destroy(&dum),  );
+	
+	/* End of cleanup handler */
+	return;
+}
+
 /* Finding a peer from a diameter id. diamid must be lowercase! 
  * Upon return, the hash line is locked and must be unlocked.
  * If item is found, it is returned in peer. otherwise, the next element is returned.
--- a/waaad/routing.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/routing.c	Thu Jan 08 15:36:06 2009 +0900
@@ -589,7 +589,7 @@
 			
 			CHECK_POSIX_DO( pthread_rwlock_rdlock(&fwd_lck), goto error );
 			
-			pthread_cleanup_push((void (*)())pthread_rwlock_unlock, &fwd_lck);
+			pthread_cleanup_push(cleanup_rwlock, &fwd_lck);
 
 			if (is_req) {
 				CHECK_FCT_DO( process_fwcb_list( &fwd_req, &msg ), goto error );
@@ -724,7 +724,7 @@
 	
 		/* call the rt_out_cb_t callbacks on the peers in the list */
 		CHECK_POSIX_DO( pthread_rwlock_rdlock(&out_lck), goto error );
-		pthread_cleanup_push((void (*)())pthread_rwlock_unlock, &out_lck);
+		pthread_cleanup_push(cleanup_rwlock, &out_lck);
 
 		CHECK_FCT_DO( process_outcb_list( &out_nrm, msg, list ), 
 			{
--- a/waaad/utils.c	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/utils.c	Thu Jan 08 15:36:06 2009 +0900
@@ -43,6 +43,74 @@
 #include "waaad-internal.h"
 
 /********************************************************************************************************/
+/*  CLEANUP routines */
+
+void cleanup_mutex(void * mutex)
+{
+	/* We have been canceled  */
+	TRACE_ENTRY("%p", mutex);
+	
+	CHECK_FCT_DO( pthread_mutex_unlock((pthread_mutex_t *)mutex), /* continue */);
+	
+	return;
+}
+	
+void cleanup_rwlock( void * rwlock )
+{
+	/* We have been canceled  */
+	TRACE_ENTRY("%p", rwlock);
+	
+	CHECK_FCT_DO( pthread_rwlock_unlock((pthread_rwlock_t *)rwlock), /* continue */);
+	
+	return;
+}
+	
+void cleanup_msg( void * msg )
+{
+	/* We have been canceled  */
+	TRACE_ENTRY("%p", msg);
+	
+	CHECK_FCT_DO( msg_free(msg, 1), /* continue */);
+	
+	return;
+}
+	
+void cleanup_msg_i( void * pmsg )
+{
+	/* We have been canceled  */
+	TRACE_ENTRY("%p", pmsg);
+	
+	if (pmsg) {
+		CHECK_FCT_DO( msg_free(*(msg_t **)pmsg, 1), /* continue */);
+	}
+	
+	return;
+}
+	
+void cleanup_buffer( void * buffer )
+{
+	/* We have been canceled  */
+	TRACE_ENTRY("%p", buffer);
+	
+	free(buffer);
+	
+	return;
+}
+	
+void cleanup_buffer_i( void * pbuffer )
+{
+	/* We have been canceled  */
+	TRACE_ENTRY("%p", pbuffer);
+	
+	if (pbuffer) {
+		free(*(void **)pbuffer);
+	}
+	
+	return;
+}
+
+
+/********************************************************************************************************/
 /* LISTS MANAGEMENT */
 
 /* Initialize a list element */
--- a/waaad/utils.h	Thu Jan 08 14:50:21 2009 +0900
+++ b/waaad/utils.h	Thu Jan 08 15:36:06 2009 +0900
@@ -360,6 +360,13 @@
 	return ret;
 }
 
+/* Cleanups for cancellation */
+void cleanup_mutex( void * mutex );
+void cleanup_rwlock( void * rwlock );
+void cleanup_msg( void * msg );
+void cleanup_msg_i( void * pmsg );
+void cleanup_buffer( void * buffer );
+void cleanup_buffer_i( void * pbuffer );
 
 /********************************************************************************************************/
 /*
"Welcome to our mercurial repository"