genericopenlibs/openenvcore/libpthread/src/pthread_detach.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     : pthread_detach.cpp
       
    15 // Part of  : PThread library
       
    16 // pthread_detach call is implemented.
       
    17 // Version:
       
    18 //
       
    19 
       
    20 
       
    21 
       
    22 #include <pthread.h>
       
    23 #include <stdio.h>
       
    24 #include <errno.h>
       
    25 #include "threadglobals.h"
       
    26 #include "pthreadmisc.h"
       
    27 
       
    28 EXPORT_C int pthread_detach(pthread_t thrHandle)
       
    29 {
       
    30     _global_data_t *glbPtr;
       
    31     _pthread_node_t *tempPtr;
       
    32     _pthread_node_t *selfNodePtr;
       
    33     _pthread_node_t *thHandle;
       
    34         
       
    35     THR_PRINTF("[pthread] Begin pthread_detach\n");
       
    36     
       
    37     //Get the TLS value (self node pointer)
       
    38     selfNodePtr = (_pthread_node_t*) _pthread_getTls();
       
    39 	//coverity[var_compare_op]
       
    40     if (NULL == selfNodePtr)
       
    41     {
       
    42         THR_PRINTF("[pthread] FATAL :TLS is not initialized \n");
       
    43         THR_PRINTF("[pthread] Terminating the process\n");
       
    44         RProcess rp;
       
    45         rp.Kill(0);                // Terminate the process
       
    46     }
       
    47     
       
    48     thHandle = (_pthread_node_t *)thrHandle;
       
    49 
       
    50 	//coverity[var_deref_op]
       
    51    
       
    52     glbPtr = selfNodePtr->glbDataPtr;  // point to global struct
       
    53     
       
    54     glbPtr->lockThreadTable.Wait();    // Acquire the thread table lock
       
    55     
       
    56     // Traverse through the list, till destination node is reached
       
    57     for (tempPtr = glbPtr->start; 
       
    58          ((tempPtr != thHandle) && (tempPtr != NULL)); 
       
    59          tempPtr = tempPtr->next)
       
    60     {
       
    61         ;                 // Empty loop 
       
    62     }
       
    63     
       
    64     if (NULL == tempPtr)                    // Not found; 
       
    65     {
       
    66         THR_PRINTF("[pthread] End of pthread_detach");
       
    67         glbPtr->lockThreadTable.Signal();  //release global lock
       
    68         return ESRCH;
       
    69     }
       
    70     
       
    71     thHandle->lockNode.Wait(); // Acquire the node lock 
       
    72 
       
    73     if (PTHREAD_CREATE_DETACHED == thHandle->detachState) //already detached
       
    74     {
       
    75         thHandle->lockNode.Signal();      // release the node lock
       
    76         glbPtr->lockThreadTable.Signal(); // release global lock
       
    77         THR_PRINTF("[pthread] End of pthread_detach\n");
       
    78         return EINVAL;
       
    79     }
       
    80 
       
    81     if (thHandle->hasAnyThreadJoined)  // some thread has already joined
       
    82     {                                               
       
    83         thHandle->lockNode.Signal();      // release the node lock
       
    84         glbPtr->lockThreadTable.Signal(); // release global lock
       
    85         THR_PRINTF("[pthread] End of pthread_detach\n");
       
    86         return EINVAL;
       
    87     }
       
    88                               
       
    89     if (_THREAD_ZOMBIE == thHandle->threadState) // already terminated
       
    90     {
       
    91         _pthread_deleteNode(thHandle, glbPtr, NULL); //Free it
       
    92         
       
    93         THR_PRINTF("[pthread] End of pthread_detach\n");
       
    94         return 0;
       
    95     }
       
    96     
       
    97     glbPtr->lockThreadTable.Signal();  // release thread table lock
       
    98     
       
    99     // Now it is clear to detach....
       
   100     thHandle->detachState = PTHREAD_CREATE_DETACHED;
       
   101     thHandle->lockNode.Signal();      // release the node lock    
       
   102     
       
   103     THR_PRINTF("[pthread] End of pthread_detach\n");
       
   104     return 0;
       
   105     
       
   106 }
       
   107 
       
   108 // End of file