/*-----------------------------------------------------------------------------
 * thread_attr.h
 *
 * Copyright 2005,2006 Sony Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *---------------------------------------------------------------------------*/
#ifndef __THREAD_ATTR_H__
#define __THREAD_ATTR_H__

#if defined(_PUT_PID_)

/* DeclareThreadAttr --> */
#ifdef __KERNEL__
/*
=========================================================================
*/
/* for kernel */
#include <linux/kernel.h> /* for printk */
#include <asm/current.h>  /* for current */

#define DECLARE_THREAD_ATTR( _thread_attr )            \
  {                                                    \
    printk("[0x%08x]pid=%d\n", _thread_attr, current->pid ); \
  }

#else /* __KERNEL__ */
/*
=========================================================================
*/

/* for user */
#ifdef __cplusplus
extern "C"{
#endif

#include <stdio.h>     /* for printf */
#include <sys/types.h> /* for getpid */
#include <unistd.h>    /* for getpid */
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>

#define THREAD_ATTR_ENTRY_BUF_SIZE    32

#define DECLARE_THREAD_ATTR( _thread_attr )            \
  {                                                    \
	int profile_fd=-1;                                 \
	char entry[THREAD_ATTR_ENTRY_BUF_SIZE]; \
  \
	profile_fd = open("/proc/sched_clock", O_WRONLY); \
	if ( profile_fd < 0 ){ \
	    printf("[0x%08X]pid=%d\n", _thread_attr, getpid()); \
	}else{ \
            memset(entry, 0, THREAD_ATTR_ENTRY_BUF_SIZE); \
            snprintf(entry, ((sizeof entry) - 1), \
                "0x%08X %d", _thread_attr, getpid()); \
            write(profile_fd, entry, strlen(entry)); \
            close(profile_fd); \
            profile_fd=-1; \
	}                                                  \
  }

#ifdef __cplusplus
}
#endif

/*
=========================================================================
*/

#endif /* __KERNEL__ */

#else  /* _PUT_PID_ */
#define DECLARE_THREAD_ATTR( _thread_attr )
#endif /* _PUT_PID_ */


/* <-- DeclareThreadAttr */
#endif /* __THREAD_ATTR_H__ */
