Skip to content
Snippets Groups Projects
Commit ac175a27 authored by edgrif's avatar edgrif
Browse files

Add more error handling code to thread creation code.

Update routines to stringify request/raply enums.
parent 3715a4f3
No related branches found
No related tags found
No related merge requests found
......@@ -26,19 +26,21 @@
* Description:
* Exported functions: See XXXXXXXXXXXXX.h
* HISTORY:
* Last edited: Jan 22 13:52 2004 (edgrif)
* Last edited: Mar 2 10:39 2004 (edgrif)
* Created: Thu Jul 24 14:37:18 2003 (edgrif)
* CVS info: $Id: zmapConn.c,v 1.2 2004-01-23 13:27:53 edgrif Exp $
* CVS info: $Id: zmapConn.c,v 1.3 2004-03-03 12:04:18 edgrif Exp $
*-------------------------------------------------------------------
*/
#include <string.h>
#include <zmapConn_P.h>
/* Turn on/off all debugging messages for threads. */
gboolean zmap_thr_debug_G = TRUE ;
static ZMapConnection createConnection(char *machine, char *port, char *protocol, char *sequence) ;
static void destroyConnection(ZMapConnection connection) ;
ZMapConnection zMapConnCreate(char *machine, char *port, char *protocol, char *sequence)
......@@ -46,50 +48,59 @@ ZMapConnection zMapConnCreate(char *machine, char *port, char *protocol, char *s
ZMapConnection connection ;
pthread_t thread_id ;
pthread_attr_t thread_attr ;
int status ;
connection = g_new(ZMapConnectionStruct, sizeof(ZMapConnectionStruct)) ;
int status = 0 ;
connection->machine = g_strdup(machine) ;
connection->port = atoi(port) ;
connection->protocol = g_strdup(protocol) ;
connection->sequence = g_strdup(sequence) ;
connection = createConnection(machine, port, protocol, sequence) ;
/* ok to just set state here because we have not started the thread yet.... */
zmapCondVarCreate(&(connection->request)) ;
connection->request.state = ZMAP_REQUEST_WAIT ;
zmapVarCreate(&(connection->reply)) ;
#ifdef ED_G_NEVER_INCLUDE_THIS_CODE
connection->reply.state = ZMAP_REPLY_INIT ;
#endif /* ED_G_NEVER_INCLUDE_THIS_CODE */
connection->reply.state = ZMAP_REPLY_WAIT ;
/* Set the new threads attributes so it will run "detached", we do not want anything from them.
* when they die, we want them to go away and release their resources. */
if ((status = pthread_attr_init(&thread_attr)) != 0)
if (status == 0
&& (status = pthread_attr_init(&thread_attr)) != 0)
{
ZMAPSYSERR(status, "Create thread attibutes") ;
}
if ((status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) != 0)
if (status == 0
&& (status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) != 0)
{
ZMAPSYSERR(status, "Set thread detached attibute") ;
}
/* Create the new thread. */
if ((status = pthread_create(&thread_id, &thread_attr, zmapNewThread, (void *)connection)) != 0)
if (status == 0
&& (status = pthread_create(&thread_id, &thread_attr, zmapNewThread, (void *)connection)) != 0)
{
ZMAPSYSERR(status, "Thread creation") ;
}
connection->thread_id = thread_id ;
if (status == 0)
connection->thread_id = thread_id ;
else
{
/* Ok to just destroy connection here as the thread was not successfully created so
* there can be no complications with interactions with condvars in connect struct. */
destroyConnection(connection) ;
connection = NULL ;
}
return connection ;
}
void zMapConnLoadData(ZMapConnection connection)
{
zmapCondVarSignal(&connection->request, ZMAP_REQUEST_GETDATA) ;
......@@ -129,13 +140,33 @@ gboolean zMapConnGetReplyWithData(ZMapConnection connection, ZMapThreadReply *st
void *zMapConnGetThreadid(ZMapConnection connection)
{
return (void *)(connection->thread_id) ;
}
/* Must be kept in step with declaration of ZMapThreadRequest enums in zmapConn_P.h */
char *zMapVarGetRequestString(ZMapThreadRequest signalled_state)
{
char *str_states[] = {"ZMAP_REQUEST_INIT", "ZMAP_REQUEST_WAIT", "ZMAP_REQUEST_TIMED_OUT",
"ZMAP_REQUEST_GETDATA"} ;
return str_states[signalled_state] ;
}
/* Must be kept in step with declaration of ZMapThreadReply enums in zmapConn_P.h */
char *zMapVarGetReplyString(ZMapThreadReply signalled_state)
{
char *str_states[] = {"ZMAP_REPLY_INIT", "ZMAP_REPLY_WAIT",
"ZMAP_REPLY_GOTDATA", "ZMAP_REPLY_DIED", "ZMAP_REPLY_CANCELLED"} ;
return str_states[signalled_state] ;
}
/* Kill the thread by cancelling it, as this will asynchronously we cannot release the connections
* resources in this call. */
void zMapConnKill(ZMapConnection connection)
......@@ -144,10 +175,10 @@ void zMapConnKill(ZMapConnection connection)
ZMAP_THR_DEBUG(("GUI: killing and destroying connection for thread %x\n", connection->thread_id)) ;
/* Really we should signal an exit here...but that lead to deadlocks, think about this bit.. */
/* we could signal an exit here by setting a condvar of EXIT...but that might lead to
* deadlocks, think about this bit.. */
/* Signal the thread to cancel it, we could set a cond var of EXIT but this will take ages
* for the thread to be cancelled. */
/* Signal the thread to cancel it */
if ((status = pthread_cancel(connection->thread_id)) != 0)
{
ZMAPSYSERR(status, "Thread cancel") ;
......@@ -182,3 +213,39 @@ void zMapConnDestroy(ZMapConnection connection)
* --------------------- Internal routines ------------------------------
*/
static ZMapConnection createConnection(char *machine, char *port, char *protocol, char *sequence)
{
ZMapConnection connection ;
connection = g_new(ZMapConnectionStruct, sizeof(ZMapConnectionStruct)) ;
connection->machine = g_strdup(machine) ;
connection->port = atoi(port) ;
connection->protocol = g_strdup(protocol) ;
connection->sequence = g_strdup(sequence) ;
return connection ;
}
/* some care needed in using this....what about the condvars, when can they be freed ?
* CHECK THIS ALL WORKS.... */
static void destroyConnection(ZMapConnection connection)
{
g_free(connection->machine) ;
g_free(connection->protocol) ;
g_free(connection->sequence) ;
/* Setting this to zero prevents subtle bugs where calling code continues
* to try to reuse a defunct control block. */
/* Should crash if this returns NULL, need my macros from acedb code.... */
memset((void *)connection, 0, sizeof(ZMapConnectionStruct)) ;
g_free(connection) ;
return ;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment