~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mit-pthreads/pthreads/pthread_detach.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ==== pthread_detach.c =======================================================
 
2
 * Copyright (c) 1994 by Chris Provenzano, proven@mit.edu
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 * 1. Redistributions of source code must retain the above copyright
 
9
 *    notice, this list of conditions and the following disclaimer.
 
10
 * 2. Redistributions in binary form must reproduce the above copyright
 
11
 *    notice, this list of conditions and the following disclaimer in the
 
12
 *    documentation and/or other materials provided with the distribution.
 
13
 * 3. All advertising materials mentioning features or use of this software
 
14
 *    must display the following acknowledgement:
 
15
 *  This product includes software developed by Chris Provenzano.
 
16
 * 4. The name of Chris Provenzano may not be used to endorse or promote 
 
17
 *        products derived from this software without specific prior written
 
18
 *        permission.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO ``AS IS'' AND
 
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO BE LIABLE FOR ANY 
 
24
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
25
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 
26
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
27
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 
30
 * SUCH DAMAGE.
 
31
 *
 
32
 * Description : pthread_join function.
 
33
 *
 
34
 *  1.00 94/01/15 proven
 
35
 *      -Started coding this file.
 
36
 */
 
37
 
 
38
#ifndef lint
 
39
static const char rcsid[] = "$Id$";
 
40
#endif
 
41
 
 
42
#include <errno.h>
 
43
#include <pthread.h>
 
44
 
 
45
/* ==========================================================================
 
46
 * pthread_detach()
 
47
 */
 
48
int pthread_detach(pthread_t pthread)
 
49
{
 
50
        struct pthread * next_thread, * high_thread, * low_thread;
 
51
        int ret;
 
52
 
 
53
        pthread_sched_prevent();
 
54
 
 
55
        /* Check that thread isn't detached already */
 
56
        if (!(pthread->attr.flags & PTHREAD_DETACHED)) {
 
57
 
 
58
                pthread->attr.flags |= PTHREAD_DETACHED;
 
59
 
 
60
                /* Wakeup all threads waiting on a join */
 
61
                if (next_thread = pthread_queue_deq(&(pthread->join_queue))) {
 
62
                        high_thread = next_thread;
 
63
                        
 
64
                        while (next_thread = pthread_queue_deq(&(pthread->join_queue))) {
 
65
                                if (high_thread->pthread_priority < next_thread->pthread_priority) {
 
66
                                        low_thread = high_thread;
 
67
                                        high_thread = next_thread;
 
68
                                } else {
 
69
                                        low_thread = next_thread;
 
70
                                }
 
71
                                pthread_prio_queue_enq(pthread_current_prio_queue, low_thread);
 
72
                                low_thread->state = PS_RUNNING;
 
73
                        }
 
74
                        /* If the thread is dead then move it to the alloc queue */
 
75
                        if (pthread_queue_remove(&pthread_dead_queue, pthread) == OK) {
 
76
                                pthread_queue_enq(&pthread_alloc_queue, pthread);
 
77
                        }
 
78
                        pthread_sched_other_resume(high_thread);
 
79
                        return(OK);
 
80
                }
 
81
                /* If the thread is dead then move it to the alloc queue */
 
82
                if (pthread_queue_remove(&pthread_dead_queue, pthread) == OK) {
 
83
                        pthread_queue_enq(&pthread_alloc_queue, pthread);
 
84
                        pthread->state = PS_UNALLOCED;
 
85
                }
 
86
                ret = OK;
 
87
        } else {
 
88
                ret = ESRCH;
 
89
        }
 
90
        pthread_sched_resume();
 
91
        return(ret);
 
92
}