Commit 983df25e4 for imagemagick.org

commit 983df25e4f854f56cce6810a32459828f429ba14
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Mon Nov 16 11:04:42 2026 -0500

    revert until after the release

diff --git a/MagickCore/semaphore.c b/MagickCore/semaphore.c
index 324e90c96..36cbe7649 100644
--- a/MagickCore/semaphore.c
+++ b/MagickCore/semaphore.c
@@ -53,16 +53,6 @@
 #include "MagickCore/thread_.h"
 #include "MagickCore/thread-private.h"
 #include "MagickCore/utility-private.h"
-#if defined(MAGICKCORE_HAVE_STDATOMIC_H)
-#include <stdatomic.h>
-#endif
-
-/*
-  Define declarations.
-*/
-#if !defined(CACHE_LINE_SIZE)
-#define CACHE_LINE_SIZE  64
-#endif

 /*
   Struct declarations.
@@ -108,46 +98,13 @@ struct SemaphoreInfo
 MagickExport void ActivateSemaphoreInfo(SemaphoreInfo **semaphore_info)
 {
   assert(semaphore_info != (SemaphoreInfo **) NULL);
-#if defined(MAGICKCORE_HAVE_STDATOMIC_H)
-  if (atomic_load_explicit((_Atomic(SemaphoreInfo *) *) semaphore_info,memory_order_acquire) == (SemaphoreInfo *) NULL)
-    {
-      LockMagickMutex();
-      if (atomic_load_explicit((_Atomic(SemaphoreInfo *) *) semaphore_info,memory_order_relaxed) == (SemaphoreInfo *) NULL)
-        {
-          SemaphoreInfo
-            *allocated_semaphore;
-
-          allocated_semaphore=AcquireSemaphoreInfo();
-          atomic_store_explicit((_Atomic(SemaphoreInfo *) *) semaphore_info,
-            allocated_semaphore,memory_order_release);
-        }
-      UnlockMagickMutex();
-    }
-#else
   if (*semaphore_info == (SemaphoreInfo *) NULL)
     {
       LockMagickMutex();
       if (*semaphore_info == (SemaphoreInfo *) NULL)
-        {
-          SemaphoreInfo
-            *allocated_semaphore;
-
-          allocated_semaphore=AcquireSemaphoreInfo();
-#if defined(MAGICKCORE_WINDOWS_SUPPORT)
-          MemoryBarrier();
-#elif defined(__GNUC__) || defined(__clang__)
-          __atomic_thread_fence(__ATOMIC_RELEASE);
-#endif
-          *semaphore_info=allocated_semaphore;
-        }
+        *semaphore_info=AcquireSemaphoreInfo();
       UnlockMagickMutex();
     }
-#if defined(MAGICKCORE_WINDOWS_SUPPORT)
-  MemoryBarrier();
-#elif defined(__GNUC__) || defined(__clang__)
-  __atomic_thread_fence(__ATOMIC_ACQUIRE);
-#endif
-#endif
 }

 /*
@@ -169,18 +126,11 @@ MagickExport void ActivateSemaphoreInfo(SemaphoreInfo **semaphore_info)
 %
 */

-static inline size_t AlignSemaphoreExtent(const size_t size,
-  const size_t alignment)
-{
-  if ((alignment == 0) || ((alignment & (alignment-1)) != 0))
-    return(0);
-  if (size > (SIZE_MAX-(alignment-1)))
-    return(0);
-  return((size+(alignment-1)) & ~(alignment-1));
-}
-
 static void *AcquireSemaphoreMemory(const size_t count,const size_t quantum)
 {
+#define AlignedExtent(size,alignment) \
+  (((size)+((alignment)-1)) & (size_t) ~((alignment)-1))
+
   size_t
     alignment,
     extent,
@@ -189,48 +139,36 @@ static void *AcquireSemaphoreMemory(const size_t count,const size_t quantum)
   void
     *memory;

-  if ((count == 0) || (quantum == 0) || (count > (SIZE_MAX/quantum)))
-    {
-      errno=ENOMEM;
-      return((void *) NULL);
-    }
   size=count*quantum;
-  alignment=CACHE_LINE_SIZE;
-  if (alignment < sizeof(void *))
-    alignment=sizeof(void *);
-  extent=AlignSemaphoreExtent(size,alignment);
-  if (extent == 0)
+  if ((count == 0) || (quantum != (size/count)))
     {
       errno=ENOMEM;
       return((void *) NULL);
     }
-  memory=(void *) NULL;
+  memory=NULL;
+  alignment=CACHE_LINE_SIZE;
+  extent=AlignedExtent(size,CACHE_LINE_SIZE);
+  if ((size == 0) || (alignment < sizeof(void *)) || (extent < size))
+    return((void *) NULL);
 #if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
   if (posix_memalign(&memory,alignment,extent) != 0)
-    memory=(void *) NULL;
+    memory=NULL;
 #elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
-  memory=_aligned_malloc(extent,alignment);
+   memory=_aligned_malloc(extent,alignment);
 #else
   {
     void
-      *raw_memory;
+      *p;

-    if (size > (SIZE_MAX-(alignment-1)-sizeof(void *)))
-      {
-        errno=ENOMEM;
-        return((void *) NULL);
-      }
-    extent=size+(alignment-1)+sizeof(void *);
-    raw_memory=malloc(extent);
-    if (raw_memory != (void *) NULL)
+    extent=(size+alignment-1)+sizeof(void *);
+    if (extent > size)
       {
-        uintptr_t
-          aligned_addr;
-
-        aligned_addr=(uintptr_t) raw_memory+sizeof(void *);
-        aligned_addr=(aligned_addr+(alignment-1)) & ~(uintptr_t) (alignment-1);
-        memory=(void *) aligned_addr;
-        *((void **) memory-1)=raw_memory;
+        p=malloc(extent);
+        if (p != NULL)
+          {
+            memory=(void *) AlignedExtent((size_t) p+sizeof(void *),alignment);
+            *((void **) memory-1)=p;
+          }
       }
   }
 #endif
@@ -248,14 +186,27 @@ static void *RelinquishSemaphoreMemory(void *memory)
 #else
   free(*((void **) memory-1));
 #endif
-  return((void *) NULL);
+  return(NULL);
 }

-static MagickBooleanType InitializeSemaphoreMutex(SemaphoreInfo *semaphore_info)
+MagickExport SemaphoreInfo *AcquireSemaphoreInfo(void)
 {
+  SemaphoreInfo
+    *semaphore_info;
+
+  /*
+    Acquire semaphore.
+  */
+  semaphore_info=(SemaphoreInfo *) AcquireSemaphoreMemory(1,
+    sizeof(*semaphore_info));
+  if (semaphore_info == (SemaphoreInfo *) NULL)
+    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+  (void) memset(semaphore_info,0,sizeof(SemaphoreInfo));
+  /*
+    Initialize the semaphore.
+  */
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
   omp_init_lock((omp_lock_t *) &semaphore_info->mutex);
-  return(MagickTrue);
 #elif defined(MAGICKCORE_THREAD_SUPPORT)
   {
     int
@@ -268,61 +219,49 @@ static MagickBooleanType InitializeSemaphoreMutex(SemaphoreInfo *semaphore_info)
     if (status != 0)
       {
         errno=status;
-        return(MagickFalse);
+        perror("unable to initialize mutex attributes");
+        _exit(1);
       }
-#if defined(MAGICKCORE_DEBUG) && defined(PTHREAD_MUTEX_ERRORCHECK)
-    status=pthread_mutexattr_settype(&mutex_info,PTHREAD_MUTEX_ERRORCHECK);
+#if defined(MAGICKCORE_DEBUG)
+#if defined(PTHREAD_MUTEX_ERRORCHECK)
+    status=pthread_mutex_settype(&mutex_info,PTHREAD_MUTEX_ERRORCHECK);
     if (status != 0)
       {
         errno=status;
-        (void) pthread_mutexattr_destroy(&mutex_info);
-        return(MagickFalse);
+        perror("unable to set mutex type");
+        _exit(1);
       }
+#endif
 #endif
     status=pthread_mutex_init(&semaphore_info->mutex,&mutex_info);
-    (void) pthread_mutexattr_destroy(&mutex_info);
     if (status != 0)
       {
         errno=status;
-        return(MagickFalse);
+        perror("unable to initialize mutex");
+        _exit(1);
+      }
+    status=pthread_mutexattr_destroy(&mutex_info);
+    if (status != 0)
+      {
+        errno=status;
+        perror("unable to destroy mutex attributes");
+        _exit(1);
       }
   }
-  return(MagickTrue);
 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
-  if (InitializeCriticalSectionAndSpinCount(&semaphore_info->mutex,0x0400) == 0)
-    {
-      errno=ENOMEM;
-      return(MagickFalse);
-    }
-  return(MagickTrue);
-#else
-  magick_unreferenced(semaphore_info);
-  return(MagickTrue);
-#endif
-}
-
-MagickExport SemaphoreInfo *AcquireSemaphoreInfo(void)
-{
-  SemaphoreInfo
-    *semaphore_info;
+  {
+    int
+      status;

-  /*
-    Acquire semaphore.
-  */
-  semaphore_info=(SemaphoreInfo *) AcquireSemaphoreMemory(1,
-    sizeof(*semaphore_info));
-  if (semaphore_info == (SemaphoreInfo *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
-  (void) memset(semaphore_info,0,sizeof(*semaphore_info));
-  /*
-    Initialize the semaphore.
-  */
-  if (InitializeSemaphoreMutex(semaphore_info) == MagickFalse)
-    {
-      semaphore_info=(SemaphoreInfo *) RelinquishSemaphoreMemory(
-        semaphore_info);
-      ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
-    }
+    status=InitializeCriticalSectionAndSpinCount(&semaphore_info->mutex,0x0400);
+    if (status == 0)
+      {
+        errno=status;
+        perror("unable to initialize critical section");
+        _exit(1);
+      }
+  }
+#endif
   semaphore_info->id=GetMagickThreadId();
   semaphore_info->reference_count=0;
   semaphore_info->signature=MagickCoreSignature;
@@ -411,43 +350,30 @@ MagickExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
 */
 MagickExport void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
 {
-  SemaphoreInfo
-    *target_semaphore;
-
   assert(semaphore_info != (SemaphoreInfo **) NULL);
+  assert((*semaphore_info) != (SemaphoreInfo *) NULL);
+  assert((*semaphore_info)->signature == MagickCoreSignature);
   LockMagickMutex();
-#if defined(MAGICKCORE_HAVE_STDATOMIC_H)
-  target_semaphore=atomic_exchange_explicit(
-    (_Atomic(SemaphoreInfo *) *) semaphore_info,(SemaphoreInfo *) NULL,
-    memory_order_acq_rel);
-#else
-  target_semaphore=(*semaphore_info);
-  *semaphore_info=(SemaphoreInfo *) NULL;
-#endif
-  if (target_semaphore != (SemaphoreInfo *) NULL)
-    {
-      assert(target_semaphore->signature == MagickCoreSignature);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-      omp_destroy_lock((omp_lock_t *) &target_semaphore->mutex);
+  omp_destroy_lock((omp_lock_t *) &(*semaphore_info)->mutex);
 #elif defined(MAGICKCORE_THREAD_SUPPORT)
-      {
-        int
-          status;
+  {
+    int
+      status;

-        status=pthread_mutex_destroy(&target_semaphore->mutex);
-        if (status != 0)
-          {
-            errno=status;
-            perror("unable to destroy mutex");
-            _exit(1);
-          }
+    status=pthread_mutex_destroy(&(*semaphore_info)->mutex);
+    if (status != 0)
+      {
+        errno=status;
+        perror("unable to destroy mutex");
+        _exit(1);
       }
+  }
 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
-      DeleteCriticalSection(&target_semaphore->mutex);
+  DeleteCriticalSection(&(*semaphore_info)->mutex);
 #endif
-      target_semaphore->signature=(~MagickCoreSignature);
-      (void) RelinquishSemaphoreMemory(target_semaphore);
-    }
+  (*semaphore_info)->signature=(~MagickCoreSignature);
+  *semaphore_info=(SemaphoreInfo *) RelinquishSemaphoreMemory(*semaphore_info);
   UnlockMagickMutex();
 }