From f9eeb629073ce19ce464db4bde656c4dde96a4f8 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Sat, 13 Dec 2025 18:18:13 +0100
Subject: [PATCH v14 2/3] Move Windows pthread compatibility functions to
 src/port

This is in preparation of a follow-up commit which will start to use
these functions in more places than just libpq.

The accompanying header, pthread-win32.h, was already in src/port. That
is not a standard location for headers though, so libpq and libpq-oauth
had to add that directory to their include path to find it. Move the
header to src/include/port, so that it can be included without any such
build system changes.
---
 configure.ac                                   | 1 +
 src/{ => include}/port/pthread-win32.h         | 2 +-
 src/interfaces/libpq-oauth/meson.build         | 3 +--
 src/interfaces/libpq/Makefile                  | 3 +--
 src/interfaces/libpq/fe-connect.c              | 2 +-
 src/interfaces/libpq/fe-secure-openssl.c       | 2 +-
 src/interfaces/libpq/fe-secure.c               | 2 +-
 src/interfaces/libpq/libpq-int.h               | 2 +-
 src/interfaces/libpq/meson.build               | 5 ++---
 src/port/meson.build                           | 1 +
 src/{interfaces/libpq => port}/pthread-win32.c | 6 +++---
 src/tools/pginclude/headerscheck               | 2 +-
 12 files changed, 15 insertions(+), 16 deletions(-)
 rename src/{ => include}/port/pthread-win32.h (94%)
 rename src/{interfaces/libpq => port}/pthread-win32.c (92%)

diff --git a/configure.ac b/configure.ac
index 18bbe596612..9b57de6550c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1975,6 +1975,7 @@ if test "$PORTNAME" = "win32"; then
   AC_LIBOBJ(dirmod)
   AC_LIBOBJ(kill)
   AC_LIBOBJ(open)
+  AC_LIBOBJ(pthread-win32)
   AC_LIBOBJ(system)
   AC_LIBOBJ(win32common)
   AC_LIBOBJ(win32dlopen)
diff --git a/src/port/pthread-win32.h b/src/include/port/pthread-win32.h
similarity index 94%
rename from src/port/pthread-win32.h
rename to src/include/port/pthread-win32.h
index 5f33269057c..29bdfbaf5f5 100644
--- a/src/port/pthread-win32.h
+++ b/src/include/port/pthread-win32.h
@@ -1,5 +1,5 @@
 /*
- * src/port/pthread-win32.h
+ * src/include/port/pthread-win32.h
  */
 #ifndef __PTHREAD_H
 #define __PTHREAD_H
diff --git a/src/interfaces/libpq-oauth/meson.build b/src/interfaces/libpq-oauth/meson.build
index ea3a900f4f1..9ff9beb1309 100644
--- a/src/interfaces/libpq-oauth/meson.build
+++ b/src/interfaces/libpq-oauth/meson.build
@@ -26,8 +26,7 @@ export_file = custom_target('libpq-oauth.exports',
   kwargs: gen_export_kwargs,
 )
 
-# port needs to be in include path due to pthread-win32.h
-libpq_oauth_inc = include_directories('.', '../libpq', '../../port')
+libpq_oauth_inc = include_directories('.', '../libpq')
 
 if build_static_lib
 libpq_oauth_st = static_library('libpq-oauth',
diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile
index 0963995eed4..dec29a93bf9 100644
--- a/src/interfaces/libpq/Makefile
+++ b/src/interfaces/libpq/Makefile
@@ -24,7 +24,7 @@ NAME= pq
 SO_MAJOR_VERSION= 5
 SO_MINOR_VERSION= $(MAJORVERSION)
 
-override CPPFLAGS := -I$(srcdir) -I$(top_builddir)/src/port -I$(top_srcdir)/src/port $(CPPFLAGS)
+override CPPFLAGS := -I$(srcdir) -I$(top_builddir)/src/port $(CPPFLAGS)
 ifneq ($(PORTNAME), win32)
 override CFLAGS += $(PTHREAD_CFLAGS)
 endif
@@ -75,7 +75,6 @@ endif
 
 ifeq ($(PORTNAME), win32)
 OBJS += \
-	pthread-win32.o \
 	win32.o
 endif
 
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index b30585c74c8..ec8e56d5cf3 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -57,7 +57,7 @@
 #endif
 
 #ifdef WIN32
-#include "pthread-win32.h"
+#include "port/pthread-win32.h"
 #else
 #include <pthread.h>
 #endif
diff --git a/src/interfaces/libpq/fe-secure-openssl.c b/src/interfaces/libpq/fe-secure-openssl.c
index 243ed240087..a213f675321 100644
--- a/src/interfaces/libpq/fe-secure-openssl.c
+++ b/src/interfaces/libpq/fe-secure-openssl.c
@@ -46,7 +46,7 @@
 #include <sys/stat.h>
 
 #ifdef WIN32
-#include "pthread-win32.h"
+#include "port/pthread-win32.h"
 #else
 #include <pthread.h>
 #endif
diff --git a/src/interfaces/libpq/fe-secure.c b/src/interfaces/libpq/fe-secure.c
index 1a8e2e6746e..d6a90cca3a6 100644
--- a/src/interfaces/libpq/fe-secure.c
+++ b/src/interfaces/libpq/fe-secure.c
@@ -36,7 +36,7 @@
 #include <sys/stat.h>
 
 #ifdef WIN32
-#include "pthread-win32.h"
+#include "port/pthread-win32.h"
 #else
 #include <pthread.h>
 #endif
diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h
index 9261fe0d309..0cc0af7c4a5 100644
--- a/src/interfaces/libpq/libpq-int.h
+++ b/src/interfaces/libpq/libpq-int.h
@@ -32,7 +32,7 @@
 #endif
 
 #ifdef WIN32
-#include "pthread-win32.h"
+#include "port/pthread-win32.h"
 #else
 #include <pthread.h>
 #endif
diff --git a/src/interfaces/libpq/meson.build b/src/interfaces/libpq/meson.build
index b0ae72167a1..78d2f9d1f85 100644
--- a/src/interfaces/libpq/meson.build
+++ b/src/interfaces/libpq/meson.build
@@ -20,7 +20,7 @@ libpq_sources = files(
 libpq_so_sources = [] # for shared lib, in addition to the above
 
 if host_system == 'windows'
-  libpq_sources += files('pthread-win32.c', 'win32.c')
+  libpq_sources += files('win32.c')
   libpq_so_sources += rc_lib_gen.process(win32ver_rc, extra_args: [
     '--NAME', 'libpq',
     '--FILEDESC', 'PostgreSQL Access Library',])
@@ -42,8 +42,7 @@ export_file = custom_target('libpq.exports',
   kwargs: gen_export_kwargs,
 )
 
-# port needs to be in include path due to pthread-win32.h
-libpq_inc = include_directories('.', '../../port')
+libpq_inc = include_directories('.')
 libpq_c_args = ['-DSO_MAJOR_VERSION=5']
 
 # The OAuth implementation differs depending on the type of library being built.
diff --git a/src/port/meson.build b/src/port/meson.build
index 922b3f64676..10833b13842 100644
--- a/src/port/meson.build
+++ b/src/port/meson.build
@@ -33,6 +33,7 @@ if host_system == 'windows'
     'dirmod.c',
     'kill.c',
     'open.c',
+    'pthread-win32.c',
     'system.c',
     'win32common.c',
     'win32dlopen.c',
diff --git a/src/interfaces/libpq/pthread-win32.c b/src/port/pthread-win32.c
similarity index 92%
rename from src/interfaces/libpq/pthread-win32.c
rename to src/port/pthread-win32.c
index cf66284f007..04594abcc44 100644
--- a/src/interfaces/libpq/pthread-win32.c
+++ b/src/port/pthread-win32.c
@@ -5,14 +5,14 @@
 *
 * Copyright (c) 2004-2026, PostgreSQL Global Development Group
 * IDENTIFICATION
-*	src/interfaces/libpq/pthread-win32.c
+*	src/port/pthread-win32.c
 *
 *-------------------------------------------------------------------------
 */
 
-#include "postgres_fe.h"
+#include "c.h"
 
-#include "pthread-win32.h"
+#include "port/pthread-win32.h"
 
 DWORD
 pthread_self(void)
diff --git a/src/tools/pginclude/headerscheck b/src/tools/pginclude/headerscheck
index 785d6f867ad..69d9af4ad6f 100755
--- a/src/tools/pginclude/headerscheck
+++ b/src/tools/pginclude/headerscheck
@@ -105,7 +105,7 @@ do
 	test "$f" = src/include/port/win32_msvc/dirent.h && continue
 	test "$f" = src/include/port/win32_msvc/utime.h && continue
 	test "$f" = src/include/port/win32ntdll.h && continue
-	test "$f" = src/port/pthread-win32.h && continue
+	test "$f" = src/include/port/pthread-win32.h && continue
 
 	# Likewise, these files are platform-specific, and the one
 	# relevant to our platform will be included by atomics.h.
-- 
2.56.1

