]> git.saurik.com Git - apple/libc.git/blob - threads/cprocs.c
99b52a635bc7d26f0cc646ea7f8c456f10c8587a
[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
123 _cproc_fork_child()
124 /*
125 * Called in the child after a fork(). Resets cproc data structures to
126 * coincide with the reality that we now have a single cproc and cthread.
127 */
128 {
129 pthread_t pself;
130
131 pself = pthread_self();
132 pself->reply_port = MACH_PORT_NULL;
133 }
134
135 /*
136 * Support for a per-thread UNIX errno.
137 */
138
139 #undef errno
140 extern int errno;
141 extern int *__error(void);
142
143 void
144 cthread_set_errno_self(error)
145 int error;
146 {
147 int *ep = __error();
148
149 if (ep != &errno)
150 *ep = error;
151
152 errno = error;
153 }
154