From ebdef464c30bfc67dd18a74c20add3e579734048 Mon Sep 17 00:00:00 2001
From: edgrif <edgrif>
Date: Fri, 20 Mar 2009 12:40:44 +0000
Subject: [PATCH] add destroy func for new thread abnormal termination.

---
 src/include/ZMap/zmapServerProtocol.h      |  8 ++--
 src/zmapServer/zmapServerProtocolHandler.c | 43 ++++++++++++++++++++--
 2 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/src/include/ZMap/zmapServerProtocol.h b/src/include/ZMap/zmapServerProtocol.h
index f7aca6439..019394846 100755
--- a/src/include/ZMap/zmapServerProtocol.h
+++ b/src/include/ZMap/zmapServerProtocol.h
@@ -27,9 +27,9 @@
  *              structs that give all the information/fields for the request/reply.
  *              
  * HISTORY:
- * Last edited: Feb  4 14:56 2009 (edgrif)
+ * Last edited: Mar 20 12:04 2009 (edgrif)
  * Created: Wed Feb  2 11:47:16 2005 (edgrif)
- * CVS info:   $Id: zmapServerProtocol.h,v 1.19 2009-02-04 15:57:48 edgrif Exp $
+ * CVS info:   $Id: zmapServerProtocol.h,v 1.20 2009-03-20 12:40:44 edgrif Exp $
  *-------------------------------------------------------------------
  */
 #ifndef ZMAP_PROTOCOL_H
@@ -222,14 +222,12 @@ typedef union
 ZMAP_ENUM_AS_EXACT_STRING_DEC(zMapServerReqType2ExactStr, ZMapServerReqType) ;
 
 ZMapServerReqAny zMapServerRequestCreate(ZMapServerReqType request_type, ...) ;
-
 void zMapServerCreateRequestDestroy(ZMapServerReqAny request) ;
-
 ZMapThreadReturnCode zMapServerRequestHandler(void **slave_data,
 					      void *request_in, void **reply_out,
 					      char **err_msg_out) ;
-
 ZMapThreadReturnCode zMapServerTerminateHandler(void **slave_data, char **err_msg_out) ;
+ZMapThreadReturnCode zMapServerDestroyHandler(void **slave_data) ;
 
 
 #endif /* !ZMAP_PROTOCOL_H */
diff --git a/src/zmapServer/zmapServerProtocolHandler.c b/src/zmapServer/zmapServerProtocolHandler.c
index b025fef32..dbd55c481 100755
--- a/src/zmapServer/zmapServerProtocolHandler.c
+++ b/src/zmapServer/zmapServerProtocolHandler.c
@@ -25,9 +25,9 @@
  * Description: 
  * Exported functions: See ZMap/zmapServerProtocol.h
  * HISTORY:
- * Last edited: Mar 19 16:18 2009 (edgrif)
+ * Last edited: Mar 20 11:59 2009 (edgrif)
  * Created: Thu Jan 27 13:17:43 2005 (edgrif)
- * CVS info:   $Id: zmapServerProtocolHandler.c,v 1.39 2009-03-19 16:19:08 edgrif Exp $
+ * CVS info:   $Id: zmapServerProtocolHandler.c,v 1.40 2009-03-20 12:40:44 edgrif Exp $
  *-------------------------------------------------------------------
  */
 
@@ -86,6 +86,7 @@ static void protocolGlobalInitFunc(ZMapProtocolInitList protocols, ZMapURL url,
 static int findProtocol(gconstpointer list_protocol, gconstpointer protocol) ;
 static ZMapThreadReturnCode getSequence(ZMapServer server, ZMapServerReqGetSequence request, char **err_msg_out) ;
 static ZMapThreadReturnCode terminateServer(ZMapServer *server, char **err_msg_out) ;
+static ZMapThreadReturnCode destroyServer(ZMapServer *server) ;
 static gboolean haveRequiredStyles(GData *all_styles, GList *required_styles, char **missing_styles_out) ;
 static void findStyleCB(gpointer data, gpointer user_data) ;
 static gboolean getStylesFromFile(char *styles_list, char *styles_file, GData **styles_out) ;
@@ -514,6 +515,23 @@ ZMapThreadReturnCode zMapServerTerminateHandler(void **slave_data, char **err_ms
 }
 
 
+/* This function is called if a thread terminates in some abnormal way (e.g. is cancelled),
+ * it enables the server to clean up.  */
+ZMapThreadReturnCode zMapServerDestroyHandler(void **slave_data)
+{
+  ZMapThreadReturnCode thread_rc = ZMAPTHREAD_RETURNCODE_OK ;
+  ZMapServer server ;
+
+  zMapAssert(slave_data) ;
+
+  server = (ZMapServer)*slave_data ;
+
+  thread_rc = destroyServer(&server) ;
+
+  return thread_rc ;
+}
+
+
 
 /* Static/global list of protocols and whether their global init/cleanup functions have been
  * called. These are functions that must only be called once. */
@@ -631,8 +649,7 @@ static ZMapThreadReturnCode terminateServer(ZMapServer *server, char **err_msg_o
 {
   ZMapThreadReturnCode thread_rc = ZMAPTHREAD_RETURNCODE_OK ;
 
-  if (zMapServerCloseConnection(*server) == ZMAP_SERVERRESPONSE_OK
-      && zMapServerFreeConnection(*server) == ZMAP_SERVERRESPONSE_OK)
+  if (zMapServerCloseConnection(*server) == ZMAP_SERVERRESPONSE_OK)
     {
       *server = NULL ;
     }
@@ -647,6 +664,24 @@ static ZMapThreadReturnCode terminateServer(ZMapServer *server, char **err_msg_o
 
 
 
+static ZMapThreadReturnCode destroyServer(ZMapServer *server)
+{
+  ZMapThreadReturnCode thread_rc = ZMAPTHREAD_RETURNCODE_OK ;
+
+  if (zMapServerFreeConnection(*server) == ZMAP_SERVERRESPONSE_OK)
+    {
+      *server = NULL ;
+    }
+  else
+    {
+      thread_rc = ZMAPTHREAD_RETURNCODE_REQFAIL ;
+    }
+
+  return thread_rc ;
+}
+
+
+
 static gboolean haveRequiredStyles(GData *all_styles, GList *required_styles, char **missing_styles_out)
 {
   gboolean result = FALSE ;
-- 
GitLab