[gjs] Use background finalizing for thread-safe stuff



commit ef33fbe1c0d912d227ee2c71ab5a1342818141a9
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sun Feb 23 23:43:53 2014 +0100

    Use background finalizing for thread-safe stuff
    
    For wrapper objects that hold thread-safe or unique C structures,
    set the appropriate flag to send the finalizer to the background
    thread, so that we don't block the mainloop more than necessary.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=725024

 gi/function.cpp                   |    3 ++-
 gi/gerror.cpp                     |    3 ++-
 gi/gtype.cpp                      |    2 +-
 gi/interface.cpp                  |    3 ++-
 gi/param.cpp                      |    3 ++-
 gjs/byteArray.cpp                 |    3 ++-
 gjs/jsapi-util.h                  |   21 +++++++++++----------
 gjs/mem.cpp                       |    2 +-
 gjs/mem.h                         |   12 ++++++------
 modules/cairo-context.cpp         |    2 +-
 modules/cairo-gradient.cpp        |    2 +-
 modules/cairo-image-surface.cpp   |    2 +-
 modules/cairo-linear-gradient.cpp |    2 +-
 modules/cairo-path.cpp            |    2 +-
 modules/cairo-pattern.cpp         |    2 +-
 modules/cairo-pdf-surface.cpp     |    2 +-
 modules/cairo-ps-surface.cpp      |    2 +-
 modules/cairo-radial-gradient.cpp |    2 +-
 modules/cairo-solid-pattern.cpp   |    2 +-
 modules/cairo-surface-pattern.cpp |    2 +-
 modules/cairo-surface.cpp         |    2 +-
 modules/cairo-svg-surface.cpp     |    2 +-
 22 files changed, 42 insertions(+), 36 deletions(-)
---
diff --git a/gi/function.cpp b/gi/function.cpp
index 86c32b1..91f3c12 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -1405,7 +1405,8 @@ function_to_string (JSContext *context,
  */
 struct JSClass gjs_function_class = {
     "GIRepositoryFunction", /* means "new GIRepositoryFunction()" works */
-    JSCLASS_HAS_PRIVATE,
+    JSCLASS_HAS_PRIVATE |
+    JSCLASS_BACKGROUND_FINALIZE,
     JS_PropertyStub,
     JS_DeletePropertyStub,
     JS_PropertyStub,
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 6174e04..891f99c 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -304,7 +304,8 @@ error_constructor_value_of(JSContext *context, unsigned argc, jsval *vp)
 struct JSClass gjs_error_class = {
     "GLib_Error",
     JSCLASS_HAS_PRIVATE |
-    JSCLASS_NEW_RESOLVE,
+    JSCLASS_NEW_RESOLVE |
+    JSCLASS_BACKGROUND_FINALIZE,
     JS_PropertyStub,
     JS_DeletePropertyStub,
     JS_PropertyStub,
diff --git a/gi/gtype.cpp b/gi/gtype.cpp
index 6ce4ca8..c45698f 100644
--- a/gi/gtype.cpp
+++ b/gi/gtype.cpp
@@ -31,7 +31,7 @@
 #include <util/log.h>
 #include <girepository.h>
 
-GJS_DEFINE_PROTO_ABSTRACT("GIRepositoryGType", gtype);
+GJS_DEFINE_PROTO_ABSTRACT("GIRepositoryGType", gtype, 0);
 
 /* priv_from_js adds a "*", so this returns "void *" */
 GJS_DEFINE_PRIV_FROM_JS(void, gjs_gtype_class);
diff --git a/gi/interface.cpp b/gi/interface.cpp
index f8d75b9..fcb6736 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -144,7 +144,8 @@ interface_new_resolve(JSContext *context,
 struct JSClass gjs_interface_class = {
     "GObject_Interface",
     JSCLASS_HAS_PRIVATE |
-    JSCLASS_NEW_RESOLVE,
+    JSCLASS_NEW_RESOLVE |
+    JSCLASS_BACKGROUND_FINALIZE,
     JS_PropertyStub,
     JS_DeletePropertyStub,
     JS_PropertyStub,
diff --git a/gi/param.cpp b/gi/param.cpp
index 3286238..4bb3125 100644
--- a/gi/param.cpp
+++ b/gi/param.cpp
@@ -411,7 +411,8 @@ param_new_internal(JSContext *cx,
  */
 struct JSClass gjs_param_class = {
     "GObject_ParamSpec",
-    JSCLASS_HAS_PRIVATE,
+    JSCLASS_HAS_PRIVATE |
+    JSCLASS_BACKGROUND_FINALIZE,
     JS_PropertyStub,
     JS_DeletePropertyStub,
     param_get_prop,
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 4aca8f1..b5ff7a9 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -55,7 +55,8 @@ static void   byte_array_finalize      (JSFreeOp     *fop,
 
 struct JSClass gjs_byte_array_class = {
     "ByteArray",
-    JSCLASS_HAS_PRIVATE,
+    JSCLASS_HAS_PRIVATE |
+    JSCLASS_BACKGROUND_FINALIZE,
     JS_PropertyStub,
     JS_DeletePropertyStub,
     (JSPropertyOp)byte_array_get_prop,
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 0b8a591..7c26232 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -109,12 +109,13 @@ typedef struct GjsRootedArray GjsRootedArray;
  * GJS_DEFINE_PROTO:
  * @tn: The name of the prototype, as a string
  * @cn: The name of the prototype, separated by _
+ * @flags: additional JSClass flags, such as JSCLASS_BACKGROUND_FINALIZE
  *
  * A convenience macro for prototype implementations.
  */
-#define GJS_DEFINE_PROTO(tn, cn) \
+#define GJS_DEFINE_PROTO(tn, cn, flags) \
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cn); \
-_GJS_DEFINE_PROTO_FULL(tn, cn, gjs_##cn##_constructor, G_TYPE_NONE)
+_GJS_DEFINE_PROTO_FULL(tn, cn, gjs_##cn##_constructor, G_TYPE_NONE, flags)
 
 /**
  * GJS_DEFINE_PROTO_ABSTRACT:
@@ -125,17 +126,17 @@ _GJS_DEFINE_PROTO_FULL(tn, cn, gjs_##cn##_constructor, G_TYPE_NONE)
  * Similar to GJS_DEFINE_PROTO but marks the prototype as abstract,
  * you won't be able to instantiate it using the new keyword
  */
-#define GJS_DEFINE_PROTO_ABSTRACT(tn, cn) \
-_GJS_DEFINE_PROTO_FULL(tn, cn, NULL, G_TYPE_NONE)
+#define GJS_DEFINE_PROTO_ABSTRACT(tn, cn, flags) \
+_GJS_DEFINE_PROTO_FULL(tn, cn, NULL, G_TYPE_NONE, flags)
 
-#define GJS_DEFINE_PROTO_WITH_GTYPE(tn, cn, gtype)   \
+#define GJS_DEFINE_PROTO_WITH_GTYPE(tn, cn, gtype, flags)   \
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cn); \
-_GJS_DEFINE_PROTO_FULL(tn, cn, gjs_##cn##_constructor, gtype)
+_GJS_DEFINE_PROTO_FULL(tn, cn, gjs_##cn##_constructor, gtype, flags)
 
-#define GJS_DEFINE_PROTO_ABSTRACT_WITH_GTYPE(tn, cn, gtype)   \
-_GJS_DEFINE_PROTO_FULL(tn, cn, NULL, gtype)
+#define GJS_DEFINE_PROTO_ABSTRACT_WITH_GTYPE(tn, cn, gtype, flags)   \
+_GJS_DEFINE_PROTO_FULL(tn, cn, NULL, gtype, flags)
 
-#define _GJS_DEFINE_PROTO_FULL(type_name, cname, ctor, gtype) \
+#define _GJS_DEFINE_PROTO_FULL(type_name, cname, ctor, gtype, jsclass_flags)     \
 extern JSPropertySpec gjs_##cname##_proto_props[]; \
 extern JSFunctionSpec gjs_##cname##_proto_funcs[]; \
 static void gjs_##cname##_finalize(JSFreeOp *fop, JSObject *obj); \
@@ -150,7 +151,7 @@ static JSBool gjs_##cname##_new_resolve(JSContext *context, \
 static struct JSClass gjs_##cname##_class = { \
     type_name, \
     JSCLASS_HAS_PRIVATE | \
-    JSCLASS_NEW_RESOLVE, \
+    JSCLASS_NEW_RESOLVE | jsclass_flags, \
     JS_PropertyStub, \
     JS_DeletePropertyStub, \
     JS_PropertyStub, \
diff --git a/gjs/mem.cpp b/gjs/mem.cpp
index 68a31e1..9106a54 100644
--- a/gjs/mem.cpp
+++ b/gjs/mem.cpp
@@ -76,7 +76,7 @@ gjs_memory_report(const char *where,
 {
     int i;
     int n_counters;
-    guint total_objects;
+    int total_objects;
 
     gjs_debug(GJS_DEBUG_MEMORY,
               "Memory report: %s",
diff --git a/gjs/mem.h b/gjs/mem.h
index 6eea42c..ee4e038 100644
--- a/gjs/mem.h
+++ b/gjs/mem.h
@@ -34,7 +34,7 @@
 G_BEGIN_DECLS
 
 typedef struct {
-    unsigned int value;
+    volatile int value;
     const char *name;
 } GjsMemCounter;
 
@@ -60,18 +60,18 @@ GJS_DECLARE_COUNTER(interface)
 
 #define GJS_INC_COUNTER(name)                \
     do {                                        \
-        gjs_counter_everything.value += 1;   \
-        gjs_counter_ ## name .value += 1;    \
+        g_atomic_int_add(&gjs_counter_everything.value, 1); \
+        g_atomic_int_add(&gjs_counter_ ## name .value, 1); \
     } while (0)
 
 #define GJS_DEC_COUNTER(name)                \
     do {                                        \
-        gjs_counter_everything.value -= 1;   \
-        gjs_counter_ ## name .value -= 1;    \
+        g_atomic_int_add(&gjs_counter_everything.value, -1); \
+        g_atomic_int_add(&gjs_counter_ ## name .value, -1); \
     } while (0)
 
 #define GJS_GET_COUNTER(name) \
-    (gjs_counter_ ## name .value)
+    g_atomic_int_get(&gjs_counter_ ## name .value)
 
 void gjs_memory_report(const char *where,
                        gboolean    die_if_leaks);
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index f55313f..0bf6489 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -253,7 +253,7 @@ typedef struct {
     cairo_t * cr;
 } GjsCairoContext;
 
-GJS_DEFINE_PROTO_WITH_GTYPE("CairoContext", cairo_context, CAIRO_GOBJECT_TYPE_CONTEXT)
+GJS_DEFINE_PROTO_WITH_GTYPE("CairoContext", cairo_context, CAIRO_GOBJECT_TYPE_CONTEXT, 
JSCLASS_BACKGROUND_FINALIZE)
 GJS_DEFINE_PRIV_FROM_JS(GjsCairoContext, gjs_cairo_context_class);
 
 static void
diff --git a/modules/cairo-gradient.cpp b/modules/cairo-gradient.cpp
index 8f14108..51e4b2b 100644
--- a/modules/cairo-gradient.cpp
+++ b/modules/cairo-gradient.cpp
@@ -27,7 +27,7 @@
 #include <cairo.h>
 #include "cairo-private.h"
 
-GJS_DEFINE_PROTO_ABSTRACT("CairoGradient", cairo_gradient)
+GJS_DEFINE_PROTO_ABSTRACT("CairoGradient", cairo_gradient, JSCLASS_BACKGROUND_FINALIZE)
 
 static void
 gjs_cairo_gradient_finalize(JSFreeOp *fop,
diff --git a/modules/cairo-image-surface.cpp b/modules/cairo-image-surface.cpp
index 78cccaf..81811f1 100644
--- a/modules/cairo-image-surface.cpp
+++ b/modules/cairo-image-surface.cpp
@@ -27,7 +27,7 @@
 #include <cairo.h>
 #include "cairo-private.h"
 
-GJS_DEFINE_PROTO("CairoImageSurface", cairo_image_surface)
+GJS_DEFINE_PROTO("CairoImageSurface", cairo_image_surface, JSCLASS_BACKGROUND_FINALIZE)
 
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_image_surface)
 {
diff --git a/modules/cairo-linear-gradient.cpp b/modules/cairo-linear-gradient.cpp
index bb9569f..7d20aed 100644
--- a/modules/cairo-linear-gradient.cpp
+++ b/modules/cairo-linear-gradient.cpp
@@ -27,7 +27,7 @@
 #include <cairo.h>
 #include "cairo-private.h"
 
-GJS_DEFINE_PROTO("CairoLinearGradient", cairo_linear_gradient)
+GJS_DEFINE_PROTO("CairoLinearGradient", cairo_linear_gradient, JSCLASS_BACKGROUND_FINALIZE)
 
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_linear_gradient)
 {
diff --git a/modules/cairo-path.cpp b/modules/cairo-path.cpp
index 21bb805..87eb600 100644
--- a/modules/cairo-path.cpp
+++ b/modules/cairo-path.cpp
@@ -33,7 +33,7 @@ typedef struct {
     cairo_path_t    *path;
 } GjsCairoPath;
 
-GJS_DEFINE_PROTO_ABSTRACT("CairoPath", cairo_path)
+GJS_DEFINE_PROTO_ABSTRACT("CairoPath", cairo_path, JSCLASS_BACKGROUND_FINALIZE)
 GJS_DEFINE_PRIV_FROM_JS(GjsCairoPath, gjs_cairo_path_class)
 
 static void
diff --git a/modules/cairo-pattern.cpp b/modules/cairo-pattern.cpp
index 005c534..b394346 100644
--- a/modules/cairo-pattern.cpp
+++ b/modules/cairo-pattern.cpp
@@ -35,7 +35,7 @@ typedef struct {
     cairo_pattern_t *pattern;
 } GjsCairoPattern;
 
-GJS_DEFINE_PROTO_ABSTRACT_WITH_GTYPE("CairoPattern", cairo_pattern, CAIRO_GOBJECT_TYPE_PATTERN)
+GJS_DEFINE_PROTO_ABSTRACT_WITH_GTYPE("CairoPattern", cairo_pattern, CAIRO_GOBJECT_TYPE_PATTERN, 
JSCLASS_BACKGROUND_FINALIZE)
 GJS_DEFINE_PRIV_FROM_JS(GjsCairoPattern, gjs_cairo_pattern_class)
 
 static void
diff --git a/modules/cairo-pdf-surface.cpp b/modules/cairo-pdf-surface.cpp
index 5c51ccb..8f1f082 100644
--- a/modules/cairo-pdf-surface.cpp
+++ b/modules/cairo-pdf-surface.cpp
@@ -30,7 +30,7 @@
 #if CAIRO_HAS_PDF_SURFACE
 #include <cairo-pdf.h>
 
-GJS_DEFINE_PROTO("CairoPDFSurface", cairo_pdf_surface)
+GJS_DEFINE_PROTO("CairoPDFSurface", cairo_pdf_surface, JSCLASS_BACKGROUND_FINALIZE)
 
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_pdf_surface)
 {
diff --git a/modules/cairo-ps-surface.cpp b/modules/cairo-ps-surface.cpp
index 02b67eb..8a5a510 100644
--- a/modules/cairo-ps-surface.cpp
+++ b/modules/cairo-ps-surface.cpp
@@ -30,7 +30,7 @@
 #if CAIRO_HAS_PS_SURFACE
 #include <cairo-ps.h>
 
-GJS_DEFINE_PROTO("CairoPSSurface", cairo_ps_surface)
+GJS_DEFINE_PROTO("CairoPSSurface", cairo_ps_surface, JSCLASS_BACKGROUND_FINALIZE)
 
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_ps_surface)
 {
diff --git a/modules/cairo-radial-gradient.cpp b/modules/cairo-radial-gradient.cpp
index 518b893..506b1c8 100644
--- a/modules/cairo-radial-gradient.cpp
+++ b/modules/cairo-radial-gradient.cpp
@@ -27,7 +27,7 @@
 #include <cairo.h>
 #include "cairo-private.h"
 
-GJS_DEFINE_PROTO("CairoRadialGradient", cairo_radial_gradient)
+GJS_DEFINE_PROTO("CairoRadialGradient", cairo_radial_gradient, JSCLASS_BACKGROUND_FINALIZE)
 
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_radial_gradient)
 {
diff --git a/modules/cairo-solid-pattern.cpp b/modules/cairo-solid-pattern.cpp
index 74cb538..583da0e 100644
--- a/modules/cairo-solid-pattern.cpp
+++ b/modules/cairo-solid-pattern.cpp
@@ -27,7 +27,7 @@
 #include <cairo.h>
 #include "cairo-private.h"
 
-GJS_DEFINE_PROTO_ABSTRACT("CairoSolidPattern", cairo_solid_pattern)
+GJS_DEFINE_PROTO_ABSTRACT("CairoSolidPattern", cairo_solid_pattern, JSCLASS_BACKGROUND_FINALIZE)
 
 static void
 gjs_cairo_solid_pattern_finalize(JSFreeOp *fop,
diff --git a/modules/cairo-surface-pattern.cpp b/modules/cairo-surface-pattern.cpp
index 6218be2..2efbf77 100644
--- a/modules/cairo-surface-pattern.cpp
+++ b/modules/cairo-surface-pattern.cpp
@@ -27,7 +27,7 @@
 #include <cairo.h>
 #include "cairo-private.h"
 
-GJS_DEFINE_PROTO("CairoSurfacePattern", cairo_surface_pattern)
+GJS_DEFINE_PROTO("CairoSurfacePattern", cairo_surface_pattern, JSCLASS_BACKGROUND_FINALIZE)
 
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_surface_pattern)
 {
diff --git a/modules/cairo-surface.cpp b/modules/cairo-surface.cpp
index ce8e621..f883322 100644
--- a/modules/cairo-surface.cpp
+++ b/modules/cairo-surface.cpp
@@ -36,7 +36,7 @@ typedef struct {
     cairo_surface_t *surface;
 } GjsCairoSurface;
 
-GJS_DEFINE_PROTO_ABSTRACT_WITH_GTYPE("CairoSurface", cairo_surface, CAIRO_GOBJECT_TYPE_SURFACE)
+GJS_DEFINE_PROTO_ABSTRACT_WITH_GTYPE("CairoSurface", cairo_surface, CAIRO_GOBJECT_TYPE_SURFACE, 
JSCLASS_BACKGROUND_FINALIZE)
 GJS_DEFINE_PRIV_FROM_JS(GjsCairoSurface, gjs_cairo_surface_class)
 
 static void
diff --git a/modules/cairo-svg-surface.cpp b/modules/cairo-svg-surface.cpp
index be80892..0f53f23 100644
--- a/modules/cairo-svg-surface.cpp
+++ b/modules/cairo-svg-surface.cpp
@@ -30,7 +30,7 @@
 #if CAIRO_HAS_SVG_SURFACE
 #include <cairo-svg.h>
 
-GJS_DEFINE_PROTO("CairoSVGSurface", cairo_svg_surface)
+GJS_DEFINE_PROTO("CairoSVGSurface", cairo_svg_surface, JSCLASS_BACKGROUND_FINALIZE)
 
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_svg_surface)
 {


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