Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tools/compat ctfmerge: fix macOS semaphore implementation



details:   https://anonhg.NetBSD.org/src/rev/e0245bb38467
branches:  trunk
changeset: 375971:e0245bb38467
user:      lukem <lukem%NetBSD.org@localhost>
date:      Tue May 23 19:53:24 2023 +0000

description:
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.

diffstat:

 external/cddl/osnet/dist/tools/ctf/cvt/barrier.c |  17 ++++++++++++++++-
 external/cddl/osnet/dist/tools/ctf/cvt/barrier.h |   7 +++++++
 tools/compat/configure.ac                        |   9 +++++----
 3 files changed, 28 insertions(+), 5 deletions(-)

diffs (116 lines):

diff -r cdfa97884d31 -r e0245bb38467 external/cddl/osnet/dist/tools/ctf/cvt/barrier.c
--- a/external/cddl/osnet/dist/tools/ctf/cvt/barrier.c  Tue May 23 18:54:58 2023 +0000
+++ b/external/cddl/osnet/dist/tools/ctf/cvt/barrier.c  Tue May 23 19:53:24 2023 +0000
@@ -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__);
diff -r cdfa97884d31 -r e0245bb38467 external/cddl/osnet/dist/tools/ctf/cvt/barrier.h
--- a/external/cddl/osnet/dist/tools/ctf/cvt/barrier.h  Tue May 23 18:54:58 2023 +0000
+++ b/external/cddl/osnet/dist/tools/ctf/cvt/barrier.h  Tue May 23 19:53:24 2023 +0000
@@ -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;
diff -r cdfa97884d31 -r e0245bb38467 tools/compat/configure.ac
--- a/tools/compat/configure.ac Tue May 23 18:54:58 2023 +0000
+++ b/tools/compat/configure.ac Tue May 23 19:53:24 2023 +0000
@@ -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])]
     )
 ])



Home | Main Index | Thread Index | Old Index