From 4786c27f190837f7184ec2450c316083b79dda90 Mon Sep 17 00:00:00 2001 From: Luke Go Date: Tue, 6 Aug 2019 17:31:50 +0900 Subject: [PATCH] wiringPi: pthread: add mutex lock to avoid null pointer error. - In some case, ISR callback function pointer can be null pointer untill pthread is not killed. So add mutext to avoid this problem and exit it. Signed-off-by: Luke Go --- wiringPi/wiringPi.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 944b732..805e9a7 100755 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -630,8 +630,15 @@ static void *interruptHandler (UNU void *arg) pinPass = -1 ; for (;;) - if (waitForInterrupt (myPin, -1) > 0) + if (waitForInterrupt (myPin, -1) > 0) { + pthread_mutex_lock (&pinMutex) ; + if (libwiring.isrFunctions[PIN_NUM_CALC_SYSFD(myPin)] == 0) { + pthread_mutex_unlock (&pinMutex) ; + break; + } libwiring.isrFunctions [PIN_NUM_CALC_SYSFD(myPin)] () ; + pthread_mutex_unlock (&pinMutex) ; + } return NULL ; } @@ -767,8 +774,10 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) delay (1) ; pthread_mutex_unlock (&pinMutex) ; + pthread_mutex_lock (&pinMutex) ; libwiring.isrFunctions [PIN_NUM_CALC_SYSFD(GpioPin)] = function ; libwiring.isrThreadIds [PIN_NUM_CALC_SYSFD(GpioPin)] = threadId ; + pthread_mutex_unlock (&pinMutex) ; return 0 ; } @@ -799,8 +808,10 @@ int wiringPiISRCancel(int pin) { "%s: wiringPiISRCancel: Unregister for the interrupt pin failed!\n", __func__); else { + pthread_mutex_lock (&pinMutex) ; libwiring.isrFunctions[PIN_NUM_CALC_SYSFD(GpioPin)] = NULL; libwiring.isrThreadIds[PIN_NUM_CALC_SYSFD(GpioPin)] = 0; + pthread_mutex_unlock (&pinMutex) ; } return 0;