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

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

kLdrDyldSem

  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1/* $Id: kLdrDyldSem.c 3577 2007-09-02 20:24:36Z 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
34#if K_OS == K_OS_OS2
35# define INCL_BASE
36# define INCL_ERRORS
37# include <os2.h>
38#elif K_OS == K_OS_WINDOWS
39# include <Windows.h>
40#else
41# error "port me"
42#endif
43
44
45
46/*******************************************************************************
47* Global Variables *
48*******************************************************************************/
49#if K_OS == K_OS_OS2
50/** The loader sempahore. */
51static HMTX g_hmtx;
52
53#elif K_OS == K_OS_WINDOWS
54/** The loader sempahore. */
55static CRITICAL_SECTION g_CritSect;
56#else
57# error "port me"
58#endif
59
60
61/**
62 * Initializes the loader semaphore.
63 *
64 * @returns 0 on success, non-zero OS status code on failure.
65 */
66int kLdrDyldSemInit(void)
67{
68#if K_OS == K_OS_OS2
69 APIRET rc;
70 g_hmtx = NULLHANDLE;
71 rc = DosCreateMutexSem(NULL, &g_hmtx, 0, FALSE);
72 if (rc)
73 return rc;
74
75#elif K_OS == K_OS_WINDOWS
76 InitializeCriticalSection(&g_CritSect);
77
78#else
79# error "port me"
80#endif
81 return 0;
82}
83
84
85/**
86 * Terminates the loader semaphore.
87 */
88void kLdrDyldSemTerm(void)
89{
90#if K_OS == K_OS_OS2
91 HMTX hmtx = g_hmtx;
92 g_hmtx = NULLHANDLE;
93 DosCloseMutexSem(hmtx);
94
95#elif K_OS == K_OS_WINDOWS
96 DeleteCriticalSection(&g_CritSect);
97
98#else
99# error "port me"
100#endif
101}
102
103
104/**
105 * Requests the loader sempahore ownership.
106 * This can be done recursivly.
107 *
108 * @returns 0 on success, non-zero OS status code on failure.
109 */
110int kLdrDyldSemRequest(void)
111{
112#if K_OS == K_OS_OS2
113 APIRET rc = DosRequestMutexSem(g_hmtx, 5000);
114 if (rc == ERROR_TIMEOUT || rc == ERROR_SEM_TIMEOUT || rc == ERROR_INTERRUPT)
115 {
116 unsigned i = 0;
117 do
118 {
119 /** @todo check for deadlocks etc. */
120 rc = DosRequestMutexSem(g_hmtx, 1000);
121 } while ( ( rc == ERROR_TIMEOUT
122 || rc == ERROR_SEM_TIMEOUT
123 || rc == ERROR_INTERRUPT)
124 && i++ < 120);
125 }
126 return rc;
127
128#elif K_OS == K_OS_WINDOWS
129 EnterCriticalSection(&g_CritSect);
130 return 0;
131
132#else
133# error "port me"
134#endif
135}
136
137
138/**
139 * Releases the loader semaphore ownership.
140 * The caller is responsible for making sure it's the semaphore owner!
141 */
142void kLdrDyldSemRelease(void)
143{
144#if K_OS == K_OS_OS2
145 APIRET rc = DosReleaseMutexSem(g_hmtx);
146 kHlpAssert(!rc); (void)rc;
147
148#elif K_OS == K_OS_WINDOWS
149 LeaveCriticalSection(&g_CritSect);
150
151#else
152# error "port me"
153#endif
154
155}
156
Note: See TracBrowser for help on using the repository browser.