From efe3a557197770ca61da9cd9bd5609eb6e7e6a08 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Wed, 15 Mar 2017 18:39:28 -0700 Subject: [PATCH] better linux platform impl --- command_line.cc | 3 +- platform_linux.cc | 73 ++++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/command_line.cc b/command_line.cc index c5f475c3..888ce755 100644 --- a/command_line.cc +++ b/command_line.cc @@ -393,7 +393,8 @@ void IndexMainLoop(IpcClient* client) { std::cerr << "Parsing/indexing took " << time.ElapsedMilliseconds() << "ms" << std::endl; time.Reset(); - auto response = IpcMessage_IndexTranslationUnitResponse(IndexUpdate(file)); + IndexUpdate update(file); + auto response = IpcMessage_IndexTranslationUnitResponse(update); std::cerr << "Creating index update took " << time.ElapsedMilliseconds() << "ms" << std::endl; time.Reset(); diff --git a/platform_linux.cc b/platform_linux.cc index d729acab..7fe08969 100644 --- a/platform_linux.cc +++ b/platform_linux.cc @@ -29,7 +29,7 @@ struct PlatformMutexLinux : public PlatformMutex { sem_t* sem_ = nullptr; PlatformMutexLinux(const std::string& name) { - std::cout << "PlatformMutexLinux name=" << name << std::endl; + std::cerr << "PlatformMutexLinux name=" << name << std::endl; sem_ = sem_open(name.c_str(), O_CREAT, 0666 /*permission*/, 1 /*initial_value*/); } @@ -42,7 +42,6 @@ struct PlatformScopedMutexLockLinux : public PlatformScopedMutexLock { sem_t* sem_ = nullptr; PlatformScopedMutexLockLinux(sem_t* sem) : sem_(sem) { - std::cout << "PlatformScopedMutexLockLinux" << std::endl; sem_wait(sem_); } @@ -51,48 +50,64 @@ struct PlatformScopedMutexLockLinux : public PlatformScopedMutexLock { } }; +void* checked(void* result, const char* expr) { + if (!result) { + std::cerr << "FAIL errno=" << errno << " in |" << expr << "|" << std::endl; + exit(1); + } + return result; +} + +int checked(int result, const char* expr) { + if (result == -1) { + std::cerr << "FAIL errno=" << errno << " in |" << expr << "|" << std::endl; + exit(1); + } + return result; +} + +#define CHECKED(expr) checked(expr, #expr) + struct PlatformSharedMemoryLinux : public PlatformSharedMemory { std::string name_; int fd_; PlatformSharedMemoryLinux(const std::string& name) : name_(name) { - std::cout << "PlatformSharedMemoryLinux name=" << name << std::endl; - fd_ = shm_open(name_.c_str(), O_CREAT, O_RDWR); - std::cout << "shm_open errno=" << errno << std::endl; - std::cout << "1" << std::endl; - ftruncate(fd_, shmem_size); - std::cout << "ftruncate errno=" << errno << std::endl; - std::cout << "2" << std::endl; + std::cerr << "PlatformSharedMemoryLinux name=" << name << std::endl; - //void *mmap(void *addr, size_t length, int prot, int flags, - // int fd, off_t offset); - //int munmap(void *addr, size_t length); - // - //shmem_size - std::cout << "3" << std::endl; - shared = mmap(nullptr /*kernel assigned starting address*/, - shmem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0 /*offset*/); - std::cout << "mmap errno=" << errno << std::endl; - std::cout << "fd_ = " << fd_ << std::endl; - std::cout << "shared = " << shared << std::endl; - std::cout << "4" << std::endl; + // Try to create shared memory but only if it does not already exist. Since + // we created the memory, we need to initialize it. + fd_ = shm_open(name_.c_str(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); + if (fd_ >= 0) { + std::cerr << "Calling ftruncate fd_=" << fd_ << std::endl; + CHECKED(ftruncate(fd_, shmem_size)); + } - /* - int fd = shm_open("shmname", O_CREAT, O_RDWR); - sem_t *sem = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - ftruncate(fd_, shmem_size); + // Otherwise, we just open existing shared memory. We don't need to + // create or initialize it. + else { + fd_ = CHECKED(shm_open(name_.c_str(), + O_RDWR, /* memory is read/write, create if needed */ + S_IRUSR | S_IWUSR /* user read/write */)); + } - sem_init(sem, 1, 1); - */ + // Map the shared memory to an address. + shared = CHECKED(mmap(nullptr /*kernel assigned starting address*/, + shmem_size, PROT_READ | PROT_WRITE, MAP_SHARED, + fd_, 0 /*offset*/)); + + std::cout << "Open shared memory name=" << name << ", fd=" << fd_ << ", shared=" << shared << std::endl; } ~PlatformSharedMemoryLinux() override { - munmap(shared, shmem_size); + CHECKED(munmap(shared, shmem_size)); + CHECKED(shm_unlink(name_.c_str())); + shared = nullptr; - shm_unlink(name_.c_str()); } }; +#undef CHECKED std::unique_ptr CreatePlatformMutex(const std::string& name) {