1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/*
* XMail by Davide Libenzi ( Intranet and Internet mail server )
* Copyright (C) 1999,..,2004 Davide Libenzi
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Davide Libenzi <davidel@xmailserver.org>
*
*/
#include "SysInclude.h"
#include "SysDep.h"
#include "SvrDefines.h"
#include "ShBlocks.h"
#include "ResLocks.h"
#include "BuffSock.h"
#include "MiscUtils.h"
#include "MessQueue.h"
#include "MailConfig.h"
#include "MailSvr.h"
#define STD_WAIT_GATES 37
#define STD_RES_HASH_SIZE 251
#define STD_RLCK_HASH_INIT 9677
struct ResWaitGate {
SYS_SEMAPHORE hSemaphore;
int iWaitingProcesses;
int iHashSize;
SysListHead *pResList;
};
struct ResLockEntry {
SysListHead LLink;
int iShLocks;
int iExLocks;
char szName[1];
};
struct ResLocator {
int iWaitGate;
int iResIdx;
};
static SYS_UINT32 RLckHashString(char const *pData, SYS_UINT32 uHashInit = STD_RLCK_HASH_INIT);
static void RLckGetResLocator(char const *pszResourceName, ResLocator * pRL);
static ResLockEntry *RLckGetEntry(ResLocator const *pRL, char const *pszResourceName);
static int RLckRemoveEntry(ResLocator const *pRL, ResLockEntry * pRLE);
static ResLockEntry *RLckAllocEntry(char const *pszResourceName);
static int RLckFreeEntry(ResLockEntry * pRLE);
static int RLckTryLockEX(ResLocator const *pRL, char const *pszResourceName);
static int RLckDoUnlockEX(ResLocator const *pRL, char const *pszResourceName);
static int RLckTryLockSH(ResLocator const *pRL, char const *pszResourceName);
static int RLckDoUnlockSH(ResLocator const *pRL, char const *pszResourceName);
static RLCK_HANDLE RLckLock(char const *pszResourceName,
int (*pLockProc) (ResLocator const *, char const *));
static int RLckUnlock(RLCK_HANDLE hLock, int (*pUnlockProc) (ResLocator const *, char const *));
static SYS_MUTEX hRLMutex = SYS_INVALID_MUTEX;
static ResWaitGate RLGates[STD_WAIT_GATES];
int RLckInitLockers(void)
{
/* Create resource locking mutex */
if ((hRLMutex = SysCreateMutex()) == SYS_INVALID_MUTEX)
return ErrGetErrorCode();
/* Initialize wait gates */
for (int ii = 0; ii < STD_WAIT_GATES; ii++) {
if ((RLGates[ii].hSemaphore = SysCreateSemaphore(0,
SYS_DEFAULT_MAXCOUNT)) ==
SYS_INVALID_SEMAPHORE) {
ErrorPush();
for (--ii; ii >= 0; ii--) {
SysFree(RLGates[ii].pResList);
SysCloseSemaphore(RLGates[ii].hSemaphore);
}
SysCloseMutex(hRLMutex);
return ErrorPop();
}
RLGates[ii].iWaitingProcesses = 0;
RLGates[ii].iHashSize = STD_RES_HASH_SIZE;
if ((RLGates[ii].pResList = (SysListHead *)
SysAlloc(RLGates[ii].iHashSize * sizeof(SysListHead))) == NULL) {
ErrorPush();
SysCloseSemaphore(RLGates[ii].hSemaphore);
for (--ii; ii >= 0; ii--) {
SysFree(RLGates[ii].pResList);
SysCloseSemaphore(RLGates[ii].hSemaphore);
}
SysCloseMutex(hRLMutex);
return ErrorPop();
}
for (int jj = 0; jj < RLGates[ii].iHashSize; jj++)
SYS_INIT_LIST_HEAD(&RLGates[ii].pResList[jj]);
}
return 0;
}
int RLckCleanupLockers(void)
{
SysLockMutex(hRLMutex, SYS_INFINITE_TIMEOUT);
for (int ii = 0; ii < STD_WAIT_GATES; ii++) {
for (int jj = 0; jj < RLGates[ii].iHashSize; jj++) {
SysListHead *pHead = &RLGates[ii].pResList[jj];
SysListHead *pLLink;
while ((pLLink = SYS_LIST_FIRST(pHead)) != NULL) {
ResLockEntry *pRLE = SYS_LIST_ENTRY(pLLink, ResLockEntry, LLink);
SYS_LIST_DEL(&pRLE->LLink);
RLckFreeEntry(pRLE);
}
}
SysFree(RLGates[ii].pResList);
SysCloseSemaphore(RLGates[ii].hSemaphore);
}
SysUnlockMutex(hRLMutex);
SysCloseMutex(hRLMutex);
return 0;
}
static SYS_UINT32 RLckHashString(char const *pData, SYS_UINT32 uHashInit)
{
SYS_UINT32 uHashVal = uHashInit;
while (*pData) {
uHashVal += (uHashVal << 5);
uHashVal ^= (SYS_UINT32) ToLower(*pData);
pData++;
}
return uHashVal;
}
static void RLckGetResLocator(char const *pszResourceName, ResLocator * pRL)
{
SYS_UINT32 uResHash = RLckHashString(pszResourceName);
pRL->iWaitGate = (int) (uResHash % STD_WAIT_GATES);
pRL->iResIdx = (int) (uResHash % (SYS_UINT32) RLGates[pRL->iWaitGate].iHashSize);
}
static ResLockEntry *RLckGetEntry(ResLocator const *pRL, char const *pszResourceName)
{
SysListHead *pHead = &RLGates[pRL->iWaitGate].pResList[pRL->iResIdx];
SysListHead *pLLink;
SYS_LIST_FOR_EACH(pLLink, pHead) {
ResLockEntry *pRLE = SYS_LIST_ENTRY(pLLink, ResLockEntry, LLink);
if (stricmp(pszResourceName, pRLE->szName) == 0)
return pRLE;
}
ErrSetErrorCode(ERR_LOCK_ENTRY_NOT_FOUND);
return NULL;
}
static int RLckRemoveEntry(ResLocator const *pRL, ResLockEntry * pRLE)
{
SysListHead *pHead = &RLGates[pRL->iWaitGate].pResList[pRL->iResIdx];
SysListHead *pLLink;
SYS_LIST_FOR_EACH(pLLink, pHead) {
ResLockEntry *pCurrRLE = SYS_LIST_ENTRY(pLLink, ResLockEntry, LLink);
if (pCurrRLE == pRLE) {
SYS_LIST_DEL(&pRLE->LLink);
RLckFreeEntry(pRLE);
return 0;
}
}
ErrSetErrorCode(ERR_LOCK_ENTRY_NOT_FOUND);
return ERR_LOCK_ENTRY_NOT_FOUND;
}
static ResLockEntry *RLckAllocEntry(char const *pszResourceName)
{
ResLockEntry *pRLE = (ResLockEntry *) SysAlloc(sizeof(ResLockEntry) +
strlen(pszResourceName));
if (pRLE == NULL)
return NULL;
SYS_INIT_LIST_LINK(&pRLE->LLink);
pRLE->iShLocks = 0;
pRLE->iExLocks = 0;
strcpy(pRLE->szName, pszResourceName);
return pRLE;
}
static int RLckFreeEntry(ResLockEntry * pRLE)
{
SysFree(pRLE);
return 0;
}
static int RLckTryLockEX(ResLocator const *pRL, char const *pszResourceName)
{
ResLockEntry *pRLE = RLckGetEntry(pRL, pszResourceName);
if (pRLE == NULL) {
SysListHead *pHead = &RLGates[pRL->iWaitGate].pResList[pRL->iResIdx];
if ((pRLE = RLckAllocEntry(pszResourceName)) == NULL)
return ErrGetErrorCode();
pRLE->iExLocks = 1;
/* Insert new entry in resource list */
SYS_LIST_ADDH(&pRLE->LLink, pHead);
} else {
if ((pRLE->iExLocks > 0) || (pRLE->iShLocks > 0)) {
ErrSetErrorCode(ERR_LOCKED_RESOURCE);
return ERR_LOCKED_RESOURCE;
}
pRLE->iExLocks = 1;
}
return 0;
}
static int RLckDoUnlockEX(ResLocator const *pRL, char const *pszResourceName)
{
ResLockEntry *pRLE = RLckGetEntry(pRL, pszResourceName);
if ((pRLE == NULL) || (pRLE->iExLocks == 0)) {
ErrSetErrorCode(ERR_RESOURCE_NOT_LOCKED);
return ERR_RESOURCE_NOT_LOCKED;
}
pRLE->iExLocks = 0;
/* Remove entry from list and delete entry memory */
if (RLckRemoveEntry(pRL, pRLE) < 0)
return ErrGetErrorCode();
/* Release waiting processes */
if (RLGates[pRL->iWaitGate].iWaitingProcesses > 0) {
SysReleaseSemaphore(RLGates[pRL->iWaitGate].hSemaphore,
RLGates[pRL->iWaitGate].iWaitingProcesses);
RLGates[pRL->iWaitGate].iWaitingProcesses = 0;
}
return 0;
}
static int RLckTryLockSH(ResLocator const *pRL, char const *pszResourceName)
{
ResLockEntry *pRLE = RLckGetEntry(pRL, pszResourceName);
if (pRLE == NULL) {
SysListHead *pHead = &RLGates[pRL->iWaitGate].pResList[pRL->iResIdx];
if ((pRLE = RLckAllocEntry(pszResourceName)) == NULL)
return ErrGetErrorCode();
pRLE->iShLocks = 1;
/* Insert new entry in resource list */
SYS_LIST_ADDH(&pRLE->LLink, pHead);
} else {
if (pRLE->iExLocks > 0) {
ErrSetErrorCode(ERR_LOCKED_RESOURCE);
return ERR_LOCKED_RESOURCE;
}
++pRLE->iShLocks;
}
return 0;
}
static int RLckDoUnlockSH(ResLocator const *pRL, char const *pszResourceName)
{
ResLockEntry *pRLE = RLckGetEntry(pRL, pszResourceName);
if ((pRLE == NULL) || (pRLE->iShLocks == 0)) {
ErrSetErrorCode(ERR_RESOURCE_NOT_LOCKED);
return ERR_RESOURCE_NOT_LOCKED;
}
if (--pRLE->iShLocks == 0) {
/* Remove entry from list and delete entry heap memory */
if (RLckRemoveEntry(pRL, pRLE) < 0)
return ErrGetErrorCode();
/* Release waiting processes */
if (RLGates[pRL->iWaitGate].iWaitingProcesses > 0) {
SysReleaseSemaphore(RLGates[pRL->iWaitGate].hSemaphore,
RLGates[pRL->iWaitGate].iWaitingProcesses);
RLGates[pRL->iWaitGate].iWaitingProcesses = 0;
}
}
return 0;
}
static RLCK_HANDLE RLckLock(char const *pszResourceName,
int (*pLockProc) (ResLocator const *, char const *))
{
ResLocator RL;
RLckGetResLocator(pszResourceName, &RL);
for (;;) {
/* Lock resources list access */
if (SysLockMutex(hRLMutex, SYS_INFINITE_TIMEOUT) < 0)
return INVALID_RLCK_HANDLE;
int iLockResult = pLockProc(&RL, pszResourceName);
if (iLockResult == ERR_LOCKED_RESOURCE) {
SYS_SEMAPHORE SemID = RLGates[RL.iWaitGate].hSemaphore;
++RLGates[RL.iWaitGate].iWaitingProcesses;
SysUnlockMutex(hRLMutex);
if (SysWaitSemaphore(SemID, SYS_INFINITE_TIMEOUT) < 0)
return INVALID_RLCK_HANDLE;
} else if (iLockResult == 0) {
SysUnlockMutex(hRLMutex);
break;
} else {
SysUnlockMutex(hRLMutex);
return INVALID_RLCK_HANDLE;
}
}
return (RLCK_HANDLE) SysStrDup(pszResourceName);
}
static int RLckUnlock(RLCK_HANDLE hLock, int (*pUnlockProc) (ResLocator const *, char const *))
{
char *pszResourceName = (char *) hLock;
ResLocator RL;
RLckGetResLocator(pszResourceName, &RL);
/* Lock resources list access */
if (SysLockMutex(hRLMutex, SYS_INFINITE_TIMEOUT) < 0)
return ErrGetErrorCode();
if (pUnlockProc(&RL, pszResourceName) < 0) {
ErrorPush();
SysUnlockMutex(hRLMutex);
SysFree(pszResourceName);
return ErrorPop();
}
SysUnlockMutex(hRLMutex);
SysFree(pszResourceName);
return 0;
}
RLCK_HANDLE RLckLockEX(char const *pszResourceName)
{
return RLckLock(pszResourceName, RLckTryLockEX);
}
int RLckUnlockEX(RLCK_HANDLE hLock)
{
return RLckUnlock(hLock, RLckDoUnlockEX);
}
RLCK_HANDLE RLckLockSH(char const *pszResourceName)
{
return RLckLock(pszResourceName, RLckTryLockSH);
}
int RLckUnlockSH(RLCK_HANDLE hLock)
{
return RLckUnlock(hLock, RLckDoUnlockSH);
}
|