Commit 2d255174 authored by Juergen Kosel's avatar Juergen Kosel

Lock the socket_mutex inside Socket_getReadySocket()

To reduce the performance penalty of commit 779ca42a
Ian has recommended to release the socket_mutex during the call of select.
See https://github.com/eclipse/paho.mqtt.c/issues/385#issuecomment-372299490

As a preparation for this, the locking of the socket_mutex is moved inside
the Socket_getReadySocket() function.
Signed-off-by: 's avatarJuergen Kosel <juergen.kosel@softing.com>
parent 3bdd5b5d
...@@ -2967,10 +2967,8 @@ static MQTTPacket* MQTTAsync_cycle(int* sock, unsigned long timeout, int* rc) ...@@ -2967,10 +2967,8 @@ static MQTTPacket* MQTTAsync_cycle(int* sock, unsigned long timeout, int* rc)
if ((*sock = SSLSocket_getPendingRead()) == -1) if ((*sock = SSLSocket_getPendingRead()) == -1)
{ {
#endif #endif
Thread_lock_mutex(socket_mutex);
/* 0 from getReadySocket indicates no work to do, -1 == error, but can happen normally */ /* 0 from getReadySocket indicates no work to do, -1 == error, but can happen normally */
*sock = Socket_getReadySocket(0, &tp); *sock = Socket_getReadySocket(0, &tp,socket_mutex);
Thread_unlock_mutex(socket_mutex);
if (!tostop && *sock == 0 && (tp.tv_sec > 0L || tp.tv_usec > 0L)) if (!tostop && *sock == 0 && (tp.tv_sec > 0L || tp.tv_usec > 0L))
MQTTAsync_sleep(100L); MQTTAsync_sleep(100L);
#if defined(OPENSSL) #if defined(OPENSSL)
......
...@@ -1684,9 +1684,7 @@ static MQTTPacket* MQTTClient_cycle(int* sock, unsigned long timeout, int* rc) ...@@ -1684,9 +1684,7 @@ static MQTTPacket* MQTTClient_cycle(int* sock, unsigned long timeout, int* rc)
{ {
/* 0 from getReadySocket indicates no work to do, -1 == error, but can happen normally */ /* 0 from getReadySocket indicates no work to do, -1 == error, but can happen normally */
#endif #endif
Thread_lock_mutex(socket_mutex); *sock = Socket_getReadySocket(0, &tp, socket_mutex);
*sock = Socket_getReadySocket(0, &tp);
Thread_unlock_mutex(socket_mutex);
#if defined(OPENSSL) #if defined(OPENSSL)
} }
#endif #endif
......
...@@ -228,7 +228,7 @@ int isReady(int socket, fd_set* read_set, fd_set* write_set) ...@@ -228,7 +228,7 @@ int isReady(int socket, fd_set* read_set, fd_set* write_set)
* @param tp the timeout to be used for the select, unless overridden * @param tp the timeout to be used for the select, unless overridden
* @return the socket next ready, or 0 if none is ready * @return the socket next ready, or 0 if none is ready
*/ */
int Socket_getReadySocket(int more_work, struct timeval *tp) int Socket_getReadySocket(int more_work, struct timeval *tp, mutex_type mutex)
{ {
int rc = 0; int rc = 0;
static struct timeval zero = {0L, 0L}; /* 0 seconds */ static struct timeval zero = {0L, 0L}; /* 0 seconds */
...@@ -236,6 +236,7 @@ int Socket_getReadySocket(int more_work, struct timeval *tp) ...@@ -236,6 +236,7 @@ int Socket_getReadySocket(int more_work, struct timeval *tp)
struct timeval timeout = one; struct timeval timeout = one;
FUNC_ENTRY; FUNC_ENTRY;
Thread_lock_mutex(mutex);
if (s.clientsds->count == 0) if (s.clientsds->count == 0)
goto exit; goto exit;
...@@ -301,6 +302,7 @@ int Socket_getReadySocket(int more_work, struct timeval *tp) ...@@ -301,6 +302,7 @@ int Socket_getReadySocket(int more_work, struct timeval *tp)
ListNextElement(s.clientsds, &s.cur_clientsds); ListNextElement(s.clientsds, &s.cur_clientsds);
} }
exit: exit:
Thread_unlock_mutex(mutex);
FUNC_EXIT_RC(rc); FUNC_EXIT_RC(rc);
return rc; return rc;
} /* end getReadySocket */ } /* end getReadySocket */
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#if !defined(SOCKET_H) #if !defined(SOCKET_H)
#define SOCKET_H #define SOCKET_H
#include "Thread.h" /* Needed for mutex_type */
#include <sys/types.h> #include <sys/types.h>
#if defined(WIN32) || defined(WIN64) #if defined(WIN32) || defined(WIN64)
...@@ -124,7 +125,7 @@ typedef struct ...@@ -124,7 +125,7 @@ typedef struct
void Socket_outInitialize(void); void Socket_outInitialize(void);
void Socket_outTerminate(void); void Socket_outTerminate(void);
int Socket_getReadySocket(int more_work, struct timeval *tp); int Socket_getReadySocket(int more_work, struct timeval *tp,mutex_type mutex);
int Socket_getch(int socket, char* c); int Socket_getch(int socket, char* c);
char *Socket_getdata(int socket, size_t bytes, size_t* actual_len); char *Socket_getdata(int socket, size_t bytes, size_t* actual_len);
int Socket_putdatas(int socket, char* buf0, size_t buf0len, int count, char** buffers, size_t* buflens, int* frees); int Socket_putdatas(int socket, char* buf0, size_t buf0len, int count, char** buffers, size_t* buflens, int* frees);
......
Markdown is supported
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