1 | /* $Id: semnoint-generic.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Generic Non-Interruptable Wait and Request Functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP RTLOGGROUP_SEM
|
---|
23 | #include <iprt/semaphore.h>
|
---|
24 | #include <iprt/time.h>
|
---|
25 | #include <iprt/err.h>
|
---|
26 | #include <iprt/assert.h>
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
31 | {
|
---|
32 | int rc;
|
---|
33 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
34 | {
|
---|
35 | do rc = RTSemEventWaitNoResume(EventSem, cMillies);
|
---|
36 | while (rc == VERR_INTERRUPTED);
|
---|
37 | }
|
---|
38 | else
|
---|
39 | {
|
---|
40 | const uint64_t u64Start = RTTimeMilliTS();
|
---|
41 | rc = RTSemEventWaitNoResume(EventSem, cMillies);
|
---|
42 | if (rc == VERR_INTERRUPTED)
|
---|
43 | {
|
---|
44 | do
|
---|
45 | {
|
---|
46 | uint64_t u64Elapsed = RTTimeMilliTS() - u64Start;
|
---|
47 | if (u64Elapsed >= cMillies)
|
---|
48 | return VERR_TIMEOUT;
|
---|
49 | rc = RTSemEventWaitNoResume(EventSem, cMillies - (unsigned)u64Elapsed);
|
---|
50 | } while (rc == VERR_INTERRUPTED);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | return rc;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventSem, unsigned cMillies)
|
---|
58 | {
|
---|
59 | int rc;
|
---|
60 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
61 | {
|
---|
62 | do rc = RTSemEventMultiWaitNoResume(EventSem, cMillies);
|
---|
63 | while (rc == VERR_INTERRUPTED);
|
---|
64 | }
|
---|
65 | else
|
---|
66 | {
|
---|
67 | const uint64_t u64Start = RTTimeMilliTS();
|
---|
68 | rc = RTSemEventMultiWaitNoResume(EventSem, cMillies);
|
---|
69 | if (rc == VERR_INTERRUPTED)
|
---|
70 | {
|
---|
71 | do
|
---|
72 | {
|
---|
73 | uint64_t u64Elapsed = RTTimeMilliTS() - u64Start;
|
---|
74 | if (u64Elapsed >= cMillies)
|
---|
75 | return VERR_TIMEOUT;
|
---|
76 | rc = RTSemEventMultiWaitNoResume(EventSem, cMillies - (unsigned)u64Elapsed);
|
---|
77 | } while (rc == VERR_INTERRUPTED);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | return rc;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | RTDECL(int) RTSemMutexRequest(RTSEMMUTEX Mutex, unsigned cMillies)
|
---|
85 | {
|
---|
86 | int rc;
|
---|
87 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
88 | {
|
---|
89 | do rc = RTSemMutexRequestNoResume(Mutex, cMillies);
|
---|
90 | while (rc == VERR_INTERRUPTED);
|
---|
91 | }
|
---|
92 | else
|
---|
93 | {
|
---|
94 | const uint64_t u64Start = RTTimeMilliTS();
|
---|
95 | rc = RTSemMutexRequestNoResume(Mutex, cMillies);
|
---|
96 | if (rc == VERR_INTERRUPTED)
|
---|
97 | {
|
---|
98 | do
|
---|
99 | {
|
---|
100 | uint64_t u64Elapsed = RTTimeMilliTS() - u64Start;
|
---|
101 | if (u64Elapsed >= cMillies)
|
---|
102 | return VERR_TIMEOUT;
|
---|
103 | rc = RTSemMutexRequestNoResume(Mutex, cMillies - (unsigned)u64Elapsed);
|
---|
104 | } while (rc == VERR_INTERRUPTED);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 |
|
---|