VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/semevent-r0drv-solaris.c@ 4071

Last change on this file since 4071 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1/* $Id: semevent-r0drv-solaris.c 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Semaphores, Ring-0 Driver, Solaris.
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/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include "the-solaris-kernel.h"
24#include <time.h>
25
26#include <iprt/semaphore.h>
27#include <iprt/alloc.h>
28#include <iprt/assert.h>
29#include <iprt/asm.h>
30#include <iprt/err.h>
31
32#include "internal/magics.h"
33
34
35/*******************************************************************************
36* Structures and Typedefs *
37*******************************************************************************/
38/**
39 * Solaris event semaphore.
40 */
41typedef struct RTSEMEVENTINTERNAL
42{
43 /** Magic value (RTSEMEVENT_MAGIC). */
44 uint32_t volatile u32Magic;
45 /** The number of waiting threads. */
46 uint32_t volatile cWaiters;
47 /** Set if the event object is signaled. */
48 uint8_t volatile fSignaled;
49 /** The number of threads in the process of waking up. */
50 uint32_t volatile cWaking;
51 /** The Solaris mutex protecting this structure and pairing up the with the cv. */
52 struct mutex Mtx;
53 /** The Solaris condition variable. */
54 kcondvar_t Cnd;
55} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
56
57
58RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
59{
60 Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
61 AssertPtrReturn(pEventSem, VERR_INVALID_POINTER);
62
63 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
64 if (pEventInt)
65 {
66 pEventInt->u32Magic = RTSEMEVENT_MAGIC;
67 pEventInt->cWaiters = 0;
68 pEventInt->cWaking = 0;
69 pEventInt->fSignaled = 0;
70 mutex_init(&pEventInt->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, NULL);
71 cv_init(&pEventInt->Cnd, "IPRT CV", CV_DRIVER, NULL);
72 *pEventSem = pEventInt;
73 return VINF_SUCCESS;
74 }
75 return VERR_NO_MEMORY;
76}
77
78
79RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
80{
81 if (EventSem == NIL_RTSEMEVENT)
82 return VERR_INVALID_HANDLE;
83 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
84 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
85 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
86 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
87 VERR_INVALID_HANDLE);
88
89 mutex_enter(&pEventInt->Mtx);
90 ASMAtomicIncU32(&pEventInt->u32Magic); /* make the handle invalid */
91 if (pEventInt->cWaiters > 0)
92 {
93 /* abort waiting thread, last man cleans up. */
94 ASMAtomicXchgU32(&pEventInt->cWaking, pEventInt->cWaking + pEventInt->cWaiters);
95 cv_signal(&pEventInt->Cnd);
96 mutex_exit(&pEventInt->Mtx);
97 }
98 else if (pEventInt->cWaking)
99 {
100 /* the last waking thread is gonna do the cleanup */
101 mutex_exit(&pEventInt->Mtx);
102 }
103 else
104 {
105 mutex_exit(&pEventInt->Mtx);
106 mutex_destroy(&pEventInt->Mtx);
107 cv_destroy(&pEventInt->Cnd);
108 RTMemFree(pEventInt);
109 }
110
111 return VINF_SUCCESS;
112}
113
114
115RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
116{
117 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
118 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
119 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
120 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
121 VERR_INVALID_HANDLE);
122
123 mutex_enter(&pEventInt->Mtx);
124
125 if (pEventInt->cWaiters > 0)
126 {
127 ASMAtomicDecU32(&pEventInt->cWaiters);
128 ASMAtomicIncU32(&pEventInt->cWaking);
129 cv_signal(&pEventInt->Cnd);
130 }
131 else
132 ASMAtomicXchgU8(&pEventInt->fSignaled, true);
133
134 mutex_exit(&pEventInt->Mtx);
135 return VINF_SUCCESS;
136}
137
138RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
139{
140 int rc;
141 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
142 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
143 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
144 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
145 VERR_INVALID_HANDLE);
146
147 mutex_enter(&pEventInt->Mtx);
148
149 if (pEventInt->fSignaled)
150 {
151 Assert(!pEventInt->cWaiters);
152 ASMAtomicXchgU8(&pEventInt->fSignaled, false);
153 rc = VINF_SUCCESS;
154 }
155 else
156 {
157 unsigned long timeout;
158 drv_getparm(LBOLT, &timeout);
159
160 /*
161 * Translate milliseconds into ticks and go to sleep.
162 */
163 int cTicks;
164 if (cMillies != RT_INDEFINITE_WAIT)
165 cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
166 else
167 cTicks = 0;
168 cTicks += timeout;
169
170 ASMAtomicIncU32(&pEventInt->cWaiters);
171
172 /** @todo r=bird: Is this interruptible or non-interruptible? */
173 rc = cv_timedwait_sig(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
174 if (rc > 0)
175 {
176 /* Retured due to call to cv_signal() or cv_broadcast() */
177 if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
178 {
179 rc = VERR_SEM_DESTROYED;
180 /** @todo r=bird: better make sure you're the last guy out before doing the cleanup.... */
181 mutex_exit(&pEventInt->Mtx);
182 cv_destroy(&pEventInt->Cnd);
183 mutex_destroy(&pEventInt->Mtx);
184 RTMemFree(pEventInt);
185 return rc;
186 }
187
188 ASMAtomicDecU32(&pEventInt->cWaking);
189 rc = VINF_SUCCESS;
190 }
191 else if (rc == -1)
192 {
193 /* Returned due to timeout being reached */
194 if (pEventInt->cWaiters > 0)
195 ASMAtomicDecU32(&pEventInt->cWaiters);
196 rc = VERR_TIMEOUT;
197 }
198 else
199 {
200 /* Returned due to pending signal */
201 if (pEventInt->cWaiters > 0)
202 ASMAtomicDecU32(&pEventInt->cWaiters);
203 rc = VERR_INTERRUPTED;
204 }
205 }
206
207 mutex_exit(&pEventInt->Mtx);
208 return rc;
209}
210
211/** @todo Implement RTSemEventWaitNoResume (interruptible variant of the uninterruptible RTSemEventWait()). */
212
Note: See TracBrowser for help on using the repository browser.

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