diff --git a/pigpio.c b/pigpio.c index ff1fb5d..a9c9190 100644 --- a/pigpio.c +++ b/pigpio.c @@ -1240,6 +1240,9 @@ static uint32_t sockNetAddr[MAX_CONNECT_ADDRESSES]; static int numSockNetAddr = 0; +// sif_mutex is used to run socket interface single threaded +static pthread_mutex_t sif_mutex = PTHREAD_MUTEX_INITIALIZER; + static uint32_t reportedLevel = 0; static int waveClockInited = 0; @@ -1868,6 +1871,8 @@ static int myDoCommand(uintptr_t *p, unsigned bufSize, char *buf) int masked; res = 0; + pthread_mutex_lock(&sif_mutex); + switch (p[0]) { case PI_CMD_BC1: @@ -2514,6 +2519,7 @@ static int myDoCommand(uintptr_t *p, unsigned bufSize, char *buf) break; } + pthread_mutex_unlock(&sif_mutex); return res; }