1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include "system.h" 29 30 #include <osl/diagnose.h> 31 #include <osl/semaphor.h> 32 33 /* 34 Implemetation notes: 35 The void* represented by oslSemaphore is used 36 to store a OS/2 HANDLE. 37 */ 38 39 typedef struct _oslSemaphoreImpl 40 { 41 HEV hevReachedZero; 42 int nCount; 43 } oslSemaphoreImpl; 44 45 // static mutex to control access to private members of oslMutexImpl 46 static HMTX MutexLock = NULL; 47 48 /*****************************************************************************/ 49 /* osl_createSemaphore */ 50 /*****************************************************************************/ 51 52 /* 53 - Erzeugen der Semaphore 54 - Z�hler auf initialCount setzen 55 */ 56 oslSemaphore SAL_CALL osl_createSemaphore(sal_uInt32 initialCount) 57 { 58 APIRET rc; 59 oslSemaphoreImpl * pSemaphoreImpl; 60 61 /* alloc mem. for our internal data structure */ 62 pSemaphoreImpl = (oslSemaphoreImpl *) malloc(sizeof(oslSemaphoreImpl)); 63 if( pSemaphoreImpl == NULL ) 64 return NULL; 65 66 /* create semaphore */ 67 rc = DosCreateEventSem( NULL, 68 &pSemaphoreImpl->hevReachedZero, 69 DC_SEM_SHARED, 70 FALSE ); 71 if( rc != NO_ERROR ) 72 { 73 free( pSemaphoreImpl ); 74 return NULL; 75 } 76 77 pSemaphoreImpl->nCount = initialCount; 78 79 // create static mutex for private members 80 if (MutexLock == NULL) 81 DosCreateMutexSem( NULL, &MutexLock, 0, FALSE ); 82 83 return (oslSemaphore) pSemaphoreImpl; 84 } 85 86 /*****************************************************************************/ 87 /* osl_destroySemaphore */ 88 /*****************************************************************************/ 89 90 /* 91 - Semaphore l�schen 92 */ 93 94 void SAL_CALL osl_destroySemaphore(oslSemaphore Semaphore) 95 { 96 oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore; 97 OSL_ASSERT(Semaphore != 0); 98 99 DosCloseEventSem( pSemaphoreImpl->hevReachedZero ); 100 101 free( pSemaphoreImpl ); 102 } 103 104 /*****************************************************************************/ 105 /* osl_acquireSemaphore */ 106 /*****************************************************************************/ 107 /* 108 - Z�hler -1 109 - wenn Z�hler < 0: blockieren 110 */ 111 112 sal_Bool SAL_CALL osl_acquireSemaphore(oslSemaphore Semaphore) 113 { 114 APIRET rc; 115 oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore; 116 int nCount; 117 OSL_ASSERT(Semaphore != 0); 118 119 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT ); 120 121 while( pSemaphoreImpl->nCount < 1 ) 122 { 123 sal_uInt32 nPostCount; 124 125 DosReleaseMutexSem( MutexLock); 126 127 rc = DosWaitEventSem(pSemaphoreImpl->hevReachedZero, SEM_INDEFINITE_WAIT ); 128 DosResetEventSem(pSemaphoreImpl->hevReachedZero, &nPostCount); 129 130 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT ); 131 } 132 133 pSemaphoreImpl->nCount--; 134 DosReleaseMutexSem( MutexLock); 135 136 return( rc == NO_ERROR ); 137 } 138 139 /*****************************************************************************/ 140 /* osl_tryToAcquireSemaphore */ 141 /*****************************************************************************/ 142 /* 143 - Z�hler -1, wenn vorher > 0 144 - wenn Z�hler < 0: mit FALSE zurueck 145 */ 146 sal_Bool SAL_CALL osl_tryToAcquireSemaphore(oslSemaphore Semaphore) 147 { 148 APIRET rc; 149 oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore; 150 int nCount; 151 OSL_ASSERT(Semaphore != 0); 152 153 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT ); 154 155 nCount = pSemaphoreImpl->nCount; 156 if( pSemaphoreImpl->nCount > 0 ) 157 pSemaphoreImpl->nCount--; 158 159 DosReleaseMutexSem( MutexLock); 160 161 return( nCount > 0 ); 162 } 163 164 /*****************************************************************************/ 165 /* osl_releaseSemaphore */ 166 /*****************************************************************************/ 167 /* 168 - Z�hler +1 169 */ 170 sal_Bool SAL_CALL osl_releaseSemaphore(oslSemaphore Semaphore) 171 { 172 APIRET rc; 173 oslSemaphoreImpl* pSemaphoreImpl = (oslSemaphoreImpl*)Semaphore; 174 int nCount; 175 OSL_ASSERT(Semaphore != 0); 176 177 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT ); 178 179 nCount = pSemaphoreImpl->nCount; 180 pSemaphoreImpl->nCount++; 181 182 DosReleaseMutexSem( MutexLock); 183 184 if( nCount == 0 ) 185 DosPostEventSem(pSemaphoreImpl->hevReachedZero); 186 187 return( rc == NO_ERROR ); 188 } 189 190 191