[Groonga-commit] groonga/groonga at 7ea0bcf [master] token_filter: fix thread unsafe code

Back to archive index

Naoya Murakami null+****@clear*****
Sun Jun 19 02:01:06 JST 2016


Naoya Murakami	2016-06-19 02:01:06 +0900 (Sun, 19 Jun 2016)

  New Revision: 7ea0bcf1eadb075fe005d80ec7bb4297b969ca3b
  https://github.com/groonga/groonga/commit/7ea0bcf1eadb075fe005d80ec7bb4297b969ca3b

  Merged 5b14fcf: Merge pull request #557 from naoa/token-filter-thread-safe

  Message:
    token_filter: fix thread unsafe code
    
    GitHub: #556

  Modified files:
    lib/grn_token_cursor.h
    lib/token_cursor.c

  Modified: lib/grn_token_cursor.h (+4 -1)
===================================================================
--- lib/grn_token_cursor.h    2016-06-16 22:40:55 +0900 (cbb4527)
+++ lib/grn_token_cursor.h    2016-06-19 02:01:06 +0900 (40ccaed)
@@ -58,7 +58,10 @@ typedef struct {
   grn_encoding encoding;
   grn_obj *tokenizer;
   grn_proc_ctx pctx;
-  grn_obj *token_filters;
+  struct {
+    grn_obj *objects;
+    grn_obj user_data_ptrs;
+  } token_filters;
   uint32_t variant;
   grn_obj *nstr;
 } grn_token_cursor;

  Modified: lib/token_cursor.c (+16 -10)
===================================================================
--- lib/token_cursor.c    2016-06-16 22:40:55 +0900 (c2f8890)
+++ lib/token_cursor.c    2016-06-19 02:01:06 +0900 (8cc9831)
@@ -24,7 +24,7 @@ static void
 grn_token_cursor_open_initialize_token_filters(grn_ctx *ctx,
                                                grn_token_cursor *token_cursor)
 {
-  grn_obj *token_filters = token_cursor->token_filters;
+  grn_obj *token_filters = token_cursor->token_filters.objects;
   unsigned int i, n_token_filters;
 
   if (token_filters) {
@@ -32,15 +32,18 @@ grn_token_cursor_open_initialize_token_filters(grn_ctx *ctx,
   } else {
     n_token_filters = 0;
   }
+  GRN_PTR_INIT(&(token_cursor->token_filters.user_data_ptrs),
+               GRN_OBJ_VECTOR, GRN_ID_NIL);
 
   for (i = 0; i < n_token_filters; i++) {
     grn_obj *token_filter_object = GRN_PTR_VALUE_AT(token_filters, i);
     grn_proc *token_filter = (grn_proc *)token_filter_object;
+    void *user_data;
 
-    token_filter->user_data =
-      token_filter->callbacks.token_filter.init(ctx,
-                                                token_cursor->table,
-                                                token_cursor->mode);
+    user_data = token_filter->callbacks.token_filter.init(ctx,
+                                                          token_cursor->table,
+                                                          token_cursor->mode);
+    GRN_PTR_PUT(ctx, &(token_cursor->token_filters.user_data_ptrs), user_data);
   }
 }
 
@@ -64,7 +67,7 @@ grn_token_cursor_open(grn_ctx *ctx, grn_obj *table,
   token_cursor->mode = mode;
   token_cursor->encoding = encoding;
   token_cursor->tokenizer = tokenizer;
-  token_cursor->token_filters = token_filters;
+  token_cursor->token_filters.objects = token_filters;
   token_cursor->orig = (const unsigned char *)str;
   token_cursor->orig_blen = str_len;
   token_cursor->curr = NULL;
@@ -126,7 +129,7 @@ grn_token_cursor_next_apply_token_filters(grn_ctx *ctx,
                                           grn_obj *current_token_data,
                                           grn_obj *status)
 {
-  grn_obj *token_filters = token_cursor->token_filters;
+  grn_obj *token_filters = token_cursor->token_filters.objects;
   unsigned int i, n_token_filters;
   grn_token current_token;
   grn_token next_token;
@@ -151,6 +154,7 @@ grn_token_cursor_next_apply_token_filters(grn_ctx *ctx,
   for (i = 0; i < n_token_filters; i++) {
     grn_obj *token_filter_object = GRN_PTR_VALUE_AT(token_filters, i);
     grn_proc *token_filter = (grn_proc *)token_filter_object;
+    void *user_data = GRN_PTR_VALUE_AT(&(token_cursor->token_filters.user_data_ptrs), i);
 
 #define SKIP_FLAGS\
     (GRN_TOKEN_SKIP |\
@@ -163,7 +167,7 @@ grn_token_cursor_next_apply_token_filters(grn_ctx *ctx,
     token_filter->callbacks.token_filter.filter(ctx,
                                                 &current_token,
                                                 &next_token,
-                                                token_filter->user_data);
+                                                user_data);
     GRN_TEXT_SET(ctx, &(current_token.data),
                  GRN_TEXT_VALUE(&(next_token.data)),
                  GRN_TEXT_LEN(&(next_token.data)));
@@ -324,7 +328,7 @@ static void
 grn_token_cursor_close_token_filters(grn_ctx *ctx,
                                      grn_token_cursor *token_cursor)
 {
-  grn_obj *token_filters = token_cursor->token_filters;
+  grn_obj *token_filters = token_cursor->token_filters.objects;
   unsigned int i, n_token_filters;
 
   if (token_filters) {
@@ -335,9 +339,11 @@ grn_token_cursor_close_token_filters(grn_ctx *ctx,
   for (i = 0; i < n_token_filters; i++) {
     grn_obj *token_filter_object = GRN_PTR_VALUE_AT(token_filters, i);
     grn_proc *token_filter = (grn_proc *)token_filter_object;
+    void *user_data = GRN_PTR_VALUE_AT(&(token_cursor->token_filters.user_data_ptrs), i);
 
-    token_filter->callbacks.token_filter.fin(ctx, token_filter->user_data);
+    token_filter->callbacks.token_filter.fin(ctx, user_data);
   }
+  GRN_OBJ_FIN(ctx, &(token_cursor->token_filters.user_data_ptrs));
 }
 
 grn_rc
-------------- next part --------------
HTML����������������������������...
Descargar 



More information about the Groonga-commit mailing list
Back to archive index