1 | /* $Id: kLdrDyldSem.c 24 2009-02-08 13:58:54Z 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 | # undef mach_task_self /* don't use the macro (if we're using bare helpers ) */
|
---|
43 |
|
---|
44 | #elif K_OS == K_OS_OS2
|
---|
45 | # define INCL_BASE
|
---|
46 | # define INCL_ERRORS
|
---|
47 | # include <os2.h>
|
---|
48 |
|
---|
49 | #elif K_OS == K_OS_WINDOWS
|
---|
50 | # include <Windows.h>
|
---|
51 |
|
---|
52 | #else
|
---|
53 | # error "port me"
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | /*******************************************************************************
|
---|
59 | * Global Variables *
|
---|
60 | *******************************************************************************/
|
---|
61 | #if K_OS == K_OS_DARWIN
|
---|
62 | /** The loader sempahore. */
|
---|
63 | static semaphore_t g_Semaphore = MACH_PORT_NULL;
|
---|
64 |
|
---|
65 | #elif K_OS == K_OS_OS2
|
---|
66 | /** The loader sempahore. */
|
---|
67 | static HMTX g_hmtx;
|
---|
68 |
|
---|
69 | #elif K_OS == K_OS_WINDOWS
|
---|
70 | /** The loader sempahore. */
|
---|
71 | static CRITICAL_SECTION g_CritSect;
|
---|
72 |
|
---|
73 | #else
|
---|
74 | # error "port me"
|
---|
75 | #endif
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Initializes the loader semaphore.
|
---|
80 | *
|
---|
81 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
82 | */
|
---|
83 | int kLdrDyldSemInit(void)
|
---|
84 | {
|
---|
85 | #if K_OS == K_OS_DARWIN
|
---|
86 | kern_return_t krc;
|
---|
87 |
|
---|
88 | krc = semaphore_create(mach_task_self(), &g_Semaphore, SYNC_POLICY_FIFO, 0);
|
---|
89 | if (krc != KERN_SUCCESS)
|
---|
90 | return krc;
|
---|
91 |
|
---|
92 | #elif K_OS == K_OS_OS2
|
---|
93 | APIRET rc;
|
---|
94 | g_hmtx = NULLHANDLE;
|
---|
95 | rc = DosCreateMutexSem(NULL, &g_hmtx, 0, FALSE);
|
---|
96 | if (rc)
|
---|
97 | return rc;
|
---|
98 |
|
---|
99 | #elif K_OS == K_OS_WINDOWS
|
---|
100 | InitializeCriticalSection(&g_CritSect);
|
---|
101 |
|
---|
102 | #else
|
---|
103 | # error "port me"
|
---|
104 | #endif
|
---|
105 | return 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Terminates the loader semaphore.
|
---|
111 | */
|
---|
112 | void kLdrDyldSemTerm(void)
|
---|
113 | {
|
---|
114 | #if K_OS == K_OS_DARWIN
|
---|
115 | kern_return_t krc;
|
---|
116 | semaphore_t Semaphore = g_Semaphore;
|
---|
117 | g_Semaphore = MACH_PORT_NULL;
|
---|
118 | krc = semaphore_destroy(mach_task_self(), Semaphore);
|
---|
119 | kHlpAssert(krc == KERN_SUCCESS); (void)krc;
|
---|
120 |
|
---|
121 | #elif K_OS == K_OS_OS2
|
---|
122 | HMTX hmtx = g_hmtx;
|
---|
123 | g_hmtx = NULLHANDLE;
|
---|
124 | DosCloseMutexSem(hmtx);
|
---|
125 |
|
---|
126 | #elif K_OS == K_OS_WINDOWS
|
---|
127 | DeleteCriticalSection(&g_CritSect);
|
---|
128 |
|
---|
129 | #else
|
---|
130 | # error "port me"
|
---|
131 | #endif
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Requests the loader sempahore ownership.
|
---|
137 | * This can be done recursivly.
|
---|
138 | *
|
---|
139 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
140 | */
|
---|
141 | int kLdrDyldSemRequest(void)
|
---|
142 | {
|
---|
143 | #if K_OS == K_OS_DARWIN
|
---|
144 | /* not sure about this... */
|
---|
145 | kern_return_t krc;
|
---|
146 | do krc = semaphore_wait(g_Semaphore);
|
---|
147 | while (krc == KERN_ABORTED);
|
---|
148 | if (krc == KERN_SUCCESS)
|
---|
149 | return 0;
|
---|
150 | return krc;
|
---|
151 |
|
---|
152 | #elif K_OS == K_OS_OS2
|
---|
153 | APIRET rc = DosRequestMutexSem(g_hmtx, 5000);
|
---|
154 | if (rc == ERROR_TIMEOUT || rc == ERROR_SEM_TIMEOUT || rc == ERROR_INTERRUPT)
|
---|
155 | {
|
---|
156 | unsigned i = 0;
|
---|
157 | do
|
---|
158 | {
|
---|
159 | /** @todo check for deadlocks etc. */
|
---|
160 | rc = DosRequestMutexSem(g_hmtx, 1000);
|
---|
161 | } while ( ( rc == ERROR_TIMEOUT
|
---|
162 | || rc == ERROR_SEM_TIMEOUT
|
---|
163 | || rc == ERROR_INTERRUPT)
|
---|
164 | && i++ < 120);
|
---|
165 | }
|
---|
166 | return rc;
|
---|
167 |
|
---|
168 | #elif K_OS == K_OS_WINDOWS
|
---|
169 | EnterCriticalSection(&g_CritSect);
|
---|
170 | return 0;
|
---|
171 |
|
---|
172 | #else
|
---|
173 | # error "port me"
|
---|
174 | #endif
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Releases the loader semaphore ownership.
|
---|
180 | * The caller is responsible for making sure it's the semaphore owner!
|
---|
181 | */
|
---|
182 | void kLdrDyldSemRelease(void)
|
---|
183 | {
|
---|
184 | #if K_OS == K_OS_DARWIN
|
---|
185 | /* not too sure about this... */
|
---|
186 | kern_return_t krc = semaphore_signal(g_Semaphore);
|
---|
187 | kHlpAssert(krc == KERN_SUCCESS); (void)krc;
|
---|
188 |
|
---|
189 | #elif K_OS == K_OS_OS2
|
---|
190 | APIRET rc = DosReleaseMutexSem(g_hmtx);
|
---|
191 | kHlpAssert(!rc); (void)rc;
|
---|
192 |
|
---|
193 | #elif K_OS == K_OS_WINDOWS
|
---|
194 | LeaveCriticalSection(&g_CritSect);
|
---|
195 |
|
---|
196 | #else
|
---|
197 | # error "port me"
|
---|
198 | #endif
|
---|
199 | }
|
---|
200 |
|
---|