MCPcopy Create free account
hub / github.com/PolyMC/PolyMC / lock

Method lock

libraries/LocalPeer/src/LockedFile_win.cpp:98–169  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

96
97
98bool LockedFile::lock(LockMode mode, bool block)
99{
100 if (!isOpen()) {
101 qWarning("QtLockedFile::lock(): file is not opened");
102 return false;
103 }
104
105 if (mode == NoLock)
106 return unlock();
107
108 if (mode == m_lock_mode)
109 return true;
110
111 if (m_lock_mode != NoLock)
112 unlock();
113
114 if (!wmutex && !(wmutex = getMutexHandle(-1, true)))
115 return false;
116
117 if (!waitMutex(wmutex, block))
118 return false;
119
120 if (mode == ReadLock) {
121 int idx = 0;
122 for (; idx < MAX_READERS; idx++) {
123 rmutex = getMutexHandle(idx, false);
124 if (!rmutex || waitMutex(rmutex, false))
125 break;
126 CloseHandle(rmutex);
127 }
128 bool ok = true;
129 if (idx >= MAX_READERS) {
130 qWarning("QtLockedFile::lock(): too many readers");
131 rmutex = 0;
132 ok = false;
133 }
134 else if (!rmutex) {
135 rmutex = getMutexHandle(idx, true);
136 if (!rmutex || !waitMutex(rmutex, false))
137 ok = false;
138 }
139 if (!ok && rmutex) {
140 CloseHandle(rmutex);
141 rmutex = 0;
142 }
143 ReleaseMutex(wmutex);
144 if (!ok)
145 return false;
146 }
147 else {
148 Q_ASSERT(rmutexes.isEmpty());
149 for (int i = 0; i < MAX_READERS; i++) {
150 Qt::HANDLE mutex = getMutexHandle(i, false);
151 if (mutex)
152 rmutexes.append(mutex);
153 }
154 if (rmutexes.size()) {
155 DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(),

Callers 1

isClientMethod · 0.45

Calls 3

isEmptyMethod · 0.80
appendMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected