]> git.saurik.com Git - apple/libc.git/blob - threads/cprocs.c
Libc-498.tar.gz
[apple/libc.git] / threads / cprocs.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Mach Operating System
25 * Copyright (c) 1989 Carnegie-Mellon University
26 * All rights reserved. The CMU software License Agreement specifies
27 * the terms and conditions for use and redistribution.
28 */
29
30 /*
31 * HISTORY
32 * 22-July-93 Blaine Garst
33 * fixed kernel cache set up of cproc info
34 *
35 * 05-April-90 Morris Meyer (mmeyer) at NeXT
36 * Fixed bug in cproc_fork_child() where the first cproc would
37 * try doing a msg_rpc() with an invalid reply port.
38 *
39 */
40
41 /*
42 * cprocs.c - by Eric Cooper
43 *
44 * Implementation of cprocs (lightweight processes)
45 * and primitive synchronization operations.
46 */
47 #include "pthread_internals.h"
48 #include <stdlib.h>
49 #include "cthreads.h"
50 #include "cthread_internals.h"
51 #include <mach/message.h>
52
53 /*
54 * C Threads imports:
55 */
56 extern void stack_init();
57 extern void alloc_stack(), _dealloc_stack();
58
59 /*
60 * Mach imports:
61 */
62 extern mach_port_t mach_thread_self();
63 extern boolean_t swtch_pri();
64
65 #ifdef CTHREADS_DEBUG
66 private void
67 print_cproc(p)
68 cproc_t p;
69 {
70 char *s;
71
72 switch (p->state) {
73 case CPROC_RUNNING:
74 s = "";
75 break;
76 case CPROC_SPINNING:
77 s = "+";
78 break;
79 case CPROC_BLOCKED:
80 s = "*";
81 break;
82 default:
83 ASSERT(SHOULDNT_HAPPEN);
84 }
85 printf(" %x(%s)%s",
86 p->id,
87 cthread_name(p->incarnation), s);
88 }
89
90 private void
91 print_cproc_queue(name, queue)
92 const char * name;
93 cthread_queue_t queue;
94 {
95 printf("[%s] %s:", cthread_name(cthread_self()), name);
96 cthread_queue_map(queue, cproc_t, print_cproc);
97 printf("\n");
98 }
99 #endif /* CTHREADS_DEBUG */
100
101 #ifdef CTHREADS_DEBUG
102 private cproc_t cprocs = NO_CPROC; /* linked list of cprocs */
103
104 private void
105 print_all_cprocs()
106 {
107 cproc_t p;
108
109 printf("[%s] cprocs:", cthread_name(cthread_self()));
110 for (p = cprocs; p != NO_CPROC; p = p->link)
111 print_cproc(p);
112 printf("\n");
113 }
114 #endif /* CTHREADS_DEBUG */
115
116 /*
117 * Routines for supporting fork() of multi-threaded programs.
118 */
119
120 void
121 _cproc_fork_child()
122 /*
123 * Called in the child after a fork(). Resets cproc data structures to
124 * coincide with the reality that we now have a single cproc and cthread.
125 */
126 {
127 pthread_t pself;
128
129 pself = pthread_self();
130 pself->reply_port = MACH_PORT_NULL;
131 }
132
133 /*
134 * Support for a per-thread UNIX errno.
135 */
136
137 #undef errno
138 extern int errno;
139 extern int *__error(void);
140
141 void
142 cthread_set_errno_self(error)
143 int error;
144 {
145 int *ep = __error();
146 extern int __unix_conforming;
147
148 if ((__unix_conforming) && (error == EINTR) && (__pthread_canceled(0) == 0))
149 pthread_exit(PTHREAD_CANCELED);
150
151 if (ep != &errno)
152 *ep = error;
153
154 errno = error;
155 }
156