trying to fix macOS build

Michael Behrisch via Foxgui-users <[email protected]> Mon, 13 May 2024 16:38:24 +0200
Newsgroups gmane.comp.lib.fox-toolkit.user
Message-ID <[email protected]>
Hi,
I just wanted to check again, whether there is a way to get this patch 
into the fox-toolkit. It would really help in getting acceptance in the 
macOS world because currently homebrew is always complaining about fox 
not being supported anymore (even if it is just a very minor version 
update).

Thanks in advance,
Michael

-------- Weitergeleitete Nachricht --------
Betreff: [Foxgui-users] trying to fix macOS build
Datum: Fri, 3 Nov 2023 09:07:02 +0100
Von: Michael Behrisch <[email protected]>
An: [email protected]
Kopie (CC): [email protected]

Hi Jeroen,
the homebrew formula of the fox toolkit 
https://formulae.brew.sh/formula/fox has been marked deprecated because 
fox 1.6.57 does not build on macOS any longer. From my limited knowledge 
and some internet search I tried to derive a solution which is basically 
backporting the semaphore implementation from fox 1.7. I would like to 
hear your comments and ask whether it can be incorporated and a 1.6.58 
may be released? A draft for a patch is attached.

Thanks a lot for taking care of the fox toolkit,
Michael

_______________________________________________
Foxgui-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/foxgui-users
semaphore_apple.patch (text/x-patch, 2.2 KB)
--- src/FXThread.cpp.orig	2018-04-04 05:42:42.000000000 +0200
+++ src/FXThread.cpp	2023-11-03 08:38:24.526029700 +0100
@@ -148,6 +148,8 @@
 
 #ifdef __APPLE__
 
+// the following changes in the semaphore implementation have been backported from fox 1.7,
+// thanks to https://yamaimo.hatenablog.jp/entry/2019/02/24/200000
 
 // Initialize semaphore
 FXSemaphore::FXSemaphore(FXint initial){
@@ -155,32 +157,51 @@
   // of sizeof(MPSemaphoreID*) is supposed to be on your
   // machine and mail it to: [email protected]!!
   //FXTRACE((150,"sizeof(MPSemaphoreID*)=%d\n",sizeof(MPSemaphoreID*)));
-  FXASSERT(sizeof(data)>=sizeof(MPSemaphoreID*));
-  MPCreateSemaphore(2147483647,initial,(MPSemaphoreID*)data);
+  FXASSERT_STATIC(sizeof(FXuval)*9 >= sizeof(pthread_cond_t));
+  FXASSERT_STATIC(sizeof(FXuval)*11 >= sizeof(pthread_mutex_t));
+  data[0]=initial;
+  pthread_cond_init((pthread_cond_t*)&data[1],nullptr);
+  pthread_mutex_init((pthread_mutex_t*)&data[10],nullptr);
   }
 
 
 // Decrement semaphore
 void FXSemaphore::wait(){
-  MPWaitOnSemaphore(*((MPSemaphoreID*)data),kDurationForever);
+  pthread_mutex_lock((pthread_mutex_t*)&data[10]);
+  while(data[0]==0){
+    pthread_cond_wait((pthread_cond_t*)&data[1],(pthread_mutex_t*)&data[10]);
+    }
+  data[0]-=1;
+  pthread_mutex_unlock((pthread_mutex_t*)&data[10]);
   }
 
 
 // Decrement semaphore but don't block
 FXbool FXSemaphore::trywait(){
-  return MPWaitOnSemaphore(*((MPSemaphoreID*)data),kDurationImmediate)==noErr;
+  pthread_mutex_lock((pthread_mutex_t*)&data[10]);
+  if(data[0]==0){
+    pthread_mutex_unlock((pthread_mutex_t*)&data[10]);
+    return false;
+    }
+  data[0]-=1;
+  pthread_mutex_unlock((pthread_mutex_t*)&data[10]);
+  return true;
   }
 
 
 // Increment semaphore
 void FXSemaphore::post(){
-  MPSignalSemaphore(*((MPSemaphoreID*)data));
+  pthread_mutex_lock((pthread_mutex_t*)&data[10]);
+  data[0]+=1;
+  pthread_cond_signal((pthread_cond_t*)&data[1]);
+  pthread_mutex_unlock((pthread_mutex_t*)&data[10]);
   }
 
 
 // Delete semaphore
 FXSemaphore::~FXSemaphore(){
-  MPDeleteSemaphore(*((MPSemaphoreID*)data));
+  pthread_mutex_destroy((pthread_mutex_t*)&data[10]);
+  pthread_cond_destroy((pthread_cond_t*)&data[1]);
   }
 
 #else