git: ad056b5d35d9 - main - libthr: trigger library initialization on rwlock calls

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sun, 16 Jul 2023 12:34:50 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=ad056b5d35d9957b1bd023abeb6461601449b725

commit ad056b5d35d9957b1bd023abeb6461601449b725
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-07-13 15:56:11 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-07-16 12:34:34 +0000

    libthr: trigger library initialization on rwlock calls
    
    Reviewed by:    tijl
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
---
 lib/libthr/thread/thr_rwlock.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/lib/libthr/thread/thr_rwlock.c b/lib/libthr/thread/thr_rwlock.c
index 135d565b0d42..af32b09963d9 100644
--- a/lib/libthr/thread/thr_rwlock.c
+++ b/lib/libthr/thread/thr_rwlock.c
@@ -163,6 +163,7 @@ int
 _thr_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
 {
 
+	_thr_check_init();
 	*rwlock = NULL;
 	return (rwlock_init(rwlock, attr));
 }
@@ -231,6 +232,7 @@ rwlock_rdlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime)
 int
 _Tthr_rwlock_rdlock(pthread_rwlock_t *rwlock)
 {
+	_thr_check_init();
 	return (rwlock_rdlock_common(rwlock, NULL));
 }
 
@@ -238,21 +240,24 @@ int
 _pthread_rwlock_timedrdlock(pthread_rwlock_t * __restrict rwlock,
     const struct timespec * __restrict abstime)
 {
+	_thr_check_init();
 	return (rwlock_rdlock_common(rwlock, abstime));
 }
 
 int
 _Tthr_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
 {
-	struct pthread *curthread = _get_curthread();
+	struct pthread *curthread;
 	pthread_rwlock_t prwlock;
 	int flags;
 	int ret;
 
+	_thr_check_init();
 	ret = check_and_init_rwlock(rwlock, &prwlock);
 	if (ret != 0)
 		return (ret);
 
+	curthread = _get_curthread();
 	if (curthread->rdlock_count) {
 		/*
 		 * To avoid having to track all the rdlocks held by
@@ -280,14 +285,16 @@ _Tthr_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
 int
 _Tthr_rwlock_trywrlock(pthread_rwlock_t *rwlock)
 {
-	struct pthread *curthread = _get_curthread();
+	struct pthread *curthread;
 	pthread_rwlock_t prwlock;
 	int ret;
 
+	_thr_check_init();
 	ret = check_and_init_rwlock(rwlock, &prwlock);
 	if (ret != 0)
 		return (ret);
 
+	curthread = _get_curthread();
 	ret = _thr_rwlock_trywrlock(&prwlock->lock);
 	if (ret == 0)
 		prwlock->owner = TID(curthread);
@@ -343,6 +350,7 @@ rwlock_wrlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime)
 int
 _Tthr_rwlock_wrlock(pthread_rwlock_t *rwlock)
 {
+	_thr_check_init();
 	return (rwlock_wrlock_common(rwlock, NULL));
 }
 
@@ -350,6 +358,7 @@ int
 _pthread_rwlock_timedwrlock(pthread_rwlock_t * __restrict rwlock,
     const struct timespec * __restrict abstime)
 {
+	_thr_check_init();
 	return (rwlock_wrlock_common(rwlock, abstime));
 }