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

Last change on this file since 3598 was 3598, checked in by bird, 18 years ago

hacking darwin.

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