Skip to content

Instantly share code, notes, and snippets.

@alfredh
Created August 10, 2021 17:44
Show Gist options
  • Save alfredh/060cad7a99ece09702dfc884120e7e84 to your computer and use it in GitHub Desktop.
Save alfredh/060cad7a99ece09702dfc884120e7e84 to your computer and use it in GitHub Desktop.
diff --git a/src/main/main.c b/src/main/main.c
index 78a47e3..7858aa9 100644
--- a/src/main/main.c
+++ b/src/main/main.c
@@ -115,6 +115,7 @@ struct re {
#ifdef HAVE_PTHREAD
pthread_mutex_t mutex; /**< Mutex for thread synchronization */
pthread_mutex_t *mutexp; /**< Pointer to active mutex */
+ pthread_t tid;
#endif
};
@@ -145,12 +146,16 @@ static struct re global_re = {
PTHREAD_MUTEX_INITIALIZER,
#endif
&global_re.mutex,
+ 0
#endif
};
#ifdef HAVE_PTHREAD
+extern pthread_t re_thread_get(void);
+
+
static void poll_close(struct re *re);
static pthread_once_t pt_once = PTHREAD_ONCE_INIT;
@@ -181,6 +186,11 @@ static struct re *re_get(void)
re = &global_re;
}
+ if (!re->tid) {
+ re->tid = pthread_self();
+ re_printf(" *** RE MAIN THREAD INITED: tid=%p\n", re->tid);
+ }
+
return re;
}
@@ -599,6 +609,15 @@ int fd_listen(int fd, int flags, fd_h *fh, void *arg)
DEBUG_INFO("fd_listen: fd=%d flags=0x%02x\n", fd, flags);
+#ifdef HAVE_PTHREAD
+ if (!pthread_equal(pthread_self(), re_thread_get())) {
+ DEBUG_WARNING("fd_listen: called from a NON-RE thread"
+ " (fd=%d, flags=0x%x, handler=%p, arg=%p)\n",
+ fd, flags, fh, arg);
+ return EPERM;
+ }
+#endif
+
if (fd < 0) {
DEBUG_WARNING("fd_listen: corrupt fd %d\n", fd);
return EBADF;
@@ -1259,3 +1278,11 @@ struct list *tmrl_get(void)
{
return &re_get()->tmrl;
}
+
+
+#ifdef HAVE_PTHREAD
+pthread_t re_thread_get(void)
+{
+ return re_get()->tid;
+}
+#endif
diff --git a/src/tmr/tmr.c b/src/tmr/tmr.c
index 5e9022c..a6ce86c 100644
--- a/src/tmr/tmr.c
+++ b/src/tmr/tmr.c
@@ -268,6 +268,11 @@ void tmr_init(struct tmr *tmr)
}
+#ifdef HAVE_PTHREAD
+ extern pthread_t re_thread_get(void);
+#endif
+
+
/**
* Start a timer
*
@@ -284,6 +289,16 @@ void tmr_start(struct tmr *tmr, uint64_t delay, tmr_h *th, void *arg)
if (!tmr)
return;
+#ifdef HAVE_PTHREAD
+
+ if (!pthread_equal(pthread_self(), re_thread_get())) {
+ DEBUG_WARNING("tmr_start: called from a NON-RE thread"
+ " (tmr=%p, delay=%llu, handler=%p, arg=%p)\n",
+ tmr, delay, th, arg);
+ BREAKPOINT;
+ }
+#endif
+
if (tmr->th) {
list_unlink(&tmr->le);
}
@alfredh
Copy link
Author

alfredh commented Jan 25, 2022

that would be great.

it would catch many bugs, that is hard for application developers to find.

@alfredh
Copy link
Author

alfredh commented Jun 6, 2022

hi @juha-h here is the patch, please apply locally and test

@juha-h
Copy link

juha-h commented Jun 6, 2022 via email

@juha-h
Copy link

juha-h commented Jun 6, 2022 via email

@sreimers
Copy link

sreimers commented Jun 6, 2022

I think it would always show a different thread id since juha is using re_thread_enter/re_thread_leave from another thread https://github.com/baresip/baresip/wiki/Using-baresip-as-a-library#examples-using-re-lock
So it needs maybe a slight modification for this use case anyway. Will try to bring this patch upstream with re_thread_enter detection, since its really useful for finding these kinds of bugs.

@sreimers
Copy link

sreimers commented Jun 6, 2022

Ok you can give this PR a try: baresip/re#389

@juha-h
Copy link

juha-h commented Jun 6, 2022 via email

@juha-h
Copy link

juha-h commented Jun 6, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment