diff options
author | Michael I. Bushnell <mib@gnu.org> | 1992-10-06 18:31:16 +0000 |
---|---|---|
committer | Michael I. Bushnell <mib@gnu.org> | 1992-10-06 18:31:16 +0000 |
commit | 964ab87456cf79f0f5787796c3e8917502d9f9d8 (patch) | |
tree | 8b51be116a45e60209dcd0490297594bd9b3c13d | |
parent | c70cb4b79a12ceb9f6c460a833199cd1e37963b1 (diff) |
entered into RCS
-rw-r--r-- | libthreads/Makefile.CMU | 93 | ||||
-rw-r--r-- | libthreads/Makefile.GNU | 34 | ||||
-rw-r--r-- | libthreads/call.c | 81 | ||||
-rw-r--r-- | libthreads/cthread_data.c | 188 | ||||
-rw-r--r-- | libthreads/i386/asm.h | 172 | ||||
-rw-r--r-- | libthreads/i386/cthread_inline.awk | 86 | ||||
-rw-r--r-- | libthreads/i386/cthreads.h | 89 | ||||
-rw-r--r-- | libthreads/i386/lock.s | 70 | ||||
-rw-r--r-- | libthreads/options.h | 90 | ||||
-rw-r--r-- | libthreads/sync.c | 83 |
10 files changed, 986 insertions, 0 deletions
diff --git a/libthreads/Makefile.CMU b/libthreads/Makefile.CMU new file mode 100644 index 00000000..5c6cf544 --- /dev/null +++ b/libthreads/Makefile.CMU @@ -0,0 +1,93 @@ +# +# Mach Operating System +# Copyright (c) 1991,1990,1989 Carnegie Mellon University +# All Rights Reserved. +# +# Permission to use, copy, modify and distribute this software and its +# documentation is hereby granted, provided that both the copyright +# notice and this permission notice appear in all copies of the +# software, derivative works or modified versions, and any portions +# thereof, and that both notices appear in supporting documentation. +# +# CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" +# CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR +# ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. +# +# Carnegie Mellon requests users of this software to return to +# +# Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU +# School of Computer Science +# Carnegie Mellon University +# Pittsburgh PA 15213-3890 +# +# any improvements or extensions that they make and grant Carnegie Mellon +# the rights to redistribute these changes. +# +# +# HISTORY +# $Log: Makefile,v $ +# Revision 2.21 92/05/22 18:38:31 jfriedl +# From Mike Kupfer <kupfer@sprite.Berkeley.EDU>: +# Enable CTHREAD_DATA for the kernel bootstrap program. +# +# Revision 2.20 92/03/05 22:46:34 rpd +# Changed to use double-colon rules for top-level targets. +# [92/02/28 rpd] +# +# Revision 2.19 92/01/23 15:22:53 rpd +# Revised for new Makefile organization. +# [92/01/16 rpd] +# +# Revision 1.4 91/09/09 15:55:51 kupfer +# MK63 merge. +# +# Revision 1.2 91/08/23 19:12:32 kupfer +# Put back the changes for the Posix thread local data support. +# + +# Define CTHREAD_DATA to enable source compatibility with the old +# "cthread_data" interface. +DEFS = -DCTHREAD_DATA + +include ${MAKETOP}Makefile-common + +# find machine-dependent files in machine subdirectory + +VPATH = .:${cpu} + +all :: libthreads.a + +install :: ${INSTALLDIR}/lib/libthreads.a + +release :: ${TRELEASEDIR}/lib/libthreads.a + +clean :: + ${RM} ${INSTALLDIR}/lib/libthreads.a + +# installation rules + +${INSTALLDIR}/lib/libthreads.a : libthreads.a + ${RM} $@ + ${CP} $? $@ + ${CHMOD_LIB} $@ + +# release rules + +${TRELEASEDIR}/lib/libthreads.a : ${FRELEASEDIR}/lib/libthreads.a + ${RM} $@ + ${CP} $? $@ + +# build rules + +OBJS = cprocs.o cthreads.o malloc.o \ + mig_support.o stack.o sync.o \ + thread.o lock.o csw.o cthread_data.o + +libthreads.a : ${OBJS} + ${RM} $@ + ${AR} cq $@ ${OBJS} + ${RANLIB} $@ + +# For lint, do ``lint -I. -un *.c mips/*.c'' + +-include Makedep diff --git a/libthreads/Makefile.GNU b/libthreads/Makefile.GNU new file mode 100644 index 00000000..bff8ed15 --- /dev/null +++ b/libthreads/Makefile.GNU @@ -0,0 +1,34 @@ +CPPFLAGS = -nostdinc -I. -I/usr1/gnu/DIST/include +CFLAGS = -g -O +CPP = /usr1/gnu/DIST/lib/gcc-cpp +AS = as +AR = ar +RANLIB = ranlib +CC = gcc + +VPATH=.:i386 + +OBJS = cprocs.o cthreads.o malloc.o \ + mig_support.o stack.o sync.o \ + thread.o lock.o csw.o + +all: libthreads.a + +install: all + cp libthreads.a /usr1/gnu/DIST/lib/libthreads.a + ranlib /usr1/gnu/DIST/lib/libthreads.a + +clean: + rm -f lib*.a Makedep* a.out core errs \ + *.d *.s *.S *.o *.BAK *.CKP */*.BAK */*.CKP + +libthreads.a: $(OBJS) + rm -f $@ + $(AR) crv $@ $(OBJS) + $(RANLIB) $@ + +%.o: %.s + $(CPP) $(CPPFLAGS) $< > $*.as + $(AS) -o $@ $*.as + rm -f $*.as + diff --git a/libthreads/call.c b/libthreads/call.c new file mode 100644 index 00000000..26efc8fc --- /dev/null +++ b/libthreads/call.c @@ -0,0 +1,81 @@ +/* + * Mach Operating System + * Copyright (c) 1991,1990,1989 Carnegie Mellon University + * All Rights Reserved. + * + * Permission to use, copy, modify and distribute this software and its + * documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR + * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie Mellon + * the rights to redistribute these changes. + */ +/* + * HISTORY + * $Log: call.c,v $ + * Revision 2.4 91/05/14 17:56:00 mrt + * Correcting copyright + * + * Revision 2.3 91/02/14 14:19:20 mrt + * Added new Mach copyright + * [91/02/13 12:40:44 mrt] + * + * Revision 2.2 90/01/19 14:36:50 rwd + * Created. Routines to replace thread_* and cthread_call_on. + * [90/01/03 rwd] + * + */ + +#include <cthreads.h> +#include "cthread_internals.h" + +#ifdef THREAD_CALLS +kern_return_t cthread_get_state(thread) +cthread_t thread; +{ + cproc_t p = thread->ur; +} + +kern_return_t cthread_set_state(thread) +cthread_t thread; +{ + cproc_t p = thread->ur; +} + +kern_return_t cthread_abort(thread) +cthread_t thread; +{ + cproc_t p = thread->ur; +} + +kern_return_t cthread_resume(thread) +cthread_t thread; +{ + cproc_t p = thread->ur; +} + +kern_return_t cthread_suspend(thread) +cthread_t thread; +{ + cproc_t p = thread->ur; +} + +kern_return_t cthread_call_on(thread) +cthread_t thread; +{ + cproc_t p = thread->ur; +} +#endif THREAD_CALLS diff --git a/libthreads/cthread_data.c b/libthreads/cthread_data.c new file mode 100644 index 00000000..0814130d --- /dev/null +++ b/libthreads/cthread_data.c @@ -0,0 +1,188 @@ +/* + * Mach Operating System + * Copyright (c) 1991 Carnegie-Mellon University + * All rights reserved. The CMU software License Agreement specifies + * the terms and conditions for use and redistribution. + */ +/* + * HISTORY + * $Log: cthread_data.c,v $ + * Revision 2.2 92/05/23 11:35:17 jfriedl + * Snarfed from multi-server sources at CMU. + * No stdio (for use with single-server). + * + * + * Revision 2.2 91/03/25 14:14:45 jjc + * For compatibility with cthread_data: + * 1) Added routines, cthread_data and cthread_set_data, + * which use the new routines in here. + * 2) Defined CTHREAD_KEY_RESERVED as the key used to + * access cthread_data. + * 3) Changed the first free key from CTHREAD_KEY_NULL + * to CTHREAD_KEY_FIRST. + * [91/03/18 jjc] + * Made simple implementation from POSIX threads specification for + * thread specific data. + * [91/03/07 jjc] + * + */ +#include <cthreads.h> + + +#ifdef CTHREAD_DATA +#define CTHREAD_KEY_FIRST (cthread_key_t)1 /* first free key */ +#else CTHREAD_DATA +#define CTHREAD_KEY_FIRST CTHREAD_KEY_NULL /* first free key */ +#endif CTHREAD_DATA +#define CTHREAD_KEY_MAX (cthread_key_t)8 /* max. no. of keys */ +#define CTHREAD_KEY_NULL (cthread_key_t)0 + +#ifdef CTHREAD_DATA +/* + * Key reserved for cthread_data + */ +#define CTHREAD_KEY_RESERVED CTHREAD_KEY_NULL +#endif CTHREAD_DATA + + +/* lock protecting key creation */ +struct mutex cthread_data_lock = MUTEX_INITIALIZER; + +/* next free key */ +cthread_key_t cthread_key = CTHREAD_KEY_FIRST; + + +/* + * Create key to private data visible to all threads in task. + * Different threads may use same key, but the values bound to the key are + * maintained on a thread specific basis. + * Returns 0 if successful and returns -1 otherwise. + */ +cthread_keycreate(key) +cthread_key_t *key; +{ + if (cthread_key >= CTHREAD_KEY_FIRST && cthread_key < CTHREAD_KEY_MAX) { + mutex_lock((mutex_t)&cthread_data_lock); + *key = cthread_key++; + mutex_unlock((mutex_t)&cthread_data_lock); + return(0); + } + else { /* out of keys */ + *key = CTHREAD_KEY_INVALID; + return(-1); + } +} + + +/* + * Get private data associated with given key + * Returns 0 if successful and returns -1 if the key is invalid. + * If the calling thread doesn't have a value for the given key, + * the value returned is CTHREAD_DATA_VALUE_NULL. + */ +cthread_getspecific(key, value) +cthread_key_t key; +any_t *value; +{ + register cthread_t self; + register any_t *thread_data; + + *value = CTHREAD_DATA_VALUE_NULL; + if (key < CTHREAD_KEY_NULL || key >= cthread_key) + return(-1); + + self = cthread_self(); + thread_data = (any_t *)(self->private_data); + if (thread_data != (any_t *)0) + *value = thread_data[key]; + + return(0); +} + + +/* + * Set private data associated with given key + * Returns 0 if successful and returns -1 otherwise. + */ +cthread_setspecific(key, value) +cthread_key_t key; +any_t value; +{ + register int i; + register cthread_t self; + register any_t *thread_data; + + if (key < CTHREAD_KEY_NULL || key >= cthread_key) + return(-1); + + self = cthread_self(); + thread_data = (any_t *)(self->private_data); + if (thread_data != (any_t *)0) + thread_data[key] = value; + else { + /* + * Allocate and initialize thread data table, + * point cthread_data at it, and then set the + * data for the given key with the given value. + */ + thread_data = (any_t *)malloc(CTHREAD_KEY_MAX * sizeof(any_t)); + if (thread_data == (any_t *)0) { + printf("cthread_setspecific: malloc failed\n"); + return(-1); + } + self->private_data = (any_t)thread_data; + + for (i = 0; i < CTHREAD_KEY_MAX; i++) + thread_data[i] = CTHREAD_DATA_VALUE_NULL; + + thread_data[key] = value; + } + return(0); +} + + +#ifdef CTHREAD_DATA +/* + * Set thread specific "global" variable, + * using new POSIX routines. + * Crash and burn if the thread given isn't the calling thread. + * XXX For compatibility with old cthread_set_data() XXX + */ +cthread_set_data(t, x) +cthread_t t; +any_t x; +{ + register cthread_t self; + + self = cthread_self(); + if (t == self) + return(cthread_setspecific(CTHREAD_KEY_RESERVED, x)); + else { + ASSERT(t == self); + } +} + + +/* + * Get thread specific "global" variable, + * using new POSIX routines. + * Crash and burn if the thread given isn't the calling thread. + * XXX For compatibility with old cthread_data() XXX + */ +any_t +cthread_data(t) +cthread_t t; +{ + register cthread_t self; + any_t value; + + self = cthread_self(); + if (t == self) { + (void)cthread_getspecific(CTHREAD_KEY_RESERVED, &value); + return(value); + } + else { + ASSERT(t == self); + } +} +#endif CTHREAD_DATA diff --git a/libthreads/i386/asm.h b/libthreads/i386/asm.h new file mode 100644 index 00000000..48129a3b --- /dev/null +++ b/libthreads/i386/asm.h @@ -0,0 +1,172 @@ +/* + * Mach Operating System + * Copyright (c) 1991,1990,1989 Carnegie Mellon University + * All Rights Reserved. + * + * Permission to use, copy, modify and distribute this software and its + * documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR + * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie Mellon + * the rights to redistribute these changes. + */ +/* + * HISTORY + * $Log: asm.h,v $ + * Revision 2.5 91/07/31 18:36:13 dbg + * Changes for STDC (from kernel version). + * [91/07/30 17:34:54 dbg] + * + * Revision 2.4 91/05/14 17:56:49 mrt + * Correcting copyright + * + * Revision 2.3 91/02/14 14:19:58 mrt + * Changed to new Mach copyright + * [91/02/13 12:15:20 mrt] + * + * Revision 2.2 90/05/03 15:54:33 dbg + * Created. + * [90/04/30 15:39:58 dbg] + * + * Typo on ENTRY if gprof + * [90/03/29 rvb] + * fix SVC for "ifdef wheeze" [kupfer] + * Fix the GPROF definitions. + * ENTRY(x) gets profiled iffdef GPROF. + * Entry(x) (and DATA(x)) is NEVER profiled. + * MCOUNT can be used by asm that intends to build a frame, + * after the frame is built. + * [90/02/26 rvb] + * Add #define addr16 .byte 0x67 + * [90/02/09 rvb] + * Added LBi, SVC and ENTRY + * [89/11/10 09:51:33 rvb] + * + * New a.out and coff compatible .s files. + * [89/10/16 rvb] + * + */ + + +#define S_ARG0 4(%esp) +#define S_ARG1 8(%esp) +#define S_ARG2 12(%esp) +#define S_ARG3 16(%esp) + +#define FRAME pushl %ebp; movl %esp, %ebp +#define EMARF leave + +#define B_ARG0 8(%ebp) +#define B_ARG1 12(%ebp) +#define B_ARG2 16(%ebp) +#define B_ARG3 20(%ebp) + +#ifdef wheeze + +#define ALIGN 4 +#define EXT(x) x +#define LEXT(x) x: +#define LCL(x) ./**/x + +#define LB(x,n) ./**/x +#define LBl(x,n) ./**/x: +#define LBb(x,n) ./**/x +#define LBf(x,n) ./**/x + +#define SVC lcall $7,$0 + +#define String .string +#define Value .value +#define Times(a,b) [a\*b] +#define Divide(a,b) [a\\b] + +#define INB inb (%dx) +#define OUTB outb (%dx) +#define INL inl (%dx) +#define OUTL outl (%dx) + +#else wheeze + +#define ALIGN 2 +#ifdef __STDC__ +#define EXT(x) _ ## x +#define LEXT(x) _ ## x ## : +#else /* __STDC__ */ +#define EXT(x) _/**/x +#define LEXT(x) _/**/x: +#endif /* __STDC__ */ +#define LCL(x) x + +#define LB(x,n) n +#ifdef __STDC__ +#define LBl(x,n) n ## : +#define LBb(x,n) n ## b +#define LBf(x,n) n ## f +#else /* __STDC__ */ +#define LBl(x,n) n/**/: +#define LBb(x,n) n/**/b +#define LBf(x,n) n/**/f +#endif /* __STDC__ */ + +#define SVC .byte 0x9a; .long 0; .word 0x7 + +#define String .ascii +#define Value .word +#define Times(a,b) (a*b) +#define Divide(a,b) (a/b) + +#define INB inb %dx, %al +#define OUTB outb %al, %dx +#define INL inl %dx, %eax +#define OUTL outl %eax, %dx + +#endif wheeze + +#define data16 .byte 0x66 +#define addr16 .byte 0x67 + + + +#ifdef GPROF +#define MCOUNT .data; LBl(x, 9) .long 0; .text; \ + lea LBb(x, 9),%edx; call mcount +#define ENTRY(x) .globl EXT(x); .align ALIGN; LEXT(x) ; \ + pushl %ebp; movl %esp, %ebp; MCOUNT; popl %ebp; +#define ENTRY2(x,y) .globl EXT(x); .globl EXT(y); \ + .align ALIGN; LEXT(x) LEXT(y) ; \ + pushl %ebp; movl %esp, %ebp; MCOUNT; popl %ebp; +#ifdef __STDC__ +#define ASENTRY(x) .globl x; .align ALIGN; x ## : ; \ + pushl %ebp; movl %esp, %ebp; MCOUNT; popl %ebp; +#else /* __STDC__ */ +#define ASENTRY(x) .globl x; .align ALIGN; x: ; \ + pushl %ebp; movl %esp, %ebp; MCOUNT; popl %ebp; +#endif /* __STDC__ */ + +#else GPROF +#define MCOUNT +#define ENTRY(x) .globl EXT(x); .align ALIGN; LEXT(x) +#define ENTRY2(x,y) .globl EXT(x); .globl EXT(y); \ + .align ALIGN; LEXT(x) LEXT(y) +#ifdef __STDC__ +#define ASENTRY(x) .globl x; .align ALIGN; x ## : +#else /* __STDC__ */ +#define ASENTRY(x) .globl x; .align ALIGN; x: +#endif /* __STDC__ */ +#endif GPROF + +#define Entry(x) .globl EXT(x); .align ALIGN; LEXT(x) +#define DATA(x) .globl EXT(x); .align ALIGN; LEXT(x) diff --git a/libthreads/i386/cthread_inline.awk b/libthreads/i386/cthread_inline.awk new file mode 100644 index 00000000..8e0cb7d4 --- /dev/null +++ b/libthreads/i386/cthread_inline.awk @@ -0,0 +1,86 @@ +# +# Mach Operating System +# Copyright (c) 1991,1990 Carnegie Mellon University +# All Rights Reserved. +# +# Permission to use, copy, modify and distribute this software and its +# documentation is hereby granted, provided that both the copyright +# notice and this permission notice appear in all copies of the +# software, derivative works or modified versions, and any portions +# thereof, and that both notices appear in supporting documentation. +# +# CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" +# CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR +# ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. +# +# Carnegie Mellon requests users of this software to return to +# +# Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU +# School of Computer Science +# Carnegie Mellon University +# Pittsburgh PA 15213-3890 +# +# any improvements or extensions that they make and grant Carnegie Mellon +# the rights to redistribute these changes. +# +# +# HISTORY +# $Log: cthread_inline.awk,v $ +# Revision 2.5 91/05/14 17:57:03 mrt +# Correcting copyright +# +# Revision 2.4 91/05/08 13:36:05 dbg +# Unlock lock with a locked instruction (xchg). +# [91/03/20 dbg] +# +# Revision 2.3 91/02/14 14:20:06 mrt +# Added new Mach copyright +# [91/02/13 12:33:05 mrt] +# +# Revision 2.2 90/05/03 15:54:56 dbg +# Created (from 68020 version). +# [90/02/05 dbg] +# +# Revision 2.2 89/12/08 19:54:30 rwd +# Inlines are now spins instead of mutexes. +# [89/10/23 rwd] +# +# Revision 2.1 89/08/04 15:15:14 rwd +# Created. +# +# Revision 1.3 89/05/05 19:00:33 mrt +# Cleanup for Mach 2.5 +# +# + +# sun/cthread_inline.awk +# +# Awk script to inline critical C Threads primitives on i386 + +NF == 2 && $1 == "call" && $2 == "_spin_try_lock" { + print "/ BEGIN INLINE spin_try_lock" + print " movl (%esp),%ecx / point at mutex" + print " movl $1,%eax / set locked value in acc" + print " xchg %eax,(%ecx) / locked swap with mutex" + print " xorl $1,%eax / logical complement" + print "/ END INLINE spin_try_lock" + continue +} +NF == 2 && $1 == "call" && $2 == "_spin_unlock" { + print "/ BEGIN INLINE " $2 + print " movl (%esp),%ecx" + print " xorl %eax,%eax / set unlocked value in acc" + print " xchg %eax,(%ecx) / locked swap with mutex" + print "/ END INLINE " $2 + continue +} +NF == 2 && $1 == "call" && $2 == "_cthread_sp" { + print "/ BEGIN INLINE cthread_sp" + print " movl %esp,%eax" + print "/ END INLINE cthread_sp" + continue +} +# default: +{ + print +} diff --git a/libthreads/i386/cthreads.h b/libthreads/i386/cthreads.h new file mode 100644 index 00000000..8ffe4b72 --- /dev/null +++ b/libthreads/i386/cthreads.h @@ -0,0 +1,89 @@ +/* + * Mach Operating System + * Copyright (c) 1991,1990 Carnegie Mellon University + * All Rights Reserved. + * + * Permission to use, copy, modify and distribute this software and its + * documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR + * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie Mellon + * the rights to redistribute these changes. + */ +/* + * HISTORY + * $Log: cthreads.h,v $ + * Revision 2.7 92/01/03 20:36:59 dbg + * Add volatile to spin_lock_t. Change spin_unlock and + * spin_try_lock definitions back to memory operands, but rely on + * volatile attribute to keep from using value in memory. + * [91/09/04 dbg] + * + * Revision 2.6 91/08/28 20:18:39 jsb + * Safer definitions for spin_unlock and spin_try_lock from mib. + * + * Revision 2.5 91/07/31 18:36:49 dbg + * Add inline substitution for cthread_sp, spin_unlock, + * spin_try_lock. + * [91/07/30 17:35:53 dbg] + * + * Revision 2.4 91/05/14 17:57:11 mrt + * Correcting copyright + * + * Revision 2.3 91/02/14 14:20:14 mrt + * Changed to new Mach copyright + * [91/02/13 12:20:00 mrt] + * + * Revision 2.2 90/11/05 14:37:23 rpd + * Created. + * [90/11/01 rwd] + * + * + */ + +#ifndef _MACHINE_CTHREADS_H_ +#define _MACHINE_CTHREADS_H_ + +typedef volatile int spin_lock_t; +#define SPIN_LOCK_INITIALIZER 0 +#define spin_lock_init(s) (*(s) = 0) +#define spin_lock_locked(s) (*(s) != 0) + +#ifdef __GNUC__ + +#define spin_unlock(p) \ + ({ register int _u__ ; \ + asm volatile("xorl %0, %0; \n\ + xchgl %0, %1" \ + : "=&r" (_u__), "=m" (*(p)) ); \ + 0; }) + +#define spin_try_lock(p)\ + ({ boolean_t _r__; \ + asm volatile("movl $1, %0; \n\ + xchgl %0, %1" \ + : "=&r" (_r__), "=m" (*(p)) ); \ + !_r__; }) + +#define cthread_sp() \ + ({ int _sp__; \ + asm("movl %%esp, %0" \ + : "=g" (_sp__) ); \ + _sp__; }) + +#endif /* __GNUC__ */ + +#endif _MACHINE_CTHREADS_H_ diff --git a/libthreads/i386/lock.s b/libthreads/i386/lock.s new file mode 100644 index 00000000..e27fa7ff --- /dev/null +++ b/libthreads/i386/lock.s @@ -0,0 +1,70 @@ +/* + * Mach Operating System + * Copyright (c) 1991,1990 Carnegie Mellon University + * All Rights Reserved. + * + * Permission to use, copy, modify and distribute this software and its + * documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR + * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie Mellon + * the rights to redistribute these changes. + */ +/* + * HISTORY + * $Log: lock.s,v $ + * Revision 2.5 91/05/14 17:57:20 mrt + * Correcting copyright + * + * Revision 2.4 91/05/08 13:36:15 dbg + * Unlock lock with a locked instruction (xchg). + * [91/03/20 dbg] + * + * Revision 2.3 91/02/14 14:20:18 mrt + * Changed to new Mach copyright + * [91/02/13 12:20:06 mrt] + * + * Revision 2.2 90/05/03 15:54:59 dbg + * Created. + * [90/02/05 dbg] + * + */ + +#include <i386/asm.h> + +/* + * boolean_t spin_try_lock(int *m) + */ +ENTRY(spin_try_lock) + movl 4(%esp),%ecx / point at mutex + movl $1,%eax / set locked value in acc + xchg %eax,(%ecx) / swap with mutex + / xchg with memory is automatically + / locked + xorl $1,%eax / 1 (locked) => FALSE + / 0 (locked) => TRUE + ret + +/* + * void spin_unlock(int *m) + */ +ENTRY(spin_unlock) + movl 4(%esp),%ecx / point at mutex + xorl %eax,%eax / set unlocked value in acc + xchg %eax,(%ecx) / swap with mutex + / xchg with memory is automatically + / locked + ret diff --git a/libthreads/options.h b/libthreads/options.h new file mode 100644 index 00000000..952720de --- /dev/null +++ b/libthreads/options.h @@ -0,0 +1,90 @@ +/* + * Mach Operating System + * Copyright (c) 1991,1990,1989 Carnegie Mellon University + * All Rights Reserved. + * + * Permission to use, copy, modify and distribute this software and its + * documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR + * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie Mellon + * the rights to redistribute these changes. + */ +/* + * HISTORY + * $Log: options.h,v $ + * Revision 2.8 91/05/14 17:58:35 mrt + * Correcting copyright + * + * Revision 2.7 91/02/14 14:21:03 mrt + * Added new Mach copyright + * [91/02/13 12:41:31 mrt] + * + * Revision 2.6 90/09/09 14:35:04 rpd + * Remove special option , debug_mutex and thread_calls. + * [90/08/24 rwd] + * + * Revision 2.5 90/06/02 15:14:14 rpd + * Removed RCS Source, Header lines. + * [90/05/03 00:07:27 rpd] + * + * Revision 2.4 90/03/14 21:12:15 rwd + * Added new option: + * WAIT_DEBUG: keep track of who a blocked thread is + * waiting for. + * [90/03/01 rwd] + * + * Revision 2.3 90/01/19 14:37:25 rwd + * New option: + * THREAD_CALLS: cthread_* version of thread_* calls. + * [90/01/03 rwd] + * + * Revision 2.2 89/12/08 19:54:09 rwd + * Added code: + * MUTEX_SPECIAL: Have extra kernel threads available for + * special mutexes to avoid deadlocks + * Removed options: + * MSGOPT, RECEIVE_YIELD + * [89/11/25 rwd] + * Added option: + * MUTEX_SPECIAL: Allow special mutexes which will + * garuntee the resulting threads runs + * on a mutex_unlock + * [89/11/21 rwd] + * Options added are: + * STATISTICS: collect [kernel/c]thread state stats. + * SPIN_RESCHED: call swtch_pri(0) when spin will block. + * MSGOPT: try to minimize message sends + * CHECK_STATUS: check status of mach calls + * RECEIVE_YIELD: yield thread if no waiting threads after + * cthread_msg_receive + * RED_ZONE: make redzone at end of stacks + * DEBUG_MUTEX: in conjunction with same in cthreads.h + * use slow mutex with held=cproc_self(). + * [89/11/13 rwd] + * Added copyright. Removed all options. + * [89/10/23 rwd] + * + */ +/* + * options.h + */ + +/*#define STATISTICS*/ +#define SPIN_RESCHED +/*#define CHECK_STATUS*/ +/*#define RED_ZONE*/ +#define WAIT_DEBUG diff --git a/libthreads/sync.c b/libthreads/sync.c new file mode 100644 index 00000000..e17280aa --- /dev/null +++ b/libthreads/sync.c @@ -0,0 +1,83 @@ +/* + * Mach Operating System + * Copyright (c) 1991,1990,1989 Carnegie Mellon University + * All Rights Reserved. + * + * Permission to use, copy, modify and distribute this software and its + * documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR + * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie Mellon + * the rights to redistribute these changes. + */ +/* + * HISTORY + * $Log: sync.c,v $ + * Revision 2.7 92/03/06 14:09:59 rpd + * Replaced swtch_pri with yield. + * [92/03/06 rpd] + * + * Revision 2.6 91/05/14 17:59:54 mrt + * Correcting copyright + * + * Revision 2.5 91/02/14 14:21:38 mrt + * Added new Mach copyright + * [91/02/13 12:41:42 mrt] + * + * Revision 2.4 90/11/05 14:38:08 rpd + * Fix casting. Use new macros. + * [90/10/31 rwd] + * + * Revision 2.3 90/08/07 14:31:50 rpd + * Removed RCS keyword nonsense. + * + * Revision 2.2 89/12/08 19:55:01 rwd + * Changed spin_lock to spin_lock_solid to optimize. + * [89/11/13 rwd] + * Added copyright. Move mutexes to cproc.c. Spin locks are now + * old mutexes. + * [89/10/23 rwd] + * + */ +/* + * sync.c + * + * Spin locks + */ + +#include <cthreads.h> +#include "cthread_internals.h" + +/* + * Spin locks. + * Use test and test-and-set logic on all architectures. + */ + +int cthread_spin_count=0; + +void +spin_lock_solid(p) + register spin_lock_t *p; +{ + while (spin_lock_locked(p) || !spin_try_lock(p)) { +#ifdef STATISTICS + cthread_spin_count++; +#endif +#ifdef SPIN_RESCHED + yield(); +#endif + } +} |