diff options
| author | ruki <[email protected]> | 2019-07-26 00:57:58 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-07-25 23:47:52 +0800 |
| commit | 59472a642f18bf17953deddd82c68678d40fa254 (patch) | |
| tree | bb348edc861fbeeca46935479637b3ef882a0cd5 /core/src | |
| parent | 5727deff9988a851f528308c1c6a941f3cab56f3 (diff) | |
improve filelock
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/io/filelock.h | 5 | ||||
| -rw-r--r-- | core/src/xmake/io/filelock_close___gc.c | 1 | ||||
| -rw-r--r-- | core/src/xmake/io/filelock_islocked.c | 53 | ||||
| -rw-r--r-- | core/src/xmake/io/filelock_lock.c | 25 | ||||
| -rw-r--r-- | core/src/xmake/io/filelock_trylock.c | 24 | ||||
| -rw-r--r-- | core/src/xmake/io/filelock_unlock.c | 1 | ||||
| -rw-r--r-- | core/src/xmake/io/openlock.c | 1 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 |
9 files changed, 88 insertions, 25 deletions
diff --git a/core/src/xmake/io/filelock.h b/core/src/xmake/io/filelock.h index f3c2a8f43..6221b2cae 100644 --- a/core/src/xmake/io/filelock.h +++ b/core/src/xmake/io/filelock.h @@ -64,9 +64,12 @@ typedef struct __xm_io_filelock_t // the lock reference tb_filelock_ref_t lock_ref; - // is opened + // is opened? tb_bool_t is_opened; + // is locked? + tb_bool_t is_locked; + // the lock name tb_char_t name[64]; diff --git a/core/src/xmake/io/filelock_close___gc.c b/core/src/xmake/io/filelock_close___gc.c index a2f263100..792d60a7b 100644 --- a/core/src/xmake/io/filelock_close___gc.c +++ b/core/src/xmake/io/filelock_close___gc.c @@ -58,6 +58,7 @@ static tb_int_t xm_io_filelock_close_impl(lua_State* lua, tb_bool_t allow_closed tb_filelock_exit(lock->lock_ref); lock->lock_ref = tb_null; lock->is_opened = tb_false; + lock->is_locked = tb_false; // free lock path if (lock->path) diff --git a/core/src/xmake/io/filelock_islocked.c b/core/src/xmake/io/filelock_islocked.c new file mode 100644 index 000000000..b2a226ef9 --- /dev/null +++ b/core/src/xmake/io/filelock_islocked.c @@ -0,0 +1,53 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2019, TBOOX Open Source Group. + * + * @author ruki + * @file filelock_islocked.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "filelock_islocked" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "filelock.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* lock:islocked() + */ +tb_int_t xm_io_filelock_islocked(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // this lock has been closed? + xm_io_filelock_t* lock = xm_io_get_filelock(lua); + if (!lock->is_opened) + xm_io_filelock_return_error_closed(lua); + else + { + lua_pushboolean(lua, lock->is_locked); + xm_io_filelock_return_success(); + } +} diff --git a/core/src/xmake/io/filelock_lock.c b/core/src/xmake/io/filelock_lock.c index 638cdd4fb..0e9a93d47 100644 --- a/core/src/xmake/io/filelock_lock.c +++ b/core/src/xmake/io/filelock_lock.c @@ -37,17 +37,23 @@ /* lock file * * exclusive lock: filelock:lock("/xxxx/filelock") - * exclusive lock: filelock:lock("/xxxx/filelock", "ex") - * shared lock: filelock:lock("/xxxx/filelock", "sh") + * shared lock: filelock:lock("/xxxx/filelock", {shared = true}) */ tb_int_t xm_io_filelock_lock(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); - // get lock mode - tb_char_t const* modestr = luaL_optstring(lua, 2, "ex"); - tb_assert_and_check_return_val(modestr, 0); + // get option argument + tb_bool_t is_shared = tb_false; + if (lua_istable(lua, 2)) + { + // is shared lock? + lua_pushstring(lua, "shared"); + lua_gettable(lua, 2); + is_shared = (tb_bool_t)lua_toboolean(lua, -1); + lua_pop(lua, 1); + } // this lock has been closed? xm_io_filelock_t* lock = xm_io_get_filelock(lua); @@ -55,15 +61,10 @@ tb_int_t xm_io_filelock_lock(lua_State* lua) xm_io_filelock_return_error_closed(lua); else { - // is exclusive mode? - tb_bool_t is_exclusive = tb_true; - if (!tb_strcmp(modestr, "sh")) is_exclusive = tb_false; - else if (tb_strcmp(modestr, "ex")) - xm_io_filelock_return_error(lua, lock, "invalid lock mode!"); - // lock it - if (tb_filelock_enter(lock->lock_ref, is_exclusive? TB_FILELOCK_MODE_EX : TB_FILELOCK_MODE_SH)) + if (lock->is_locked || tb_filelock_enter(lock->lock_ref, is_shared? TB_FILELOCK_MODE_SH : TB_FILELOCK_MODE_EX)) { + lock->is_locked = tb_true; lua_pushboolean(lua, tb_true); xm_io_filelock_return_success(); } diff --git a/core/src/xmake/io/filelock_trylock.c b/core/src/xmake/io/filelock_trylock.c index 4195b2312..f13d04936 100644 --- a/core/src/xmake/io/filelock_trylock.c +++ b/core/src/xmake/io/filelock_trylock.c @@ -37,17 +37,23 @@ /* try to lock file * * exclusive lock: filelock:trylock("/xxxx/filelock") - * exclusive lock: filelock:trylock("/xxxx/filelock", "ex") - * shared lock: filelock:trylock("/xxxx/filelock", "sh") + * shared lock: filelock:trylock("/xxxx/filelock", {shared = true}) */ tb_int_t xm_io_filelock_trylock(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); - // get lock mode - tb_char_t const* modestr = luaL_optstring(lua, 2, "ex"); - tb_assert_and_check_return_val(modestr, 0); + // get option argument + tb_bool_t is_shared = tb_false; + if (lua_istable(lua, 2)) + { + // is shared lock? + lua_pushstring(lua, "shared"); + lua_gettable(lua, 2); + is_shared = (tb_bool_t)lua_toboolean(lua, -1); + lua_pop(lua, 1); + } // this lock has been closed? xm_io_filelock_t* lock = xm_io_get_filelock(lua); @@ -55,14 +61,8 @@ tb_int_t xm_io_filelock_trylock(lua_State* lua) xm_io_filelock_return_error_closed(lua); else { - // is exclusive mode? - tb_bool_t is_exclusive = tb_true; - if (!tb_strcmp(modestr, "sh")) is_exclusive = tb_false; - else if (tb_strcmp(modestr, "ex")) - xm_io_filelock_return_error(lua, lock, "invalid lock mode!"); - // try to lock it - if (tb_filelock_enter_try(lock->lock_ref, is_exclusive? TB_FILELOCK_MODE_EX : TB_FILELOCK_MODE_SH)) + if (lock->is_locked || tb_filelock_enter_try(lock->lock_ref, is_shared? TB_FILELOCK_MODE_SH : TB_FILELOCK_MODE_EX)) { lua_pushboolean(lua, tb_true); xm_io_filelock_return_success(); diff --git a/core/src/xmake/io/filelock_unlock.c b/core/src/xmake/io/filelock_unlock.c index 266aab205..a6b0150d4 100644 --- a/core/src/xmake/io/filelock_unlock.c +++ b/core/src/xmake/io/filelock_unlock.c @@ -49,6 +49,7 @@ tb_int_t xm_io_filelock_unlock(lua_State* lua) // unlock it if (tb_filelock_leave(lock->lock_ref)) { + lock->is_locked = tb_false; lua_pushboolean(lua, tb_true); xm_io_filelock_return_success(); } diff --git a/core/src/xmake/io/openlock.c b/core/src/xmake/io/openlock.c index 5df847563..12bb242e3 100644 --- a/core/src/xmake/io/openlock.c +++ b/core/src/xmake/io/openlock.c @@ -54,6 +54,7 @@ tb_int_t xm_io_openlock(lua_State* lua) xm_io_filelock_t* xmlock = xm_io_new_filelock(lua); xmlock->lock_ref = lock; xmlock->is_opened = tb_true; + xmlock->is_locked = tb_false; // save lock path tb_size_t pathlen = tb_strlen(path); diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 78bddbc2b..5e34d8fd5 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -111,6 +111,7 @@ tb_int_t xm_io_filelock_path(lua_State* lua); tb_int_t xm_io_filelock_lock(lua_State* lua); tb_int_t xm_io_filelock_unlock(lua_State* lua); tb_int_t xm_io_filelock_trylock(lua_State* lua); +tb_int_t xm_io_filelock_islocked(lua_State* lua); tb_int_t xm_io_filelock_close(lua_State* lua); tb_int_t xm_io_filelock___gc(lua_State* lua); tb_int_t xm_io_filelock___tostring(lua_State* lua); @@ -260,6 +261,7 @@ static luaL_Reg const g_io_filelock_functions[] = , { "lock", xm_io_filelock_lock } , { "unlock", xm_io_filelock_unlock } , { "trylock", xm_io_filelock_trylock } +, { "islocked", xm_io_filelock_islocked } , { "__gc", xm_io_filelock___gc } , { "__tostring", xm_io_filelock___tostring } , { tb_null, tb_null } diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 89500e727..7f8e5e4c5 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -57,6 +57,7 @@ xmake_C_FILES += \ io/filelock_lock \ io/filelock_unlock \ io/filelock_trylock \ + io/filelock_islocked \ io/filelock___tostring \ io/filelock_close___gc \ io/open \ |
