source: trunk/kStuff/kLdr/kLdrDyldSem.c

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

license update.

  • Property svn:keywords set to Id
File size: 5.0 KB
RevLine 
[2944]1/* $Id: kLdrDyldSem.c 3601 2007-10-29 00:21:13Z bird $ */
2/** @file
3 * kLdr - The Dynamic Loader, Semaphore Helper Functions.
[3601]4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
[2944]8 *
[3601]9 * This file is part of kStuff.
[2944]10 *
[3601]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.
[2944]15 *
[3601]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.
[2944]21 *
[3601]22 * kStuff is distributed in the hope that it will be useful,
[2944]23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
[3601]24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
[2944]26 *
[3601]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
[2944]31 */
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
[3573]36#include <k/kDefs.h>
37#include <k/kHlpSem.h>
[3598]38#include <k/kHlpAssert.h>
[3573]39
[3598]40#if K_OS == K_OS_DARWIN
41# include <mach/mach.h>
42
43#elif K_OS == K_OS_OS2
[2944]44# define INCL_BASE
45# define INCL_ERRORS
46# include <os2.h>
[3598]47
[3573]48#elif K_OS == K_OS_WINDOWS
[2944]49# include <Windows.h>
[3598]50
[2944]51#else
52# error "port me"
53#endif
54
55
56
57/*******************************************************************************
58* Global Variables *
59*******************************************************************************/
[3598]60#if K_OS == K_OS_DARWIN
[2944]61/** The loader sempahore. */
[3598]62static semaphore_t g_Semaphore = MACH_PORT_NULL;
63
64#elif K_OS == K_OS_OS2
65/** The loader sempahore. */
[2944]66static HMTX g_hmtx;
67
[3573]68#elif K_OS == K_OS_WINDOWS
[2944]69/** The loader sempahore. */
70static CRITICAL_SECTION g_CritSect;
[3598]71
[2944]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 */
[3598]82int kLdrDyldSemInit(void)
[2944]83{
[3598]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
[2944]92 APIRET rc;
93 g_hmtx = NULLHANDLE;
94 rc = DosCreateMutexSem(NULL, &g_hmtx, 0, FALSE);
95 if (rc)
96 return rc;
97
[3573]98#elif K_OS == K_OS_WINDOWS
[2944]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 */
[3598]111void kLdrDyldSemTerm(void)
[2944]112{
[3598]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
[2944]121 HMTX hmtx = g_hmtx;
122 g_hmtx = NULLHANDLE;
123 DosCloseMutexSem(hmtx);
124
[3573]125#elif K_OS == K_OS_WINDOWS
[2944]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 */
[3598]140int kLdrDyldSemRequest(void)
[2944]141{
[3598]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
[2944]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
[3573]167#elif K_OS == K_OS_WINDOWS
[2944]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 */
[3598]181void kLdrDyldSemRelease(void)
[2944]182{
[3598]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
[2944]189 APIRET rc = DosReleaseMutexSem(g_hmtx);
[3573]190 kHlpAssert(!rc); (void)rc;
[2944]191
[3573]192#elif K_OS == K_OS_WINDOWS
[2944]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.