[Darwin] Install dispatch source handler for SIGINT/SIGTERM instead of posix signals to make it more reliable (#38910)

diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp
index 58d9888..6beaa00 100644
--- a/examples/platform/linux/AppMain.cpp
+++ b/examples/platform/linux/AppMain.cpp
@@ -809,11 +809,31 @@
     //       registered with sigaction() call and TSAN is enabled. The problem seems to be
     //       related with the dispatch_semaphore_wait() function in the RunEventLoop() method.
     //       If this call is commented out, the signal handler is called as expected...
-#if defined(__APPLE__)
+#if CHIP_DEVICE_LAYER_TARGET_DARWIN
+#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
+    dispatch_queue_t workQueue = chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue();
+
+    dispatch_source_t sourceSigInt = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, workQueue);
+    dispatch_source_set_event_handler(sourceSigInt, ^{
+        StopSignalHandler(SIGINT);
+        dispatch_release(sourceSigInt);
+    });
+    dispatch_resume(sourceSigInt);
+    signal(SIGINT, SIG_IGN);
+
+    dispatch_source_t sourceSigTerm = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, workQueue);
+    dispatch_source_set_event_handler(sourceSigTerm, ^{
+        StopSignalHandler(SIGTERM);
+        dispatch_release(sourceSigTerm);
+    });
+    dispatch_resume(sourceSigTerm);
+    signal(SIGTERM, SIG_IGN);
+#else
     // NOLINTBEGIN(bugprone-signal-handler)
     signal(SIGINT, StopSignalHandler);
     signal(SIGTERM, StopSignalHandler);
     // NOLINTEND(bugprone-signal-handler)
+#endif
 #else
     struct sigaction sa                        = {};
     sa.sa_handler                              = StopSignalHandler;