Show
Ignore:
Timestamp:
09/27/10 17:25:14 (3 years ago)
Author:
sampson
Message:

Updated gen_locks

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/windows-client/src/common/gen-locks/gen-win-locks.c

    r8517 r8520  
    2020 
    2121#ifndef __GEN_NULL_LOCKING__ 
     22 
     23/* Global variables */ 
     24/* TODO: may need to init and delete in DLL enter/exit functions */ 
     25LPCRITICAL_SECTION cond_list_lock = NULL; 
     26 
     27pgen_cond_t cond_list_head = NULL; 
     28pgen_cond_t cond_list_tail = NULL; 
     29 
    2230/* 
    2331 * gen_mutex_init() 
     
    7987    HANDLE *mut) 
    8088{ 
    81     return (pthread_mutex_trylock(mut)); 
     89    DWORD dwWaitResult; 
     90    int rc; 
     91       
     92    dwWaitResult = WaitForSingleObject(*mut, 0); 
     93    if (dwWaitResult == WAIT_OBJECT_0 || dwWaitResult == WAIT_ABANDONED) 
     94    { 
     95        rc = 0; 
     96    } 
     97    else 
     98    { 
     99        rc = -1; 
     100        if (dwWaitResult == WAIT_TIMEOUT) 
     101        { 
     102            errno = EBUSY; 
     103        } 
     104        else 
     105        { 
     106            errno = GetLastError(); 
     107        } 
     108    } 
     109 
     110    return rc; 
    82111} 
    83112 
     
    95124    if (!mut || *mut == INVALID_HANDLE_VALUE) 
    96125    { 
    97         return (-EINVAL); 
    98     } 
     126        return (-EINVAL); 
     127    } 
     128     
    99129    CloseHandle(*mut); 
    100130 
     
    104134HANDLE gen_win_thread_self(void) 
    105135{ 
    106     return pthread_self(); 
     136    return GetCurrentThread(); 
    107137} 
    108138 
     
    138168} 
    139169 
    140 int gen_win_cond_init(HANDLE cond, void *attr) 
    141 { 
    142     return pthread_cond_init(cond, attr); 
     170int gen_win_cond_init(pgen_cond_t *cond) 
     171{ 
     172    int rc; 
     173    pgen_cond_t cv = NULL; 
     174 
     175    if (!cond) 
     176    { 
     177        return EINVAL; 
     178    } 
     179 
     180    /* Allocate condition variable */ 
     181    cv = (pgen_cond_t) calloc(1, sizeof(*cv)); 
     182    if (cv == NULL) 
     183    { 
     184        rc = ENOMEM; 
     185        goto DONE; 
     186    } 
     187 
     188    /* Create locking semaphore */ 
     189    cv->semBlockLock = CreateSemaphore(NULL, 1, LONG_MAX, NULL); 
     190    if (cv->semBlockLock == NULL) 
     191    { 
     192        rc = (int) GetLastError(); 
     193        goto FAIL0; 
     194    } 
     195 
     196    /* Create queue semaphore */ 
     197    cv->semBlockQueue = CreateSemaphore(NULL, 0, LONG_MAX, NULL); 
     198    if (cv->semBlockQueue == NULL)  
     199    { 
     200        rc = (int) GetLastError(); 
     201        goto FAIL1; 
     202    } 
     203 
     204    /* Create unblock/lock mutex */ 
     205    if ((rc = gen_mutex_init(&(cv->mtxUnblockLock))) != 0) 
     206    { 
     207        goto FAIL2; 
     208    } 
     209 
     210    rc = 0; 
     211 
     212    goto DONE; 
     213 
     214    /* 
     215     * Error conditions 
     216     */ 
     217FAIL2: 
     218    CloseHandle(cv->semBlockQueue); 
     219 
     220FAIL1: 
     221    CloseHandle(cv->semBlockLock); 
     222 
     223FAIL0: 
     224    free(cv); 
     225    cv = NULL; 
     226 
     227DONE: 
     228    if (rc == 0) 
     229    { 
     230        if (cond_list_lock == NULL) 
     231        { 
     232            InitializeCriticalSection(cond_list_lock); 
     233        } 
     234 
     235        EnterCriticalSection(cond_list_lock); 
     236 
     237        cv->next = NULL; 
     238        cv->prev = cond_list_tail; 
     239 
     240        if (cond_list_tail != NULL)  
     241        { 
     242            cond_list_tail->next = cv; 
     243        } 
     244 
     245        cond_list_tail = cv; 
     246 
     247        if (cond_list_head == NULL)  
     248        { 
     249            cond_list_head = cv; 
     250        } 
     251 
     252        LeaveCriticalSection(cond_list_lock); 
     253 
     254    } 
     255 
     256    *cond = cv; 
     257 
     258    return rc; 
    143259} 
    144260