| 62 | DUChainLock::~DUChainLock() = default; |
| 63 | |
| 64 | bool DUChainLock::lockForRead(unsigned int timeout) |
| 65 | { |
| 66 | Q_D(DUChainLock); |
| 67 | |
| 68 | ///Step 1: Increase the own reader-recursion. This will make sure no further write-locks will succeed |
| 69 | d->changeOwnReaderRecursion(1); |
| 70 | |
| 71 | QThread* w = d->m_writer.loadAcquire(); |
| 72 | if (w == nullptr || w == QThread::currentThread()) { |
| 73 | //Successful lock: Either there is no writer, or we hold the write-lock by ourselves |
| 74 | } else { |
| 75 | ///Step 2: Start spinning until there is no writer any more |
| 76 | |
| 77 | QElapsedTimer t; |
| 78 | if (timeout) { |
| 79 | t.start(); |
| 80 | } |
| 81 | |
| 82 | while (d->m_writer.loadAcquire()) { |
| 83 | if (!timeout || t.elapsed() < timeout) { |
| 84 | QThread::usleep(uSleepTime); |
| 85 | } else { |
| 86 | //Fail! |
| 87 | d->changeOwnReaderRecursion(-1); |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | void DUChainLock::releaseReadLock() |
| 97 | { |
no test coverage detected