From 71e002b6dfa4042462da27896ca2901d077dbf11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Thu, 1 Apr 2010 07:20:19 +0000 Subject: made connection thread safe the connection thread modifies the object's state (and calls clock_delay()) since this is not thread safe, it is now protected by sys_lock() NOTE1: i remember someone saying that clock_delay() is thread safe NOTE2: this might still crash if the object is deleted before while the thread is executing svn path=/trunk/externals/iem/iemnet/; revision=13326 --- tcpclient.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'tcpclient.c') diff --git a/tcpclient.c b/tcpclient.c index 850736c..b7af925 100644 --- a/tcpclient.c +++ b/tcpclient.c @@ -30,8 +30,6 @@ #include - - static t_class *tcpclient_class; static char objName[] = "tcpclient"; @@ -55,7 +53,6 @@ typedef struct _tcpclient int x_port; // port we're connected to long x_addr; // address we're connected to as 32bit int - /* multithread stuff */ pthread_t x_threadid; /* id of child thread */ pthread_attr_t x_threadattr; /* attributes of child thread */ @@ -102,6 +99,9 @@ static void *tcpclient_child_connect(void *w) struct hostent *hp; int sockfd; + t_iemnet_sender*sender; + t_iemnet_receiver*receiver; + if (x->x_fd >= 0) { error("%s_connect: already connected", objName); @@ -136,16 +136,23 @@ static void *tcpclient_child_connect(void *w) sys_closesocket(sockfd); return (x); } + + sender=iemnet__sender_create(sockfd); + receiver=iemnet__receiver_create(sockfd, x, tcpclient_receive_callback); + + /* update the tcpclient-object (thread safe) */ + sys_lock(); x->x_fd = sockfd; x->x_addr = ntohl(*(long *)hp->h_addr); - x->x_sender=iemnet__sender_create(sockfd); - x->x_receiver=iemnet__receiver_create(sockfd, x, tcpclient_receive_callback); + x->x_sender=sender; + x->x_receiver=receiver; x->x_connectstate = 1; /* use callback to set outlet in main thread */ clock_delay(x->x_clock, 0); + sys_unlock(); return (x); } static void tcpclient_tick(t_tcpclient *x) -- cgit v1.2.1