From: Volker Lendecke Date: Mon, 13 Aug 2018 13:07:06 +0000 (+0200) Subject: g_lock: Simplify g_lock_trylock X-Git-Tag: ldb-1.5.0~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=00513daf78eb29996c81ca4d3be9e71a8e872829;p=thirdparty%2Fsamba.git g_lock: Simplify g_lock_trylock While chasing a bug in g_lock (not in master) I saw some opportunity to simplify g_lock_trylock a bit. This is array handling, and array handling is just extremely error-prone. This *might* be a little less efficient or large numbers of READ locks, but this remains to be seen. For now, simplify the code. First, we make two passes now: One to remove ourselves, and the other one to search for conflicts. Mixing up both made it pretty hard for me to follow the code. Second, I've removed the _mylock and mylock pointer/struct logic and replaced it with the "mylock.pid.pid != 0 ? &mylock : NULL" when calling g_lock_store. To me, this focuses the logic whether to add ourselves in one place instead of spreading it around in the whole routine. Signed-off-by: Volker Lendecke Reviewed-by: Jeremy Allison Autobuild-User(master): Volker Lendecke Autobuild-Date(master): Tue Aug 14 11:42:10 CEST 2018 on sn-devel-144 --- diff --git a/source3/lib/g_lock.c b/source3/lib/g_lock.c index c824f4b0a58..de24b6c847b 100644 --- a/source3/lib/g_lock.c +++ b/source3/lib/g_lock.c @@ -200,8 +200,7 @@ static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self, TDB_DATA data; size_t i; struct g_lock lck; - struct g_lock_rec _mylock; - struct g_lock_rec *mylock = NULL; + struct g_lock_rec mylock = {0}; NTSTATUS status; bool modified = false; bool ok; @@ -233,9 +232,13 @@ static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self, } } - i = 0; + /* + * For the lock upgrade/downgrade case, remove ourselves from + * the list. We re-add ourselves later after we checked the + * other entries for conflict. + */ - while (i < lck.num_recs) { + for (i=0; i