MCPcopy Create free account
hub / github.com/Tencent/MMKV / testInterProcessLock

Function testInterProcessLock

POSIX/demo/demo.cpp:308–353  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

306}
307
308void testInterProcessLock() {
309 //auto mmkv = MMKV::mmkvWithID("TestInterProcessLock", MMKV_MULTI_PROCESS);
310 //mmkv->set(true, "bool");
311
312 auto fd = open("/tmp/mmkv/TestInterProcessLock.file", O_RDWR | O_CREAT | O_CLOEXEC, S_IRWXU);
313 FileLock flock(fd);
314
315// auto fd2 = open("/tmp/mmkv/TestInterProcessLock2.file", O_RDWR | O_CREAT | O_CLOEXEC, S_IRWXU);
316// FileLock flock2(fd2);
317// flock2.lock(SharedLockType);
318// auto ptr = ::mmap(nullptr, DEFAULT_MMAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd2, 0);
319// printf("mmap fd %d to %p\n", fd2, ptr);
320// ::close(fd2);
321// flock2.destroyLock();
322
323 auto pid = fork();
324 // this is child
325 if (pid <= 0) {
326 execl("TestInterProcessLock", "TestInterProcessLock", nullptr);
327 perror("execl"); // execl doesn't return unless there is a problem
328 exit(1);
329 }
330 printf("Waiting for child %d to start ...\n", pid);
331 sem_t *sem = sem_open("/mmkv_main", O_CREAT, 0644, 0);
332 if (!sem) {
333 printf("fail to create semaphore: %d(%s)\n", errno, strerror(errno));
334 exit(1);
335 }
336 sem_wait(sem);
337 printf("Child %d to started\n", pid);
338
339 //mmkv->clearAll();
340 //mmkv->lock();
341 flock.lock(ExclusiveLockType);
342
343 sem_post(sem);
344 sem_close(sem);
345
346 printf("Waiting for child %d to finish...\n", pid);
347 waitpid(pid, nullptr, 0);
348 printf("Child %d to finished\n", pid);
349
350 //mmkv->unlock();
351 flock.unlock(ExclusiveLockType);
352 close(fd);
353}
354
355void cornetSizeTest() {
356 string aesKey = "aes";

Callers 1

mainFunction · 0.85

Calls 3

closeFunction · 0.85
lockMethod · 0.45
unlockMethod · 0.45

Tested by

no test coverage detected