[gjs/gnome-40: 28/30] gjs-test-tools: Throw error if we can't create threads




commit dfadd4bb340116c5b878aefeeb6796a8b1aced41
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed May 5 05:05:56 2021 +0200

    gjs-test-tools: Throw error if we can't create threads
    
    When running in some machines we may not be able to created many
    threads, in those cases throw the error so that we can catch it at JS
    side.

 .../js/libgjstesttools/gjs-test-tools.cpp          | 67 +++++++++++++---------
 .../js/libgjstesttools/gjs-test-tools.h            | 17 +++---
 2 files changed, 51 insertions(+), 33 deletions(-)
---
diff --git a/installed-tests/js/libgjstesttools/gjs-test-tools.cpp 
b/installed-tests/js/libgjstesttools/gjs-test-tools.cpp
index 8f401e30..036202d8 100644
--- a/installed-tests/js/libgjstesttools/gjs-test-tools.cpp
+++ b/installed-tests/js/libgjstesttools/gjs-test-tools.cpp
@@ -90,9 +90,11 @@ void gjs_test_tools_clear_saved() {
     }
 }
 
-void gjs_test_tools_ref_other_thread(GObject* object) {
-    // cppcheck-suppress leakNoVarFunctionCall
-    g_thread_join(g_thread_new("ref_object", g_object_ref, object));
+void gjs_test_tools_ref_other_thread(GObject* object, GError** error) {
+    auto* thread = g_thread_try_new("ref_object", g_object_ref, object, error);
+    if (thread)
+        g_thread_join(thread);
+    // cppcheck-suppress memleak
 }
 
 typedef enum {
@@ -150,20 +152,23 @@ static void* ref_thread_func(void* data) {
     return nullptr;
 }
 
-void gjs_test_tools_unref_other_thread(GObject* object) {
-    // cppcheck-suppress leakNoVarFunctionCall
-    g_thread_join(g_thread_new("unref_object", ref_thread_func,
-                               ref_thread_data_new(object, -1, UNREF)));
+void gjs_test_tools_unref_other_thread(GObject* object, GError** error) {
+    auto* thread =
+        g_thread_try_new("unref_object", ref_thread_func,
+                         ref_thread_data_new(object, -1, UNREF), error);
+    if (thread)
+        g_thread_join(thread);
+    // cppcheck-suppress memleak
 }
 
 /**
  * gjs_test_tools_delayed_ref_other_thread:
  * Returns: (transfer full)
  */
-GThread* gjs_test_tools_delayed_ref_other_thread(GObject* object,
-                                                 int interval) {
-    return g_thread_new("ref_object", ref_thread_func,
-                        ref_thread_data_new(object, interval, REF));
+GThread* gjs_test_tools_delayed_ref_other_thread(GObject* object, int interval,
+                                                 GError** error) {
+    return g_thread_try_new("ref_object", ref_thread_func,
+                            ref_thread_data_new(object, interval, REF), error);
 }
 
 /**
@@ -171,9 +176,11 @@ GThread* gjs_test_tools_delayed_ref_other_thread(GObject* object,
  * Returns: (transfer full)
  */
 GThread* gjs_test_tools_delayed_unref_other_thread(GObject* object,
-                                                   int interval) {
-    return g_thread_new("unref_object", ref_thread_func,
-                        ref_thread_data_new(object, interval, UNREF));
+                                                   int interval,
+                                                   GError** error) {
+    return g_thread_try_new("unref_object", ref_thread_func,
+                            ref_thread_data_new(object, interval, UNREF),
+                            error);
 }
 
 /**
@@ -181,21 +188,25 @@ GThread* gjs_test_tools_delayed_unref_other_thread(GObject* object,
  * Returns: (transfer full)
  */
 GThread* gjs_test_tools_delayed_ref_unref_other_thread(GObject* object,
-                                                       int interval) {
-    return g_thread_new("ref_unref_object", ref_thread_func,
-                        ref_thread_data_new(object, interval,
-                                            static_cast<RefType>(REF | UNREF)));
+                                                       int interval,
+                                                       GError** error) {
+    return g_thread_try_new(
+        "ref_unref_object", ref_thread_func,
+        ref_thread_data_new(object, interval,
+                            static_cast<RefType>(REF | UNREF)),
+        error);
 }
 
-void gjs_test_tools_run_dispose_other_thread(GObject* object) {
-    // cppcheck-suppress leakNoVarFunctionCall
-    g_thread_join(g_thread_new(
+void gjs_test_tools_run_dispose_other_thread(GObject* object, GError** error) {
+    auto* thread = g_thread_try_new(
         "run_dispose",
         [](void* object) -> void* {
             g_object_run_dispose(G_OBJECT(object));
             return nullptr;
         },
-        object));
+        object, error);
+    // cppcheck-suppress leakNoVarFunctionCall
+    g_clear_pointer(&thread, g_thread_join);
 }
 
 /**
@@ -237,12 +248,16 @@ GObject* gjs_test_tools_get_weak() {
  * gjs_test_tools_get_weak_other_thread:
  * Returns: (transfer full)
  */
-GObject* gjs_test_tools_get_weak_other_thread() {
+GObject* gjs_test_tools_get_weak_other_thread(GError** error) {
+    auto* thread = g_thread_try_new(
+        "weak_get", [](void*) -> void* { return gjs_test_tools_get_weak(); },
+        NULL, error);
+    if (!thread)
+        return nullptr;
+
     return static_cast<GObject*>(
         // cppcheck-suppress leakNoVarFunctionCall
-        g_thread_join(g_thread_new(
-            "weak_get",
-            [](void*) -> void* { return gjs_test_tools_get_weak(); }, NULL)));
+        g_thread_join(thread));
 }
 
 /**
diff --git a/installed-tests/js/libgjstesttools/gjs-test-tools.h 
b/installed-tests/js/libgjstesttools/gjs-test-tools.h
index 3948e3ff..a4e521d5 100644
--- a/installed-tests/js/libgjstesttools/gjs-test-tools.h
+++ b/installed-tests/js/libgjstesttools/gjs-test-tools.h
@@ -32,24 +32,27 @@ _GJS_TEST_TOOL_EXTERN
 void gjs_test_tools_delayed_dispose(GObject* object, int interval);
 
 _GJS_TEST_TOOL_EXTERN
-void gjs_test_tools_ref_other_thread(GObject* object);
+void gjs_test_tools_ref_other_thread(GObject* object, GError** error);
 
 _GJS_TEST_TOOL_EXTERN
-GThread* gjs_test_tools_delayed_ref_other_thread(GObject* object, int interval);
+GThread* gjs_test_tools_delayed_ref_other_thread(GObject* object, int interval,
+                                                 GError** error);
 
 _GJS_TEST_TOOL_EXTERN
-void gjs_test_tools_unref_other_thread(GObject* object);
+void gjs_test_tools_unref_other_thread(GObject* object, GError** error);
 
 _GJS_TEST_TOOL_EXTERN
 GThread* gjs_test_tools_delayed_unref_other_thread(GObject* object,
-                                                   int interval);
+                                                   int interval,
+                                                   GError** error);
 
 _GJS_TEST_TOOL_EXTERN
 GThread* gjs_test_tools_delayed_ref_unref_other_thread(GObject* object,
-                                                       int interval);
+                                                       int interval,
+                                                       GError** error);
 
 _GJS_TEST_TOOL_EXTERN
-void gjs_test_tools_run_dispose_other_thread(GObject* object);
+void gjs_test_tools_run_dispose_other_thread(GObject* object, GError** error);
 
 _GJS_TEST_TOOL_EXTERN
 void gjs_test_tools_save_object(GObject* object);
@@ -76,7 +79,7 @@ _GJS_TEST_TOOL_EXTERN
 GObject* gjs_test_tools_get_weak();
 
 _GJS_TEST_TOOL_EXTERN
-GObject* gjs_test_tools_get_weak_other_thread();
+GObject* gjs_test_tools_get_weak_other_thread(GError** error);
 
 _GJS_TEST_TOOL_EXTERN
 GObject* gjs_test_tools_get_disposed(GObject* object);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]