[gjs: 1/3] gjs-test-tools: Throw error if we can't create threads




commit adbc65240de469c57f4c2833769db26b6ca70ac7
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 06431a54..fad74a30 100644
--- a/installed-tests/js/libgjstesttools/gjs-test-tools.cpp
+++ b/installed-tests/js/libgjstesttools/gjs-test-tools.cpp
@@ -99,9 +99,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 {
@@ -159,20 +161,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);
 }
 
 /**
@@ -180,9 +185,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);
 }
 
 /**
@@ -190,21 +197,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);
 }
 
 /**
@@ -256,12 +267,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 5bb05827..13fcb7a8 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);
@@ -79,7 +82,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]