diff mbox series

[4/5] Add ability to detect if we're on the main thread.

Message ID 20200722053445.27987-5-nazard@nazar.ca (mailing list archive)
State New, archived
Headers show
Series libtirpc patches | expand

Commit Message

Doug Nazar July 22, 2020, 5:34 a.m. UTC
Signed-off-by: Doug Nazar <nazard@nazar.ca>
---
 src/mt_misc.c     | 17 +++++++++++++++++
 tirpc/reentrant.h |  1 +
 2 files changed, 18 insertions(+)

Comments

Steve Dickson July 29, 2020, 2:27 p.m. UTC | #1
On 7/22/20 1:34 AM, Doug Nazar wrote:
> Signed-off-by: Doug Nazar <nazard@nazar.ca>
> ---
>  src/mt_misc.c     | 17 +++++++++++++++++
>  tirpc/reentrant.h |  1 +
>  2 files changed, 18 insertions(+)
> 
> diff --git a/src/mt_misc.c b/src/mt_misc.c
> index 5a49b78..020b55d 100644
> --- a/src/mt_misc.c
> +++ b/src/mt_misc.c
> @@ -151,3 +151,20 @@ void tsd_key_delete(void)
>  	return;
>  }
>  
> +static pthread_t main_thread_id;
> +
> +__attribute__((constructor))
> +static void
> +get_thread_id(void)
> +{
> +	/* This will only work if we're opened by the main thread.
> +	 * Shouldn't be a problem in practice since we expect to be
> +	 * linked against, not dlopen() from a random thread.
> +	 */
> +	main_thread_id = pthread_self();
> +}
> +
> +int thr_main(void)
> +{
> +	return pthread_equal(main_thread_id, pthread_self());
> +}
> diff --git a/tirpc/reentrant.h b/tirpc/reentrant.h
> index 5bb581a..ee65454 100644
> --- a/tirpc/reentrant.h
> +++ b/tirpc/reentrant.h
> @@ -76,4 +76,5 @@
>  #define thr_self()		pthread_self()
>  #define thr_exit(x)		pthread_exit(x)
>  
> +extern int thr_main(void);
>  #endif
> 
Again... why is this needed? 

Your description part of these patches are a bit thin ;-)

steved.
diff mbox series

Patch

diff --git a/src/mt_misc.c b/src/mt_misc.c
index 5a49b78..020b55d 100644
--- a/src/mt_misc.c
+++ b/src/mt_misc.c
@@ -151,3 +151,20 @@  void tsd_key_delete(void)
 	return;
 }
 
+static pthread_t main_thread_id;
+
+__attribute__((constructor))
+static void
+get_thread_id(void)
+{
+	/* This will only work if we're opened by the main thread.
+	 * Shouldn't be a problem in practice since we expect to be
+	 * linked against, not dlopen() from a random thread.
+	 */
+	main_thread_id = pthread_self();
+}
+
+int thr_main(void)
+{
+	return pthread_equal(main_thread_id, pthread_self());
+}
diff --git a/tirpc/reentrant.h b/tirpc/reentrant.h
index 5bb581a..ee65454 100644
--- a/tirpc/reentrant.h
+++ b/tirpc/reentrant.h
@@ -76,4 +76,5 @@ 
 #define thr_self()		pthread_self()
 #define thr_exit(x)		pthread_exit(x)
 
+extern int thr_main(void);
 #endif