135538Sjb/*
235538Sjb * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
335538Sjb * Copyright (c) 1994 by Chris Provenzano, proven@mit.edu
435538Sjb * All rights reserved.
535538Sjb *
635538Sjb * Redistribution and use in source and binary forms, with or without
735538Sjb * modification, are permitted provided that the following conditions
835538Sjb * are met:
935538Sjb * 1. Redistributions of source code must retain the above copyright
1035538Sjb *    notice, this list of conditions and the following disclaimer.
1135538Sjb * 2. Redistributions in binary form must reproduce the above copyright
1235538Sjb *    notice, this list of conditions and the following disclaimer in the
1335538Sjb *    documentation and/or other materials provided with the distribution.
1435538Sjb * 3. All advertising materials mentioning features or use of this software
1535538Sjb *    must display the following acknowledgement:
1635538Sjb *	This product includes software developed by John Birrell
1735538Sjb *  and Chris Provenzano.
1835538Sjb * 4. Neither the name of the author nor the names of any co-contributors
1935538Sjb *    may be used to endorse or promote products derived from this software
2035538Sjb *    without specific prior written permission.
2135538Sjb *
2235538Sjb * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
2335538Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2435538Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2535538Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2635538Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2735538Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2835538Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2935538Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3035538Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3135538Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3235538Sjb * SUCH DAMAGE.
3335538Sjb *
3450476Speter * $FreeBSD$
3535538Sjb */
36174112Sdeischen#include <errno.h>
3735538Sjb#include <pthread.h>
38113657Sdeischen#include "libc_private.h"
39103388Smini#include "thr_private.h"
40113942Sdeischen
41139023Sdeischen#undef errno
4235538Sjbextern	int	errno;
4335538Sjb
44113942Sdeischenint *
45113942Sdeischen__error(void)
4635538Sjb{
47113657Sdeischen	struct pthread *curthread;
48113657Sdeischen
49113657Sdeischen	if (__isthreaded == 0)
50113657Sdeischen		return (&errno);
51113942Sdeischen	else if (_kse_in_critical())
52113942Sdeischen		return &(_get_curkse()->k_error);
53113657Sdeischen	else {
54113657Sdeischen		curthread = _get_curthread();
55113657Sdeischen		if ((curthread == NULL) || (curthread == _thr_initial))
56113657Sdeischen			return (&errno);
57113657Sdeischen		else
58113657Sdeischen			return (&curthread->error);
5935538Sjb	}
6035538Sjb}
61