genericopenlibs/openenvcore/libpthread/src/sem_wait.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name     : sem_wait.cpp
       
    15 // Part of  : PThread library
       
    16 // sem_wait call is implemented.
       
    17 // Version:
       
    18 //
       
    19 
       
    20 
       
    21 
       
    22 #include <e32def.h>
       
    23 #include <errno.h>
       
    24 
       
    25 #include "semaphore.h"
       
    26 #include "semaphoretypes.h"
       
    27 #include "pthread.h"
       
    28 
       
    29 EXPORT_C int sem_wait (sem_t * sem)
       
    30 {
       
    31     if (!sem || !(*sem))
       
    32     {
       
    33         errno = EINVAL;
       
    34         return -1;
       
    35     }
       
    36 
       
    37     _sem_t* lsem = *sem;
       
    38     if(lsem->iState != _sem_t::EInitialized)
       
    39     {
       
    40         errno = EINVAL;
       
    41         return -1;
       
    42     }
       
    43 
       
    44     if(_cancelable_mutex_wait(lsem) != 0)
       
    45     {
       
    46         return -1;
       
    47     }
       
    48 
       
    49     int prevCount = lsem->iCount;
       
    50     lsem->iCount--;    
       
    51     lsem->iMutex.Signal();
       
    52 
       
    53     if(prevCount <=0)
       
    54     {
       
    55         //pthread_cleanup_push();
       
    56         lsem->iSemaphore.Wait();
       
    57         //pthread_cleanup_pop();
       
    58     }
       
    59     return 0;
       
    60 }//sem_wait()
       
    61 
       
    62 // End of File
       
    63