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