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

Method checkProcessMode

Core/MMKV_Android.cpp:322–375  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

320#ifndef MMKV_OHOS
321
322bool MMKV::checkProcessMode() {
323 // avoid exception on open() error
324 if (!m_file->isFileValid()) {
325 return true;
326 }
327 // avoid invalid status
328 if (!g_enableProcessModeCheck || !m_fileModeLock || !m_sharedProcessModeLock) {
329 return true;
330 }
331
332 if (isMultiProcess()) {
333 if (!m_exclusiveProcessModeLock) {
334 m_exclusiveProcessModeLock = new InterProcessLock(m_fileModeLock, ExclusiveLockType);
335 }
336 // avoid multiple processes get shared lock at the same time, https://github.com/Tencent/MMKV/issues/523
337 auto tryAgain = false;
338 auto exclusiveLocked = m_exclusiveProcessModeLock->try_lock(&tryAgain);
339 if (exclusiveLocked) {
340 return true;
341 }
342 auto shareLocked = m_sharedProcessModeLock->try_lock();
343 if (!shareLocked) {
344 // this call will fail on most case, just do it to make sure
345 m_exclusiveProcessModeLock->try_lock();
346 return true;
347 } else {
348 if (!tryAgain) {
349 // something wrong with the OS/filesystem, let's try again
350 exclusiveLocked = m_exclusiveProcessModeLock->try_lock(&tryAgain);
351 if (!exclusiveLocked && !tryAgain) {
352 // still something wrong, we have to give up and assume it passed the test
353 MMKVWarning("Got a shared lock, but fail to exclusive lock [%s], assume it's ok", m_mmapID.c_str());
354 exclusiveLocked = true;
355 }
356 }
357 if (!exclusiveLocked) {
358 MMKVError("Got a shared lock, but fail to exclusive lock [%s]", m_mmapID.c_str());
359 }
360 return exclusiveLocked;
361 }
362 } else {
363 auto tryAgain = false;
364 auto shareLocked = m_sharedProcessModeLock->try_lock(&tryAgain);
365 if (!shareLocked && !tryAgain) {
366 // something wrong with the OS/filesystem, we have to give up and assume it passed the test
367 MMKVWarning("Fail to shared lock [%s], assume it's ok", m_mmapID.c_str());
368 shareLocked = true;
369 }
370 if (!shareLocked) {
371 MMKVError("Fail to share lock [%s]", m_mmapID.c_str());
372 }
373 return shareLocked;
374 }
375}
376
377void MMKV::enableDisableProcessMode(bool enable) {
378 MMKVInfo("process mode check enable/disable: %d", enable);

Callers

nothing calls this directly

Calls 3

isMultiProcessFunction · 0.70
isFileValidMethod · 0.45
try_lockMethod · 0.45

Tested by

no test coverage detected