[PATCH] D60285: Make calls into the pthread library use weak symbols.

Sterling Augustine via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 4 14:10:56 PDT 2019


saugustine created this revision.
saugustine added reviewers: ldionne, grosbach, kledzik.
Herald added subscribers: llvm-commits, jfb, dexonsmith, christof.
Herald added a project: LLVM.

This makes the formerly hard dependency on libpthread into a weak
dependency.


Repository:
  rL LLVM

https://reviews.llvm.org/D60285

Files:
  libunwind/src/RWMutex.hpp


Index: libunwind/src/RWMutex.hpp
===================================================================
--- libunwind/src/RWMutex.hpp
+++ libunwind/src/RWMutex.hpp
@@ -58,12 +58,34 @@
 
 #else
 
+extern "C" int __attribute__((weak))
+pthread_create(pthread_t *thread, const pthread_attr_t *attr,
+               void *(*start_routine)(void *), void *arg);
+extern "C" int __attribute__((weak))
+pthread_rwlock_rdlock(pthread_rwlock_t *lock);
+extern "C" int __attribute__((weak))
+pthread_rwlock_wrlock(pthread_rwlock_t *lock);
+extern "C" int __attribute__((weak))
+pthread_rwlock_unlock(pthread_rwlock_t *lock);
+
+// Calls to the locking functions are gated on pthread_create, and not the
+// functions themselves, because the data structure should only be locked if
+// another thread has been created. This is what similar libraries do.
+
 class _LIBUNWIND_HIDDEN RWMutex {
 public:
-  bool lock_shared() { return pthread_rwlock_rdlock(&_lock) == 0; }
-  bool unlock_shared() { return pthread_rwlock_unlock(&_lock) == 0; }
-  bool lock() { return pthread_rwlock_wrlock(&_lock) == 0; }
-  bool unlock() { return pthread_rwlock_unlock(&_lock) == 0; }
+  bool lock_shared() {
+    return !pthread_create || (pthread_rwlock_rdlock(&_lock) == 0);
+  }
+  bool unlock_shared() {
+    return !pthread_create || (pthread_rwlock_unlock(&_lock) == 0);
+  }
+  bool lock() {
+    return !pthread_create || (pthread_rwlock_wrlock(&_lock) == 0);
+  }
+  bool unlock() {
+    return !pthread_create || (pthread_rwlock_unlock(&_lock) == 0);
+  }
 
 private:
   pthread_rwlock_t _lock = PTHREAD_RWLOCK_INITIALIZER;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60285.193780.patch
Type: text/x-patch
Size: 1601 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190404/c0b75091/attachment.bin>


More information about the llvm-commits mailing list