/* $Id: kLdrHlpSem.c 2944 2007-01-13 15:55:40Z bird $ */ /** @file * * kLdr - The Dynamic Loader, Semaphore Helper Functions. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLdr. * * kLdr is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * kLdr is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with kLdr; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /******************************************************************************* * Header Files * *******************************************************************************/ #ifdef __OS2__ # define INCL_BASE # define INCL_ERRORS # include #elif defined(__WIN__) # include #else # error "port me" #endif #include #include "kLdrHlp.h" /******************************************************************************* * Global Variables * *******************************************************************************/ #ifdef __OS2__ /** The loader sempahore. */ static HMTX g_hmtx; #elif defined(__WIN__) /** The loader sempahore. */ static CRITICAL_SECTION g_CritSect; #else # error "port me" #endif /** * Initializes the loader semaphore. * * @returns 0 on success, non-zero OS status code on failure. */ int kldrHlpSemInit(void) { #ifdef __OS2__ APIRET rc; g_hmtx = NULLHANDLE; rc = DosCreateMutexSem(NULL, &g_hmtx, 0, FALSE); if (rc) return rc; #elif defined(__WIN__) InitializeCriticalSection(&g_CritSect); #else # error "port me" #endif return 0; } /** * Terminates the loader semaphore. */ void kldrHlpSemTerm(void) { #ifdef __OS2__ HMTX hmtx = g_hmtx; g_hmtx = NULLHANDLE; DosCloseMutexSem(hmtx); #elif defined(__WIN__) DeleteCriticalSection(&g_CritSect); #else # error "port me" #endif } /** * Requests the loader sempahore ownership. * This can be done recursivly. * * @returns 0 on success, non-zero OS status code on failure. */ int kldrHlpSemRequest(void) { #ifdef __OS2__ APIRET rc = DosRequestMutexSem(g_hmtx, 5000); if (rc == ERROR_TIMEOUT || rc == ERROR_SEM_TIMEOUT || rc == ERROR_INTERRUPT) { unsigned i = 0; do { /** @todo check for deadlocks etc. */ rc = DosRequestMutexSem(g_hmtx, 1000); } while ( ( rc == ERROR_TIMEOUT || rc == ERROR_SEM_TIMEOUT || rc == ERROR_INTERRUPT) && i++ < 120); } return rc; #elif defined(__WIN__) EnterCriticalSection(&g_CritSect); return 0; #else # error "port me" #endif } /** * Releases the loader semaphore ownership. * The caller is responsible for making sure it's the semaphore owner! */ void kldrHlpSemRelease(void) { #ifdef __OS2__ APIRET rc = DosReleaseMutexSem(g_hmtx); kldrHlpAssert(!rc); (void)rc; #elif defined(__WIN__) LeaveCriticalSection(&g_CritSect); #else # error "port me" #endif }