From 304e91bff06c60922bb46a31b2745d3b65661bdf Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <vital.had@gmail.com>
Date: Wed, 15 Feb 2023 20:20:17 +0800
Subject: [PATCH] osd.h: fix spinlocks for macOS

---
 include/osx/osd.h | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/include/osx/osd.h b/include/osx/osd.h
index a7eace242..b88074b82 100644
--- modules/libfabric/include/osx/osd.h
+++ modules/libfabric/include/osx/osd.h
@@ -50,6 +50,8 @@
 
 #include <limits.h>
 
+#include <AvailabilityMacros.h>
+
 #include "unix/osd.h"
 #include "rdma/fi_errno.h"
 #include "config.h"
@@ -171,7 +173,12 @@ ssize_t ofi_recvmsg_tcp(SOCKET fd, struct msghdr *msg, int flags);
  * used os_unfair_lock to implement pthread_spinlock.
  * os_unfair_lock does not enforce fairness or lock ordering (hence
  * the name unfair), which is similar to pthread_spinlock.
+ * New code supported only on 10.12+: https://developer.apple.com/documentation/os/os_unfair_lock
+ * Fallback: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/spinlock.3.html
  */
+
+#if __MAC_OS_X_VERSION_MIN_REQUIRED > 101100
+
 #include <os/lock.h>
 
 typedef os_unfair_lock pthread_spinlock_t;
@@ -204,6 +211,42 @@ static inline int pthread_spin_destroy(pthread_spinlock_t *lock)
 	return 0;
 }
 
+#else
+
+#include <libkern/OSAtomic.h>
+
+typedef OSSpinLock pthread_spinlock_t;
+
+static inline int pthread_spin_init(pthread_spinlock_t *lock, int type)
+{
+	*lock = OS_SPINLOCK_INIT;
+	return 0;
+}
+
+static inline int pthread_spin_lock(pthread_spinlock_t *lock)
+{
+	OSSpinLockLock(lock);
+	return 0;
+}
+
+static inline int pthread_spin_unlock(pthread_spinlock_t *lock)
+{
+	OSSpinLockUnlock(lock);
+	return 0;
+}
+
+static inline int pthread_spin_trylock(pthread_spinlock_t *lock)
+{
+	return OSSpinLockTry(lock) ? 0 : EBUSY;
+}
+
+static inline int pthread_spin_destroy(pthread_spinlock_t *lock)
+{
+	return 0;
+}
+
+#endif
+
 #ifdef __cplusplus
 }
 #endif
