[1] | 1 | /* $Id: semrw-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Read-Write Semaphore, Generic.
|
---|
[1] | 4 | *
|
---|
| 5 | * This is a generic implementation for OSes which don't have
|
---|
| 6 | * native RW semaphores.
|
---|
| 7 | */
|
---|
| 8 |
|
---|
| 9 | /*
|
---|
[98103] | 10 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 11 | *
|
---|
[96407] | 12 | * This file is part of VirtualBox base platform packages, as
|
---|
| 13 | * available from https://www.virtualbox.org.
|
---|
[5999] | 14 | *
|
---|
[96407] | 15 | * This program is free software; you can redistribute it and/or
|
---|
| 16 | * modify it under the terms of the GNU General Public License
|
---|
| 17 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 18 | * License.
|
---|
| 19 | *
|
---|
| 20 | * This program is distributed in the hope that it will be useful, but
|
---|
| 21 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 23 | * General Public License for more details.
|
---|
| 24 | *
|
---|
| 25 | * You should have received a copy of the GNU General Public License
|
---|
| 26 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 27 | *
|
---|
[5999] | 28 | * The contents of this file may alternatively be used under the terms
|
---|
| 29 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 30 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 31 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 32 | * CDDL are applicable instead of those of the GPL.
|
---|
| 33 | *
|
---|
| 34 | * You may elect to license modified versions of this file under the
|
---|
| 35 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 36 | *
|
---|
| 37 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 38 | */
|
---|
| 39 |
|
---|
| 40 |
|
---|
[57358] | 41 | /*********************************************************************************************************************************
|
---|
| 42 | * Header Files *
|
---|
| 43 | *********************************************************************************************************************************/
|
---|
[36190] | 44 | #define RTSEMRW_WITHOUT_REMAPPING
|
---|
[1] | 45 | #include <iprt/semaphore.h>
|
---|
[21337] | 46 | #include "internal/iprt.h"
|
---|
| 47 |
|
---|
[25612] | 48 | #include <iprt/asm.h>
|
---|
| 49 | #include <iprt/assert.h>
|
---|
[20601] | 50 | #include <iprt/critsect.h>
|
---|
[25612] | 51 | #include <iprt/err.h>
|
---|
| 52 | #include <iprt/lockvalidator.h>
|
---|
| 53 | #include <iprt/mem.h>
|
---|
[1] | 54 | #include <iprt/time.h>
|
---|
| 55 | #include <iprt/thread.h>
|
---|
| 56 |
|
---|
[20601] | 57 | #include "internal/magics.h"
|
---|
[25612] | 58 | #include "internal/strict.h"
|
---|
[1] | 59 |
|
---|
| 60 |
|
---|
[57358] | 61 | /*********************************************************************************************************************************
|
---|
| 62 | * Structures and Typedefs *
|
---|
| 63 | *********************************************************************************************************************************/
|
---|
[20601] | 64 |
|
---|
[1] | 65 | /** Internal representation of a Read-Write semaphore for the
|
---|
| 66 | * Generic implementation. */
|
---|
| 67 | struct RTSEMRWINTERNAL
|
---|
| 68 | {
|
---|
[20601] | 69 | /** The usual magic. (RTSEMRW_MAGIC) */
|
---|
| 70 | uint32_t u32Magic;
|
---|
| 71 | /* Alignment padding. */
|
---|
| 72 | uint32_t u32Padding;
|
---|
[1] | 73 | /** This critical section serializes the access to and updating of the structure members. */
|
---|
[20601] | 74 | RTCRITSECT CritSect;
|
---|
| 75 | /** The current number of reads. (pure read recursion counts too) */
|
---|
| 76 | uint32_t cReads;
|
---|
| 77 | /** The current number of writes. (recursion counts too) */
|
---|
| 78 | uint32_t cWrites;
|
---|
| 79 | /** Number of read recursions by the writer. */
|
---|
| 80 | uint32_t cWriterReads;
|
---|
| 81 | /** Number of writers waiting. */
|
---|
| 82 | uint32_t cWritesWaiting;
|
---|
| 83 | /** The write owner of the lock. */
|
---|
[25522] | 84 | RTNATIVETHREAD hWriter;
|
---|
[1] | 85 | /** The handle of the event object on which the waiting readers block. (manual reset). */
|
---|
[20601] | 86 | RTSEMEVENTMULTI ReadEvent;
|
---|
| 87 | /** The handle of the event object on which the waiting writers block. (automatic reset). */
|
---|
| 88 | RTSEMEVENT WriteEvent;
|
---|
[25513] | 89 | /** Need to reset ReadEvent. */
|
---|
| 90 | bool fNeedResetReadEvent;
|
---|
[25612] | 91 | #ifdef RTSEMRW_STRICT
|
---|
| 92 | /** The validator record for the writer. */
|
---|
| 93 | RTLOCKVALRECEXCL ValidatorWrite;
|
---|
| 94 | /** The validator record for the readers. */
|
---|
| 95 | RTLOCKVALRECSHRD ValidatorRead;
|
---|
| 96 | #endif
|
---|
[1] | 97 | };
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 |
|
---|
[25707] | 101 | RTDECL(int) RTSemRWCreate(PRTSEMRW phRWSem)
|
---|
| 102 | {
|
---|
| 103 | return RTSemRWCreateEx(phRWSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, "RTSemRW");
|
---|
| 104 | }
|
---|
| 105 | RT_EXPORT_SYMBOL(RTSemRWCreate);
|
---|
[25620] | 106 |
|
---|
[25707] | 107 |
|
---|
| 108 | RTDECL(int) RTSemRWCreateEx(PRTSEMRW phRWSem, uint32_t fFlags,
|
---|
| 109 | RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
|
---|
[1] | 110 | {
|
---|
[25707] | 111 | AssertReturn(!(fFlags & ~RTSEMRW_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
[1] | 112 |
|
---|
| 113 | /*
|
---|
| 114 | * Allocate memory.
|
---|
| 115 | */
|
---|
[25707] | 116 | int rc;
|
---|
[20601] | 117 | struct RTSEMRWINTERNAL *pThis = (struct RTSEMRWINTERNAL *)RTMemAlloc(sizeof(struct RTSEMRWINTERNAL));
|
---|
| 118 | if (pThis)
|
---|
[1] | 119 | {
|
---|
| 120 | /*
|
---|
| 121 | * Create the semaphores.
|
---|
| 122 | */
|
---|
[25748] | 123 | rc = RTSemEventCreateEx(&pThis->WriteEvent, RTSEMEVENT_FLAGS_NO_LOCK_VAL, NIL_RTLOCKVALCLASS, NULL);
|
---|
[1] | 124 | if (RT_SUCCESS(rc))
|
---|
| 125 | {
|
---|
[25748] | 126 | rc = RTSemEventMultiCreateEx(&pThis->ReadEvent, RTSEMEVENT_FLAGS_NO_LOCK_VAL, NIL_RTLOCKVALCLASS, NULL);
|
---|
[1] | 127 | if (RT_SUCCESS(rc))
|
---|
| 128 | {
|
---|
[25711] | 129 | rc = RTCritSectInitEx(&pThis->CritSect, RTCRITSECT_FLAGS_NO_LOCK_VAL,
|
---|
| 130 | NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
[1] | 131 | if (RT_SUCCESS(rc))
|
---|
| 132 | {
|
---|
| 133 | /*
|
---|
| 134 | * Signal the read semaphore and initialize other variables.
|
---|
| 135 | */
|
---|
[20601] | 136 | rc = RTSemEventMultiSignal(pThis->ReadEvent);
|
---|
[1] | 137 | if (RT_SUCCESS(rc))
|
---|
| 138 | {
|
---|
[25513] | 139 | pThis->u32Padding = UINT32_C(0xa5a55a5a);
|
---|
| 140 | pThis->cReads = 0;
|
---|
| 141 | pThis->cWrites = 0;
|
---|
| 142 | pThis->cWriterReads = 0;
|
---|
| 143 | pThis->cWritesWaiting = 0;
|
---|
[25522] | 144 | pThis->hWriter = NIL_RTNATIVETHREAD;
|
---|
[25513] | 145 | pThis->fNeedResetReadEvent = true;
|
---|
| 146 | pThis->u32Magic = RTSEMRW_MAGIC;
|
---|
[25612] | 147 | #ifdef RTSEMRW_STRICT
|
---|
[25707] | 148 | bool const fLVEnabled = !(fFlags & RTSEMRW_FLAGS_NO_LOCK_VAL);
|
---|
[25831] | 149 | if (!pszNameFmt)
|
---|
| 150 | {
|
---|
| 151 | static uint32_t volatile s_iSemRWAnon = 0;
|
---|
| 152 | uint32_t i = ASMAtomicIncU32(&s_iSemRWAnon) - 1;
|
---|
| 153 | RTLockValidatorRecExclInit(&pThis->ValidatorWrite, hClass, uSubClass, pThis,
|
---|
| 154 | fLVEnabled, "RTSemRW-%u", i);
|
---|
| 155 | RTLockValidatorRecSharedInit(&pThis->ValidatorRead, hClass, uSubClass, pThis,
|
---|
| 156 | false /*fSignaller*/, fLVEnabled, "RTSemRW-%u", i);
|
---|
| 157 | }
|
---|
| 158 | else
|
---|
| 159 | {
|
---|
| 160 | va_list va;
|
---|
| 161 | va_start(va, pszNameFmt);
|
---|
| 162 | RTLockValidatorRecExclInitV(&pThis->ValidatorWrite, hClass, uSubClass, pThis,
|
---|
| 163 | fLVEnabled, pszNameFmt, va);
|
---|
| 164 | va_end(va);
|
---|
| 165 | va_start(va, pszNameFmt);
|
---|
| 166 | RTLockValidatorRecSharedInitV(&pThis->ValidatorRead, hClass, uSubClass, pThis,
|
---|
| 167 | false /*fSignaller*/, fLVEnabled, pszNameFmt, va);
|
---|
| 168 | va_end(va);
|
---|
| 169 | }
|
---|
[25612] | 170 | RTLockValidatorRecMakeSiblings(&pThis->ValidatorWrite.Core, &pThis->ValidatorRead.Core);
|
---|
[62592] | 171 | #else
|
---|
| 172 | RT_NOREF_PV(hClass); RT_NOREF_PV(uSubClass); RT_NOREF_PV(pszNameFmt);
|
---|
[25612] | 173 | #endif
|
---|
[25707] | 174 | *phRWSem = pThis;
|
---|
[1] | 175 | return VINF_SUCCESS;
|
---|
| 176 | }
|
---|
[20601] | 177 | RTCritSectDelete(&pThis->CritSect);
|
---|
[1] | 178 | }
|
---|
[20601] | 179 | RTSemEventMultiDestroy(pThis->ReadEvent);
|
---|
[1] | 180 | }
|
---|
[20601] | 181 | RTSemEventDestroy(pThis->WriteEvent);
|
---|
[1] | 182 | }
|
---|
[20601] | 183 | RTMemFree(pThis);
|
---|
[1] | 184 | }
|
---|
| 185 | else
|
---|
| 186 | rc = VERR_NO_MEMORY;
|
---|
| 187 |
|
---|
| 188 | return rc;
|
---|
| 189 | }
|
---|
[21337] | 190 | RT_EXPORT_SYMBOL(RTSemRWCreate);
|
---|
[1] | 191 |
|
---|
| 192 |
|
---|
[25723] | 193 | RTDECL(int) RTSemRWDestroy(RTSEMRW hRWSem)
|
---|
[1] | 194 | {
|
---|
[25723] | 195 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
[25513] | 196 |
|
---|
[1] | 197 | /*
|
---|
| 198 | * Validate handle.
|
---|
| 199 | */
|
---|
[25513] | 200 | if (pThis == NIL_RTSEMRW)
|
---|
| 201 | return VINF_SUCCESS;
|
---|
| 202 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
| 203 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
|
---|
[1] | 204 |
|
---|
| 205 | /*
|
---|
| 206 | * Check if busy.
|
---|
| 207 | */
|
---|
[20601] | 208 | int rc = RTCritSectTryEnter(&pThis->CritSect);
|
---|
[1] | 209 | if (RT_SUCCESS(rc))
|
---|
| 210 | {
|
---|
[20601] | 211 | if (!pThis->cReads && !pThis->cWrites)
|
---|
[1] | 212 | {
|
---|
| 213 | /*
|
---|
| 214 | * Make it invalid and unusable.
|
---|
| 215 | */
|
---|
[25513] | 216 | ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMRW_MAGIC);
|
---|
[62448] | 217 | pThis->cReads = UINT32_MAX;
|
---|
[1] | 218 |
|
---|
| 219 | /*
|
---|
[20601] | 220 | * Do actual cleanup. None of these can now fail.
|
---|
[1] | 221 | */
|
---|
[20601] | 222 | rc = RTSemEventMultiDestroy(pThis->ReadEvent);
|
---|
[25513] | 223 | AssertMsgRC(rc, ("RTSemEventMultiDestroy failed! rc=%Rrc\n", rc));
|
---|
[20601] | 224 | pThis->ReadEvent = NIL_RTSEMEVENTMULTI;
|
---|
[1] | 225 |
|
---|
[20601] | 226 | rc = RTSemEventDestroy(pThis->WriteEvent);
|
---|
[25513] | 227 | AssertMsgRC(rc, ("RTSemEventDestroy failed! rc=%Rrc\n", rc));
|
---|
[20601] | 228 | pThis->WriteEvent = NIL_RTSEMEVENT;
|
---|
[1] | 229 |
|
---|
[20601] | 230 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 231 | rc = RTCritSectDelete(&pThis->CritSect);
|
---|
[25513] | 232 | AssertMsgRC(rc, ("RTCritSectDelete failed! rc=%Rrc\n", rc));
|
---|
[1] | 233 |
|
---|
[25612] | 234 | #ifdef RTSEMRW_STRICT
|
---|
| 235 | RTLockValidatorRecSharedDelete(&pThis->ValidatorRead);
|
---|
| 236 | RTLockValidatorRecExclDelete(&pThis->ValidatorWrite);
|
---|
| 237 | #endif
|
---|
[20601] | 238 | RTMemFree(pThis);
|
---|
[1] | 239 | rc = VINF_SUCCESS;
|
---|
| 240 | }
|
---|
| 241 | else
|
---|
| 242 | {
|
---|
| 243 | rc = VERR_SEM_BUSY;
|
---|
[20601] | 244 | RTCritSectLeave(&pThis->CritSect);
|
---|
[1] | 245 | }
|
---|
| 246 | }
|
---|
| 247 | else
|
---|
[20601] | 248 | {
|
---|
[25513] | 249 | AssertMsgRC(rc, ("RTCritSectTryEnter failed! rc=%Rrc\n", rc));
|
---|
[20601] | 250 | rc = VERR_SEM_BUSY;
|
---|
| 251 | }
|
---|
[1] | 252 |
|
---|
[20601] | 253 | return rc;
|
---|
[1] | 254 | }
|
---|
[21337] | 255 | RT_EXPORT_SYMBOL(RTSemRWDestroy);
|
---|
[1] | 256 |
|
---|
| 257 |
|
---|
[25707] | 258 | RTDECL(uint32_t) RTSemRWSetSubClass(RTSEMRW hRWSem, uint32_t uSubClass)
|
---|
| 259 | {
|
---|
| 260 | #ifdef RTSEMRW_STRICT
|
---|
| 261 | /*
|
---|
| 262 | * Validate handle.
|
---|
| 263 | */
|
---|
| 264 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
| 265 | AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
|
---|
| 266 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
|
---|
| 267 |
|
---|
| 268 | RTLockValidatorRecSharedSetSubClass(&pThis->ValidatorRead, uSubClass);
|
---|
| 269 | return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorWrite, uSubClass);
|
---|
| 270 | #else
|
---|
[62592] | 271 | RT_NOREF_PV(hRWSem); RT_NOREF_PV(uSubClass);
|
---|
[25707] | 272 | return RTLOCKVAL_SUB_CLASS_INVALID;
|
---|
| 273 | #endif
|
---|
| 274 | }
|
---|
| 275 | RT_EXPORT_SYMBOL(RTSemRWSetSubClass);
|
---|
| 276 |
|
---|
| 277 |
|
---|
[25723] | 278 | DECL_FORCE_INLINE(int) rtSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies, bool fInterruptible, PCRTLOCKVALSRCPOS pSrcPos)
|
---|
[1] | 279 | {
|
---|
| 280 | /*
|
---|
| 281 | * Validate handle.
|
---|
| 282 | */
|
---|
[25723] | 283 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
[25513] | 284 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
| 285 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
|
---|
[1] | 286 |
|
---|
[25724] | 287 | RTMSINTERVAL cMilliesInitial = cMillies;
|
---|
[25522] | 288 | uint64_t tsStart = 0;
|
---|
[25515] | 289 | if (cMillies != RT_INDEFINITE_WAIT && cMillies != 0)
|
---|
[1] | 290 | tsStart = RTTimeNanoTS();
|
---|
| 291 |
|
---|
[25614] | 292 | #ifdef RTSEMRW_STRICT
|
---|
| 293 | RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
|
---|
| 294 | if (cMillies > 0)
|
---|
| 295 | {
|
---|
[25710] | 296 | int rc9;
|
---|
| 297 | if (pThis->hWriter != NIL_RTTHREAD && pThis->hWriter == RTThreadNativeSelf())
|
---|
| 298 | rc9 = RTLockValidatorRecExclCheckOrder(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, cMillies);
|
---|
| 299 | else
|
---|
| 300 | rc9 = RTLockValidatorRecSharedCheckOrder(&pThis->ValidatorRead, hThreadSelf, pSrcPos, cMillies);
|
---|
[25614] | 301 | if (RT_FAILURE(rc9))
|
---|
| 302 | return rc9;
|
---|
| 303 | }
|
---|
| 304 | #endif
|
---|
| 305 |
|
---|
[20601] | 306 | /*
|
---|
| 307 | * Take critsect.
|
---|
| 308 | */
|
---|
| 309 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
[1] | 310 | if (RT_FAILURE(rc))
|
---|
| 311 | {
|
---|
[25723] | 312 | AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
|
---|
[1] | 313 | return rc;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[25515] | 316 | /*
|
---|
| 317 | * Check if the state of affairs allows read access.
|
---|
| 318 | * Do not block further readers if there is a writer waiting, as
|
---|
| 319 | * that will break/deadlock reader recursion.
|
---|
| 320 | */
|
---|
[25522] | 321 | if ( pThis->hWriter == NIL_RTNATIVETHREAD
|
---|
| 322 | #if 0
|
---|
| 323 | && ( !pThis->cWritesWaiting
|
---|
| 324 | || pThis->cReads)
|
---|
| 325 | #endif
|
---|
[25515] | 326 | )
|
---|
[1] | 327 | {
|
---|
[25515] | 328 | pThis->cReads++;
|
---|
[25522] | 329 | Assert(pThis->cReads > 0);
|
---|
[25614] | 330 | #ifdef RTSEMRW_STRICT
|
---|
[25638] | 331 | RTLockValidatorRecSharedAddOwner(&pThis->ValidatorRead, hThreadSelf, pSrcPos);
|
---|
[25614] | 332 | #endif
|
---|
[1] | 333 |
|
---|
[25515] | 334 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 335 | return VINF_SUCCESS;
|
---|
| 336 | }
|
---|
[25513] | 337 |
|
---|
[25522] | 338 | RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
|
---|
| 339 | if (pThis->hWriter == hNativeSelf)
|
---|
[25515] | 340 | {
|
---|
[25614] | 341 | #ifdef RTSEMRW_STRICT
|
---|
| 342 | int rc9 = RTLockValidatorRecExclRecursionMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core, pSrcPos);
|
---|
| 343 | if (RT_FAILURE(rc9))
|
---|
| 344 | {
|
---|
| 345 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 346 | return rc9;
|
---|
| 347 | }
|
---|
| 348 | #endif
|
---|
| 349 |
|
---|
[25515] | 350 | pThis->cWriterReads++;
|
---|
[25522] | 351 | Assert(pThis->cWriterReads > 0);
|
---|
[1] | 352 |
|
---|
[20601] | 353 | RTCritSectLeave(&pThis->CritSect);
|
---|
[25515] | 354 | return VINF_SUCCESS;
|
---|
| 355 | }
|
---|
[20601] | 356 |
|
---|
[25515] | 357 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 358 |
|
---|
| 359 | /*
|
---|
| 360 | * Wait till it's ready for reading.
|
---|
| 361 | */
|
---|
| 362 | if (cMillies == 0)
|
---|
| 363 | return VERR_TIMEOUT;
|
---|
| 364 |
|
---|
[25614] | 365 | #ifndef RTSEMRW_STRICT
|
---|
| 366 | RTTHREAD hThreadSelf = RTThreadSelf();
|
---|
| 367 | #endif
|
---|
[25515] | 368 | for (;;)
|
---|
| 369 | {
|
---|
[20601] | 370 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
[1] | 371 | {
|
---|
[20601] | 372 | int64_t tsDelta = RTTimeNanoTS() - tsStart;
|
---|
[1] | 373 | if (tsDelta >= 1000000)
|
---|
| 374 | {
|
---|
[25515] | 375 | tsDelta /= 1000000;
|
---|
| 376 | if ((uint64_t)tsDelta < cMilliesInitial)
|
---|
[25724] | 377 | cMilliesInitial = (RTMSINTERVAL)tsDelta;
|
---|
[25515] | 378 | else
|
---|
| 379 | cMilliesInitial = 1;
|
---|
[1] | 380 | }
|
---|
| 381 | }
|
---|
[25614] | 382 | #ifdef RTSEMRW_STRICT
|
---|
[25638] | 383 | rc = RTLockValidatorRecSharedCheckBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true,
|
---|
[25685] | 384 | cMillies, RTTHREADSTATE_RW_READ, false);
|
---|
[25614] | 385 | if (RT_FAILURE(rc))
|
---|
| 386 | break;
|
---|
[25618] | 387 | #else
|
---|
[25638] | 388 | RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_READ, false);
|
---|
[62592] | 389 | RT_NOREF_PV(pSrcPos);
|
---|
[25614] | 390 | #endif
|
---|
[25620] | 391 | int rcWait;
|
---|
| 392 | if (fInterruptible)
|
---|
| 393 | rcWait = rc = RTSemEventMultiWaitNoResume(pThis->ReadEvent, cMillies);
|
---|
| 394 | else
|
---|
| 395 | rcWait = rc = RTSemEventMultiWait(pThis->ReadEvent, cMillies);
|
---|
[25614] | 396 | RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_READ);
|
---|
[25515] | 397 | if (RT_FAILURE(rc) && rc != VERR_TIMEOUT) /* handle timeout below */
|
---|
[1] | 398 | {
|
---|
[25723] | 399 | AssertMsgRC(rc, ("RTSemEventMultiWait failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
|
---|
[1] | 400 | break;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[20601] | 403 | if (pThis->u32Magic != RTSEMRW_MAGIC)
|
---|
| 404 | {
|
---|
| 405 | rc = VERR_SEM_DESTROYED;
|
---|
| 406 | break;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[1] | 409 | /*
|
---|
[33540] | 410 | * Re-take critsect and repeat the check we did before the loop.
|
---|
[1] | 411 | */
|
---|
[20601] | 412 | rc = RTCritSectEnter(&pThis->CritSect);
|
---|
[1] | 413 | if (RT_FAILURE(rc))
|
---|
| 414 | {
|
---|
[25723] | 415 | AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
|
---|
[1] | 416 | break;
|
---|
| 417 | }
|
---|
[25515] | 418 |
|
---|
[25522] | 419 | if ( pThis->hWriter == NIL_RTNATIVETHREAD
|
---|
| 420 | #if 0
|
---|
| 421 | && ( !pThis->cWritesWaiting
|
---|
| 422 | || pThis->cReads)
|
---|
| 423 | #endif
|
---|
[25515] | 424 | )
|
---|
| 425 | {
|
---|
| 426 | pThis->cReads++;
|
---|
[25614] | 427 | Assert(pThis->cReads > 0);
|
---|
| 428 | #ifdef RTSEMRW_STRICT
|
---|
[25638] | 429 | RTLockValidatorRecSharedAddOwner(&pThis->ValidatorRead, hThreadSelf, pSrcPos);
|
---|
[25614] | 430 | #endif
|
---|
[25515] | 431 |
|
---|
| 432 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 433 | return VINF_SUCCESS;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 437 |
|
---|
| 438 | /*
|
---|
| 439 | * Quit if the wait already timed out.
|
---|
| 440 | */
|
---|
| 441 | if (rcWait == VERR_TIMEOUT)
|
---|
| 442 | {
|
---|
| 443 | rc = VERR_TIMEOUT;
|
---|
| 444 | break;
|
---|
| 445 | }
|
---|
[1] | 446 | }
|
---|
| 447 |
|
---|
[25522] | 448 | /* failed */
|
---|
[1] | 449 | return rc;
|
---|
| 450 | }
|
---|
[25620] | 451 |
|
---|
| 452 |
|
---|
[25723] | 453 | RTDECL(int) RTSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
|
---|
[25620] | 454 | {
|
---|
| 455 | #ifndef RTSEMRW_STRICT
|
---|
[25723] | 456 | return rtSemRWRequestRead(hRWSem, cMillies, false, NULL);
|
---|
[25620] | 457 | #else
|
---|
| 458 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
[25723] | 459 | return rtSemRWRequestRead(hRWSem, cMillies, false, &SrcPos);
|
---|
[25620] | 460 | #endif
|
---|
| 461 | }
|
---|
[21337] | 462 | RT_EXPORT_SYMBOL(RTSemRWRequestRead);
|
---|
[1] | 463 |
|
---|
| 464 |
|
---|
[25723] | 465 | RTDECL(int) RTSemRWRequestReadDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
[25620] | 466 | {
|
---|
| 467 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
[25723] | 468 | return rtSemRWRequestRead(hRWSem, cMillies, false, &SrcPos);
|
---|
[25620] | 469 | }
|
---|
| 470 | RT_EXPORT_SYMBOL(RTSemRWRequestReadDebug);
|
---|
| 471 |
|
---|
| 472 |
|
---|
[25723] | 473 | RTDECL(int) RTSemRWRequestReadNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
|
---|
[1] | 474 | {
|
---|
[25620] | 475 | #ifndef RTSEMRW_STRICT
|
---|
[25723] | 476 | return rtSemRWRequestRead(hRWSem, cMillies, true, NULL);
|
---|
[25620] | 477 | #else
|
---|
| 478 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
[25723] | 479 | return rtSemRWRequestRead(hRWSem, cMillies, true, &SrcPos);
|
---|
[25620] | 480 | #endif
|
---|
[1] | 481 | }
|
---|
[21337] | 482 | RT_EXPORT_SYMBOL(RTSemRWRequestReadNoResume);
|
---|
[1] | 483 |
|
---|
| 484 |
|
---|
[25723] | 485 | RTDECL(int) RTSemRWRequestReadNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
[25620] | 486 | {
|
---|
| 487 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
[25723] | 488 | return rtSemRWRequestRead(hRWSem, cMillies, true, &SrcPos);
|
---|
[25620] | 489 | }
|
---|
| 490 | RT_EXPORT_SYMBOL(RTSemRWRequestReadNoResumeDebug);
|
---|
| 491 |
|
---|
| 492 |
|
---|
[25723] | 493 | RTDECL(int) RTSemRWReleaseRead(RTSEMRW hRWSem)
|
---|
[1] | 494 | {
|
---|
[25723] | 495 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
[25513] | 496 |
|
---|
[1] | 497 | /*
|
---|
| 498 | * Validate handle.
|
---|
| 499 | */
|
---|
[25513] | 500 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
| 501 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
|
---|
[1] | 502 |
|
---|
| 503 | /*
|
---|
[20601] | 504 | * Take critsect.
|
---|
[1] | 505 | */
|
---|
[20601] | 506 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
[1] | 507 | if (RT_SUCCESS(rc))
|
---|
| 508 | {
|
---|
[25522] | 509 | if (pThis->hWriter == NIL_RTNATIVETHREAD)
|
---|
[1] | 510 | {
|
---|
[25614] | 511 | #ifdef RTSEMRW_STRICT
|
---|
| 512 | rc = RTLockValidatorRecSharedCheckAndRelease(&pThis->ValidatorRead, NIL_RTTHREAD);
|
---|
| 513 | if (RT_SUCCESS(rc))
|
---|
| 514 | #endif
|
---|
[25522] | 515 | {
|
---|
[25614] | 516 | if (RT_LIKELY(pThis->cReads > 0))
|
---|
| 517 | {
|
---|
| 518 | pThis->cReads--;
|
---|
[25522] | 519 |
|
---|
[25614] | 520 | /* Kick off a writer if appropriate. */
|
---|
| 521 | if ( pThis->cWritesWaiting > 0
|
---|
| 522 | && !pThis->cReads)
|
---|
| 523 | {
|
---|
| 524 | rc = RTSemEventSignal(pThis->WriteEvent);
|
---|
[25723] | 525 | AssertMsgRC(rc, ("Failed to signal writers on rwsem %p, rc=%Rrc\n", hRWSem, rc));
|
---|
[25614] | 526 | }
|
---|
| 527 | }
|
---|
| 528 | else
|
---|
[25522] | 529 | {
|
---|
[25614] | 530 | AssertFailed();
|
---|
| 531 | rc = VERR_NOT_OWNER;
|
---|
[25522] | 532 | }
|
---|
| 533 | }
|
---|
[20601] | 534 | }
|
---|
| 535 | else
|
---|
| 536 | {
|
---|
[25522] | 537 | RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
|
---|
| 538 | if (pThis->hWriter == hNativeSelf)
|
---|
[1] | 539 | {
|
---|
[25522] | 540 | if (pThis->cWriterReads > 0)
|
---|
[25614] | 541 | {
|
---|
| 542 | #ifdef RTSEMRW_STRICT
|
---|
| 543 | rc = RTLockValidatorRecExclUnwindMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core);
|
---|
| 544 | if (RT_SUCCESS(rc))
|
---|
| 545 | #endif
|
---|
| 546 | {
|
---|
| 547 | pThis->cWriterReads--;
|
---|
| 548 | }
|
---|
| 549 | }
|
---|
[25522] | 550 | else
|
---|
| 551 | {
|
---|
| 552 | AssertFailed();
|
---|
| 553 | rc = VERR_NOT_OWNER;
|
---|
| 554 | }
|
---|
[1] | 555 | }
|
---|
[25522] | 556 | else
|
---|
| 557 | {
|
---|
| 558 | AssertFailed();
|
---|
| 559 | rc = VERR_NOT_OWNER;
|
---|
| 560 | }
|
---|
[1] | 561 | }
|
---|
| 562 |
|
---|
[20601] | 563 | RTCritSectLeave(&pThis->CritSect);
|
---|
[1] | 564 | }
|
---|
| 565 | else
|
---|
[25723] | 566 | AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
|
---|
[1] | 567 |
|
---|
| 568 | return rc;
|
---|
| 569 | }
|
---|
[21337] | 570 | RT_EXPORT_SYMBOL(RTSemRWReleaseRead);
|
---|
[1] | 571 |
|
---|
| 572 |
|
---|
[25723] | 573 | DECL_FORCE_INLINE(int) rtSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies, bool fInterruptible, PCRTLOCKVALSRCPOS pSrcPos)
|
---|
[1] | 574 | {
|
---|
| 575 | /*
|
---|
| 576 | * Validate handle.
|
---|
| 577 | */
|
---|
[25724] | 578 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
[25513] | 579 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
| 580 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
|
---|
[1] | 581 |
|
---|
[25724] | 582 | RTMSINTERVAL cMilliesInitial = cMillies;
|
---|
| 583 | uint64_t tsStart = 0;
|
---|
[25513] | 584 | if (cMillies != RT_INDEFINITE_WAIT && cMillies != 0)
|
---|
[1] | 585 | tsStart = RTTimeNanoTS();
|
---|
| 586 |
|
---|
[25614] | 587 | #ifdef RTSEMRW_STRICT
|
---|
| 588 | RTTHREAD hThreadSelf = NIL_RTTHREAD;
|
---|
| 589 | if (cMillies)
|
---|
| 590 | {
|
---|
| 591 | hThreadSelf = RTThreadSelfAutoAdopt();
|
---|
[25685] | 592 | int rc9 = RTLockValidatorRecExclCheckOrder(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, cMillies);
|
---|
[25614] | 593 | if (RT_FAILURE(rc9))
|
---|
| 594 | return rc9;
|
---|
| 595 | }
|
---|
| 596 | #endif
|
---|
| 597 |
|
---|
[1] | 598 | /*
|
---|
[20601] | 599 | * Take critsect.
|
---|
[1] | 600 | */
|
---|
[20601] | 601 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
[1] | 602 | if (RT_FAILURE(rc))
|
---|
| 603 | {
|
---|
[25723] | 604 | AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
|
---|
[1] | 605 | return rc;
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | /*
|
---|
[25515] | 609 | * Check if the state of affairs allows write access.
|
---|
[1] | 610 | */
|
---|
[25522] | 611 | RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
|
---|
[25616] | 612 | if ( !pThis->cReads
|
---|
| 613 | && ( ( !pThis->cWrites
|
---|
| 614 | && ( !pThis->cWritesWaiting /* play fair if we can wait */
|
---|
| 615 | || !cMillies)
|
---|
| 616 | )
|
---|
| 617 | || pThis->hWriter == hNativeSelf
|
---|
| 618 | )
|
---|
| 619 | )
|
---|
[1] | 620 | {
|
---|
| 621 | /*
|
---|
[25515] | 622 | * Reset the reader event semaphore if necessary.
|
---|
[1] | 623 | */
|
---|
[25515] | 624 | if (pThis->fNeedResetReadEvent)
|
---|
[1] | 625 | {
|
---|
[25515] | 626 | pThis->fNeedResetReadEvent = false;
|
---|
| 627 | rc = RTSemEventMultiReset(pThis->ReadEvent);
|
---|
[25723] | 628 | AssertMsgRC(rc, ("Failed to reset readers, rwsem %p, rc=%Rrc.\n", hRWSem, rc));
|
---|
[1] | 629 | }
|
---|
| 630 |
|
---|
[25515] | 631 | pThis->cWrites++;
|
---|
[25522] | 632 | pThis->hWriter = hNativeSelf;
|
---|
[25614] | 633 | #ifdef RTSEMRW_STRICT
|
---|
| 634 | RTLockValidatorRecExclSetOwner(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, pThis->cWrites == 1);
|
---|
| 635 | #endif
|
---|
[20601] | 636 | RTCritSectLeave(&pThis->CritSect);
|
---|
[25515] | 637 | return VINF_SUCCESS;
|
---|
| 638 | }
|
---|
[20601] | 639 |
|
---|
[25515] | 640 | /*
|
---|
| 641 | * Signal writer presence.
|
---|
| 642 | */
|
---|
| 643 | if (cMillies != 0)
|
---|
| 644 | pThis->cWritesWaiting++;
|
---|
| 645 |
|
---|
| 646 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 647 |
|
---|
| 648 | /*
|
---|
| 649 | * Wait till it's ready for writing.
|
---|
| 650 | */
|
---|
| 651 | if (cMillies == 0)
|
---|
| 652 | return VERR_TIMEOUT;
|
---|
| 653 |
|
---|
[25614] | 654 | #ifndef RTSEMRW_STRICT
|
---|
| 655 | RTTHREAD hThreadSelf = RTThreadSelf();
|
---|
| 656 | #endif
|
---|
[25515] | 657 | for (;;)
|
---|
| 658 | {
|
---|
| 659 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
[1] | 660 | {
|
---|
[20601] | 661 | int64_t tsDelta = RTTimeNanoTS() - tsStart;
|
---|
[1] | 662 | if (tsDelta >= 1000000)
|
---|
| 663 | {
|
---|
[25515] | 664 | tsDelta /= 1000000;
|
---|
| 665 | if ((uint64_t)tsDelta < cMilliesInitial)
|
---|
[25724] | 666 | cMilliesInitial = (RTMSINTERVAL)tsDelta;
|
---|
[25515] | 667 | else
|
---|
| 668 | cMilliesInitial = 1;
|
---|
[1] | 669 | }
|
---|
| 670 | }
|
---|
[25618] | 671 |
|
---|
[25614] | 672 | #ifdef RTSEMRW_STRICT
|
---|
[25638] | 673 | rc = RTLockValidatorRecExclCheckBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true,
|
---|
[25685] | 674 | cMillies, RTTHREADSTATE_RW_WRITE, false);
|
---|
[25614] | 675 | if (RT_FAILURE(rc))
|
---|
| 676 | break;
|
---|
[25618] | 677 | #else
|
---|
[25638] | 678 | RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_WRITE, false);
|
---|
[62592] | 679 | RT_NOREF_PV(pSrcPos);
|
---|
[25614] | 680 | #endif
|
---|
[25620] | 681 | int rcWait;
|
---|
| 682 | if (fInterruptible)
|
---|
| 683 | rcWait = rc = RTSemEventWaitNoResume(pThis->WriteEvent, cMillies);
|
---|
| 684 | else
|
---|
| 685 | rcWait = rc = RTSemEventWait(pThis->WriteEvent, cMillies);
|
---|
[25614] | 686 | RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
|
---|
[25515] | 687 | if (RT_UNLIKELY(RT_FAILURE_NP(rc) && rc != VERR_TIMEOUT)) /* timeouts are handled below */
|
---|
[20601] | 688 | {
|
---|
[25723] | 689 | AssertMsgRC(rc, ("RTSemEventWait failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
|
---|
[20601] | 690 | break;
|
---|
| 691 | }
|
---|
[1] | 692 |
|
---|
[25513] | 693 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMRW_MAGIC))
|
---|
[20601] | 694 | {
|
---|
| 695 | rc = VERR_SEM_DESTROYED;
|
---|
| 696 | break;
|
---|
| 697 | }
|
---|
[1] | 698 |
|
---|
| 699 | /*
|
---|
[33540] | 700 | * Re-take critsect and repeat the check we did prior to this loop.
|
---|
[1] | 701 | */
|
---|
[20601] | 702 | rc = RTCritSectEnter(&pThis->CritSect);
|
---|
| 703 | if (RT_FAILURE(rc))
|
---|
[1] | 704 | {
|
---|
[25723] | 705 | AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
|
---|
[20601] | 706 | break;
|
---|
[1] | 707 | }
|
---|
[25515] | 708 |
|
---|
[25522] | 709 | if (!pThis->cReads && (!pThis->cWrites || pThis->hWriter == hNativeSelf))
|
---|
[25515] | 710 | {
|
---|
| 711 | /*
|
---|
| 712 | * Reset the reader event semaphore if necessary.
|
---|
| 713 | */
|
---|
| 714 | if (pThis->fNeedResetReadEvent)
|
---|
| 715 | {
|
---|
| 716 | pThis->fNeedResetReadEvent = false;
|
---|
| 717 | rc = RTSemEventMultiReset(pThis->ReadEvent);
|
---|
[25723] | 718 | AssertMsgRC(rc, ("Failed to reset readers, rwsem %p, rc=%Rrc.\n", hRWSem, rc));
|
---|
[25515] | 719 | }
|
---|
| 720 |
|
---|
| 721 | pThis->cWrites++;
|
---|
[25522] | 722 | pThis->hWriter = hNativeSelf;
|
---|
[25515] | 723 | pThis->cWritesWaiting--;
|
---|
[25614] | 724 | #ifdef RTSEMRW_STRICT
|
---|
| 725 | RTLockValidatorRecExclSetOwner(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true);
|
---|
| 726 | #endif
|
---|
[25515] | 727 |
|
---|
| 728 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 729 | return VINF_SUCCESS;
|
---|
| 730 | }
|
---|
| 731 |
|
---|
| 732 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 733 |
|
---|
| 734 | /*
|
---|
| 735 | * Quit if the wait already timed out.
|
---|
| 736 | */
|
---|
| 737 | if (rcWait == VERR_TIMEOUT)
|
---|
| 738 | {
|
---|
| 739 | rc = VERR_TIMEOUT;
|
---|
| 740 | break;
|
---|
| 741 | }
|
---|
[1] | 742 | }
|
---|
| 743 |
|
---|
| 744 | /*
|
---|
[20601] | 745 | * Timeout/error case, clean up.
|
---|
[1] | 746 | */
|
---|
[20601] | 747 | if (pThis->u32Magic == RTSEMRW_MAGIC)
|
---|
| 748 | {
|
---|
| 749 | RTCritSectEnter(&pThis->CritSect);
|
---|
| 750 | /* Adjust this counter, whether we got the critsect or not. */
|
---|
| 751 | pThis->cWritesWaiting--;
|
---|
| 752 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 753 | }
|
---|
| 754 | return rc;
|
---|
[1] | 755 | }
|
---|
[25620] | 756 |
|
---|
| 757 |
|
---|
[25723] | 758 | RTDECL(int) RTSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
|
---|
[25620] | 759 | {
|
---|
| 760 | #ifndef RTSEMRW_STRICT
|
---|
[25723] | 761 | return rtSemRWRequestWrite(hRWSem, cMillies, false, NULL);
|
---|
[25620] | 762 | #else
|
---|
| 763 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
[25723] | 764 | return rtSemRWRequestWrite(hRWSem, cMillies, false, &SrcPos);
|
---|
[25620] | 765 | #endif
|
---|
| 766 | }
|
---|
[21337] | 767 | RT_EXPORT_SYMBOL(RTSemRWRequestWrite);
|
---|
[1] | 768 |
|
---|
| 769 |
|
---|
[25723] | 770 | RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
[25620] | 771 | {
|
---|
| 772 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
[25723] | 773 | return rtSemRWRequestWrite(hRWSem, cMillies, false, &SrcPos);
|
---|
[25620] | 774 | }
|
---|
| 775 | RT_EXPORT_SYMBOL(RTSemRWRequestWriteDebug);
|
---|
| 776 |
|
---|
| 777 |
|
---|
[25723] | 778 | RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
|
---|
[1] | 779 | {
|
---|
[25620] | 780 | #ifndef RTSEMRW_STRICT
|
---|
[25723] | 781 | return rtSemRWRequestWrite(hRWSem, cMillies, true, NULL);
|
---|
[25620] | 782 | #else
|
---|
| 783 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
[25723] | 784 | return rtSemRWRequestWrite(hRWSem, cMillies, true, &SrcPos);
|
---|
[25620] | 785 | #endif
|
---|
[1] | 786 | }
|
---|
[21337] | 787 | RT_EXPORT_SYMBOL(RTSemRWRequestWriteNoResume);
|
---|
[1] | 788 |
|
---|
| 789 |
|
---|
[25723] | 790 | RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
[25620] | 791 | {
|
---|
| 792 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
[25723] | 793 | return rtSemRWRequestWrite(hRWSem, cMillies, true, &SrcPos);
|
---|
[25620] | 794 | }
|
---|
| 795 | RT_EXPORT_SYMBOL(RTSemRWRequestWriteNoResumeDebug);
|
---|
| 796 |
|
---|
| 797 |
|
---|
[25723] | 798 | RTDECL(int) RTSemRWReleaseWrite(RTSEMRW hRWSem)
|
---|
[1] | 799 | {
|
---|
[25513] | 800 |
|
---|
[1] | 801 | /*
|
---|
| 802 | * Validate handle.
|
---|
| 803 | */
|
---|
[25723] | 804 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
[25513] | 805 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
| 806 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
|
---|
[1] | 807 |
|
---|
| 808 | /*
|
---|
[20601] | 809 | * Take critsect.
|
---|
| 810 | */
|
---|
| 811 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
[25522] | 812 | AssertRCReturn(rc, rc);
|
---|
[20601] | 813 |
|
---|
| 814 | /*
|
---|
[1] | 815 | * Check if owner.
|
---|
| 816 | */
|
---|
[25522] | 817 | RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
|
---|
| 818 | if (pThis->hWriter != hNativeSelf)
|
---|
[1] | 819 | {
|
---|
[20601] | 820 | RTCritSectLeave(&pThis->CritSect);
|
---|
[25723] | 821 | AssertMsgFailed(("Not read-write owner of rwsem %p.\n", hRWSem));
|
---|
[1] | 822 | return VERR_NOT_OWNER;
|
---|
| 823 | }
|
---|
| 824 |
|
---|
[25614] | 825 | #ifdef RTSEMRW_STRICT
|
---|
| 826 | if (pThis->cWrites > 1 || !pThis->cWriterReads) /* don't check+release if VERR_WRONG_ORDER */
|
---|
| 827 | {
|
---|
| 828 | int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorWrite, pThis->cWrites == 1);
|
---|
| 829 | if (RT_FAILURE(rc9))
|
---|
| 830 | {
|
---|
| 831 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 832 | return rc9;
|
---|
| 833 | }
|
---|
| 834 | }
|
---|
| 835 | #endif
|
---|
| 836 |
|
---|
[1] | 837 | /*
|
---|
[20601] | 838 | * Release ownership and remove ourselves from the writers count.
|
---|
[1] | 839 | */
|
---|
[25522] | 840 | Assert(pThis->cWrites > 0);
|
---|
[20601] | 841 | pThis->cWrites--;
|
---|
| 842 | if (!pThis->cWrites)
|
---|
[25522] | 843 | {
|
---|
| 844 | if (RT_UNLIKELY(pThis->cWriterReads > 0))
|
---|
| 845 | {
|
---|
| 846 | pThis->cWrites++;
|
---|
| 847 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 848 | AssertMsgFailed(("All recursive read locks need to be released prior to the final write lock! (%p)n\n", pThis));
|
---|
| 849 | return VERR_WRONG_ORDER;
|
---|
| 850 | }
|
---|
[20601] | 851 |
|
---|
[25522] | 852 | pThis->hWriter = NIL_RTNATIVETHREAD;
|
---|
| 853 | }
|
---|
| 854 |
|
---|
[20601] | 855 | /*
|
---|
| 856 | * Release the readers if no more writers waiting, otherwise the writers.
|
---|
| 857 | */
|
---|
| 858 | if (!pThis->cWritesWaiting)
|
---|
[1] | 859 | {
|
---|
[20601] | 860 | rc = RTSemEventMultiSignal(pThis->ReadEvent);
|
---|
[25723] | 861 | AssertMsgRC(rc, ("RTSemEventMultiSignal failed for rwsem %p, rc=%Rrc.\n", hRWSem, rc));
|
---|
[25513] | 862 | pThis->fNeedResetReadEvent = true;
|
---|
[1] | 863 | }
|
---|
[20601] | 864 | else
|
---|
| 865 | {
|
---|
| 866 | rc = RTSemEventSignal(pThis->WriteEvent);
|
---|
[25723] | 867 | AssertMsgRC(rc, ("Failed to signal writers on rwsem %p, rc=%Rrc\n", hRWSem, rc));
|
---|
[20601] | 868 | }
|
---|
| 869 | RTCritSectLeave(&pThis->CritSect);
|
---|
[1] | 870 |
|
---|
[20601] | 871 | return rc;
|
---|
| 872 | }
|
---|
[21337] | 873 | RT_EXPORT_SYMBOL(RTSemRWReleaseWrite);
|
---|
[20601] | 874 |
|
---|
| 875 |
|
---|
[25723] | 876 | RTDECL(bool) RTSemRWIsWriteOwner(RTSEMRW hRWSem)
|
---|
[20601] | 877 | {
|
---|
[1] | 878 | /*
|
---|
[20601] | 879 | * Validate handle.
|
---|
[1] | 880 | */
|
---|
[25908] | 881 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
[25793] | 882 | AssertPtrReturn(pThis, false);
|
---|
| 883 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
|
---|
[1] | 884 |
|
---|
| 885 | /*
|
---|
[20601] | 886 | * Check ownership.
|
---|
[1] | 887 | */
|
---|
[25522] | 888 | RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
|
---|
| 889 | RTNATIVETHREAD hWriter;
|
---|
| 890 | ASMAtomicUoReadHandle(&pThis->hWriter, &hWriter);
|
---|
| 891 | return hWriter == hNativeSelf;
|
---|
[20601] | 892 | }
|
---|
[21337] | 893 | RT_EXPORT_SYMBOL(RTSemRWIsWriteOwner);
|
---|
[20601] | 894 |
|
---|
| 895 |
|
---|
[25908] | 896 | RTDECL(bool) RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear)
|
---|
| 897 | {
|
---|
| 898 | /*
|
---|
| 899 | * Validate handle.
|
---|
| 900 | */
|
---|
| 901 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
| 902 | AssertPtrReturn(pThis, false);
|
---|
| 903 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
|
---|
| 904 |
|
---|
| 905 | /*
|
---|
| 906 | * Check write ownership. The writer is also a valid reader.
|
---|
| 907 | */
|
---|
| 908 | RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
|
---|
| 909 | RTNATIVETHREAD hWriter;
|
---|
| 910 | ASMAtomicUoReadHandle(&pThis->hWriter, &hWriter);
|
---|
| 911 | if (hWriter == hNativeSelf)
|
---|
| 912 | return true;
|
---|
| 913 | if (hWriter != NIL_RTNATIVETHREAD)
|
---|
| 914 | return false;
|
---|
| 915 |
|
---|
| 916 | #ifdef RTSEMRW_STRICT
|
---|
| 917 | /*
|
---|
| 918 | * Ask the lock validator.
|
---|
| 919 | */
|
---|
[39083] | 920 | NOREF(fWannaHear);
|
---|
[25908] | 921 | return RTLockValidatorRecSharedIsOwner(&pThis->ValidatorRead, NIL_RTTHREAD);
|
---|
| 922 | #else
|
---|
| 923 | /*
|
---|
| 924 | * If there are no reads we cannot be one of them... But if there are we
|
---|
| 925 | * cannot know and can only return what the caller want to hear.
|
---|
| 926 | */
|
---|
| 927 | if (pThis->cReads == 0)
|
---|
| 928 | return false;
|
---|
| 929 | return fWannaHear;
|
---|
| 930 | #endif
|
---|
| 931 | }
|
---|
| 932 | RT_EXPORT_SYMBOL(RTSemRWIsReadOwner);
|
---|
| 933 |
|
---|
| 934 |
|
---|
[25723] | 935 | RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW hRWSem)
|
---|
[20601] | 936 | {
|
---|
[25723] | 937 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
[25513] | 938 |
|
---|
[20601] | 939 | /*
|
---|
| 940 | * Validate handle.
|
---|
| 941 | */
|
---|
[25793] | 942 | AssertPtrReturn(pThis, 0);
|
---|
| 943 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, 0);
|
---|
[1] | 944 |
|
---|
[20601] | 945 | /*
|
---|
| 946 | * Return the requested data.
|
---|
| 947 | */
|
---|
| 948 | return pThis->cWrites;
|
---|
[1] | 949 | }
|
---|
[21337] | 950 | RT_EXPORT_SYMBOL(RTSemRWGetWriteRecursion);
|
---|
[1] | 951 |
|
---|
[20601] | 952 |
|
---|
[25723] | 953 | RTDECL(uint32_t) RTSemRWGetWriterReadRecursion(RTSEMRW hRWSem)
|
---|
[20601] | 954 | {
|
---|
[25723] | 955 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
[25513] | 956 |
|
---|
[20601] | 957 | /*
|
---|
| 958 | * Validate handle.
|
---|
| 959 | */
|
---|
[25793] | 960 | AssertPtrReturn(pThis, 0);
|
---|
| 961 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, 0);
|
---|
[20601] | 962 |
|
---|
| 963 | /*
|
---|
| 964 | * Return the requested data.
|
---|
| 965 | */
|
---|
| 966 | return pThis->cWriterReads;
|
---|
| 967 | }
|
---|
[21337] | 968 | RT_EXPORT_SYMBOL(RTSemRWGetWriterReadRecursion);
|
---|
[25616] | 969 |
|
---|
| 970 |
|
---|
[25723] | 971 | RTDECL(uint32_t) RTSemRWGetReadCount(RTSEMRW hRWSem)
|
---|
[25616] | 972 | {
|
---|
| 973 | /*
|
---|
| 974 | * Validate input.
|
---|
| 975 | */
|
---|
[25723] | 976 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
[25616] | 977 | AssertPtrReturn(pThis, 0);
|
---|
| 978 | AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
|
---|
| 979 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
| 980 | 0);
|
---|
| 981 |
|
---|
| 982 | /*
|
---|
| 983 | * Return the requested data.
|
---|
| 984 | */
|
---|
| 985 | return pThis->cReads;
|
---|
| 986 | }
|
---|
| 987 | RT_EXPORT_SYMBOL(RTSemRWGetReadCount);
|
---|
| 988 |
|
---|