1 | /* $Id: kLdrDyldSem.c 29 2009-07-01 20:30:29Z 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 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <k/kDefs.h>
|
---|
35 | #include <k/kHlpSem.h>
|
---|
36 | #include <k/kHlpAssert.h>
|
---|
37 |
|
---|
38 | #if K_OS == K_OS_DARWIN
|
---|
39 | # include <mach/mach.h>
|
---|
40 | # undef mach_task_self /* don't use the macro (if we're using bare helpers ) */
|
---|
41 |
|
---|
42 | #elif K_OS == K_OS_OS2
|
---|
43 | # define INCL_BASE
|
---|
44 | # define INCL_ERRORS
|
---|
45 | # include <os2.h>
|
---|
46 |
|
---|
47 | #elif K_OS == K_OS_WINDOWS
|
---|
48 | # include <Windows.h>
|
---|
49 |
|
---|
50 | #else
|
---|
51 | # error "port me"
|
---|
52 | #endif
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 | /*******************************************************************************
|
---|
57 | * Global Variables *
|
---|
58 | *******************************************************************************/
|
---|
59 | #if K_OS == K_OS_DARWIN
|
---|
60 | /** The loader sempahore. */
|
---|
61 | static semaphore_t g_Semaphore = MACH_PORT_NULL;
|
---|
62 |
|
---|
63 | #elif K_OS == K_OS_OS2
|
---|
64 | /** The loader sempahore. */
|
---|
65 | static HMTX g_hmtx;
|
---|
66 |
|
---|
67 | #elif K_OS == K_OS_WINDOWS
|
---|
68 | /** The loader sempahore. */
|
---|
69 | static CRITICAL_SECTION g_CritSect;
|
---|
70 |
|
---|
71 | #else
|
---|
72 | # error "port me"
|
---|
73 | #endif
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Initializes the loader semaphore.
|
---|
78 | *
|
---|
79 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
80 | */
|
---|
81 | int kLdrDyldSemInit(void)
|
---|
82 | {
|
---|
83 | #if K_OS == K_OS_DARWIN
|
---|
84 | kern_return_t krc;
|
---|
85 |
|
---|
86 | krc = semaphore_create(mach_task_self(), &g_Semaphore, SYNC_POLICY_FIFO, 0);
|
---|
87 | if (krc != KERN_SUCCESS)
|
---|
88 | return krc;
|
---|
89 |
|
---|
90 | #elif K_OS == K_OS_OS2
|
---|
91 | APIRET rc;
|
---|
92 | g_hmtx = NULLHANDLE;
|
---|
93 | rc = DosCreateMutexSem(NULL, &g_hmtx, 0, FALSE);
|
---|
94 | if (rc)
|
---|
95 | return rc;
|
---|
96 |
|
---|
97 | #elif K_OS == K_OS_WINDOWS
|
---|
98 | InitializeCriticalSection(&g_CritSect);
|
---|
99 |
|
---|
100 | #else
|
---|
101 | # error "port me"
|
---|
102 | #endif
|
---|
103 | return 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Terminates the loader semaphore.
|
---|
109 | */
|
---|
110 | void kLdrDyldSemTerm(void)
|
---|
111 | {
|
---|
112 | #if K_OS == K_OS_DARWIN
|
---|
113 | kern_return_t krc;
|
---|
114 | semaphore_t Semaphore = g_Semaphore;
|
---|
115 | g_Semaphore = MACH_PORT_NULL;
|
---|
116 | krc = semaphore_destroy(mach_task_self(), Semaphore);
|
---|
117 | kHlpAssert(krc == KERN_SUCCESS); (void)krc;
|
---|
118 |
|
---|
119 | #elif K_OS == K_OS_OS2
|
---|
120 | HMTX hmtx = g_hmtx;
|
---|
121 | g_hmtx = NULLHANDLE;
|
---|
122 | DosCloseMutexSem(hmtx);
|
---|
123 |
|
---|
124 | #elif K_OS == K_OS_WINDOWS
|
---|
125 | DeleteCriticalSection(&g_CritSect);
|
---|
126 |
|
---|
127 | #else
|
---|
128 | # error "port me"
|
---|
129 | #endif
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Requests the loader sempahore ownership.
|
---|
135 | * This can be done recursivly.
|
---|
136 | *
|
---|
137 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
138 | */
|
---|
139 | int kLdrDyldSemRequest(void)
|
---|
140 | {
|
---|
141 | #if K_OS == K_OS_DARWIN
|
---|
142 | /* not sure about this... */
|
---|
143 | kern_return_t krc;
|
---|
144 | do krc = semaphore_wait(g_Semaphore);
|
---|
145 | while (krc == KERN_ABORTED);
|
---|
146 | if (krc == KERN_SUCCESS)
|
---|
147 | return 0;
|
---|
148 | return krc;
|
---|
149 |
|
---|
150 | #elif K_OS == K_OS_OS2
|
---|
151 | APIRET rc = DosRequestMutexSem(g_hmtx, 5000);
|
---|
152 | if (rc == ERROR_TIMEOUT || rc == ERROR_SEM_TIMEOUT || rc == ERROR_INTERRUPT)
|
---|
153 | {
|
---|
154 | unsigned i = 0;
|
---|
155 | do
|
---|
156 | {
|
---|
157 | /** @todo check for deadlocks etc. */
|
---|
158 | rc = DosRequestMutexSem(g_hmtx, 1000);
|
---|
159 | } while ( ( rc == ERROR_TIMEOUT
|
---|
160 | || rc == ERROR_SEM_TIMEOUT
|
---|
161 | || rc == ERROR_INTERRUPT)
|
---|
162 | && i++ < 120);
|
---|
163 | }
|
---|
164 | return rc;
|
---|
165 |
|
---|
166 | #elif K_OS == K_OS_WINDOWS
|
---|
167 | EnterCriticalSection(&g_CritSect);
|
---|
168 | return 0;
|
---|
169 |
|
---|
170 | #else
|
---|
171 | # error "port me"
|
---|
172 | #endif
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Releases the loader semaphore ownership.
|
---|
178 | * The caller is responsible for making sure it's the semaphore owner!
|
---|
179 | */
|
---|
180 | void kLdrDyldSemRelease(void)
|
---|
181 | {
|
---|
182 | #if K_OS == K_OS_DARWIN
|
---|
183 | /* not too sure about this... */
|
---|
184 | kern_return_t krc = semaphore_signal(g_Semaphore);
|
---|
185 | kHlpAssert(krc == KERN_SUCCESS); (void)krc;
|
---|
186 |
|
---|
187 | #elif K_OS == K_OS_OS2
|
---|
188 | APIRET rc = DosReleaseMutexSem(g_hmtx);
|
---|
189 | kHlpAssert(!rc); (void)rc;
|
---|
190 |
|
---|
191 | #elif K_OS == K_OS_WINDOWS
|
---|
192 | LeaveCriticalSection(&g_CritSect);
|
---|
193 |
|
---|
194 | #else
|
---|
195 | # error "port me"
|
---|
196 | #endif
|
---|
197 | }
|
---|
198 |
|
---|