]> git.saurik.com Git - apple/libc.git/blob - threads/cprocs.c
72b6f5b447e7c225539d6858ffab07a68165e2cd
[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 private int cprocs_started = FALSE;
68
69 #ifdef CTHREADS_DEBUG
70 private void
71 print_cproc(p)
72 cproc_t p;
73 {
74 char *s;
75
76 switch (p->state) {
77 case CPROC_RUNNING:
78 s = "";
79 break;
80 case CPROC_SPINNING:
81 s = "+";
82 break;
83 case CPROC_BLOCKED:
84 s = "*";
85 break;
86 default:
87 ASSERT(SHOULDNT_HAPPEN);
88 }
89 printf(" %x(%s)%s",
90 p->id,
91 cthread_name(p->incarnation), s);
92 }
93
94 private void
95 print_cproc_queue(name, queue)
96 const char * name;
97 cthread_queue_t queue;
98 {
99 printf("[%s] %s:", cthread_name(cthread_self()), name);
100 cthread_queue_map(queue, cproc_t, print_cproc);
101 printf("\n");
102 }
103 #endif CTHREADS_DEBUG
104
105 private int cproc_lock = 0; /* unlocked */
106 private cproc_t cprocs = NO_CPROC; /* linked list of cprocs */
107
108 #ifdef CTHREADS_DEBUG
109 private void
110 print_all_cprocs()
111 {
112 cproc_t p;
113
114 printf("[%s] cprocs:", cthread_name(cthread_self()));
115 for (p = cprocs; p != NO_CPROC; p = p->link)
116 print_cproc(p);
117 printf("\n");
118 }
119 #endif CTHREADS_DEBUG
120
121 /*
122 * Routines for supporting fork() of multi-threaded programs.
123 */
124
125 void _cproc_fork_child()
126 /*
127 * Called in the child after a fork(). Resets cproc data structures to
128 * coincide with the reality that we now have a single cproc and cthread.
129 */
130 {
131 pthread_t pself;
132
133 pself = pthread_self();
134 pself->reply_port = MACH_PORT_NULL;
135 }
136
137 /*
138 * Support for a per-thread UNIX errno.
139 */
140
141 #undef errno
142 extern int errno;
143 void cthread_set_errno_self(error)
144 int error;
145 {
146 pthread_t t;
147
148 t = pthread_self();
149 if (t && (t->sig == _PTHREAD_SIG)) {
150 t->err_no = error;
151 }
152 errno = error;
153 }
154