mirror of
				https://github.com/MaskRay/ccls.git
				synced 2025-11-03 22:04:24 +00:00 
			
		
		
		
	better linux platform impl
This commit is contained in:
		
							parent
							
								
									ec8bf8a113
								
							
						
					
					
						commit
						efe3a55719
					
				@ -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();
 | 
			
		||||
 | 
			
		||||
@ -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<PlatformMutex> CreatePlatformMutex(const std::string& name) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user