VirtualBox

source: kStuff/trunk/kLdr/kLdrDyldSem.c@ 2

Last change on this file since 2 was 2, checked in by bird, 17 years ago

Imported http://svn.netlabs.org/repos/libc/trunk/kStuff, revision 3612.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.8 KB
Line 
1/* $Id: kLdrDyldSem.c 2 2007-11-16 16:07:14Z bird $ */
2/** @file
3 * kLdr - The Dynamic Loader, Semaphore Helper Functions.
4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <[email protected]>
8 *
9 * This file is part of kStuff.
10 *
11 * kStuff is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * In addition to the permissions in the GNU Lesser General Public
17 * License, you are granted unlimited permission to link the compiled
18 * version of this file into combinations with other programs, and to
19 * distribute those combinations without any restriction coming from
20 * the use of this file.
21 *
22 * kStuff is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with kStuff; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 * 02110-1301, USA
31 */
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include <k/kDefs.h>
37#include <k/kHlpSem.h>
38#include <k/kHlpAssert.h>
39
40#if K_OS == K_OS_DARWIN
41# include <mach/mach.h>
42
43#elif K_OS == K_OS_OS2
44# define INCL_BASE
45# define INCL_ERRORS
46# include <os2.h>
47
48#elif K_OS == K_OS_WINDOWS
49# include <Windows.h>
50
51#else
52# error "port me"
53#endif
54
55
56
57/*******************************************************************************
58* Global Variables *
59*******************************************************************************/
60#if K_OS == K_OS_DARWIN
61/** The loader sempahore. */
62static semaphore_t g_Semaphore = MACH_PORT_NULL;
63
64#elif K_OS == K_OS_OS2
65/** The loader sempahore. */
66static HMTX g_hmtx;
67
68#elif K_OS == K_OS_WINDOWS
69/** The loader sempahore. */
70static CRITICAL_SECTION g_CritSect;
71
72#else
73# error "port me"
74#endif
75
76
77/**
78 * Initializes the loader semaphore.
79 *
80 * @returns 0 on success, non-zero OS status code on failure.
81 */
82int kLdrDyldSemInit(void)
83{
84#if K_OS == K_OS_DARWIN
85 kern_return_t krc;
86
87 krc = semaphore_create(mach_task_self(), &g_Semaphore, SYNC_POLICY_FIFO, 0);
88 if (krc != KERN_SUCCESS)
89 return krc;
90
91#elif K_OS == K_OS_OS2
92 APIRET rc;
93 g_hmtx = NULLHANDLE;
94 rc = DosCreateMutexSem(NULL, &g_hmtx, 0, FALSE);
95 if (rc)
96 return rc;
97
98#elif K_OS == K_OS_WINDOWS
99 InitializeCriticalSection(&g_CritSect);
100
101#else
102# error "port me"
103#endif
104 return 0;
105}
106
107
108/**
109 * Terminates the loader semaphore.
110 */
111void kLdrDyldSemTerm(void)
112{
113#if K_OS == K_OS_DARWIN
114 kern_return_t krc;
115 semaphore_t Semaphore = g_Semaphore;
116 g_Semaphore = MACH_PORT_NULL;
117 krc = semaphore_destroy(mach_task_self(), Semaphore);
118 kHlpAssert(krc == KERN_SUCCESS); (void)krc;
119
120#elif K_OS == K_OS_OS2
121 HMTX hmtx = g_hmtx;
122 g_hmtx = NULLHANDLE;
123 DosCloseMutexSem(hmtx);
124
125#elif K_OS == K_OS_WINDOWS
126 DeleteCriticalSection(&g_CritSect);
127
128#else
129# error "port me"
130#endif
131}
132
133
134/**
135 * Requests the loader sempahore ownership.
136 * This can be done recursivly.
137 *
138 * @returns 0 on success, non-zero OS status code on failure.
139 */
140int kLdrDyldSemRequest(void)
141{
142#if K_OS == K_OS_DARWIN
143 /* not sure about this... */
144 kern_return_t krc;
145 do krc = semaphore_wait(g_Semaphore);
146 while (krc == KERN_ABORTED);
147 if (krc == KERN_SUCCESS)
148 return 0;
149 return krc;
150
151#elif K_OS == K_OS_OS2
152 APIRET rc = DosRequestMutexSem(g_hmtx, 5000);
153 if (rc == ERROR_TIMEOUT || rc == ERROR_SEM_TIMEOUT || rc == ERROR_INTERRUPT)
154 {
155 unsigned i = 0;
156 do
157 {
158 /** @todo check for deadlocks etc. */
159 rc = DosRequestMutexSem(g_hmtx, 1000);
160 } while ( ( rc == ERROR_TIMEOUT
161 || rc == ERROR_SEM_TIMEOUT
162 || rc == ERROR_INTERRUPT)
163 && i++ < 120);
164 }
165 return rc;
166
167#elif K_OS == K_OS_WINDOWS
168 EnterCriticalSection(&g_CritSect);
169 return 0;
170
171#else
172# error "port me"
173#endif
174}
175
176
177/**
178 * Releases the loader semaphore ownership.
179 * The caller is responsible for making sure it's the semaphore owner!
180 */
181void kLdrDyldSemRelease(void)
182{
183#if K_OS == K_OS_DARWIN
184 /* not too sure about this... */
185 kern_return_t krc = semaphore_signal(g_Semaphore);
186 kHlpAssert(krc == KERN_SUCCESS); (void)krc;
187
188#elif K_OS == K_OS_OS2
189 APIRET rc = DosReleaseMutexSem(g_hmtx);
190 kHlpAssert(!rc); (void)rc;
191
192#elif K_OS == K_OS_WINDOWS
193 LeaveCriticalSection(&g_CritSect);
194
195#else
196# error "port me"
197#endif
198}
199
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