| 81 | } |
| 82 | |
| 83 | comm::RetCode HeartBeatLock::Init(Consumer *consumer, const int shmkey, const string &lockpath, const int nproc) { |
| 84 | comm::RetCode ret; |
| 85 | |
| 86 | impl_->consumer = consumer; |
| 87 | impl_->nproc = nproc; |
| 88 | |
| 89 | const int topic_id = impl_->consumer->GetTopicID(); |
| 90 | |
| 91 | // init QueueInfo |
| 92 | { |
| 93 | void *shm_addr{nullptr}; |
| 94 | size_t shm_size = sizeof(QueueBuf_t) + sizeof(Queue_t) * nproc; |
| 95 | int shm_id = shmget(shmkey, shm_size, 0666); |
| 96 | if (shm_id < 0) { |
| 97 | /* create */ |
| 98 | shm_id = shmget(shmkey, shm_size, 0666 | IPC_CREAT); |
| 99 | |
| 100 | if (shm_id < 0) { |
| 101 | |
| 102 | /* remove old */ |
| 103 | |
| 104 | if ((shm_id = shmget(shmkey, 0, 0666)) < 0) { |
| 105 | QLErr("ERR: shmget ret %d %s", shm_id, strerror(errno)); |
| 106 | return comm::RetCode::RET_ERR_SYS; |
| 107 | } |
| 108 | if (shmctl(shm_id, IPC_RMID, NULL) < 0) { |
| 109 | QLErr("ERR: shmctl %s", strerror(errno)); |
| 110 | return comm::RetCode::RET_ERR_SYS; |
| 111 | } |
| 112 | |
| 113 | /* recreate */ |
| 114 | if ((shm_id = shmget(shmkey, shm_size, 0666 | IPC_CREAT)) < 0) { |
| 115 | QLErr("ERR: shmget ret %d %s", shm_id, strerror(errno)); |
| 116 | return comm::RetCode::RET_ERR_SYS; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | shm_addr = shmat(shm_id, nullptr, 0); |
| 121 | if (shm_addr == (void *)-1) { |
| 122 | QLErr("ERR: shmat %s", strerror(errno)); |
| 123 | return comm::RetCode::RET_ERR_SYS; |
| 124 | } |
| 125 | |
| 126 | memset(shm_addr, 0, shm_size); |
| 127 | } else { |
| 128 | /* exists */ |
| 129 | shm_addr = shmat(shm_id, NULL, 0); |
| 130 | if (shm_addr == (void *)-1) { |
| 131 | QLErr("ERR: shmat %s", strerror(errno)); |
| 132 | return comm::RetCode::RET_ERR_SYS; |
| 133 | } |
| 134 | } |
| 135 | impl_->buf = (QueueBuf_t *)shm_addr; |
| 136 | } |
| 137 | |
| 138 | // init multi-process rwlock |
| 139 | { |
| 140 | pthread_rwlockattr_t attr; |
no test coverage detected