pthread_detach.cpp revision 19e246dda6772ffc532b1762cd7870d6c3b01c12
19d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes/* 29d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * Copyright (C) 2008 The Android Open Source Project 39d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * All rights reserved. 49d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * 59d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * Redistribution and use in source and binary forms, with or without 69d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * modification, are permitted provided that the following conditions 79d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * are met: 89d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * * Redistributions of source code must retain the above copyright 99d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * notice, this list of conditions and the following disclaimer. 109d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * * Redistributions in binary form must reproduce the above copyright 119d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * notice, this list of conditions and the following disclaimer in 129d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * the documentation and/or other materials provided with the 139d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * distribution. 149d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * 159d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 169d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 179d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 189d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 199d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 209d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 219d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 229d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 239d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 249d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 259d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 269d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes * SUCH DAMAGE. 279d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes */ 289d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 299d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes#include <errno.h> 3019e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui#include <pthread.h> 319d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 329d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes#include "pthread_accessor.h" 339d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 349d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughesint pthread_detach(pthread_t t) { 3519e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui { 3619e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui pthread_accessor thread(t); 3719e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui if (thread.get() == NULL) { 389d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes return ESRCH; 3919e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui } 409d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4119e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) { 4219e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui return EINVAL; // Already detached. 4319e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui } 449d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4519e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui if (thread->attr.flags & PTHREAD_ATTR_FLAG_JOINED) { 4619e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui return 0; // Already being joined; silently do nothing, like glibc. 4719e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui } 489d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4919e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui // If the thread has not exited, we can detach it safely. 5019e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui if ((thread->attr.flags & PTHREAD_ATTR_FLAG_ZOMBIE) == 0) { 5119e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED; 5219e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui return 0; 5319e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui } 5404620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes } 5504620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes 5619e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui // The thread is in zombie state, use pthread_join to clean it up. 5719e246dda6772ffc532b1762cd7870d6c3b01c12Yabin Cui return pthread_join(t, NULL); 589d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes} 59