From d71e72248096d12ce2a8ca7ccb36ef97ae486583 Mon Sep 17 00:00:00 2001 From: Vikas Gorur Date: Tue, 7 Apr 2009 03:27:24 -0700 Subject: Add lock-less versions of fd_ctx_* Added __fd_ctx_get __fd_ctx_set __fd_ctx_del which do not hold any lock. Signed-off-by: Anand V. Avati --- libglusterfs/src/fd.c | 147 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 95 insertions(+), 52 deletions(-) (limited to 'libglusterfs/src/fd.c') diff --git a/libglusterfs/src/fd.c b/libglusterfs/src/fd.c index deb3c8001..fc78acc7c 100644 --- a/libglusterfs/src/fd.c +++ b/libglusterfs/src/fd.c @@ -586,7 +586,7 @@ fd_list_empty (inode_t *inode) } int -fd_ctx_set (fd_t *fd, xlator_t *xlator, uint64_t value) +__fd_ctx_set (fd_t *fd, xlator_t *xlator, uint64_t value) { int index = 0; int ret = 0; @@ -595,37 +595,52 @@ fd_ctx_set (fd_t *fd, xlator_t *xlator, uint64_t value) if (!fd || !xlator) return -1; - LOCK (&fd->lock); - { - for (index = 0; index < xlator->ctx->xl_count; index++) { - if (!fd->_ctx[index].key) { - if (set_idx == -1) - set_idx = index; - /* dont break, to check if key already exists - further on */ - } - if (fd->_ctx[index].key == (uint64_t)(long) xlator) { + for (index = 0; index < xlator->ctx->xl_count; index++) { + if (!fd->_ctx[index].key) { + if (set_idx == -1) set_idx = index; - break; - } + /* dont break, to check if key already exists + further on */ } - - if (set_idx == -1) { - ret = -1; - goto unlock; + if (fd->_ctx[index].key == (uint64_t)(long) xlator) { + set_idx = index; + break; } + } + + if (set_idx == -1) { + ret = -1; + goto out; + } + + fd->_ctx[set_idx].key = (uint64_t)(long) xlator; + fd->_ctx[set_idx].value = value; + +out: + return ret; +} + + +int +fd_ctx_set (fd_t *fd, xlator_t *xlator, uint64_t value) +{ + int ret = 0; - fd->_ctx[set_idx].key = (uint64_t)(long) xlator; - fd->_ctx[set_idx].value = value; + if (!fd || !xlator) + return -1; + + LOCK (&fd->lock); + { + ret = __fd_ctx_set (fd, xlator, value); } -unlock: UNLOCK (&fd->lock); - return ret; + return ret; } + int -fd_ctx_get (fd_t *fd, xlator_t *xlator, uint64_t *value) +__fd_ctx_get (fd_t *fd, xlator_t *xlator, uint64_t *value) { int index = 0; int ret = 0; @@ -633,24 +648,68 @@ fd_ctx_get (fd_t *fd, xlator_t *xlator, uint64_t *value) if (!fd || !xlator) return -1; + for (index = 0; index < xlator->ctx->xl_count; index++) { + if (fd->_ctx[index].key == (uint64_t)(long)xlator) + break; + } + + if (index == xlator->ctx->xl_count) { + ret = -1; + goto out; + } + + if (value) + *value = fd->_ctx[index].value; + +out: + return ret; +} + + +int +fd_ctx_get (fd_t *fd, xlator_t *xlator, uint64_t *value) +{ + int ret = 0; + + if (!fd || !xlator) + return -1; + LOCK (&fd->lock); { - for (index = 0; index < xlator->ctx->xl_count; index++) { - if (fd->_ctx[index].key == (uint64_t)(long)xlator) - break; - } + ret = __fd_ctx_get (fd, xlator, value); + } + UNLOCK (&fd->lock); - if (index == xlator->ctx->xl_count) { - ret = -1; - goto unlock; - } + return ret; +} + + +int +__fd_ctx_del (fd_t *fd, xlator_t *xlator, uint64_t *value) +{ + int index = 0; + int ret = 0; - if (value) - *value = fd->_ctx[index].value; + if (!fd || !xlator) + return -1; + + for (index = 0; index < xlator->ctx->xl_count; index++) { + if (fd->_ctx[index].key == (uint64_t)(long)xlator) + break; } -unlock: - UNLOCK (&fd->lock); + + if (index == xlator->ctx->xl_count) { + ret = -1; + goto out; + } + + if (value) + *value = fd->_ctx[index].value; + + fd->_ctx[index].key = 0; + fd->_ctx[index].value = 0; +out: return ret; } @@ -658,7 +717,6 @@ unlock: int fd_ctx_del (fd_t *fd, xlator_t *xlator, uint64_t *value) { - int index = 0; int ret = 0; if (!fd || !xlator) @@ -666,24 +724,9 @@ fd_ctx_del (fd_t *fd, xlator_t *xlator, uint64_t *value) LOCK (&fd->lock); { - for (index = 0; index < xlator->ctx->xl_count; index++) { - if (fd->_ctx[index].key == (uint64_t)(long)xlator) - break; - } - - if (index == xlator->ctx->xl_count) { - ret = -1; - goto unlock; - } - - if (value) - *value = fd->_ctx[index].value; - - fd->_ctx[index].key = 0; - fd->_ctx[index].value = 0; + ret = __fd_ctx_del (fd, xlator, value); } -unlock: UNLOCK (&fd->lock); - return ret; + return ret; } -- cgit