| 80 | } |
| 81 | |
| 82 | comm::RetCode SyncCtrl::Init() { |
| 83 | auto opt(impl_->store->GetStoreOption()); |
| 84 | |
| 85 | impl_->buf_size = opt->nsub * opt->nqueue * sizeof (SyncCtrlItem_t); |
| 86 | |
| 87 | auto sync_path(opt->data_dir_path + "/sync"); |
| 88 | int fd = open(sync_path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); |
| 89 | if (fd < 0) { |
| 90 | QLErr("open err %s path %s", strerror(errno), sync_path.c_str()); |
| 91 | return comm::RetCode::RET_ERR_SYS; |
| 92 | } |
| 93 | |
| 94 | if (ftruncate(fd, (off_t)impl_->buf_size) < 0) { |
| 95 | QLErr("ftruncate err %s", strerror(errno)); |
| 96 | close(fd); |
| 97 | return comm::RetCode::RET_ERR_SYS; |
| 98 | } |
| 99 | |
| 100 | void *pa{mmap(nullptr, impl_->buf_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)}; |
| 101 | |
| 102 | close(fd); |
| 103 | |
| 104 | if ((char *)pa == MAP_FAILED) { |
| 105 | QLErr("mmap err %s", strerror(errno)); |
| 106 | return comm::RetCode::RET_ERR_SYS; |
| 107 | } |
| 108 | |
| 109 | impl_->buf = (SyncCtrlItem_t*)pa; |
| 110 | |
| 111 | impl_->locks.reset(new mutex[opt->nsub * opt->nqueue]); |
| 112 | |
| 113 | return comm::RetCode::RET_OK;; |
| 114 | } |
| 115 | |
| 116 | static inline void GetIdx(const int nsub, const int sub_id, const int queue_id, size_t &idx) { |
| 117 | idx = queue_id * nsub + sub_id - 1; |
nothing calls this directly
no test coverage detected