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

📄 ipc.c.svn-base

📁 RT-Thread是发展中的下一代微内核嵌入式实时操作系统
💻 SVN-BASE
📖 第 1 页 / 共 4 页
字号:
				}				/* insert thread */				rt_list_insert_before(&(event->thread_list[bit]), &(thread->tlist));			}			break;		}		/* if there is timeout, active thread timer */		if (timeout > 0)		{			/* reset the timeout of thread timer and start it */			rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);			rt_timer_start(&(thread->thread_timer));		}	}	/* enable interrupt */	rt_hw_interrupt_enable(level);#ifdef RT_USING_HOOK	if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(event->parent));#endif	return thread->error;}/** * This function can get or set some extra attributions of a fast event object. * * @param event the event object * @param cmd the execution command * @param arg the execution argument * * @return the error code */rt_err_t rt_fast_event_control (rt_fast_event_t event, rt_uint8 cmd, void* arg){	return RT_EOK;}#endif#ifdef RT_USING_EVENT/** * This function will initialize an event and put it under control of resource * management. * * @param event the event object * @param name the name of event * @param flag the flag of event * * @return the operation status, RT_EOK on successful */rt_err_t rt_event_init(rt_event_t event, const char* name, rt_uint8 flag){	RT_ASSERT(event != RT_NULL);	/* init object */	rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name);	/* set parent flag */	event->parent.parent.flag = flag;	/* init ipc object */	rt_ipc_object_init(&(event->parent));	/* init event */	event->set = 0;	return RT_EOK;}/** * This function will detach an event object from resource management * * @param event the event object * * @return the operation status, RT_EOK on successful */rt_err_t rt_event_detach(rt_event_t event){	/* parameter check */	RT_ASSERT(event != RT_NULL);	/* resume all suspended thread */	rt_ipc_object_resume_all(&(event->parent));	/* detach mailbox object */	rt_object_detach(&(event->parent.parent));	return RT_EOK;}#ifdef RT_USING_HEAP/** * This function will create an event object from system resource * * @param name the name of event * @param flag the flag of event * * @return the created event, RT_NULL on error happen */rt_event_t rt_event_create (const char* name, rt_uint8 flag){	rt_event_t event;	/* allocate object */	event = (rt_event_t) rt_object_allocate(RT_Object_Class_Event, name);	if (event == RT_NULL) return event;	/* set parent */	event->parent.parent.flag = flag;	/* init ipc object */	rt_ipc_object_init(&(event->parent));	/* init event */	event->set = 0;	return event;}/** * This function will delete an event object and release the memory * * @param event the event object * * @return the error code */rt_err_t rt_event_delete (rt_event_t event){	/* parameter check */	RT_ASSERT(event != RT_NULL);	/* resume all suspended thread */	rt_ipc_object_resume_all(&(event->parent));	/* delete event object */	rt_object_delete(&(event->parent.parent));	return RT_EOK;}#endif/** * This function will send an event to the event object, if there are threads  * suspended on event object, it will be waked up. * * @param event the event object * @param set the event set * * @return the error code */rt_err_t rt_event_send(rt_event_t event, rt_uint32 set){	struct rt_list_node *n;	struct rt_thread *thread;	register rt_ubase_t level;	register rt_base_t status;	/* parameter check */	RT_ASSERT(event != RT_NULL);	if (set == 0) return -RT_ERROR;#ifdef RT_USING_HOOK	if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(event->parent.parent));#endif	/* disable interrupt */	level = rt_hw_interrupt_disable();	/* set event */	event->set |= set;	if (event->parent.suspend_thread_count > 0)	{		/* search thread list to resume thread */		n = event->parent.suspend_thread.next;		while (n != &(event->parent.suspend_thread))		{			/* get thread */			thread = rt_list_entry(n, struct rt_thread, tlist);			status = -RT_ERROR;			if (thread->event_info & RT_EVENT_FLAG_AND)			{				if ((thread->event_set & event->set) == thread->event_set)				{					status = RT_EOK;				}			}			else if (thread->event_info & RT_EVENT_FLAG_OR)			{				if (thread->event_set & event->set)				{					status = RT_EOK;				}			}			/* move node to the nexe */			n = n->next;			/* condition is satisfied, resume thread */			if (status == RT_EOK)			{				/* resume thread, and thread list breaks out */				rt_thread_resume(thread);				/* decrease suspended thread count */				event->parent.suspend_thread_count--;				if (thread->event_info & RT_EVENT_FLAG_CLEAR)					event->set &= ~thread->event_set;			}		}	}	/* enable interrupt */	rt_hw_interrupt_enable(level);	/* do a schedule */	rt_schedule();	return RT_EOK;}/** * This function will receive an event from event object, if the event is unavailable,  * the thread shall wait for a specified time. * * @param event the fast event object * @param set the interested event set * @param option the receive option * @param timeout the waiting time * @param recved the received event * * @return the error code */rt_err_t rt_event_recv(rt_event_t event, rt_uint32 set, rt_uint8 option, rt_int32 timeout, rt_uint32* recved){	struct rt_thread *thread;	register rt_ubase_t level;	register rt_base_t status;	/* parameter check */	RT_ASSERT(event != RT_NULL);	if (set == 0) return -RT_ERROR;	/* init status */	status = -RT_ERROR;#ifdef RT_USING_HOOK	if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(event->parent.parent));#endif	/* disable interrupt */	level = rt_hw_interrupt_disable();	/* check event set */	if (option & RT_EVENT_FLAG_AND)	{		if ((event->set & set) == set) status = RT_EOK;	}	else if (option & RT_EVENT_FLAG_OR)	{		if (event->set & set) status = RT_EOK;	}	/* get current thread */	thread = rt_thread_self();	/* reset thread error */	thread->error = RT_EOK;	if (status == RT_EOK)	{		/* set received event */		*recved = event->set;		/* received event */		if (option & RT_EVENT_FLAG_CLEAR) event->set &= ~set;	}	else if (timeout == 0)	{		/* no waiting */		thread->error = -RT_ETIMEOUT;	}	else	{		/* fill thread event info */		thread->event_set  = set;		thread->event_info = option;		/* put thread to suspended thread list */		rt_ipc_object_suspend(&(event->parent), thread);		/* if there is a waiting timeout, active thread timer */		if (timeout > 0)		{			/* reset the timeout of thread timer and start it */			rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);			rt_timer_start(&(thread->thread_timer));		}		/* enable interrupt */		rt_hw_interrupt_enable(level);		/* do a schedule */		rt_schedule();		if (thread->error != RT_EOK) return thread->error;		/* disable interrupt */		level = rt_hw_interrupt_disable();		/* get received event */		*recved = event->set;	}	/* enable interrupt */	rt_hw_interrupt_enable(level);#ifdef RT_USING_HOOK	if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(event->parent.parent));#endif	return thread->error;}/** * This function can get or set some extra attributions of an event object. * * @param event the event object * @param cmd the execution command * @param arg the execution argument * * @return the error code */rt_err_t rt_event_control (rt_event_t event, rt_uint8 cmd, void* arg){	return RT_EOK;}#endif /* end of RT_USING_EVENT */#ifdef RT_USING_MAILBOX/** * This function will initialize a mailbox and put it under control of resource * management. * * @param mb the mailbox object * @param name the name of mailbox * @param msgpool the begin address of buffer to save received mail * @param size the size of mailbox * @param flag the flag of mailbox * * @return the operation status, RT_EOK on successful */rt_err_t rt_mb_init(rt_mailbox_t mb, const char* name, void* msgpool, rt_size_t size, rt_uint8 flag){	RT_ASSERT(mb != RT_NULL);	/* init object */	rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);	/* set parent flag */	mb->parent.parent.flag = flag;	/* init ipc object */	rt_ipc_object_init(&(mb->parent));	/* init mailbox */	mb->msg_pool = msgpool;	mb->size 	 = size;	mb->in_offset 	= 0;	mb->out_offset 	= 0;	return RT_EOK;}/** * This function will detach a mailbox from resource management * * @param mb the mailbox object * * @return the operation status, RT_EOK on successful */rt_err_t rt_mb_detach(rt_mailbox_t mb){	/* parameter check */	RT_ASSERT(mb != RT_NULL);	/* resume all suspended thread */	rt_ipc_object_resume_all(&(mb->parent));	/* detach mailbox object */	rt_object_detach(&(mb->parent.parent));	return RT_EOK;}#ifdef RT_USING_HEAP/** * This function will create a mailbox object from system resource * * @param name the name of mailbox * @param size the size of mailbox * @param flag the flag of mailbox * * @return the created mailbox, RT_NULL on error happen */rt_mailbox_t rt_mb_create (const char* name, rt_size_t size, rt_uint8 flag){	rt_mailbox_t mb;	/* allocate object */	mb = (rt_mailbox_t) rt_object_allocate(RT_Object_Class_MailBox, name);	if (mb == RT_NULL) return mb;	/* set parent */	mb->parent.parent.flag = flag;	/* init ipc object */	rt_ipc_object_init(&(mb->parent));	/* init mailbox */	mb->size 	 	= size;	mb->msg_pool 	= rt_malloc(mb->size);	if (mb->msg_pool == RT_NULL)	{		/* delete mailbox object */		rt_object_delete(&(mb->parent.parent));		return RT_NULL;	}	mb->in_offset 	= 0;	mb->out_offset 	= 0;	return mb;}/** * This function will delete a mailbox object and release the memory * * @param mb the mailbox object * * @return the error code */rt_err_t rt_mb_delete (rt_mailbox_t mb){	/* parameter check */	RT_ASSERT(mb != RT_NULL);	/* resume all suspended thread */	rt_ipc_object_resume_all(&(mb->parent));	/* free mailbox pool */	rt_free(mb->msg_pool);	/* delete mailbox object */	rt_object_delete(&(mb->parent.parent));	return RT_EOK;}#endif/** * This function will send a mail to mailbox object, if there are threads suspended  * on mailbox object, it will be waked up. * * @param mb the mailbox object * @param value the mail * * @return the error code */rt_err_t rt_mb_send (rt_mailbox_t mb, rt_uint32 value){	register rt_ubase_t temp;	/* parameter check */	RT_ASSERT(mb != RT_NULL);#ifdef RT_USING_HOOK	if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(mb->parent.parent));#endif	/* disable interrupt */	temp = rt_hw_interrupt_disable();	/* mailbox is full */	if (mb->entry == mb->size)	{		/* enable interrupt */		rt_hw_interrupt_enable(temp);		return -RT_EFULL;	}	/* set ptr */	mb->msg_pool[mb->in_offset] = value;	/* increase input offset */	mb->in_offset = (++ mb->in_offset) % mb->size;	/* increase message entry */	mb->entry ++;	/* resume suspended thread */	if (mb->parent.suspend_thread_count > 0)	{		rt_ipc_object_resume(&(mb->parent));	}	/* enable interrupt */	rt_hw_interrupt_enable(temp);	rt_schedule();	return RT_EOK;}/** * This function will receive a mail from mailbox object, if there is no mail in * mailbox object, the thread shall wait for a specified time. * * @param mb the mailbox object * @param value the received mail will be saved in * @param timeout the waiting time * * @return the error code */rt_err_t rt_mb_recv (rt_mailbox_t mb, rt_uint32* value, rt_int32 timeout){	struct rt_thread *thread;	register rt_ubase_t temp;

⌨️ 快捷键说明

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