From aefa08843fad6db6cf8efc8bbc7898ed3945caa6 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Mon, 5 Oct 2015 17:39:16 -0700 Subject: [PATCH] rgw: add rwlock to RGWObjectCtx It can be used from multiple threads now. Signed-off-by: Yehuda Sadeh --- src/rgw/rgw_rados.cc | 30 ++++++++++++++++++++++++++++-- src/rgw/rgw_rados.h | 5 +++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/rgw/rgw_rados.cc b/src/rgw/rgw_rados.cc index 4f277953cfc3d..3ea5896ef9286 100644 --- a/src/rgw/rgw_rados.cc +++ b/src/rgw/rgw_rados.cc @@ -1919,20 +1919,45 @@ public: }; RGWObjState *RGWObjectCtx::get_state(rgw_obj& obj) { + RGWObjState *result; + map::iterator iter; + lock.get_read(); if (!obj.get_object().empty()) { - return &objs_state[obj]; + iter = objs_state.find(obj); + if (iter != objs_state.end()) { + result = &iter->second; + lock.unlock(); + } else { + lock.unlock(); + lock.get_write(); + result = &objs_state[obj]; + lock.unlock(); + } + return result; } else { rgw_obj new_obj(store->zone.domain_root, obj.bucket.name); - return &objs_state[new_obj]; + iter = objs_state.find(new_obj); + if (iter != objs_state.end()) { + result = &iter->second; + lock.unlock(); + } else { + lock.unlock(); + lock.get_write(); + result = &objs_state[new_obj]; + lock.unlock(); + } + return result; } } void RGWObjectCtx::invalidate(rgw_obj& obj) { + RWLock::WLocker wl(lock); objs_state.erase(obj); } void RGWObjectCtx::set_atomic(rgw_obj& obj) { + RWLock::WLocker wl(lock); if (!obj.get_object().empty()) { objs_state[obj].is_atomic = true; } else { @@ -1942,6 +1967,7 @@ void RGWObjectCtx::set_atomic(rgw_obj& obj) { } void RGWObjectCtx::set_prefetch_data(rgw_obj& obj) { + RWLock::WLocker wl(lock); if (!obj.get_object().empty()) { objs_state[obj].prefetch_data = true; } else { diff --git a/src/rgw/rgw_rados.h b/src/rgw/rgw_rados.h index c65cf8030c94a..217e2398cde4d 100644 --- a/src/rgw/rgw_rados.h +++ b/src/rgw/rgw_rados.h @@ -1508,10 +1508,11 @@ public: struct RGWObjectCtx { RGWRados *store; map objs_state; + RWLock lock; void *user_ctx; - explicit RGWObjectCtx(RGWRados *_store) : store(_store), user_ctx(NULL) { } - RGWObjectCtx(RGWRados *_store, void *_user_ctx) : store(_store), user_ctx(_user_ctx) { } + explicit RGWObjectCtx(RGWRados *_store) : store(_store), lock("RGWObjectCtx"), user_ctx(NULL) { } + RGWObjectCtx(RGWRados *_store, void *_user_ctx) : store(_store), lock("RGWObjectCtx"), user_ctx(_user_ctx) { } RGWObjState *get_state(rgw_obj& obj); void set_atomic(rgw_obj& obj); -- 2.39.5