Module Name:    src
Committed By:   lukem
Date:           Tue May 23 19:53:25 UTC 2023

Modified Files:
        src/external/cddl/osnet/dist/tools/ctf/cvt: barrier.c barrier.h
        src/tools/compat: configure.ac

Log Message:
ctfmerge: fix macOS semaphore implementation

Use dispatch_semaphore_create() if present instead of sem_init().

macOS doesn't actually implement sem_init() (et al)
(even though it provides the prototypes as deprecated).
This was detected by the previous commit to ctfmerge
that added error handling.

Implement ctfmerge's barrier operations in terms of
dispatch(3) APIs such as dispatch_semaphore_create() (et al).

Update tools/compat/configure.ac to find dispatch_semaphore_create().

Fixes ctfmerge on macOS hosts.

Inspired by https://stackoverflow.com/a/27847103.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.c
cvs rdiff -u -r1.3 -r1.4 src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.h
cvs rdiff -u -r1.99 -r1.100 src/tools/compat/configure.ac

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.c
diff -u src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.c:1.6 src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.c:1.7
--- src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.c:1.6	Tue May 23 18:54:58 2023
+++ src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.c	Tue May 23 19:53:24 2023
@@ -60,6 +60,9 @@ barrier_init(barrier_t *bar, int nthread
 #ifdef illumos
 	if ((errno = sema_init(&bar->bar_sem, 0, USYNC_THREAD, NULL)) != 0)
 		terminate("%s: sema_init(bar_sem)", __func__);
+#elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE)
+	if ((bar->bar_sem = dispatch_semaphore_create(0)) == NULL)
+		terminate("%s: dispatch_semaphore_create()\n", __func__);
 #else
 	if (sem_init(&bar->bar_sem, 0, 0) == -1)
 		terminate("%s: sem_init(bar_sem)", __func__);
@@ -72,6 +75,9 @@ barrier_init(barrier_t *bar, int nthread
 int
 barrier_wait(barrier_t *bar)
 {
+#if defined(HAVE_DISPATCH_SEMAPHORE_CREATE)
+	long error;
+#endif
 	if ((errno = pthread_mutex_lock(&bar->bar_lock)) != 0)
 		terminate("%s: pthread_mutex_lock(bar_lock)", __func__);
 
@@ -82,6 +88,10 @@ barrier_wait(barrier_t *bar)
 #ifdef illumos
 		if ((errno = sema_wait(&bar->bar_sem)) != 0)
 			terminate("%s: sema_wait(bar_sem)", __func__);
+#elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE)
+		if ((error = dispatch_semaphore_wait(bar->bar_sem, DISPATCH_TIME_FOREVER)) != 0)
+			terminate("%s: dispatch_semaphore_wait(bar_sem) = %ld\n",
+			    __func__, error);
 #else
 		if (sem_wait(&bar->bar_sem) == -1)
 			terminate("%s: sem_wait(bar_sem)", __func__);
@@ -94,14 +104,19 @@ barrier_wait(barrier_t *bar)
 
 		/* reset for next use */
 		bar->bar_numin = 0;
-		for (i = 1; i < bar->bar_nthr; i++)
+		for (i = 1; i < bar->bar_nthr; i++) {
 #ifdef illumos
 			if ((errno = sema_post(&bar->bar_sem)) != 0)
 				terminate("%s: sema_post(bar_sem)", __func__);
+#elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE)
+			if ((error = dispatch_semaphore_signal(bar->bar_sem)) != 0)
+				terminate("%s: dispatch_semaphore_signal(bar_sem) = %ld\n",
+				    __func__, error);
 #else
 			if (sem_post(&bar->bar_sem) == -1)
 				terminate("%s: sem_post(bar_sem)", __func__);
 #endif
+		}
 		if ((errno = pthread_mutex_unlock(&bar->bar_lock)) != 0)
 			terminate("%s: pthread_mutex_unlock(bar_lock)",
 			    __func__);

Index: src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.h
diff -u src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.h:1.3 src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.h:1.4
--- src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.h:1.3	Mon May 28 21:05:06 2018
+++ src/external/cddl/osnet/dist/tools/ctf/cvt/barrier.h	Tue May 23 19:53:24 2023
@@ -33,8 +33,15 @@
  * APIs for the barrier synchronization primitive.
  */
 
+#ifdef HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
 #ifdef illumos
 #include <synch.h>
+#elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE)
+#include <dispatch/dispatch.h>
+typedef dispatch_semaphore_t sema_t;
 #else
 #include <semaphore.h>
 typedef sem_t	sema_t;

Index: src/tools/compat/configure.ac
diff -u src/tools/compat/configure.ac:1.99 src/tools/compat/configure.ac:1.100
--- src/tools/compat/configure.ac:1.99	Thu Feb 25 13:41:58 2021
+++ src/tools/compat/configure.ac	Tue May 23 19:53:24 2023
@@ -1,10 +1,10 @@
-#	$NetBSD: configure.ac,v 1.99 2021/02/25 13:41:58 christos Exp $
+#	$NetBSD: configure.ac,v 1.100 2023/05/23 19:53:24 lukem Exp $
 #
 # Autoconf definition file for libnbcompat.
 #
 # When you edit configure.ac:
 # 0. Create the tools versions of autoconf and autoheader:
-#        cd ${SRCDIR} && build.sh -V MKMAINTAINERTOOLS=yes tools
+#        cd ${SRCDIR} && ./build.sh -V MKMAINTAINERTOOLS=yes tools
 #    (This might not work if you try it after editing configure.ac.)
 # 1. edit configure.ac
 # 2. Regenerate "configure" and "nbtool_config.h.in" from "configure.ac":
@@ -12,7 +12,7 @@
 #    (Please don't use a non-tools version of autoconf or autoheader.)
 # 3. Test that the tools still build:
 #        mv ${TOOLDIR} ${TOOLDIR}.bak
-#        cd ${SRCDIR} && build.sh -V MKMAINTAINERTOOLS=yes tools
+#        cd ${SRCDIR} && ./build.sh -V MKMAINTAINERTOOLS=yes tools
 # 4. cvs commit files that you edited.
 # 5. Regen again, to pick up changed RCS IDs from the above commit:
 #        cd ${SRCDIR}/tools/compat && ${TOOLDIR}/bin/nbmake-${MACHINE} regen
@@ -220,6 +220,7 @@ AC_CHECK_DECLS(sys_signame,,, [#include 
 # Library functions (where a .h check isn't enough).
 AC_FUNC_ALLOCA
 AC_CHECK_FUNCS(atoll asprintf asnprintf basename devname dirfd dirname \
+	dispatch_semaphore_create \
 	dprintf esetfunc fgetln flock fpurge __fpurge futimes getline \
 	getopt getopt_long group_from_gid gid_from_group \
 	heapsort isblank issetugid lchflags lchmod lchown lutimes mkstemp \
@@ -313,7 +314,7 @@ main(void)
         [AC_MSG_RESULT(yes)],
         [AC_MSG_RESULT(no)
          AC_DEFINE(BROKEN_FPARSELN, 1,
-            [Define to 1 if your `fparseln' function is broken.])],
+            [Define to 1 if your 'fparseln' function is broken.])],
         [AC_MSG_WARN([cross compiling: not checking farseln])]
     )
 ])

Reply via email to