VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/semevent-posix.cpp@ 25802

Last change on this file since 25802 was 25724, checked in by vboxsync, 15 years ago

iprt: Use RTMSINTERVAL for timeouts. Fixed missing timeout underflow checks in two RTFileAioCtxWait implementations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.9 KB
Line 
1/* $Id: semevent-posix.cpp 25724 2010-01-11 14:45:34Z vboxsync $ */
2/** @file
3 * IPRT - Event Semaphore, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/semaphore.h>
35#include "internal/iprt.h"
36
37#include <iprt/asm.h>
38#include <iprt/assert.h>
39#include <iprt/err.h>
40#include <iprt/mem.h>
41#include <iprt/lockvalidator.h>
42
43#include "internal/strict.h"
44
45#include <errno.h>
46#include <pthread.h>
47#include <unistd.h>
48#include <sys/time.h>
49
50#ifdef RT_OS_DARWIN
51# define pthread_yield() pthread_yield_np()
52#endif
53
54#ifdef RT_OS_SOLARIS
55# include <sched.h>
56# define pthread_yield() sched_yield()
57#endif
58
59
60/*******************************************************************************
61* Structures and Typedefs *
62*******************************************************************************/
63
64/** Internal representation of the POSIX implementation of an Event semaphore.
65 * The POSIX implementation uses a mutex and a condition variable to implement
66 * the automatic reset event semaphore semantics.
67 */
68struct RTSEMEVENTINTERNAL
69{
70 /** pthread condition. */
71 pthread_cond_t Cond;
72 /** pthread mutex which protects the condition and the event state. */
73 pthread_mutex_t Mutex;
74 /** The state of the semaphore.
75 * This is operated while owning mutex and using atomic updating. */
76 volatile uint32_t u32State;
77 /** Number of waiters. */
78 volatile uint32_t cWaiters;
79#ifdef RTSEMEVENT_STRICT
80 /** Signallers. */
81 RTLOCKVALRECSHRD Signallers;
82 /** Indicates that lock validation should be performed. */
83 bool volatile fEverHadSignallers;
84#endif
85};
86
87/** The valus of the u32State variable in a RTSEMEVENTINTERNAL.
88 * @{ */
89/** The object isn't initialized. */
90#define EVENT_STATE_UNINITIALIZED 0
91/** The semaphore is signaled. */
92#define EVENT_STATE_SIGNALED 0xff00ff00
93/** The semaphore is not signaled. */
94#define EVENT_STATE_NOT_SIGNALED 0x00ff00ff
95/** @} */
96
97
98RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
99{
100 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
101}
102
103
104RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
105{
106 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
107
108 /*
109 * Allocate semaphore handle.
110 */
111 int rc;
112 struct RTSEMEVENTINTERNAL *pThis = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL));
113 if (pThis)
114 {
115 /*
116 * Create the condition variable.
117 */
118 pthread_condattr_t CondAttr;
119 rc = pthread_condattr_init(&CondAttr);
120 if (!rc)
121 {
122 rc = pthread_cond_init(&pThis->Cond, &CondAttr);
123 if (!rc)
124 {
125 /*
126 * Create the semaphore.
127 */
128 pthread_mutexattr_t MutexAttr;
129 rc = pthread_mutexattr_init(&MutexAttr);
130 if (!rc)
131 {
132 rc = pthread_mutex_init(&pThis->Mutex, &MutexAttr);
133 if (!rc)
134 {
135 pthread_mutexattr_destroy(&MutexAttr);
136 pthread_condattr_destroy(&CondAttr);
137
138 ASMAtomicXchgU32(&pThis->u32State, EVENT_STATE_NOT_SIGNALED);
139 ASMAtomicXchgU32(&pThis->cWaiters, 0);
140#ifdef RTSEMEVENT_STRICT
141 va_list va;
142 va_start(va, pszNameFmt);
143 RTLockValidatorRecSharedInitV(&pThis->Signallers, hClass, RTLOCKVAL_SUB_CLASS_ANY, pThis,
144 true /*fSignaller*/, !(fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL),
145 pszNameFmt, va);
146 va_end(va);
147 pThis->fEverHadSignallers = false;
148#endif
149
150 *phEventSem = pThis;
151 return VINF_SUCCESS;
152 }
153
154 pthread_mutexattr_destroy(&MutexAttr);
155 }
156 pthread_cond_destroy(&pThis->Cond);
157 }
158 pthread_condattr_destroy(&CondAttr);
159 }
160
161 rc = RTErrConvertFromErrno(rc);
162 RTMemFree(pThis);
163 }
164 else
165 rc = VERR_NO_MEMORY;
166
167 return rc;
168}
169
170
171RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
172{
173 /*
174 * Validate handle.
175 */
176 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
177 if (pThis == NIL_RTSEMEVENT)
178 return VINF_SUCCESS;
179 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
180 uint32_t u32 = pThis->u32State;
181 AssertReturn(u32 == EVENT_STATE_NOT_SIGNALED || u32 == EVENT_STATE_SIGNALED, VERR_INVALID_HANDLE);
182
183 /*
184 * Abort all waiters forcing them to return failure.
185 */
186 int rc;
187 for (int i = 30; i > 0; i--)
188 {
189 ASMAtomicXchgU32(&pThis->u32State, EVENT_STATE_UNINITIALIZED);
190 rc = pthread_cond_destroy(&pThis->Cond);
191 if (rc != EBUSY)
192 break;
193 pthread_cond_broadcast(&pThis->Cond);
194 usleep(1000);
195 }
196 if (rc)
197 {
198 AssertMsgFailed(("Failed to destroy event sem %p, rc=%d.\n", pThis, rc));
199 return RTErrConvertFromErrno(rc);
200 }
201
202 /*
203 * Destroy the semaphore
204 * If it's busy we'll wait a bit to give the threads a chance to be scheduled.
205 */
206 for (int i = 30; i > 0; i--)
207 {
208 rc = pthread_mutex_destroy(&pThis->Mutex);
209 if (rc != EBUSY)
210 break;
211 usleep(1000);
212 }
213 if (rc)
214 {
215 AssertMsgFailed(("Failed to destroy event sem %p, rc=%d. (mutex)\n", pThis, rc));
216 return RTErrConvertFromErrno(rc);
217 }
218
219 /*
220 * Free the semaphore memory and be gone.
221 */
222#ifdef RTSEMEVENT_STRICT
223 RTLockValidatorRecSharedDelete(&pThis->Signallers);
224#endif
225 RTMemFree(pThis);
226 return VINF_SUCCESS;
227}
228
229
230RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
231{
232 /*
233 * Validate input.
234 */
235 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
236 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
237 uint32_t u32 = pThis->u32State;
238 AssertReturn(u32 == EVENT_STATE_NOT_SIGNALED || u32 == EVENT_STATE_SIGNALED, VERR_INVALID_HANDLE);
239
240#ifdef RTSEMEVENT_STRICT
241 if (pThis->fEverHadSignallers)
242 {
243 int rc9 = RTLockValidatorRecSharedCheckSignaller(&pThis->Signallers, NIL_RTTHREAD);
244 if (RT_FAILURE(rc9))
245 return rc9;
246 }
247#endif
248
249 /*
250 * Lock the mutex semaphore.
251 */
252 int rc = pthread_mutex_lock(&pThis->Mutex);
253 if (rc)
254 {
255 AssertMsgFailed(("Failed to lock event sem %p, rc=%d.\n", hEventSem, rc));
256 return RTErrConvertFromErrno(rc);
257 }
258
259 /*
260 * Check the state.
261 */
262 if (pThis->u32State == EVENT_STATE_NOT_SIGNALED)
263 {
264 ASMAtomicXchgU32(&pThis->u32State, EVENT_STATE_SIGNALED);
265 rc = pthread_cond_signal(&pThis->Cond);
266 AssertMsg(!rc, ("Failed to signal event sem %p, rc=%d.\n", hEventSem, rc));
267 }
268 else if (pThis->u32State == EVENT_STATE_SIGNALED)
269 {
270 rc = pthread_cond_signal(&pThis->Cond); /* give'm another kick... */
271 AssertMsg(!rc, ("Failed to signal event sem %p, rc=%d. (2)\n", hEventSem, rc));
272 }
273 else
274 rc = VERR_SEM_DESTROYED;
275
276 /*
277 * Release the mutex and return.
278 */
279 int rc2 = pthread_mutex_unlock(&pThis->Mutex);
280 AssertMsg(!rc2, ("Failed to unlock event sem %p, rc=%d.\n", hEventSem, rc));
281 if (rc)
282 return RTErrConvertFromErrno(rc);
283 if (rc2)
284 return RTErrConvertFromErrno(rc2);
285
286 return VINF_SUCCESS;
287}
288
289
290DECL_FORCE_INLINE(int) rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fAutoResume)
291{
292 PCRTLOCKVALSRCPOS pSrcPos = NULL;
293
294 /*
295 * Validate input.
296 */
297 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
298 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
299 uint32_t u32 = pThis->u32State;
300 AssertReturn(u32 == EVENT_STATE_NOT_SIGNALED || u32 == EVENT_STATE_SIGNALED, VERR_INVALID_HANDLE);
301
302 /*
303 * Timed or indefinite wait?
304 */
305 if (cMillies == RT_INDEFINITE_WAIT)
306 {
307 /* for fairness, yield before going to sleep. */
308 if ( ASMAtomicIncU32(&pThis->cWaiters) > 1
309 && pThis->u32State == EVENT_STATE_SIGNALED)
310 pthread_yield();
311
312 /* take mutex */
313 int rc = pthread_mutex_lock(&pThis->Mutex);
314 if (rc)
315 {
316 ASMAtomicDecU32(&pThis->cWaiters);
317 AssertMsgFailed(("Failed to lock event sem %p, rc=%d.\n", hEventSem, rc));
318 return RTErrConvertFromErrno(rc);
319 }
320
321 for (;;)
322 {
323 /* check state. */
324 if (pThis->u32State == EVENT_STATE_SIGNALED)
325 {
326 ASMAtomicXchgU32(&pThis->u32State, EVENT_STATE_NOT_SIGNALED);
327 ASMAtomicDecU32(&pThis->cWaiters);
328 rc = pthread_mutex_unlock(&pThis->Mutex);
329 AssertMsg(!rc, ("Failed to unlock event sem %p, rc=%d.\n", hEventSem, rc)); NOREF(rc);
330 return VINF_SUCCESS;
331 }
332 if (pThis->u32State == EVENT_STATE_UNINITIALIZED)
333 {
334 rc = pthread_mutex_unlock(&pThis->Mutex);
335 AssertMsg(!rc, ("Failed to unlock event sem %p, rc=%d.\n", hEventSem, rc)); NOREF(rc);
336 return VERR_SEM_DESTROYED;
337 }
338
339 /* wait */
340#ifdef RTSEMEVENT_STRICT
341 RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
342 if (pThis->fEverHadSignallers)
343 {
344 rc = RTLockValidatorRecSharedCheckBlocking(&pThis->Signallers, hThreadSelf, pSrcPos, false,
345 cMillies, RTTHREADSTATE_EVENT, true);
346 if (RT_FAILURE(rc))
347 {
348 ASMAtomicDecU32(&pThis->cWaiters);
349 pthread_mutex_unlock(&pThis->Mutex);
350 return rc;
351 }
352 }
353#else
354 RTTHREAD hThreadSelf = RTThreadSelf();
355#endif
356 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_EVENT, true);
357 rc = pthread_cond_wait(&pThis->Cond, &pThis->Mutex);
358 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_EVENT);
359 if (rc)
360 {
361 AssertMsgFailed(("Failed to wait on event sem %p, rc=%d.\n", hEventSem, rc));
362 ASMAtomicDecU32(&pThis->cWaiters);
363 int rc2 = pthread_mutex_unlock(&pThis->Mutex);
364 AssertMsg(!rc2, ("Failed to unlock event sem %p, rc=%d.\n", hEventSem, rc2)); NOREF(rc2);
365 return RTErrConvertFromErrno(rc);
366 }
367 }
368 }
369 else
370 {
371 /*
372 * Get current time and calc end of wait time.
373 */
374 struct timespec ts = {0,0};
375#ifdef RT_OS_DARWIN
376 struct timeval tv = {0,0};
377 gettimeofday(&tv, NULL);
378 ts.tv_sec = tv.tv_sec;
379 ts.tv_nsec = tv.tv_usec * 1000;
380#else
381 clock_gettime(CLOCK_REALTIME, &ts);
382#endif
383 if (cMillies != 0)
384 {
385 ts.tv_nsec += (cMillies % 1000) * 1000000;
386 ts.tv_sec += cMillies / 1000;
387 if (ts.tv_nsec >= 1000000000)
388 {
389 ts.tv_nsec -= 1000000000;
390 ts.tv_sec++;
391 }
392 }
393
394 /* for fairness, yield before going to sleep. */
395 if (ASMAtomicIncU32(&pThis->cWaiters) > 1 && cMillies)
396 pthread_yield();
397
398 /* take mutex */
399 int rc = pthread_mutex_lock(&pThis->Mutex);
400 if (rc)
401 {
402 ASMAtomicDecU32(&pThis->cWaiters);
403 AssertMsg(rc == ETIMEDOUT, ("Failed to lock event sem %p, rc=%d.\n", hEventSem, rc));
404 return RTErrConvertFromErrno(rc);
405 }
406
407 for (;;)
408 {
409 /* check state. */
410 if (pThis->u32State == EVENT_STATE_SIGNALED)
411 {
412 ASMAtomicXchgU32(&pThis->u32State, EVENT_STATE_NOT_SIGNALED);
413 ASMAtomicDecU32(&pThis->cWaiters);
414 rc = pthread_mutex_unlock(&pThis->Mutex);
415 AssertMsg(!rc, ("Failed to unlock event sem %p, rc=%d.\n", hEventSem, rc)); NOREF(rc);
416 return VINF_SUCCESS;
417 }
418 if (pThis->u32State == EVENT_STATE_UNINITIALIZED)
419 {
420 rc = pthread_mutex_unlock(&pThis->Mutex);
421 AssertMsg(!rc, ("Failed to unlock event sem %p, rc=%d.\n", hEventSem, rc)); NOREF(rc);
422 return VERR_SEM_DESTROYED;
423 }
424
425 /* we're done if the timeout is 0. */
426 if (!cMillies)
427 {
428 ASMAtomicDecU32(&pThis->cWaiters);
429 rc = pthread_mutex_unlock(&pThis->Mutex);
430 return VERR_TIMEOUT;
431 }
432
433 /* wait */
434#ifdef RTSEMEVENT_STRICT
435 RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
436 if (pThis->fEverHadSignallers)
437 {
438 rc = RTLockValidatorRecSharedCheckBlocking(&pThis->Signallers, hThreadSelf, pSrcPos, false,
439 cMillies, RTTHREADSTATE_EVENT, true);
440 if (RT_FAILURE(rc))
441 {
442 ASMAtomicDecU32(&pThis->cWaiters);
443 pthread_mutex_unlock(&pThis->Mutex);
444 return rc;
445 }
446 }
447#else
448 RTTHREAD hThreadSelf = RTThreadSelf();
449#endif
450 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_EVENT, true);
451 rc = pthread_cond_timedwait(&pThis->Cond, &pThis->Mutex, &ts);
452 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_EVENT);
453 if (rc && (rc != EINTR || !fAutoResume)) /* according to SuS this function shall not return EINTR, but linux man page says differently. */
454 {
455 AssertMsg(rc == ETIMEDOUT, ("Failed to wait on event sem %p, rc=%d.\n", hEventSem, rc));
456 ASMAtomicDecU32(&pThis->cWaiters);
457 int rc2 = pthread_mutex_unlock(&pThis->Mutex);
458 AssertMsg(!rc2, ("Failed to unlock event sem %p, rc2=%d.\n", hEventSem, rc2)); NOREF(rc2);
459 return RTErrConvertFromErrno(rc);
460 }
461 } /* for (;;) */
462 }
463}
464
465
466RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
467{
468 int rc = rtSemEventWait(hEventSem, cMillies, true);
469 Assert(rc != VERR_INTERRUPTED);
470 return rc;
471}
472
473
474RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
475{
476 return rtSemEventWait(hEventSem, cMillies, false);
477}
478
479
480RTDECL(void) RTSemEventSetSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
481{
482#ifdef RTSEMEVENT_STRICT
483 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
484 AssertPtrReturnVoid(pThis);
485 uint32_t u32 = pThis->u32State;
486 AssertReturnVoid(u32 == EVENT_STATE_NOT_SIGNALED || u32 == EVENT_STATE_SIGNALED);
487
488 ASMAtomicWriteBool(&pThis->fEverHadSignallers, true);
489 RTLockValidatorRecSharedResetOwner(&pThis->Signallers, hThread, NULL);
490#endif
491}
492
493
494RTDECL(void) RTSemEventAddSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
495{
496#ifdef RTSEMEVENT_STRICT
497 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
498 AssertPtrReturnVoid(pThis);
499 uint32_t u32 = pThis->u32State;
500 AssertReturnVoid(u32 == EVENT_STATE_NOT_SIGNALED || u32 == EVENT_STATE_SIGNALED);
501
502 ASMAtomicWriteBool(&pThis->fEverHadSignallers, true);
503 RTLockValidatorRecSharedAddOwner(&pThis->Signallers, hThread, NULL);
504#endif
505}
506
507
508RTDECL(void) RTSemEventRemoveSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
509{
510#ifdef RTSEMEVENT_STRICT
511 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
512 AssertPtrReturnVoid(pThis);
513 uint32_t u32 = pThis->u32State;
514 AssertReturnVoid(u32 == EVENT_STATE_NOT_SIGNALED || u32 == EVENT_STATE_SIGNALED);
515
516 RTLockValidatorRecSharedRemoveOwner(&pThis->Signallers, hThread);
517#endif
518}
519
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette