genericopenlibs/openenvcore/libpthread/src/sem_destroy.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_destroy.cpp
       
    15 // Part of  : PThread library
       
    16 // sem_destroy 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_destroy (sem_t * sem)
       
    30 {
       
    31     if (!sem || !(*sem))
       
    32     {
       
    33         errno = EINVAL;
       
    34         return -1;
       
    35     }
       
    36 
       
    37     _sem_t* lsem = *sem;
       
    38     
       
    39     if (_findAndFreeSem(lsem) == _SEM_NOT_FOUND_IN_LIST)
       
    40     {
       
    41         errno = EINVAL;
       
    42         return -1;    
       
    43     }    
       
    44     
       
    45     if(lsem->iState != _sem_t::EInitialized)
       
    46     {
       
    47         errno = EINVAL;
       
    48         return -1;
       
    49     }
       
    50 
       
    51     if(lsem->iCount <0)
       
    52     {
       
    53         // Add sem to sem-list
       
    54         _addToSemList(lsem);  
       
    55     
       
    56         errno = EBUSY;
       
    57         return -1;
       
    58     }
       
    59 
       
    60 #ifndef NOUSE_INTERNALS     
       
    61     void* tlsPtr;
       
    62 
       
    63     // Get the TLS pointer
       
    64     tlsPtr = _pthread_getTls();
       
    65     if(NULL == tlsPtr)
       
    66     {
       
    67         User::Panic(_L("pthread lib could not initailize"),0);
       
    68         errno = EBUSY;
       
    69         return -1;
       
    70     }
       
    71     _pthread_atomicMutexLock(tlsPtr);
       
    72 #endif
       
    73 
       
    74     if(lsem->iState == _sem_t::EDestroyed)
       
    75     {
       
    76 #ifndef NOUSE_INTERNALS     
       
    77         _pthread_atomicMutexUnlock(tlsPtr);
       
    78 #endif
       
    79         return 0;
       
    80     }
       
    81 
       
    82     lsem->iMutex.Wait();
       
    83     lsem->iState = _sem_t::EDestroyed;
       
    84     lsem->iSemaphore.Close();
       
    85     lsem->iMutex.Signal();
       
    86     lsem->iMutex.Close();  
       
    87 
       
    88     delete lsem; 
       
    89     *sem = NULL;
       
    90 
       
    91 #ifndef NOUSE_INTERNALS       
       
    92     _pthread_atomicMutexUnlock(tlsPtr);
       
    93 #endif  
       
    94     return 0;
       
    95 }//sem_destroy()
       
    96 
       
    97 //End of File
       
    98