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