⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smsc_threading.c

📁 由smsc公司改进的lwip2.1.1基于嵌入式系统的TCP/ip协议栈
💻 C
字号:
/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved. 
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. 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.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
 *
 * This file is part of the lwIP TCP/IP stack.
 * 
 * Author: Adam Dunkels <adam@sics.se>
 *
 */
/* 
Modifications of this software Copyright(C) 2008, Standard Microsystems Corporation
  All Rights Reserved.
  The modifications to this program code are proprietary to SMSC and may not be copied,
  distributed, or used without a license to do so.  Such license may have
  Limited or Restricted Rights. Please refer to the license for further
  clarification.
*/
/***************************************************************************
 * File: smsc_threading.c
 */

#include "smsc_environment.h"
#include "smsc_threading.h"

#if !SMSC_THREADING_ENABLED
#error SMSC_THREADING_ENABLED==0
#endif


/* Debug bits */
#define DEBUG_TRACE_BIT		(0x10)
#define DEBUG_NOTICE_BIT	(0x20)
#define DEBUG_WARNING_BIT	(0x40)

#ifndef SMSC_TRACE_ENABLED
#define SMSC_TRACE_ENABLED	(1)
#endif
#ifndef SMSC_NOTICE_ENABLED
#define SMSC_NOTICE_ENABLED	(1)
#endif
#ifndef SMSC_WARNING_ENABLED
#define SMSC_WARNING_ENABLED	(1)
#endif
#ifndef SMSC_ERROR_ENABLED
#define SMSC_ERROR_ENABLED	(1)
#endif

/* Debug Utilities */
/* SMSC_TRACE is used for informational purposes only,
	it may be useful during debugging but is usually too
	noisy to keep it enabled all the time.
	'condition' is usually filled in with an individual module 
	debug enable switch, but it could also be used to specify a runtime
	condition that activates the message. */
#if (SMSC_TRACE_ENABLED)
#define SMSC_TRACE(condition,message) 				\
	do {											\
		if((condition)&(1|DEBUG_TRACE_BIT)) {		\
			SMSC_PLATFORM_MESSAGE(("  TRACE: "));	\
			SMSC_PLATFORM_MESSAGE(message); 		\
			SMSC_PLATFORM_MESSAGE(("\n"));			\
		}											\
	} while(0)
#define SMSC_PRINT(condition,message)			\
	do {										\
		if((condition)&(1|DEBUG_TRACE_BIT)) {	\
			SMSC_PLATFORM_MESSAGE(message);		\
		}										\
	} while(0)
#else
#define SMSC_TRACE(condition,message)
#define SMSC_PRINT(condition,message)
#endif

/* SMSC_NOTICE is intended to be used only when an error is detected
   and fully handled and recoverable. (example is dropping packets) 
	'condition' is usually filled in with an individual module 
	debug enable switch, but it could also be used to specify a runtime
	condition that activates the message. */
#if (SMSC_NOTICE_ENABLED)
#define SMSC_NOTICE(condition,message) 				\
	do {											\
		if((condition)&(1|DEBUG_NOTICE_BIT)) {		\
			SMSC_PLATFORM_MESSAGE((" NOTICE: "));	\
			SMSC_PLATFORM_MESSAGE(message);			\
			SMSC_PLATFORM_MESSAGE(("\n"));			\
		}											\
	} while(0)
#else
#define SMSC_NOTICE(condition,message)
#endif

/* SMSC_WARNING is intended to be used only when an error is detected
   and but may not be handled or recoverable. (example is how to handle unknown codes) 
	'condition' is usually filled in with an individual module 
	debug enable switch, but it could also be used to specify a runtime
	condition that activates the message. */
#if (SMSC_WARNING_ENABLED)
#define SMSC_WARNING(condition,message) 			\
	do {											\
		if((condition)&(1|DEBUG_WARNING_BIT)) {		\
			SMSC_PLATFORM_MESSAGE(("WARNING: "));	\
			SMSC_PLATFORM_MESSAGE(message);			\
			SMSC_PLATFORM_MESSAGE(("\n"));			\
		}											\
	} while(0)
#else
#define SMSC_WARNING(condition,message)
#endif

/* SMSC_ERROR is to be used only when a fatal unrecoverable error has 
   accured. It contains a halt command to prevent further corruption.
   SMSC_ASSERT is similar to SMSC_ERROR in that both can be used to notify
   of a fatal error. SMSC_ASSERT allow the user to provide a condition
   which if false will trigger the error. If the error is triggered an 
   halt command will prevent further execution.
   Neither SMSC_ERROR or SMSC_ASSERT can be enabled on a per module based.
   If they are enabled they are enabled for all modules.
   */
#if (SMSC_ERROR_ENABLED)
#define SMSC_ERROR(message) 								\
	do { 													\
		SMSC_PLATFORM_MESSAGE(("!ERROR!: "));				\
		SMSC_PLATFORM_MESSAGE(message);						\
		SMSC_PLATFORM_HALT_MESSAGE(							\
			("\n!ERROR!: file=%s, line=%d\n",				\
				__FILE__,__LINE__));						\
	} while(0)

#define SMSC_ASSERT(condition) 								\
	do { 													\
		if(!(condition)) {									\
			SMSC_PLATFORM_HALT_MESSAGE(						\
				("ASSERTION FAILURE: file=%s, line=%d\n", 	\
                	__FILE__, __LINE__));					\
        }													\
   	} while(0)
            
/*
* The following macros help by verifying you have a valid structure pointer
*   This is especially useful when void pointers are used to pass structures
*   around. These macros can be used to make sure a structure has been 
*   initialized and make sure it is the correct structure.
*/
#define DECLARE_SIGNATURE	u32_t mSignature;
#define ASSIGN_SIGNATURE(structure_pointer,signature) 				\
	do {															\
		(structure_pointer)->mSignature=((u32_t)signature);			\
	}while(0)
#define CHECK_SIGNATURE(structure_pointer,signature) 				\
	do {															\
		if((structure_pointer)->mSignature!=((u32_t)signature)) {		\
			SMSC_PLATFORM_HALT_MESSAGE(								\
				("SIGNATURE_FAILURE: file=%s, line=%d\n",			\
					__FILE__, __LINE__));							\
		}															\
	} while(0)
#define CLEAR_SIGNATURE(structure_pointer)			\
	do {											\
		(structure_pointer)->mSignature=(u32_t)0);	\
	} while(0)
#else /* SMSC_ERROR_ENABLED */
#define SMSC_ERROR(message)
#define SMSC_ASSERT(condition)  
#define DECLARE_SIGNATURE
#define ASSIGN_SIGNATURE(structure_pointer,signature)
#define CHECK_SIGNATURE(structure_pointer,signature)
#define CLEAR_SIGNATURE(structure_pointer)
#endif /* SMSC_ERROR_ENABLED */

void smsc_threading_initialize(void)
{
}

PSMSC_SEMAPHORE smsc_semaphore_create(int count)
{
	PSMSC_SEMAPHORE pmutex;
	pmutex = (PSMSC_SEMAPHORE) malloc(sizeof(pthread_mutex_t));
	if (pmutex!=NULL) {
		if(pthread_mutex_init(pmutex, NULL)!=0) {
			SMSC_WARNING(1,("pthread_mutex_init failed"));
		}
	}
	return pmutex;
}

void smsc_semaphore_initialize(PSMSC_SEMAPHORE pmutex, int count)
{
	if(pthread_mutex_init(pmutex, NULL)!=0) {
		SMSC_WARNING(1,("pthread_mutex_init failed"));
	}
}

/* returns the number of milliseconds that passed before the 
   semaphore was signaled, or returns SMSC_TIMEOUT if a timeout accured */
u32_t smsc_semaphore_wait_timeout(PSMSC_SEMAPHORE sem, u32_t milliSeconds)
{
	int ret;
	struct timespec timenow;
	u32_t msec_start,msec_end;

	/* Get the time */
	clock_gettime(CLOCK_REALTIME,&timenow);	

	msec_start=timenow.tv_sec * 1000 + timenow.tv_nsec / 1000000;

	timenow.tv_nsec=(milliSeconds*1000000)+timenow.tv_nsec;

	ret=pthread_mutex_timedlock(&sem, &timenow);
	if(ret!=0) {
		return SMSC_TIMEOUT;
	}

	/* Get the time */
	clock_gettime(CLOCK_REALTIME,&timenow);	
	
	msec_end=timenow.tv_sec * 1000 + timenow.tv_nsec / 1000000;

	return (msec_end - msec_start);
}
        
        
/*
 * Create new thread 
 */
PSMSC_THREAD smsc_thread_create(void (*function) (void *arg), void *arg, u32_t stackSize, int priority, char * name, u8_t flags)
{
	pthread_t new_thread;
	int result;

	SMSC_TRACE(1,("smsc_thread_create.."));

	if ((result=pthread_create(&new_thread,NULL,(void *(*)(void *)) function,arg)) != 0) {
		SMSC_WARNING(1,("pthread_create failed"));
 	}
	return result;
}

void smsc_thread_resume(PSMSC_THREAD threadHandle)
{
	/* Thread resume, not implemented */
}

void smsc_thread_suspend(PSMSC_THREAD threadHandle)
{
	/* Thread suspend, not implemented */
}

void smsc_thread_set_priority(PSMSC_THREAD threadHandle, int priority)
{
	struct sched_param param;
	param.sched_priority=priority;
	pthread_setschedparam(threadHandle, SCHED_OTHER, &param);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -