/*
 * Copyright (c) 2001-2007 MIPS Technologies, Inc.
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *      copyright
 *        notice, this list of conditions and the following disclaimer
 *        in the documentation and/or other materials provided with
 *        the distribution.
 *      * Neither the name of MIPS Technologies Inc. nor the names of its
 *        contributors may be used to endorse or promote products derived
 *        from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* 
 * libc_thread.h: per-thread and per-process support
 */


#ifndef __LIBC_THREAD_H__
#define __LIBC_THREAD_H__

#include <sdethread.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
 * This variable is initally 0 when there is exactly one thread.
 * It should never decrease.
 */
extern int __isthreaded;

/*
 * Use WEAK_NAME(n) to get a libc-private name for n (_weak_n),
 *     WEAK_ALIAS(n) to generate the weak symbol n pointing to _weak_n,
 *     WEAK_PROTOTYPE(n) to generate a prototype for _weak_n (based on n).
 */

#if defined(__STDC__) || defined(__cplusplus)
#define WEAK_NAME(name)			_weak_ ## name
#else
#define WEAK_NAME(name)			_weak_/**/name
#endif
#define WEAK_ALIAS(name)		__c_weak_alias(name, WEAK_NAME(name))
#define WEAK_PROTOTYPE(name)		__typeof__(name) WEAK_NAME(name)


/*
 * These macros help in making persistent storage thread-specific.
 * Libc makes extensive use of private static data structures
 * that hold state across function invocation, and these macros
 * are no-ops when running single-threaded.
 *
 * Linking against the user-thread library causes these macros to
 * allocate storage on a per-thread basis.
 */
	
#if defined(__STDC__) || defined(__cplusplus)
#define __THREAD_KEY_NAME(name)		_libc_storage_key_ ## name
#define __THREAD_KEY_MUTEX(name)	__SDETHREAD_MUTEX_INITIALIZER("libc." #name)
#else
#define __THREAD_KEY_NAME(name)		_libc_storage_key_/**/name
#define __THREAD_KEY_MUTEX(name)	__SDETHREAD_MUTEX_INITIALIZER("libc.name")
#endif

struct _thread_private_key_struct {
	__sdethread_mutex_t	mutex;
	int			inited;
	void			(*cleanfn)(void *);
	__sdethread_key_t	key;
};

/* Declare a thread-private storage key. */
#define _THREAD_PRIVATE_KEY(name)					\
	static struct _thread_private_key_struct			\
	__THREAD_KEY_NAME(name) = {					\
	        __THREAD_KEY_MUTEX(name),				\
		0,							\
		NULL,							\
		0							\
	}

/* Declare a thread-private storage key with cleanup. */
#define _THREAD_PRIVATE_KEY_CLEANUP(name,cleanfn)			\
	static struct _thread_private_key_struct			\
	__THREAD_KEY_NAME(name) = {					\
	        __THREAD_KEY_MUTEX(name),				\
		0,							\
		cleanfn,						\
		0							\
	}

/*
 * In threaded mode, return a pointer to thread-private memory of
 * the same size as, and (initially) with the same contents as 'storage'. If
 * an error occurs, the 'error' parameter is returned.
 * In single-threaded mode, no storage is allocated. Instead, a pointer
 * to storage is always returned.
 * The 'cleanfn' function of the key structure is called to free the storage.
 * If 'cleanfn' is NULL, then free() is used. This hook can be useful for
 * getting rid of memory leaks.
 */
#define _THREAD_PRIVATE(keyname, storage, error) 			\
	_libc_private_storage(&__THREAD_KEY_NAME(keyname),		\
		&(storage), sizeof (storage), error)

void *	_libc_private_storage(struct _thread_private_key_struct *,
			      void *, size_t, void *);
		
/* 
 * One of these structures exists for each "process" level thread, and
 * allows each process to have its own set of file handles and other
 * important data.
 */
struct _libc_data {
    struct _iob *	_stdin;
    struct _iob *	_stdout;
    struct _iob *	_stderr;
    struct _iov *	_iov;
    struct _atexit *	_atexit;
    int			_opterr;
    int			_optind;
    int			_optopt;
    int			_optreset;
    char *		_optarg;
    char *		_optplace;
    int			_tmpnamsuf;
    void *		_malloc;
    void 		(*_cleanup) (void);
    int			_exiting;
    int			_ieee754csr;
    int			_spare[8];
};

extern struct _libc_data *	_libc_data;
extern struct _libc_data *	_libc_new (void);
extern void 			_libc_delete (struct _libc_data *);

/* user-visible per-process symbols */
#define stdin		(_libc_data->_stdin)
#define stdout		(_libc_data->_stdout)
#define stderr		(_libc_data->_stderr)
#define ieee754csr	(*(struct ieee754csr *)(&_libc_data->_ieee754csr))

#ifdef __cplusplus
}
#endif
#endif /* __LIBC_PRIVATE_H__ */
