grn_rc grn_ctx_fin(grn_ctx *ctx) { grn_rc rc = GRN_SUCCESS; if (!ctx) { return GRN_INVALID_ARGUMENT; } if (ctx->stat == GRN_CTX_FIN) { return GRN_INVALID_ARGUMENT; } if (!(ctx->flags & GRN_CTX_ALLOCATED)) { CRITICAL_SECTION_ENTER(grn_glock); ctx->next->prev = ctx->prev; ctx->prev->next = ctx->next; CRITICAL_SECTION_LEAVE(grn_glock); } if (ctx->impl) { grn_ctx_impl_clear_n_same_error_messagges(ctx); if (ctx->impl->finalizer) { ctx->impl->finalizer(ctx, 0, NULL, &(ctx->user_data)); } grn_ctx_impl_mrb_fin(ctx); grn_ctx_loader_clear(ctx); if (ctx->impl->parser) { grn_expr_parser_close(ctx); } GRN_OBJ_FIN(ctx, &ctx->impl->current_request_id); if (ctx->impl->values) { #ifndef USE_MEMORY_DEBUG grn_db_obj *o; GRN_ARRAY_EACH(ctx, ctx->impl->values, 0, 0, id, &o, { grn_obj_close(ctx, *((grn_obj **)o)); });
void * grn_plugin_sym(grn_ctx *ctx, grn_id id, const char *symbol) { grn_plugin *plugin; grn_dl_symbol func; if (id == GRN_ID_NIL) { return NULL; } CRITICAL_SECTION_ENTER(grn_plugins_lock); if (!grn_hash_get_value(&grn_gctx, grn_plugins, id, &plugin)) { func = NULL; goto exit; } grn_dl_clear_error(); if (!(func = grn_dl_sym(plugin->dl, symbol))) { const char *label; label = grn_dl_sym_error_label(); SERR("%s", label); } exit: CRITICAL_SECTION_LEAVE(grn_plugins_lock); return func; }
grn_com_queue_entry * grn_com_queue_deque(grn_ctx *ctx, grn_com_queue *q) { grn_com_queue_entry *e = NULL; CRITICAL_SECTION_ENTER(q->cs); if (q->next) { e = q->next; if (!(q->next = e->next)) { q->tail = &q->next; } } CRITICAL_SECTION_LEAVE(q->cs); /* if (q->first == q->last) { if (q->next) { CRITICAL_SECTION_ENTER(q->cs); e = q->next; if (!(q->next = e->next)) { q->tail = &q->next; } CRITICAL_SECTION_LEAVE(q->cs); } } else { e = q->bins[q->first++]; } */ return e; }
static grn_rc grn_plugin_call_register(grn_ctx *ctx, grn_id id) { grn_plugin *plugin; int size; CRITICAL_SECTION_ENTER(grn_plugins_lock); size = grn_hash_get_value(&grn_plugins_ctx, grn_plugins, id, &plugin); CRITICAL_SECTION_LEAVE(grn_plugins_lock); if (size == 0) { return GRN_INVALID_ARGUMENT; } #ifdef GRN_WITH_MRUBY if (!plugin->dl) { return grn_plugin_call_register_mrb(ctx, id, plugin); } #endif /* GRN_WITH_MRUBY */ if (plugin->register_func) { return plugin->register_func(ctx); } return GRN_SUCCESS; }
grn_rc grn_plugin_close(grn_ctx *ctx, grn_id id) { grn_rc rc; grn_plugin *plugin; if (id == GRN_ID_NIL) { return GRN_INVALID_ARGUMENT; } CRITICAL_SECTION_ENTER(grn_plugins_lock); if (!grn_hash_get_value(&grn_gctx, grn_plugins, id, &plugin)) { rc = GRN_INVALID_ARGUMENT; goto exit; } if (--plugin->refcount) { rc = GRN_SUCCESS; goto exit; } if (plugin->dl) { grn_plugin_call_fin(ctx, id); if (!grn_dl_close(plugin->dl)) { const char *label; label = grn_dl_close_error_label(); SERR("%s", label); } } GRN_GFREE(plugin); rc = grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL); exit: CRITICAL_SECTION_LEAVE(grn_plugins_lock); return rc; }
const char * grn_plugin_path(grn_ctx *ctx, grn_id id) { const char *path; grn_plugin *plugin; int value_size; const char *system_plugins_dir; size_t system_plugins_dir_size; if (id == GRN_ID_NIL) { return NULL; } CRITICAL_SECTION_ENTER(grn_plugins_lock); value_size = grn_hash_get_value(&grn_gctx, grn_plugins, id, &plugin); CRITICAL_SECTION_LEAVE(grn_plugins_lock); if (!plugin) { return NULL; } path = plugin->path; system_plugins_dir = grn_plugin_get_system_plugins_dir(); system_plugins_dir_size = strlen(system_plugins_dir); if (strncmp(system_plugins_dir, path, system_plugins_dir_size) == 0) { const char *plugin_name = path + system_plugins_dir_size; while (plugin_name[0] == '/') { plugin_name++; } /* TODO: remove suffix too? */ return plugin_name; } else { return path; } }
const char * grn_plugin_path(grn_ctx *ctx, grn_id id) { const char *path; uint32_t key_size; const char *system_plugins_dir; size_t system_plugins_dir_size; CRITICAL_SECTION_ENTER(grn_plugins_lock); path = _grn_hash_key(&grn_gctx, grn_plugins, id, &key_size); CRITICAL_SECTION_LEAVE(grn_plugins_lock); if (!path) { return NULL; } system_plugins_dir = grn_plugin_get_system_plugins_dir(); system_plugins_dir_size = strlen(system_plugins_dir); if (strncmp(system_plugins_dir, path, system_plugins_dir_size) == 0) { const char *plugin_name = path + system_plugins_dir_size; while (plugin_name[0] == '/') { plugin_name++; } /* TODO: remove suffix too? */ return plugin_name; } else { return path; } }
grn_rc grn_com_queue_enque(grn_ctx *ctx, grn_com_queue *q, grn_com_queue_entry *e) { CRITICAL_SECTION_ENTER(q->cs); e->next = NULL; *q->tail = e; q->tail = &e->next; CRITICAL_SECTION_LEAVE(q->cs); /* uint8_t i = q->last + 1; e->next = NULL; if (q->first == i || q->next) { CRITICAL_SECTION_ENTER(q->cs); if (q->first == i || q->next) { *q->tail = e; q->tail = &e->next; } else { q->bins[q->last] = e; q->last = i; } CRITICAL_SECTION_LEAVE(q->cs); } else { q->bins[q->last] = e; q->last = i; } */ return GRN_SUCCESS; }
static grn_rc grn_ctx_init_internal(grn_ctx *ctx, int flags) { if (!ctx) { return GRN_INVALID_ARGUMENT; } // if (ctx->stat != GRN_CTX_FIN) { return GRN_INVALID_ARGUMENT; } ERRCLR(ctx); ctx->flags = flags; if (grn_ctx_per_db) { ctx->flags |= GRN_CTX_PER_DB; } ctx->stat = GRN_CTX_INITED; ctx->encoding = grn_gctx.encoding; ctx->seqno = 0; ctx->seqno2 = 0; ctx->subno = 0; ctx->impl = NULL; ctx->user_data.ptr = NULL; CRITICAL_SECTION_ENTER(grn_glock); ctx->next = grn_gctx.next; ctx->prev = &grn_gctx; grn_gctx.next->prev = ctx; grn_gctx.next = ctx; CRITICAL_SECTION_LEAVE(grn_glock); ctx->errline = 0; ctx->errfile = ""; ctx->errfunc = ""; ctx->trace[0] = NULL; ctx->errbuf[0] = '\0'; return GRN_SUCCESS; }
/* ----------------------------------------------------------------- Destroy Controler Dialog ----------------------------------------------------------------- */ static void DestroyControler(void) { if (CRITICAL_SECTION_ENTER(controlerbuild_lock)) { if (hwndControler != NULL) DestroyWindow(hwndControler); CRITICAL_SECTION_LEAVE(controlerbuild_lock); } }
grn_id grn_plugin_open(grn_ctx *ctx, const char *filename) { grn_id id; grn_dl dl; grn_plugin **plugin = NULL; CRITICAL_SECTION_ENTER(grn_plugins_lock); if ((id = grn_hash_get(&grn_gctx, grn_plugins, filename, PATHLEN(filename), (void **)&plugin))) { (*plugin)->refcount++; goto exit; } if ((dl = grn_dl_open(filename))) { if ((id = grn_hash_add(&grn_gctx, grn_plugins, filename, PATHLEN(filename), (void **)&plugin, NULL))) { *plugin = GRN_GMALLOCN(grn_plugin, 1); if (*plugin) { if (grn_plugin_initialize(ctx, *plugin, dl, id, filename)) { GRN_GFREE(*plugin); *plugin = NULL; } } if (!*plugin) { grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL); if (grn_dl_close(dl)) { /* Now, __FILE__ set in plugin is invalid. */ ctx->errline = 0; ctx->errfile = NULL; } else { const char *label; label = grn_dl_close_error_label(); SERR(label); } id = GRN_ID_NIL; } else { (*plugin)->refcount = 1; } } else { if (!grn_dl_close(dl)) { const char *label; label = grn_dl_close_error_label(); SERR(label); } } } else { const char *label; label = grn_dl_open_error_label(); SERR(label); } exit: CRITICAL_SECTION_LEAVE(grn_plugins_lock); return id; }
static void default_logger_fin(grn_ctx *ctx, void *user_data) { CRITICAL_SECTION_ENTER(default_logger_lock); if (default_logger_file) { fclose(default_logger_file); default_logger_file = NULL; } CRITICAL_SECTION_LEAVE(default_logger_lock); }
static void default_logger_reopen(grn_ctx *ctx, void *user_data) { GRN_LOG(ctx, GRN_LOG_NOTICE, "log will be closed."); CRITICAL_SECTION_ENTER(default_logger_lock); if (default_logger_file) { fclose(default_logger_file); default_logger_file = NULL; } CRITICAL_SECTION_LEAVE(default_logger_lock); GRN_LOG(ctx, GRN_LOG_NOTICE, "log opened."); }
static void default_query_logger_close(grn_ctx *ctx, void *user_data) { GRN_QUERY_LOG(ctx, GRN_QUERY_LOG_DESTINATION, " ", "query log will be closed: <%s>", default_query_logger_path); CRITICAL_SECTION_ENTER(default_query_logger_lock); if (default_query_logger_file) { fclose(default_query_logger_file); default_query_logger_file = NULL; } CRITICAL_SECTION_LEAVE(default_query_logger_lock); }
/* ----------------------------------------------------------------- Called by DlgProc & Subclassed WndProc of Controler Dialog (SendMessage API is used) ----------------------------------------------------------------- */ static void AdjustPosition(HWND hwnd) { static CRITICAL_SECTION_OBJECT(lock); RECT rect1, rect2; if (CRITICAL_SECTION_ENTER(lock)) { if (hwndWinamp != NULL) { GetWindowRect(hwndWinamp, &rect1); GetWindowRect(hwnd, &rect2); SetWindowPos(hwnd, hwndWinamp, rect1.left, rect1.bottom,rect1.right - rect1.left, rect2.bottom - rect2.top,SWP_NOACTIVATE); } CRITICAL_SECTION_LEAVE(lock); } }
static void default_logger_log(grn_ctx *ctx, grn_log_level level, const char *timestamp, const char *title, const char *message, const char *location, void *user_data) { const char slev[] = " EACewnid-"; if (default_logger_path) { CRITICAL_SECTION_ENTER(default_logger_lock); if (!default_logger_file) { default_logger_file = grn_fopen(default_logger_path, "a"); default_logger_size = 0; if (default_logger_file) { struct stat stat; if (fstat(fileno(default_logger_file), &stat) != -1) { default_logger_size = stat.st_size; } } } if (default_logger_file) { char label = *(slev + level); int written; if (location && *location) { if (title && *title) { written = fprintf(default_logger_file, "%s|%c|%s: %s %s\n", timestamp, label, location, title, message); } else { written = fprintf(default_logger_file, "%s|%c|%s: %s\n", timestamp, label, location, message); } } else { written = fprintf(default_logger_file, "%s|%c|%s %s\n", timestamp, label, title, message); } if (written > 0) { default_logger_size += written; if (LOGGER_NEED_ROTATE(default_logger_size, default_logger_rotate_threshold_size)) { fclose(default_logger_file); default_logger_file = NULL; rotate_log_file(ctx, default_logger_path); } else { fflush(default_logger_file); } } } CRITICAL_SECTION_LEAVE(default_logger_lock); } }
grn_id grn_plugin_reference(grn_ctx *ctx, const char *filename) { grn_id id; grn_plugin **plugin = NULL; CRITICAL_SECTION_ENTER(grn_plugins_lock); id = grn_hash_get(&grn_gctx, grn_plugins, filename, PATHLEN(filename), (void **)&plugin); if (plugin) { (*plugin)->refcount++; } CRITICAL_SECTION_LEAVE(grn_plugins_lock); return id; }
/* ----------------------------------------------------------------- Create Controler Dialog ----------------------------------------------------------------- */ static void CreateControler(void) { if (CRITICAL_SECTION_ENTER(controlerbuild_lock)) { if (hwndControler == NULL) { HINSTANCE hInst = (HINSTANCE)GetModuleHandle(NULL); hwndControler = CreateDialog( winampGetInModule2()->hDllInstance, MAKEINTRESOURCE(IDD_NEZSONGNO), GetDesktopWindow(), ControlerDlgProc ); SubclassDialog(hwndControler); PostMessage(hwndControler, WM_TIMER, 1, 0); } CRITICAL_SECTION_LEAVE(controlerbuild_lock); } }
grn_rc grn_ctx_init(grn_ctx *ctx, int flags) { grn_rc rc; rc = grn_ctx_init_internal(ctx, flags); if (rc == GRN_SUCCESS) { grn_ctx_impl_init(ctx); rc = ctx->rc; if (rc != GRN_SUCCESS) { grn_ctx_fin(ctx); if (flags & GRN_CTX_ALLOCATED) { CRITICAL_SECTION_ENTER(grn_glock); ctx->next->prev = ctx->prev; ctx->prev->next = ctx->next; CRITICAL_SECTION_LEAVE(grn_glock); } } } return rc; }
static void default_query_logger_log(grn_ctx *ctx, unsigned int flag, const char *timestamp, const char *info, const char *message, void *user_data) { if (default_query_logger_path) { CRITICAL_SECTION_ENTER(default_query_logger_lock); if (!default_query_logger_file) { default_query_logger_file = grn_fopen(default_query_logger_path, "a"); default_query_logger_size = 0; if (default_query_logger_file) { struct stat stat; if (fstat(fileno(default_query_logger_file), &stat) != -1) { default_query_logger_size = stat.st_size; } } } if (default_query_logger_file) { int written; written = fprintf(default_query_logger_file, "%s|%s%s\n", timestamp, info, message); if (written > 0) { default_query_logger_size += written; if (LOGGER_NEED_ROTATE(default_query_logger_size, default_query_logger_rotate_threshold_size)) { fclose(default_query_logger_file); default_query_logger_file = NULL; rotate_log_file(ctx, default_query_logger_path); } else { fflush(default_query_logger_file); } } } CRITICAL_SECTION_LEAVE(default_query_logger_lock); } }
/* This function is called for a full text search query or a document to be indexed. This means that both short/long strings are given. The return value of this function is ignored. When an error occurs in this function, `ctx->rc' is overwritten with an error code (not GRN_SUCCESS). */ static grn_obj * mecab_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) { grn_obj *str; int nflags = 0; char *buf, *p; const char *s; grn_obj *table = args[0]; grn_obj_flags table_flags; grn_encoding table_encoding; grn_mecab_tokenizer *token; unsigned int bufsize, len; if (!(str = grn_ctx_pop(ctx))) { ERR(GRN_INVALID_ARGUMENT, "missing argument"); return NULL; } if (!sole_mecab) { CRITICAL_SECTION_ENTER(sole_mecab_lock); if (!sole_mecab) { sole_mecab = mecab_new2("-Owakati"); if (!sole_mecab) { ERR(GRN_TOKENIZER_ERROR, "mecab_new2 failed on grn_mecab_init: %s", mecab_strerror(NULL)); } else { sole_mecab_encoding = get_mecab_encoding(sole_mecab); } } CRITICAL_SECTION_LEAVE(sole_mecab_lock); } if (!sole_mecab) { return NULL; } grn_table_get_info(ctx, table, &table_flags, &table_encoding, NULL); if (table_encoding != sole_mecab_encoding) { ERR(GRN_TOKENIZER_ERROR, "MeCab dictionary charset (%s) does not match the context encoding: <%s>", grn_enctostr(sole_mecab_encoding), grn_enctostr(table_encoding)); return NULL; } if (!(token = GRN_MALLOC(sizeof(grn_mecab_tokenizer)))) { return NULL; } token->mecab = sole_mecab; token->encoding = table_encoding; nflags |= (table_flags & GRN_OBJ_KEY_NORMALIZE); if (!(token->nstr = grn_str_open_(ctx, GRN_TEXT_VALUE(str), GRN_TEXT_LEN(str), nflags, token->encoding))) { GRN_FREE(token); ERR(GRN_TOKENIZER_ERROR, "grn_str_open failed at grn_token_open"); return NULL; } len = token->nstr->norm_blen; CRITICAL_SECTION_ENTER(sole_mecab_lock); s = mecab_sparse_tostr2(token->mecab, token->nstr->norm, len); if (!s) { ERR(GRN_TOKENIZER_ERROR, "mecab_sparse_tostr failed len=%d err=%s", len, mecab_strerror(token->mecab)); } else { bufsize = strlen(s) + 1; if (!(buf = GRN_MALLOC(bufsize))) { GRN_LOG(ctx, GRN_LOG_ALERT, "buffer allocation on mecab_init failed !"); } else { memcpy(buf, s, bufsize); } } CRITICAL_SECTION_LEAVE(sole_mecab_lock); if (!s || !buf) { grn_str_close(ctx, token->nstr); GRN_FREE(token); return NULL; } /* A certain version of mecab returns trailing lf or spaces. */ for (p = buf + bufsize - 2; buf <= p && isspace(*(unsigned char *)p); p--) { *p = '\0'; } user_data->ptr = token; token->buf = buf; token->next = buf; token->end = p + 1; GRN_TEXT_INIT(&token->curr_, GRN_OBJ_DO_SHALLOW_COPY); GRN_UINT32_INIT(&token->stat_, 0); return NULL; }
grn_id grn_plugin_open(grn_ctx *ctx, const char *filename) { grn_id id = GRN_ID_NIL; grn_dl dl; grn_plugin **plugin = NULL; size_t filename_size; filename_size = GRN_PLUGIN_KEY_SIZE(filename); CRITICAL_SECTION_ENTER(grn_plugins_lock); if ((id = grn_hash_get(&grn_gctx, grn_plugins, filename, filename_size, (void **)&plugin))) { (*plugin)->refcount++; goto exit; } #ifdef GRN_WITH_MRUBY { const char *mrb_suffix; mrb_suffix = grn_plugin_get_ruby_suffix(); if (filename_size > strlen(mrb_suffix) && strcmp(filename + (strlen(filename) - strlen(mrb_suffix)), mrb_suffix) == 0) { id = grn_plugin_open_mrb(ctx, filename, filename_size); goto exit; } } #endif /* GRN_WITH_MRUBY */ if ((dl = grn_dl_open(filename))) { if ((id = grn_hash_add(&grn_gctx, grn_plugins, filename, filename_size, (void **)&plugin, NULL))) { *plugin = GRN_GMALLOCN(grn_plugin, 1); if (*plugin) { grn_memcpy((*plugin)->path, filename, filename_size); if (grn_plugin_initialize(ctx, *plugin, dl, id, filename)) { GRN_GFREE(*plugin); *plugin = NULL; } } if (!*plugin) { grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL); if (grn_dl_close(dl)) { /* Now, __FILE__ set in plugin is invalid. */ ctx->errline = 0; ctx->errfile = NULL; } else { const char *label; label = grn_dl_close_error_label(); SERR("%s", label); } id = GRN_ID_NIL; } else { (*plugin)->refcount = 1; } } else { if (!grn_dl_close(dl)) { const char *label; label = grn_dl_close_error_label(); SERR("%s", label); } } } else { const char *label; label = grn_dl_open_error_label(); SERR("%s", label); } exit: CRITICAL_SECTION_LEAVE(grn_plugins_lock); return id; }