VirtualBox

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

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

Solaris

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