[1] | 1 | /* |
---|
| 2 | * kern/sys_thread_detach.c - detach a joinable thread |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless |
---|
| 5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
---|
| 6 | * |
---|
| 7 | * This file is part of ALMOS-kernel. |
---|
| 8 | * |
---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
---|
| 10 | * under the terms of the GNU General Public License as published by |
---|
| 11 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 12 | * |
---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 16 | * General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #include <list.h> |
---|
| 24 | #include <thread.h> |
---|
| 25 | #include <kmem.h> |
---|
| 26 | #include <kmagics.h> |
---|
| 27 | #include <errno.h> |
---|
| 28 | #include <task.h> |
---|
| 29 | #include <spinlock.h> |
---|
| 30 | |
---|
| 31 | int sys_thread_detach (pthread_t tid) |
---|
| 32 | { |
---|
| 33 | register struct task_s *task; |
---|
| 34 | struct thread_s *target_th; |
---|
| 35 | uint_t state; |
---|
| 36 | error_t err; |
---|
| 37 | |
---|
| 38 | task = current_task; |
---|
| 39 | |
---|
| 40 | if(tid > task->max_order) |
---|
| 41 | { |
---|
| 42 | err = EINVAL; |
---|
| 43 | goto fail_arg; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | spinlock_lock(&task->th_lock); |
---|
| 47 | |
---|
| 48 | target_th = task->th_tbl[tid]; |
---|
| 49 | |
---|
| 50 | if((target_th == NULL) || |
---|
| 51 | (target_th->signature != THREAD_ID) || |
---|
| 52 | (target_th->info.attr.key != tid)) |
---|
| 53 | { |
---|
| 54 | err = ESRCH; |
---|
| 55 | goto fail_srch; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | if(!(thread_isJoinable(target_th))) |
---|
| 59 | { |
---|
| 60 | err = EINVAL; |
---|
| 61 | goto fail_not_joinable; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | err = 0; |
---|
| 65 | |
---|
| 66 | spinlock_lock(&target_th->lock); |
---|
| 67 | |
---|
| 68 | thread_clear_joinable(target_th); |
---|
| 69 | |
---|
| 70 | if((target_th->info.join == NULL) && |
---|
| 71 | !(state = wait_queue_isEmpty(&target_th->info.wait_queue))) |
---|
| 72 | { |
---|
| 73 | wakeup_one(&target_th->info.wait_queue, WAIT_ANY); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | spinlock_unlock(&target_th->lock); |
---|
| 77 | |
---|
| 78 | fail_not_joinable: |
---|
| 79 | fail_srch: |
---|
| 80 | spinlock_unlock(&task->lock); |
---|
| 81 | |
---|
| 82 | fail_arg: |
---|
| 83 | current_thread->info.errno = err; |
---|
| 84 | return err; |
---|
| 85 | } |
---|