]> git.saurik.com Git - apple/libc.git/blob - threads/cprocs.c
Libc-262.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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Mach Operating System
24 * Copyright (c) 1989 Carnegie-Mellon University
25 * All rights reserved. The CMU software License Agreement specifies
26 * the terms and conditions for use and redistribution.
27 */
28
29 /*
30 * HISTORY
31 * 22-July-93 Blaine Garst
32 * fixed kernel cache set up of cproc info
33 *
34 * 05-April-90 Morris Meyer (mmeyer) at NeXT
35 * Fixed bug in cproc_fork_child() where the first cproc would
36 * try doing a msg_rpc() with an invalid reply port.
37 *
38 */
39
40 /*
41 * cprocs.c - by Eric Cooper
42 *
43 * Implementation of cprocs (lightweight processes)
44 * and primitive synchronization operations.
45 */
46 #include <stdlib.h>
47 #include "pthread_internals.h"
48 #include "cthreads.h"
49 #include "cthread_internals.h"
50 #include <mach/message.h>
51
52 /*
53 * C Threads imports:
54 */
55 extern void stack_init();
56 extern void alloc_stack(), _dealloc_stack();
57
58 /*
59 * Mach imports:
60 */
61 extern mach_port_t mach_thread_self();
62 extern boolean_t swtch_pri();
63
64 private int cprocs_started = FALSE;
65
66 #ifdef CTHREADS_DEBUG
67 private void
68 print_cproc(p)
69 cproc_t p;
70 {
71 char *s;
72
73 switch (p->state) {
74 case CPROC_RUNNING:
75 s = "";
76 break;
77 case CPROC_SPINNING:
78 s = "+";
79 break;
80 case CPROC_BLOCKED:
81 s = "*";
82 break;
83 default:
84 ASSERT(SHOULDNT_HAPPEN);
85 }
86 printf(" %x(%s)%s",
87 p->id,
88 cthread_name(p->incarnation), s);
89 }
90
91 private void
92 print_cproc_queue(name, queue)
93 const char * name;
94 cthread_queue_t queue;
95 {
96 printf("[%s] %s:", cthread_name(cthread_self()), name);
97 cthread_queue_map(queue, cproc_t, print_cproc);
98 printf("\n");
99 }
100 #endif CTHREADS_DEBUG
101
102 private int cproc_lock = 0; /* unlocked */
103 private cproc_t cprocs = NO_CPROC; /* linked list of cprocs */
104
105 #ifdef CTHREADS_DEBUG
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