aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2010-04-01 07:20:19 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2010-04-01 07:20:19 +0000
commit71e002b6dfa4042462da27896ca2901d077dbf11 (patch)
treee72d636365fe9434cb2ac4c514aa42bb89c2f7fe
parent698506ba98a73f3484b7c7f93d18455b3744c9fd (diff)
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
-rw-r--r--tcpclient.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/tcpclient.c b/tcpclient.c
index 850736c..b7af925 100644
--- a/tcpclient.c
+++ b/tcpclient.c
@@ -30,8 +30,6 @@
#include <pthread.h>
-
-
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)