From 004cbe6849fa62f03dc1629f893875c38e3862ef Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 23 May 2025 19:59:54 +0300
Subject: [PATCH 68/68] Add fc_thread_self() and fc_threads_equal()

See RM #1434

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 utility/fcthread.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 utility/fcthread.h |  5 +++++
 2 files changed, 53 insertions(+)

diff --git a/utility/fcthread.c b/utility/fcthread.c
index c52b43d763..9ba681c49c 100644
--- a/utility/fcthread.c
+++ b/utility/fcthread.c
@@ -73,6 +73,22 @@ void fc_thread_wait(fc_thread *thread)
   thrd_join(*thread, return_value);
 }
 
+/*******************************************************************//**
+  Get thread id
+***********************************************************************/
+fc_thread_id fc_thread_self(void)
+{
+  return thrd_current();
+}
+
+/*******************************************************************//**
+  Tell if two threads are the same
+***********************************************************************/
+bool fc_threads_equal(fc_thread_id thr1, fc_thread_id thr2)
+{
+  return thrd_equal(thr1, thr2);
+}
+
 /*******************************************************************//**
   Initialize mutex
 ***********************************************************************/
@@ -196,6 +212,22 @@ void fc_thread_wait(fc_thread *thread)
   pthread_join(*thread, return_value);
 }
 
+/*******************************************************************//**
+  Get thread id
+***********************************************************************/
+fc_thread_id fc_thread_self(void)
+{
+  return pthread_self();
+}
+
+/*******************************************************************//**
+  Tell if two threads are the same
+***********************************************************************/
+bool fc_threads_equal(fc_thread_id thr1, fc_thread_id thr2)
+{
+  return pthread_equal(thr1, thr2);
+}
+
 /*******************************************************************//**
   Initialize mutex
 ***********************************************************************/
@@ -326,6 +358,22 @@ void fc_thread_wait(fc_thread *thread)
   CloseHandle(*thread);
 }
 
+/*******************************************************************//**
+  Get thread id
+***********************************************************************/
+fc_thread_id fc_thread_self(void)
+{
+  return GetCurrentThreadId();
+}
+
+/*******************************************************************//**
+  Tell if two threads are the same
+***********************************************************************/
+bool fc_threads_equal(fc_thread_id thr1, fc_thread_id thr2)
+{
+  return thr1 == thr2;
+} 
+
 /*******************************************************************//**
   Initialize mutex
 ***********************************************************************/
diff --git a/utility/fcthread.h b/utility/fcthread.h
index 9394619cf7..54c31e0904 100644
--- a/utility/fcthread.h
+++ b/utility/fcthread.h
@@ -39,6 +39,7 @@ extern "C" {
 #define fc_thread      thrd_t
 #define fc_mutex       mtx_t
 #define fc_thread_cond cnd_t
+#define fc_thread_id   thrd_t
 
 #elif defined(FREECIV_HAVE_PTHREAD)
 
@@ -47,12 +48,14 @@ extern "C" {
 #define fc_thread      pthread_t
 #define fc_mutex       pthread_mutex_t
 #define fc_thread_cond pthread_cond_t
+#define fc_thread_id   pthread_t
 
 #elif defined (FREECIV_HAVE_WINTHREADS)
 
 #include <windows.h>
 #define fc_thread      HANDLE *
 #define fc_mutex       HANDLE *
+#define fc_thread_id   DWORD
 
 #ifndef FREECIV_HAVE_THREAD_COND
 #define fc_thread_cond char
@@ -68,6 +71,8 @@ extern "C" {
 
 int fc_thread_start(fc_thread *thread, void (*function) (void *arg), void *arg);
 void fc_thread_wait(fc_thread *thread);
+fc_thread_id fc_thread_self(void);
+bool fc_threads_equal(fc_thread_id thr1, fc_thread_id thr2);
 
 void fc_mutex_init(fc_mutex *mutex);
 void fc_mutex_destroy(fc_mutex *mutex);
-- 
2.47.2

