Skip to content

Commit f6f313c

Browse files
committed
module_mutex_lock: detect dead-locks
1 parent b9487fa commit f6f313c

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/module.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@
3535
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636
*/
3737

38-
#include <assert.h>
39-
#include <stdlib.h>
40-
#include <string.h>
38+
#include "module.h"
39+
40+
#include <assert.h> // for assert
41+
#include <errno.h> // for ETIMEDOUT
42+
#include <ctype.h> // for isdigit
43+
#include <pthread.h> // for pthread_mutex_init, pthread_mutex_destroy
44+
#include <stdio.h> // for printf, fprintf, putchar, snprintf, stderr
45+
#include <stdlib.h> // for free, atoi, calloc
46+
#include <string.h> // for strlen, strchr, memcpy, strdup, strncat
47+
#include <time.h> // for clock_gettime
4148

49+
#include "compat/strings.h" // for strcasecmp
4250
#include "debug.h"
43-
#include "module.h"
51+
#include "messaging.h" // for check_message, free_message, ...
4452
#include "utils/list.h"
4553
#include "utils/macros.h" // for ARR_COUNT, to_fourcc
4654

@@ -56,6 +64,25 @@ static void
5664
module_mutex_lock(pthread_mutex_t *lock)
5765
{
5866
MSG(DEBUG, "Locking lock %p\n", lock);
67+
68+
#if _POSIX_TIMEOUTS > 0 // macOS doesn't have pthread_mutex_timedlock()
69+
struct timespec ts;
70+
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
71+
perror("clock_gettime");
72+
} else {
73+
ts.tv_sec += 1;
74+
const int rc = pthread_mutex_timedlock(lock, &ts);
75+
if (rc == 0) {
76+
return;
77+
}
78+
if (rc == ETIMEDOUT) {
79+
bug_msg(LOG_LEVEL_ERROR, MOD_NAME
80+
"Waiting for lock, possible deadlock... ");
81+
} else {
82+
perror("pthread_mutex_timedlock");
83+
}
84+
}
85+
#endif
5986
pthread_mutex_lock(lock);
6087
}
6188

0 commit comments

Comments
 (0)