From ae1a0eb46d9c08382a4d5550e5f606c2c869fc5b Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Wed, 30 Apr 2003 03:09:34 +0000
Subject: [PATCH] General clean-up of YIELD and SLEEP macros... SLEEP shouldn't
 have been touched in the first place (Twice the bugs in half the time!) use
 use -D_THREAD_SAFE to compile object files for a threaded app (You have to do
 that on BSD already anyways)

---
 src/xpdev/genwrap.h    | 58 +++++++++++++++++++++++++++---------------
 src/xpdev/threadwrap.h | 16 ------------
 2 files changed, 38 insertions(+), 36 deletions(-)

diff --git a/src/xpdev/genwrap.h b/src/xpdev/genwrap.h
index 46af9c0f5f..f9eae2bb17 100644
--- a/src/xpdev/genwrap.h
+++ b/src/xpdev/genwrap.h
@@ -43,15 +43,13 @@
 #include "wrapdll.h"	/* DLLEXPORT and DLLCALL */
 
 #if defined(__unix__)
-	#include <unistd.h>		/* usleep */
-	#include <sys/time.h>	/* struct timeval */
 	#include <sched.h>		/* sched_yield */
-#endif
-
-#if defined(__QNX__)
+	#include <sys/time.h>	/* struct timeval */
 	#include <strings.h>	/* strcasecmp() */
+	#include <unistd.h>		/* usleep */
 #endif
 
+
 #if defined(__cplusplus)
 extern "C" {
 #endif
@@ -90,7 +88,6 @@ extern "C" {
 /* Macros */
 /**********/
 
-#define FORK()	fork()
 /* Target Platform Description */
 #if defined(_WIN64)
 	#define PLATFORM_DESC	"Win64"
@@ -118,8 +115,6 @@ extern "C" {
 	#define PLATFORM_DESC	"GNU/Hurd"
 #elif defined(__QNX__)
 	#define PLATFORM_DESC	"QNX"
-	#undef FORK
-	#define FORK()	vfork()
 #elif defined(__unix__)
 	#define PLATFORM_DESC	"Unix"
 #else
@@ -175,19 +170,28 @@ extern "C" {
 
 #elif defined(__unix__)
 
-#ifndef SLEEP
-	#ifndef YIELD
-	#if defined(__OpenBSD__) || defined(__NetBSD__)
-		#define YIELD()
+
+	#if defined(_PTH_PTHREAD_H_)
+		#define SLEEP(x)  ({ int y=x; struct timeval tv; \
+			tv.tv_sec=(y/1000); tv.tv_usec=((y%1000)*1000); \
+			pth_nap(tv); })
 	#else
-		#define	YIELD()			sched_yield()
-	#endif
-	#endif
-	#define SLEEP(x)		({	int y=x; struct timeval tv; \
+		#define SLEEP(x)		({	int y=x; struct timeval tv; \
 								tv.tv_sec=(y/1000); tv.tv_usec=((y%1000)*1000); \
-								YIELD(); \
 								select(0,NULL,NULL,NULL,&tv); })
-#endif
+	#endif
+
+	/*
+	 * QNX doesn't support fork() in threaded apps (yet) using vfork() instead
+	 * works, but relies on undefined behaviours not being nasty.  On most OSs
+	 * vfork() will share the stack between the parent and child...
+	 */
+	#if defined(__QNX__)
+		#define FORK()	vfork()
+	#else
+		#define FORK()	fork()
+	#endif
+
 	#define BEEP(freq,dur)	unix_beep(freq,dur)
 	DLLEXPORT void	DLLCALL	unix_beep(int freq, int dur);
 
@@ -223,8 +227,22 @@ DLLEXPORT char*		DLLCALL	lastchar(const char* str);
 }
 #endif
 
-#ifndef YIELD
-#define	YIELD()
+#ifdef _THREAD_SAFE
+	#if defined(__FreeBSD__)
+		#define YIELD()         pthread_yield()
+	#elif defined(_PTH_PTHREAD_H_)
+		#define YIELD()         pth_yield(NULL)
+	#elif defined(_POSIX_PRIORITY_SCHEDULING)
+		#define YIELD()         sched_yield()
+	#else
+		#define YIELD()
+	#endif
+#else
+	#if defined(_POSIX_PRIORITY_SCHEDULING)
+		#define	YIELD()			sched_yield()
+	#else
+		#define YIELD()
+	#endif
 #endif
 
 #endif	/* Don't add anything after this line */
diff --git a/src/xpdev/threadwrap.h b/src/xpdev/threadwrap.h
index edbf3fc723..38225dc747 100644
--- a/src/xpdev/threadwrap.h
+++ b/src/xpdev/threadwrap.h
@@ -49,14 +49,6 @@ extern "C" {
 
 	#include <sys/param.h>
 	#include <pthread.h>	/* POSIX threads and mutexes */
-	#undef YIELD
-	#if defined(__linux__) || defined(__QNX__)
-		#define YIELD()		sched_yield()
-	#elif defined(_PTH_PTHREAD_H_)
-		#define YIELD()		pth_yield(NULL)
-	#else
-		#define YIELD()		pthread_yield()
-	#endif
 	#if defined(_NEED_SEM)
 		#include "sem.h"
 	#else
@@ -124,12 +116,4 @@ extern "C" {
 }
 #endif
 
-#ifdef _PTH_PTHREAD_H_
-#include <pth.h>
-#undef SLEEP
-#define SLEEP(x)		({	int y=x; struct timeval tv; \
-								tv.tv_sec=(y/1000); tv.tv_usec=((y%1000)*1000); \
-								pth_nap(tv); })
-#endif
-
 #endif	/* Don't add anything after this line */
-- 
GitLab