]> git.saurik.com Git - apple/libc.git/blame - threads/cprocs.c
Libc-391.tar.gz
[apple/libc.git] / threads / cprocs.c
CommitLineData
e9ce8d39
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
734aad71
A
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
e9ce8d39
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
734aad71
A
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.
e9ce8d39
A
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 */
e9ce8d39 47#include "pthread_internals.h"
3d9156a7 48#include <stdlib.h>
e9ce8d39
A
49#include "cthreads.h"
50#include "cthread_internals.h"
51#include <mach/message.h>
52
53/*
54 * C Threads imports:
55 */
56extern void stack_init();
57extern void alloc_stack(), _dealloc_stack();
58
59/*
60 * Mach imports:
61 */
62extern mach_port_t mach_thread_self();
63extern boolean_t swtch_pri();
64
e9ce8d39
A
65#ifdef CTHREADS_DEBUG
66private void
67print_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
90private void
91print_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}
9385eb3d 99#endif /* CTHREADS_DEBUG */
e9ce8d39 100
9385eb3d 101#ifdef CTHREADS_DEBUG
e9ce8d39
A
102private cproc_t cprocs = NO_CPROC; /* linked list of cprocs */
103
e9ce8d39
A
104private void
105print_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}
9385eb3d 114#endif /* CTHREADS_DEBUG */
e9ce8d39
A
115
116/*
117 * Routines for supporting fork() of multi-threaded programs.
118 */
119
59e0d9fe
A
120void
121_cproc_fork_child()
e9ce8d39
A
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
138extern int errno;
59e0d9fe
A
139extern int *__error(void);
140
141void
142cthread_set_errno_self(error)
e9ce8d39
A
143 int error;
144{
59e0d9fe 145 int *ep = __error();
3d9156a7
A
146 extern int __unix_conforming;
147
148 if ((__unix_conforming) && (error == EINTR) && (__pthread_canceled(0) == 0))
149 pthread_exit(PTHREAD_CANCELED);
59e0d9fe
A
150
151 if (ep != &errno)
152 *ep = error;
e9ce8d39 153
e9ce8d39
A
154 errno = error;
155}
156