Kouhei Sutou
null+****@clear*****
Mon Jul 30 15:58:08 JST 2018
Kouhei Sutou 2018-07-30 15:58:08 +0900 (Mon, 30 Jul 2018) New Revision: 894043bfc48d68b9202da0814e5aced1725b3fa6 https://github.com/groonga/groonga/commit/894043bfc48d68b9202da0814e5aced1725b3fa6 Message: table: add macro to lock Modified files: lib/db.c lib/grn_table.h Modified: lib/db.c (+18 -84) =================================================================== --- lib/db.c 2018-07-30 15:21:28 +0900 (3c0d1e048) +++ lib/db.c 2018-07-30 15:58:08 +0900 (4cffeb4aa) @@ -1604,16 +1604,9 @@ grn_table_add(grn_ctx *ctx, grn_obj *table, const void *key, unsigned int key_si { grn_pat *pat = (grn_pat *)table; WITH_NORMALIZE(pat, key, key_size, { - if (pat->io && !(pat->io->flags & GRN_IO_TEMPORARY)) { - if (grn_io_lock(ctx, pat->io, grn_lock_timeout)) { - id = GRN_ID_NIL; - } else { - id = grn_pat_add(ctx, pat, key, key_size, NULL, &added_); - grn_io_unlock(pat->io); - } - } else { + GRN_TABLE_LOCK_BEGIN(ctx, table) { id = grn_pat_add(ctx, pat, key, key_size, NULL, &added_); - } + } GRN_TABLE_LOCK_END(ctx, table); }); if (added) { *added = added_; } } @@ -1622,16 +1615,9 @@ grn_table_add(grn_ctx *ctx, grn_obj *table, const void *key, unsigned int key_si { grn_dat *dat = (grn_dat *)table; WITH_NORMALIZE(dat, key, key_size, { - if (dat->io && !(dat->io->flags & GRN_IO_TEMPORARY)) { - if (grn_io_lock(ctx, dat->io, grn_lock_timeout)) { - id = GRN_ID_NIL; - } else { - id = grn_dat_add(ctx, dat, key, key_size, NULL, &added_); - grn_io_unlock(dat->io); - } - } else { + GRN_TABLE_LOCK_BEGIN(ctx, table) { id = grn_dat_add(ctx, dat, key, key_size, NULL, &added_); - } + } GRN_TABLE_LOCK_END(ctx, table); }); if (added) { *added = added_; } } @@ -1640,16 +1626,9 @@ grn_table_add(grn_ctx *ctx, grn_obj *table, const void *key, unsigned int key_si { grn_hash *hash = (grn_hash *)table; WITH_NORMALIZE(hash, key, key_size, { - if (hash->io && !(hash->io->flags & GRN_IO_TEMPORARY)) { - if (grn_io_lock(ctx, hash->io, grn_lock_timeout)) { - id = GRN_ID_NIL; - } else { - id = grn_hash_add(ctx, hash, key, key_size, NULL, &added_); - grn_io_unlock(hash->io); - } - } else { + GRN_TABLE_LOCK_BEGIN(ctx, table) { id = grn_hash_add(ctx, hash, key, key_size, NULL, &added_); - } + } GRN_TABLE_LOCK_END(ctx, table); }); if (added) { *added = added_; } } @@ -1657,16 +1636,9 @@ grn_table_add(grn_ctx *ctx, grn_obj *table, const void *key, unsigned int key_si case GRN_TABLE_NO_KEY : { grn_array *array = (grn_array *)table; - if (array->io && !(array->io->flags & GRN_IO_TEMPORARY)) { - if (grn_io_lock(ctx, array->io, grn_lock_timeout)) { - id = GRN_ID_NIL; - } else { - id = grn_array_add(ctx, array, NULL); - grn_io_unlock(array->io); - } - } else { + GRN_TABLE_LOCK_BEGIN(ctx, table) { id = grn_array_add(ctx, array, NULL); - } + } GRN_TABLE_LOCK_END(ctx, table); added_ = id ? 1 : 0; if (added) { *added = added_; } } @@ -2216,58 +2188,34 @@ grn_table_delete(grn_ctx *ctx, grn_obj *table, const void *key, unsigned int key case GRN_TABLE_PAT_KEY : WITH_NORMALIZE((grn_pat *)table, key, key_size, { grn_pat *pat = (grn_pat *)table; - if (pat->io && !(pat->io->flags & GRN_IO_TEMPORARY)) { - if (!(rc = grn_io_lock(ctx, pat->io, grn_lock_timeout))) { - rc = grn_table_delete_prepare(ctx, table, rid, key, key_size); - if (rc == GRN_SUCCESS) { - rc = grn_pat_delete(ctx, pat, key, key_size, NULL); - } - grn_io_unlock(pat->io); - } - } else { + GRN_TABLE_LOCK_BEGIN(ctx, table) { rc = grn_table_delete_prepare(ctx, table, rid, key, key_size); if (rc == GRN_SUCCESS) { rc = grn_pat_delete(ctx, pat, key, key_size, NULL); } - } + } GRN_TABLE_LOCK_END(ctx, table); }); break; case GRN_TABLE_DAT_KEY : WITH_NORMALIZE((grn_dat *)table, key, key_size, { grn_dat *dat = (grn_dat *)table; - if (dat->io && !(dat->io->flags & GRN_IO_TEMPORARY)) { - if (!(rc = grn_io_lock(ctx, dat->io, grn_lock_timeout))) { - rc = grn_table_delete_prepare(ctx, table, rid, key, key_size); - if (rc == GRN_SUCCESS) { - rc = grn_dat_delete(ctx, dat, key, key_size, NULL); - } - grn_io_unlock(dat->io); - } - } else { + GRN_TABLE_LOCK_BEGIN(ctx, table) { rc = grn_table_delete_prepare(ctx, table, rid, key, key_size); if (rc == GRN_SUCCESS) { rc = grn_dat_delete(ctx, dat, key, key_size, NULL); } - } + } GRN_TABLE_LOCK_END(ctx, table); }); break; case GRN_TABLE_HASH_KEY : WITH_NORMALIZE((grn_hash *)table, key, key_size, { grn_hash *hash = (grn_hash *)table; - if (hash->io && !(hash->io->flags & GRN_IO_TEMPORARY)) { - if (!(rc = grn_io_lock(ctx, hash->io, grn_lock_timeout))) { - rc = grn_table_delete_prepare(ctx, table, rid, key, key_size); - if (rc == GRN_SUCCESS) { - rc = grn_hash_delete(ctx, hash, key, key_size, NULL); - } - grn_io_unlock(hash->io); - } - } else { + GRN_TABLE_LOCK_BEGIN(ctx, table) { rc = grn_table_delete_prepare(ctx, table, rid, key, key_size); if (rc == GRN_SUCCESS) { rc = grn_hash_delete(ctx, hash, key, key_size, NULL); } - } + } GRN_TABLE_LOCK_END(ctx, table); }); break; } @@ -2276,7 +2224,6 @@ grn_table_delete(grn_ctx *ctx, grn_obj *table, const void *key, unsigned int key } } } -exit : GRN_API_RETURN(rc); } @@ -2322,16 +2269,10 @@ grn_rc grn_table_delete_by_id(grn_ctx *ctx, grn_obj *table, grn_id id) { grn_rc rc; - grn_io *io; GRN_API_ENTER; - if ((io = grn_obj_get_io(ctx, table)) && !(io->flags & GRN_IO_TEMPORARY)) { - if (!(rc = grn_io_lock(ctx, io, grn_lock_timeout))) { - rc = _grn_table_delete_by_id(ctx, table, id, NULL); - grn_io_unlock(io); - } - } else { + GRN_TABLE_LOCK_BEGIN(ctx, table) { rc = _grn_table_delete_by_id(ctx, table, id, NULL); - } + } GRN_TABLE_LOCK_END(ctx, table); if (rc == GRN_SUCCESS) { grn_obj_touch(ctx, table, NULL); } @@ -10475,16 +10416,9 @@ grn_table_update_by_id(grn_ctx *ctx, grn_obj *table, grn_id id, GRN_API_ENTER; if (table->header.type == GRN_TABLE_DAT_KEY) { grn_dat *dat = (grn_dat *)table; - if (dat->io && !(dat->io->flags & GRN_IO_TEMPORARY)) { - if (grn_io_lock(ctx, dat->io, grn_lock_timeout)) { - rc = ctx->rc; - } else { - rc = grn_dat_update_by_id(ctx, dat, id, dest_key, dest_key_size); - grn_io_unlock(dat->io); - } - } else { + GRN_TABLE_LOCK_BEGIN(ctx, table) { rc = grn_dat_update_by_id(ctx, dat, id, dest_key, dest_key_size); - } + } GRN_TABLE_LOCK_END(ctx, table); } GRN_API_RETURN(rc); } Modified: lib/grn_table.h (+19 -0) =================================================================== --- lib/grn_table.h 2018-07-30 15:21:28 +0900 (6111d54b8) +++ lib/grn_table.h 2018-07-30 15:58:08 +0900 (b84a2f19d) @@ -46,6 +46,25 @@ void grn_table_module_set_options(grn_ctx *ctx, void grn_table_module_fin(grn_ctx *ctx, grn_table_module *module); +#define GRN_TABLE_LOCK_BEGIN(ctx, table) do { \ + grn_io *io_ = grn_obj_get_io(ctx, table); \ + grn_bool locked_ = GRN_FALSE; \ + grn_bool can_run_ = GRN_TRUE; \ + if (io_ && !(io_->flags & GRN_IO_TEMPORARY)) { \ + if (grn_io_lock(ctx, io_, grn_lock_timeout) == GRN_SUCCESS) { \ + locked_ = GRN_TRUE; \ + } else { \ + can_run_ = GRN_FALSE; \ + } \ + } \ + if (can_run_) { +#define GRN_TABLE_LOCK_END(ctx, table) \ + } \ + if (locked_) { \ + grn_io_unlock(io_); \ + } \ +} while (GRN_FALSE) + #ifdef __cplusplus } #endif -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180730/edb28cb4/attachment-0001.htm