[Groonga-commit] groonga/groonga [master] support finalizer of grn_ctx. #719

Back to archive index

null+****@clear***** null+****@clear*****
2010年 11月 22日 (月) 16:49:09 JST


Kouhei Sutou	2010-11-22 07:49:09 +0000 (Mon, 22 Nov 2010)

  New Revision: 61f68344b92ff514219ccfdc3d6ac8f6d1173c05

  Log:
    support finalizer of grn_ctx. #719

  Modified files:
    include/groonga.h
    lib/ctx.c
    lib/ql.h

  Modified: include/groonga.h (+9 -0)
===================================================================
--- include/groonga.h    2010-11-18 07:33:43 +0000 (8170b64)
+++ include/groonga.h    2010-11-22 07:49:09 +0000 (975a018)
@@ -232,6 +232,15 @@ GRN_API grn_ctx *grn_ctx_open(int flags);
 GRN_API grn_rc grn_ctx_fin(grn_ctx *ctx);
 
 /**
+ * grn_ctx_set_finalizer:
+ * @ctx: 対象ctx
+ * @func: @ctxを破棄するときに呼ばれる関数
+ *
+ * @ctxを破棄するときに呼ばれる関数を設定する。
+ **/
+GRN_API grn_rc grn_ctx_set_finalizer(grn_ctx *ctx, grn_proc_func *func);
+
+/**
  * grn_get_default_encoding:
  *
  * デフォルトのencodingを返します。

  Modified: lib/ctx.c (+17 -0)
===================================================================
--- lib/ctx.c    2010-11-18 07:33:43 +0000 (87c1006)
+++ lib/ctx.c    2010-11-22 07:49:09 +0000 (832643d)
@@ -293,6 +293,8 @@ grn_ctx_impl_init(grn_ctx *ctx)
       grn_get_default_match_escalation_threshold();
   }
 
+  ctx->impl->finalizer = NULL;
+
   ctx->impl->phs = NIL;
   ctx->impl->code = NIL;
   ctx->impl->dump = NIL;
@@ -429,6 +431,9 @@ grn_ctx_fin(grn_ctx *ctx)
     CRITICAL_SECTION_LEAVE(grn_glock);
   }
   if (ctx->impl) {
+    if (ctx->impl->finalizer) {
+      ctx->impl->finalizer(ctx, 0, NULL, &(ctx->user_data));
+    }
     grn_ctx_loader_clear(ctx);
     if (ctx->impl->objects) {
       grn_cell *o;
@@ -503,6 +508,18 @@ grn_ctx_fin(grn_ctx *ctx)
   return rc;
 }
 
+grn_rc
+grn_ctx_set_finalizer(grn_ctx *ctx, grn_proc_func *finalizer)
+{
+  if (!ctx) { return GRN_INVALID_ARGUMENT; }
+  if (!ctx->impl) {
+    grn_ctx_impl_init(ctx);
+    if (ERRP(ctx, GRN_ERROR)) { return ctx->rc; }
+  }
+  ctx->impl->finalizer = finalizer;
+  return GRN_SUCCESS;
+}
+
 grn_timeval grn_starttime;
 const char *grn_log_path = GRN_LOG_PATH;
 const char *grn_qlog_path = NULL;

  Modified: lib/ql.h (+3 -0)
===================================================================
--- lib/ql.h    2010-11-18 07:33:43 +0000 (9a9edae)
+++ lib/ql.h    2010-11-22 07:49:09 +0000 (69ad929)
@@ -224,6 +224,9 @@ struct _grn_ctx_impl {
   /* match escalation portion */
   int64_t match_escalation_threshold;
 
+  /* lifetime portion */
+  grn_proc_func *finalizer;
+
   /* ql portion */
   uint32_t ncells;
   uint32_t seqno;




Groonga-commit メーリングリストの案内
Back to archive index