| | |
| | | class NullMutex |
| | | { |
| | | public: |
| | | template <class... T> |
| | | explicit NullMutex(T &&...t) {} // easy test. |
| | | bool try_lock() { return true; } |
| | | void lock() {} |
| | | void unlock() {} |
| | |
| | | mkdir(dir.c_str(), 0777); |
| | | return dir + "/fm_" + std::to_string(id); |
| | | } |
| | | static int Open(id_t id) { return open(GetPath(id).c_str(), O_CREAT | O_RDWR, 0666); } |
| | | static int Open(id_t id) { return open(GetPath(id).c_str(), O_CREAT | O_RDONLY, 0666); } |
| | | static int Close(int fd) { return close(fd); } |
| | | void FLock(); |
| | | void FUnlock(); |
| | | id_t id_; |
| | | int fd_; |
| | | std::mutex mtx_; |
| | |
| | | { |
| | | public: |
| | | SemMutex(key_t key) : |
| | | key_(key), sem_id_(semget(key, 1, 0666 | IPC_CREAT)) |
| | | key_(key), sem_id_(semget(key, 1, 0666)) |
| | | { |
| | | if (sem_id_ == -1) { throw "error create semaphore."; } |
| | | union semun init_val; |
| | | init_val.val = 1; |
| | | semctl(sem_id_, 0, SETVAL, init_val); |
| | | } |
| | | ~SemMutex() |
| | | { |
| | | // semctl(sem_id_, 0, IPC_RMID, semun{}); |
| | | } |
| | | ~SemMutex() {} |
| | | |
| | | bool try_lock() |
| | | { |