genericopenlibs/openenvcore/libpthread/src/sem_microsleepwait_np.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_microsleepwait_np.cpp
       
    15 // Part of  : PThread library
       
    16 // sem_microsleepwait_np 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 
       
    30 int sem_microsleepwait_np (sem_t* sem, const long aSleepTime)
       
    31 {
       
    32     if (!sem || !(*sem) )
       
    33     {
       
    34         errno = EINVAL;
       
    35         return -1;
       
    36     }
       
    37 
       
    38     _sem_t* lsem = *sem;
       
    39     if(lsem->iState != _sem_t::EInitialized)
       
    40     {
       
    41         errno = EINVAL;
       
    42         return -1;
       
    43     }
       
    44 
       
    45     if(_cancelable_mutex_wait(lsem) != 0)
       
    46     {
       
    47         return -1;
       
    48     }
       
    49 
       
    50     int count = --(lsem->iCount);
       
    51     lsem->iMutex.Signal();
       
    52 
       
    53     int retval = 0;
       
    54     if(count <0)
       
    55     {
       
    56     //push to cleanup
       
    57 
       
    58     retval = lsem->iSemaphore.Wait(aSleepTime);
       
    59     if(retval != KErrNone)
       
    60     {
       
    61         //timeout
       
    62         //the semaphore count goes up
       
    63         lsem->iMutex.Wait();
       
    64         if(lsem->iState != _sem_t::EInitialized)
       
    65         {
       
    66             errno = EINVAL;
       
    67             return -1;
       
    68         }
       
    69         ++(lsem->iCount);
       
    70         lsem->iMutex.Signal();
       
    71         errno =  ETIMEDOUT;
       
    72         return -1;
       
    73     }
       
    74 
       
    75     //pop from cleanup
       
    76     }
       
    77     return 0;
       
    78 
       
    79 }//sem_microsleepwait_np()
       
    80 
       
    81 //End of File