2 Commits

4 changed files with 43 additions and 3 deletions
Split View
  1. +22
    -1
      db/vlog.h
  2. +14
    -1
      db/vlog_gc.h
  3. +6
    -0
      db/vlog_set.cpp
  4. +1
    -1
      test/db_test1.cc

+ 22
- 1
db/vlog.h View File

@ -68,8 +68,29 @@ struct vlog_info {
};
struct vlog_handler {
std::mutex vlog_handler_latch_;
size_t curr_access_thread_nums;
SharedLock vlog_latch_; // vlog上的并发情况soft_lockhard_lock
vlog_handler() {}
vlog_handler() : curr_access_thread_nums(0) {}
inline void incre_access_thread_nums() {
vlog_handler_latch_.lock();
curr_access_thread_nums ++;
vlog_handler_latch_.unlock();
}
inline void decre_access_thread_nums() {
vlog_handler_latch_.lock();
curr_access_thread_nums --;
vlog_handler_latch_.unlock();
}
inline bool non_access_thread() {
bool flag = false;
vlog_handler_latch_.lock();
if (!curr_access_thread_nums) {
flag = true;
}
vlog_handler_latch_.unlock();
return flag;
}
};

+ 14
- 1
db/vlog_gc.h View File

@ -8,6 +8,8 @@
#include <string>
#include <mutex>
#include <thread>
#include <chrono>
#include "../db/slotpage.h"
#include "../db/vlog.h"
#include "../db/gc_executor.h"
@ -22,7 +24,18 @@ friend class gc_executor;
public:
VlogGC(SlotPage *s, VlogSet *vs) : slot_page_(s), vlog_set(vs),max_thread_nums_(THREAD_NUM),
curr_thread_nums_(0), gc_num(0) {}
~VlogGC() {}
~VlogGC() {
while (true) {
curr_thread_nums_latch_.lock();
if (curr_thread_nums_) {
curr_thread_nums_latch_.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(2));
} else {
curr_thread_nums_latch_.unlock();
break;
}
}
}
void do_gc(size_t old_vlog_num, size_t new_vlog_num);

+ 6
- 0
db/vlog_set.cpp View File

@ -107,9 +107,11 @@ void VlogSet::get_value(const struct slot_content &sc, std::string *value) {
vhandler = get_vlog_handler(vinfo->vlog_num_for_gc);
}
vhandler->vlog_latch_.soft_lock();
vhandler->incre_access_thread_nums(); // FIXME: increase thread nums
mtx.unlock(); // for better performance
vinfo->vlog_info_latch_.unlock();
read_vlog_value(sc, value);
vhandler->decre_access_thread_nums(); // FIXME: decrease thread nums
vhandler->vlog_latch_.soft_unlock();
}
@ -154,9 +156,11 @@ void VlogSet::put_value(struct slot_content &sc, size_t slot_num, const leveldb:
}
vhandler->vlog_latch_.hard_lock();
vhandler->incre_access_thread_nums(); // FIXME: increase thread nums
mtx.unlock(); // for better performance
vinfo->vlog_info_latch_.unlock();
write_vlog_value(sc, slot_num, value);
vhandler->decre_access_thread_nums(); // FIXME: decrease thread nums
vhandler->vlog_latch_.hard_unlock();
}
@ -171,9 +175,11 @@ void VlogSet::del_value(const struct slot_content &sc) {
}
vhandler->vlog_latch_.hard_lock();
vhandler->decre_access_thread_nums(); // FIXME: increase thread nums
mtx.unlock(); // for better performance
vinfo->vlog_info_latch_.unlock();
mark_del_value(sc);
vhandler->decre_access_thread_nums(); // FIXME: decrease thread nums
vhandler->vlog_latch_.hard_unlock();
}

+ 1
- 1
test/db_test1.cc View File

@ -37,7 +37,7 @@ int test2() {
assert(status.ok());
const std::string value_prefix = "value_";
const size_t loop_times = 1000;
const size_t loop_times = 100000;
for (auto i = 0; i < loop_times; i++) {
auto key = std::to_string(i);
auto value = value_prefix + key;

Loading…
Cancel
Save