|
@@ -6,12 +6,18 @@
|
|
|
* Change Logs:
|
|
|
* Date Author Notes
|
|
|
* 2019-10-12 Jesven first version
|
|
|
+ * 2023-07-25 Shell Remove usage of rt_hw_interrupt API in the lwp
|
|
|
* 2023-09-16 zmq810150896 Increased versatility of some features on dfs v2
|
|
|
*/
|
|
|
+
|
|
|
+#define DBG_TAG "lwp.ipc"
|
|
|
+#define DBG_LVL DBG_WARNING
|
|
|
+#include <rtdbg.h>
|
|
|
+
|
|
|
#include <rtthread.h>
|
|
|
#include <rthw.h>
|
|
|
-#include <lwp.h>
|
|
|
|
|
|
+#include "lwp_internal.h"
|
|
|
#include "lwp_ipc.h"
|
|
|
#include "lwp_ipc_internal.h"
|
|
|
|
|
@@ -49,6 +55,10 @@ static rt_ipc_msg_t _ipc_msg_free_list = (rt_ipc_msg_t)RT_NULL; /* released chai
|
|
|
static int rt_ipc_msg_used = 0; /* first unallocated entry */
|
|
|
static struct rt_ipc_msg ipc_msg_pool[RT_CH_MSG_MAX_NR]; /* initial message array */
|
|
|
|
|
|
+static rt_spinlock_t ipc_big_lock;
|
|
|
+#define ipc_list_lock ipc_big_lock
|
|
|
+#define ipc_ch_lock ipc_big_lock
|
|
|
+
|
|
|
/**
|
|
|
* Allocate an IPC message from the statically-allocated array.
|
|
|
*/
|
|
@@ -119,19 +129,16 @@ rt_inline rt_err_t rt_channel_list_resume(rt_list_t *list)
|
|
|
/**
|
|
|
* Wakeup all the suspended threads in the list.
|
|
|
*/
|
|
|
-rt_inline rt_err_t rt_channel_list_resume_all(rt_list_t *list)
|
|
|
+rt_inline rt_err_t _channel_list_resume_all_locked(rt_list_t *list)
|
|
|
{
|
|
|
struct rt_thread *thread;
|
|
|
- register rt_ubase_t temp;
|
|
|
|
|
|
/* wakeup all suspended threads for sending */
|
|
|
while (!rt_list_isempty(list))
|
|
|
{
|
|
|
- temp = rt_hw_interrupt_disable();
|
|
|
thread = rt_list_entry(list->next, struct rt_thread, tlist);
|
|
|
thread->error = -RT_ERROR;
|
|
|
rt_thread_resume(thread);
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
}
|
|
|
|
|
|
return RT_EOK;
|
|
@@ -154,19 +161,14 @@ rt_inline rt_err_t rt_channel_list_suspend(rt_list_t *list, struct rt_thread *th
|
|
|
}
|
|
|
|
|
|
|
|
|
-static void _rt_channel_check_wq_wakup(rt_channel_t ch)
|
|
|
+static void _rt_channel_check_wq_wakup_locked(rt_channel_t ch)
|
|
|
{
|
|
|
- rt_base_t level;
|
|
|
-
|
|
|
- level = rt_hw_interrupt_disable();
|
|
|
if (rt_list_isempty(&ch->wait_msg))
|
|
|
{
|
|
|
- rt_hw_interrupt_enable(level);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
rt_wqueue_wakeup(&ch->reader_queue, 0);
|
|
|
- rt_hw_interrupt_enable(level);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -174,28 +176,39 @@ static void _rt_channel_check_wq_wakup(rt_channel_t ch)
|
|
|
*/
|
|
|
rt_channel_t rt_raw_channel_open(const char *name, int flags)
|
|
|
{
|
|
|
- register rt_ubase_t temp = 0;
|
|
|
+ rt_err_t err = RT_EOK;
|
|
|
rt_channel_t ch = RT_NULL;
|
|
|
|
|
|
struct rt_object *object;
|
|
|
struct rt_list_node *node;
|
|
|
struct rt_object_information *information;
|
|
|
|
|
|
- temp = rt_hw_interrupt_disable();
|
|
|
+ RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Brief: Match an existing channel from object list with the same name
|
|
|
+ * If no such channel found, it will create a new channel if O_CREAT
|
|
|
+ * is set in the flag
|
|
|
+ *
|
|
|
+ * Note: Critical Section
|
|
|
+ * - Channel Object list (RW; this may write to a channel if needed, and
|
|
|
+ * the RCU operation of the routine should be atomic)
|
|
|
+ */
|
|
|
+ rt_spin_lock(&ipc_list_lock);
|
|
|
information = rt_object_get_information(RT_Object_Class_Channel);
|
|
|
RT_ASSERT(information != RT_NULL);
|
|
|
|
|
|
- /* retrieve the existing IPC channels */
|
|
|
- for (node = information->object_list.next;
|
|
|
- node != &(information->object_list);
|
|
|
- node = node->next)
|
|
|
+ for (node = information->object_list.next;
|
|
|
+ node != &(information->object_list);
|
|
|
+ node = node->next)
|
|
|
{
|
|
|
object = rt_list_entry(node, struct rt_object, list);
|
|
|
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
|
|
|
{
|
|
|
if ((flags & O_CREAT) && (flags & O_EXCL))
|
|
|
{
|
|
|
- goto quit;
|
|
|
+ err = -RT_EFULL;
|
|
|
+ break;
|
|
|
}
|
|
|
/* find the IPC channel with the specific name */
|
|
|
ch = (rt_channel_t)object;
|
|
@@ -203,31 +216,30 @@ rt_channel_t rt_raw_channel_open(const char *name, int flags)
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if (!ch) /* create a new IPC channel */
|
|
|
+
|
|
|
+ if (!ch && err == RT_EOK)
|
|
|
{
|
|
|
+ /* create a new IPC channel */
|
|
|
if (flags & O_CREAT)
|
|
|
{
|
|
|
- RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
-
|
|
|
/* allocate a real IPC channel structure */
|
|
|
ch = (rt_channel_t)rt_object_allocate(RT_Object_Class_Channel, name);
|
|
|
}
|
|
|
|
|
|
- if (!ch)
|
|
|
+ if (ch)
|
|
|
{
|
|
|
- goto quit;
|
|
|
+ rt_channel_object_init(&ch->parent); /* suspended receivers */
|
|
|
+ rt_list_init(&ch->wait_msg); /* unhandled messages */
|
|
|
+ rt_list_init(&ch->wait_thread); /* suspended senders */
|
|
|
+ rt_wqueue_init(&ch->reader_queue); /* reader poll queue */
|
|
|
+ ch->reply = RT_NULL;
|
|
|
+ ch->stat = RT_IPC_STAT_IDLE; /* no suspended threads */
|
|
|
+ ch->ref = 1;
|
|
|
}
|
|
|
-
|
|
|
- rt_channel_object_init(&ch->parent); /* suspended receivers */
|
|
|
- rt_list_init(&ch->wait_msg); /* unhandled messages */
|
|
|
- rt_list_init(&ch->wait_thread); /* suspended senders */
|
|
|
- rt_wqueue_init(&ch->reader_queue); /* reader poll queue */
|
|
|
- ch->reply = RT_NULL;
|
|
|
- ch->stat = RT_IPC_STAT_IDLE; /* no suspended threads */
|
|
|
- ch->ref = 1;
|
|
|
}
|
|
|
-quit:
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
+
|
|
|
+ rt_spin_unlock(&ipc_list_lock);
|
|
|
+
|
|
|
return ch;
|
|
|
}
|
|
|
|
|
@@ -236,46 +248,56 @@ quit:
|
|
|
*/
|
|
|
rt_err_t rt_raw_channel_close(rt_channel_t ch)
|
|
|
{
|
|
|
- register rt_ubase_t temp;
|
|
|
+ rt_err_t rc = RT_EOK;
|
|
|
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
|
|
|
if (ch == RT_NULL)
|
|
|
{
|
|
|
- return -RT_EIO;
|
|
|
- }
|
|
|
-
|
|
|
- temp = rt_hw_interrupt_disable();
|
|
|
- if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
|
|
|
- {
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_EIO;
|
|
|
+ rc = -RT_EIO;
|
|
|
}
|
|
|
- if (rt_object_is_systemobject(&ch->parent.parent) != RT_FALSE)
|
|
|
+ else
|
|
|
{
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_EIO;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * Brief: Remove the channel from object list
|
|
|
+ *
|
|
|
+ * Note: Critical Section
|
|
|
+ * - the channel
|
|
|
+ */
|
|
|
+ rt_spin_lock(&ipc_ch_lock);
|
|
|
|
|
|
- if (ch->ref == 0)
|
|
|
- {
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_EIO;
|
|
|
- }
|
|
|
- ch->ref--;
|
|
|
- if (ch->ref == 0)
|
|
|
- {
|
|
|
- /* wakeup all the suspended receivers and senders */
|
|
|
- rt_channel_list_resume_all(&ch->parent.suspend_thread);
|
|
|
- rt_channel_list_resume_all(&ch->wait_thread);
|
|
|
+ if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
|
|
|
+ {
|
|
|
+ rc = -RT_EIO;
|
|
|
+ }
|
|
|
+ else if (rt_object_is_systemobject(&ch->parent.parent) != RT_FALSE)
|
|
|
+ {
|
|
|
+ rc = -RT_EIO;
|
|
|
+ }
|
|
|
+ else if (ch->ref == 0)
|
|
|
+ {
|
|
|
+ rc = -RT_EIO;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ch->ref--;
|
|
|
+ if (ch->ref == 0)
|
|
|
+ {
|
|
|
+ /* wakeup all the suspended receivers and senders */
|
|
|
+ _channel_list_resume_all_locked(&ch->parent.suspend_thread);
|
|
|
+ _channel_list_resume_all_locked(&ch->wait_thread);
|
|
|
|
|
|
- /* all ipc msg will lost */
|
|
|
- rt_list_init(&ch->wait_msg);
|
|
|
+ /* all ipc msg will lost */
|
|
|
+ rt_list_init(&ch->wait_msg);
|
|
|
|
|
|
- rt_object_delete(&ch->parent.parent); /* release the IPC channel structure */
|
|
|
+ rt_object_delete(&ch->parent.parent); /* release the IPC channel structure */
|
|
|
+ }
|
|
|
+ rc = RT_EOK;
|
|
|
+ }
|
|
|
+ rt_spin_unlock(&ipc_ch_lock);
|
|
|
}
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return RT_EOK;
|
|
|
+
|
|
|
+ return rc;
|
|
|
}
|
|
|
|
|
|
static rt_err_t wakeup_sender_wait_recv(void *object, struct rt_thread *thread)
|
|
@@ -430,16 +452,15 @@ static int _ipc_msg_fd_new(void *file)
|
|
|
return fd;
|
|
|
}
|
|
|
|
|
|
+static rt_err_t _do_send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, int need_reply, rt_channel_msg_t data_ret, rt_int32_t time, rt_ipc_msg_t msg);
|
|
|
+
|
|
|
/**
|
|
|
* Send data through an IPC channel, wait for the reply or not.
|
|
|
*/
|
|
|
-static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, int need_reply, rt_channel_msg_t data_ret, rt_int32_t time)
|
|
|
+static rt_err_t _send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, int need_reply, rt_channel_msg_t data_ret, rt_int32_t time)
|
|
|
{
|
|
|
rt_ipc_msg_t msg;
|
|
|
- struct rt_thread *thread_recv, *thread_send = 0;
|
|
|
- register rt_base_t temp;
|
|
|
- rt_err_t ret;
|
|
|
- void (*old_timeout_func)(void *) = 0;
|
|
|
+ rt_err_t rc = -RT_ERROR;
|
|
|
|
|
|
if (need_reply)
|
|
|
{
|
|
@@ -448,29 +469,38 @@ static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_ms
|
|
|
|
|
|
if (ch == RT_NULL)
|
|
|
{
|
|
|
- return -RT_EIO;
|
|
|
- }
|
|
|
-
|
|
|
- temp = rt_hw_interrupt_disable();
|
|
|
-
|
|
|
- if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
|
|
|
- {
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_EIO;
|
|
|
+ rc = -RT_EIO;
|
|
|
}
|
|
|
- if (need_reply && time == 0)
|
|
|
+ else
|
|
|
{
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_ETIMEOUT;
|
|
|
+ if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
|
|
|
+ {
|
|
|
+ rc = -RT_EIO;
|
|
|
+ }
|
|
|
+ else if (need_reply && time == 0)
|
|
|
+ {
|
|
|
+ rc = -RT_ETIMEOUT;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ /* allocate an IPC message */
|
|
|
+ msg = _ipc_msg_alloc();
|
|
|
+ if (!msg)
|
|
|
+ rc = -RT_ENOMEM;
|
|
|
+ else
|
|
|
+ rc = _do_send_recv_timeout(ch, data, need_reply, data_ret, time, msg);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /* allocate an IPC message */
|
|
|
- msg = _ipc_msg_alloc();
|
|
|
- if (!msg)
|
|
|
- {
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_ENOMEM;
|
|
|
- }
|
|
|
+ return rc;
|
|
|
+}
|
|
|
+
|
|
|
+static rt_err_t _do_send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, int need_reply, rt_channel_msg_t data_ret, rt_int32_t time, rt_ipc_msg_t msg)
|
|
|
+{
|
|
|
+ DEF_RETURN_CODE(rc);
|
|
|
+ rt_thread_t thread_recv;
|
|
|
+ rt_thread_t thread_send = 0;
|
|
|
+ void (*old_timeout_func)(void *) = 0;
|
|
|
|
|
|
/* IPC message : file descriptor */
|
|
|
if (data->type == RT_CHANNEL_FD)
|
|
@@ -486,43 +516,48 @@ static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_ms
|
|
|
thread_send->error = RT_EOK;
|
|
|
}
|
|
|
|
|
|
+ rt_spin_lock(&ipc_ch_lock);
|
|
|
+
|
|
|
switch (ch->stat)
|
|
|
{
|
|
|
case RT_IPC_STAT_IDLE:
|
|
|
case RT_IPC_STAT_ACTIVE:
|
|
|
if (need_reply)
|
|
|
{
|
|
|
- ret = rt_channel_list_suspend(&ch->wait_thread, thread_send);
|
|
|
- if (ret != RT_EOK)
|
|
|
+ rc = rt_channel_list_suspend(&ch->wait_thread, thread_send);
|
|
|
+ if (rc != RT_EOK)
|
|
|
{
|
|
|
_ipc_msg_free(msg);
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return ret;
|
|
|
}
|
|
|
- rt_thread_wakeup_set(thread_send, wakeup_sender_wait_recv, (void*)ch);
|
|
|
- if (time > 0)
|
|
|
+ else
|
|
|
{
|
|
|
- rt_timer_control(&(thread_send->thread_timer),
|
|
|
- RT_TIMER_CTRL_GET_FUNC,
|
|
|
- &old_timeout_func);
|
|
|
- rt_timer_control(&(thread_send->thread_timer),
|
|
|
- RT_TIMER_CTRL_SET_FUNC,
|
|
|
- sender_timeout);
|
|
|
- /* reset the timeout of thread timer and start it */
|
|
|
- rt_timer_control(&(thread_send->thread_timer),
|
|
|
- RT_TIMER_CTRL_SET_TIME,
|
|
|
- &time);
|
|
|
- rt_timer_start(&(thread_send->thread_timer));
|
|
|
+ rt_thread_wakeup_set(thread_send, wakeup_sender_wait_recv, (void*)ch);
|
|
|
+ if (time > 0)
|
|
|
+ {
|
|
|
+ rt_timer_control(&(thread_send->thread_timer),
|
|
|
+ RT_TIMER_CTRL_GET_FUNC,
|
|
|
+ &old_timeout_func);
|
|
|
+ rt_timer_control(&(thread_send->thread_timer),
|
|
|
+ RT_TIMER_CTRL_SET_FUNC,
|
|
|
+ sender_timeout);
|
|
|
+ /* reset the timeout of thread timer and start it */
|
|
|
+ rt_timer_control(&(thread_send->thread_timer),
|
|
|
+ RT_TIMER_CTRL_SET_TIME,
|
|
|
+ &time);
|
|
|
+ rt_timer_start(&(thread_send->thread_timer));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- /*
|
|
|
+
|
|
|
+ /**
|
|
|
* If there is no thread waiting for messages, chain the message
|
|
|
* into the list.
|
|
|
*/
|
|
|
- rt_list_insert_before(&ch->wait_msg, &msg->mlist);
|
|
|
+ if (rc == RT_EOK)
|
|
|
+ rt_list_insert_before(&ch->wait_msg, &msg->mlist);
|
|
|
break;
|
|
|
case RT_IPC_STAT_WAIT:
|
|
|
- /*
|
|
|
+ /**
|
|
|
* If there are suspended receivers on the IPC channel, transfer the
|
|
|
* pointer of the message to the first receiver directly and wake it
|
|
|
* up.
|
|
@@ -531,80 +566,85 @@ static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_ms
|
|
|
|
|
|
if (need_reply)
|
|
|
{
|
|
|
- ret = rt_channel_list_suspend(&ch->wait_thread, thread_send);
|
|
|
- if (ret != RT_EOK)
|
|
|
+ rc = rt_channel_list_suspend(&ch->wait_thread, thread_send);
|
|
|
+ if (rc != RT_EOK)
|
|
|
{
|
|
|
_ipc_msg_free(msg);
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return ret;
|
|
|
}
|
|
|
- ch->reply = thread_send; /* record the current waiting sender */
|
|
|
- ch->stat = RT_IPC_STAT_ACTIVE;
|
|
|
- rt_thread_wakeup_set(thread_send, wakeup_sender_wait_reply, (void*)ch);
|
|
|
- if (time > 0)
|
|
|
+ else
|
|
|
{
|
|
|
- rt_timer_control(&(thread_send->thread_timer),
|
|
|
- RT_TIMER_CTRL_GET_FUNC,
|
|
|
- &old_timeout_func);
|
|
|
- rt_timer_control(&(thread_send->thread_timer),
|
|
|
- RT_TIMER_CTRL_SET_FUNC,
|
|
|
- sender_timeout);
|
|
|
- /* reset the timeout of thread timer and start it */
|
|
|
- rt_timer_control(&(thread_send->thread_timer),
|
|
|
- RT_TIMER_CTRL_SET_TIME,
|
|
|
- &time);
|
|
|
- rt_timer_start(&(thread_send->thread_timer));
|
|
|
+ ch->reply = thread_send; /* record the current waiting sender */
|
|
|
+ ch->stat = RT_IPC_STAT_ACTIVE;
|
|
|
+ rt_thread_wakeup_set(thread_send, wakeup_sender_wait_reply, (void*)ch);
|
|
|
+ if (time > 0)
|
|
|
+ {
|
|
|
+ rt_timer_control(&(thread_send->thread_timer),
|
|
|
+ RT_TIMER_CTRL_GET_FUNC,
|
|
|
+ &old_timeout_func);
|
|
|
+ rt_timer_control(&(thread_send->thread_timer),
|
|
|
+ RT_TIMER_CTRL_SET_FUNC,
|
|
|
+ sender_timeout);
|
|
|
+ /* reset the timeout of thread timer and start it */
|
|
|
+ rt_timer_control(&(thread_send->thread_timer),
|
|
|
+ RT_TIMER_CTRL_SET_TIME,
|
|
|
+ &time);
|
|
|
+ rt_timer_start(&(thread_send->thread_timer));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
ch->stat = RT_IPC_STAT_IDLE;
|
|
|
}
|
|
|
- thread_recv = rt_list_entry(ch->parent.suspend_thread.next, struct rt_thread, tlist);
|
|
|
- thread_recv->msg_ret = msg; /* to the first suspended receiver */
|
|
|
- thread_recv->error = RT_EOK;
|
|
|
- rt_channel_list_resume(&ch->parent.suspend_thread);
|
|
|
+
|
|
|
+ if (!need_reply || rc == RT_EOK)
|
|
|
+ {
|
|
|
+ thread_recv = rt_list_entry(ch->parent.suspend_thread.next, struct rt_thread, tlist);
|
|
|
+ thread_recv->msg_ret = msg; /* to the first suspended receiver */
|
|
|
+ thread_recv->error = RT_EOK;
|
|
|
+ rt_channel_list_resume(&ch->parent.suspend_thread);
|
|
|
+ }
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- if ( ch->stat == RT_IPC_STAT_IDLE)
|
|
|
+ if (rc == RT_EOK)
|
|
|
{
|
|
|
- _rt_channel_check_wq_wakup(ch);
|
|
|
- }
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
-
|
|
|
- /* reschedule in order to let the potential receivers run */
|
|
|
- rt_schedule();
|
|
|
-
|
|
|
- if (need_reply)
|
|
|
- {
|
|
|
- temp = rt_hw_interrupt_disable();
|
|
|
- if (old_timeout_func)
|
|
|
+ if (ch->stat == RT_IPC_STAT_IDLE)
|
|
|
{
|
|
|
- rt_timer_control(&(thread_send->thread_timer),
|
|
|
- RT_TIMER_CTRL_SET_FUNC,
|
|
|
- old_timeout_func);
|
|
|
+ _rt_channel_check_wq_wakup_locked(ch);
|
|
|
}
|
|
|
- ret = thread_send->error;
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
+ rt_spin_unlock(&ipc_ch_lock);
|
|
|
|
|
|
- if (ret != RT_EOK)
|
|
|
+ /* reschedule in order to let the potential receivers run */
|
|
|
+ rt_schedule();
|
|
|
+
|
|
|
+ rt_spin_lock(&ipc_ch_lock);
|
|
|
+ if (need_reply)
|
|
|
{
|
|
|
- return ret;
|
|
|
- }
|
|
|
+ if (old_timeout_func)
|
|
|
+ {
|
|
|
+ rt_timer_control(&(thread_send->thread_timer),
|
|
|
+ RT_TIMER_CTRL_SET_FUNC,
|
|
|
+ old_timeout_func);
|
|
|
+ }
|
|
|
+ rc = thread_send->error;
|
|
|
+
|
|
|
+ if (rc == RT_EOK)
|
|
|
+ {
|
|
|
+ /* If the sender gets the chance to run, the requested reply must be valid. */
|
|
|
+ RT_ASSERT(data_ret != RT_NULL);
|
|
|
+ *data_ret = ((rt_ipc_msg_t)(thread_send->msg_ret))->msg; /* extract data */
|
|
|
+ _ipc_msg_free(thread_send->msg_ret); /* put back the message to kernel */
|
|
|
|
|
|
- /* If the sender gets the chance to run, the requested reply must be valid. */
|
|
|
- RT_ASSERT(data_ret != RT_NULL);
|
|
|
- *data_ret = ((rt_ipc_msg_t)(thread_send->msg_ret))->msg; /* extract data */
|
|
|
- temp = rt_hw_interrupt_disable();
|
|
|
- _ipc_msg_free(thread_send->msg_ret); /* put back the message to kernel */
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- thread_send->msg_ret = RT_NULL;
|
|
|
+ thread_send->msg_ret = RT_NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+ rt_spin_unlock(&ipc_ch_lock);
|
|
|
|
|
|
- return RT_EOK;
|
|
|
+ return rc;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -612,7 +652,7 @@ static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_ms
|
|
|
*/
|
|
|
rt_err_t rt_raw_channel_send(rt_channel_t ch, rt_channel_msg_t data)
|
|
|
{
|
|
|
- return _rt_raw_channel_send_recv_timeout(ch, data, 0, 0, RT_WAITING_FOREVER);
|
|
|
+ return _send_recv_timeout(ch, data, 0, 0, RT_WAITING_FOREVER);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -620,7 +660,7 @@ rt_err_t rt_raw_channel_send(rt_channel_t ch, rt_channel_msg_t data)
|
|
|
*/
|
|
|
rt_err_t rt_raw_channel_send_recv(rt_channel_t ch, rt_channel_msg_t data, rt_channel_msg_t data_ret)
|
|
|
{
|
|
|
- return _rt_raw_channel_send_recv_timeout(ch, data, 1, data_ret, RT_WAITING_FOREVER);
|
|
|
+ return _send_recv_timeout(ch, data, 1, data_ret, RT_WAITING_FOREVER);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -628,7 +668,7 @@ rt_err_t rt_raw_channel_send_recv(rt_channel_t ch, rt_channel_msg_t data, rt_cha
|
|
|
*/
|
|
|
rt_err_t rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_channel_msg_t data_ret, rt_int32_t time)
|
|
|
{
|
|
|
- return _rt_raw_channel_send_recv_timeout(ch, data, 1, data_ret, time);
|
|
|
+ return _send_recv_timeout(ch, data, 1, data_ret, time);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -636,56 +676,58 @@ rt_err_t rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data
|
|
|
*/
|
|
|
rt_err_t rt_raw_channel_reply(rt_channel_t ch, rt_channel_msg_t data)
|
|
|
{
|
|
|
+ DEF_RETURN_CODE(rc);
|
|
|
rt_ipc_msg_t msg;
|
|
|
struct rt_thread *thread;
|
|
|
- register rt_base_t temp;
|
|
|
|
|
|
if (ch == RT_NULL)
|
|
|
{
|
|
|
- return -RT_EIO;
|
|
|
- }
|
|
|
-
|
|
|
- temp = rt_hw_interrupt_disable();
|
|
|
-
|
|
|
- if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
|
|
|
- {
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_EIO;
|
|
|
- }
|
|
|
-
|
|
|
- if (ch->stat != RT_IPC_STAT_ACTIVE)
|
|
|
- {
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_ERROR;
|
|
|
+ rc = -RT_EIO;
|
|
|
}
|
|
|
-
|
|
|
- if (ch->reply == RT_NULL)
|
|
|
+ else
|
|
|
{
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_ERROR;
|
|
|
- }
|
|
|
+ rt_spin_lock(&ipc_ch_lock);
|
|
|
|
|
|
- /* allocate an IPC message */
|
|
|
- msg = _ipc_msg_alloc();
|
|
|
- if (!msg)
|
|
|
- {
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_ENOMEM;
|
|
|
- }
|
|
|
+ if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
|
|
|
+ {
|
|
|
+ rc = -RT_EIO;
|
|
|
+ }
|
|
|
+ else if (ch->stat != RT_IPC_STAT_ACTIVE)
|
|
|
+ {
|
|
|
+ rc = -RT_ERROR;
|
|
|
+ }
|
|
|
+ else if (ch->reply == RT_NULL)
|
|
|
+ {
|
|
|
+ rc = -RT_ERROR;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ /* allocate an IPC message */
|
|
|
+ msg = _ipc_msg_alloc();
|
|
|
+ if (!msg)
|
|
|
+ {
|
|
|
+ rc = -RT_ENOMEM;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rt_ipc_msg_init(msg, data, 0);
|
|
|
|
|
|
- rt_ipc_msg_init(msg, data, 0);
|
|
|
+ thread = ch->reply;
|
|
|
+ thread->msg_ret = msg; /* transfer the reply to the sender */
|
|
|
+ rt_thread_resume(thread); /* wake up the sender */
|
|
|
+ ch->stat = RT_IPC_STAT_IDLE;
|
|
|
+ ch->reply = RT_NULL;
|
|
|
|
|
|
- thread = ch->reply;
|
|
|
- thread->msg_ret = msg; /* transfer the reply to the sender */
|
|
|
- rt_thread_resume(thread); /* wake up the sender */
|
|
|
- ch->stat = RT_IPC_STAT_IDLE;
|
|
|
- ch->reply = RT_NULL;
|
|
|
+ _rt_channel_check_wq_wakup_locked(ch);
|
|
|
+ rc = RT_EOK;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rt_spin_unlock(&ipc_ch_lock);
|
|
|
|
|
|
- _rt_channel_check_wq_wakup(ch);
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- rt_schedule();
|
|
|
+ rt_schedule();
|
|
|
+ }
|
|
|
|
|
|
- return RT_EOK;
|
|
|
+ RETURN(rc);
|
|
|
}
|
|
|
|
|
|
static rt_err_t wakeup_receiver(void *object, struct rt_thread *thread)
|
|
@@ -697,7 +739,11 @@ static rt_err_t wakeup_receiver(void *object, struct rt_thread *thread)
|
|
|
ch->stat = RT_IPC_STAT_IDLE;
|
|
|
thread->error = -RT_EINTR;
|
|
|
ret = rt_channel_list_resume(&ch->parent.suspend_thread);
|
|
|
- _rt_channel_check_wq_wakup(ch);
|
|
|
+
|
|
|
+ rt_spin_lock(&ipc_ch_lock);
|
|
|
+ _rt_channel_check_wq_wakup_locked(ch);
|
|
|
+ rt_spin_unlock(&ipc_ch_lock);
|
|
|
+
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -712,11 +758,14 @@ static void receiver_timeout(void *parameter)
|
|
|
thread->error = -RT_ETIMEOUT;
|
|
|
thread->wakeup.func = RT_NULL;
|
|
|
|
|
|
+ rt_spin_lock(&ipc_ch_lock);
|
|
|
rt_list_remove(&(thread->tlist));
|
|
|
/* insert to schedule ready list */
|
|
|
rt_schedule_insert_thread(thread);
|
|
|
|
|
|
- _rt_channel_check_wq_wakup(ch);
|
|
|
+ _rt_channel_check_wq_wakup_locked(ch);
|
|
|
+ rt_spin_unlock(&ipc_ch_lock);
|
|
|
+
|
|
|
/* do schedule */
|
|
|
rt_schedule();
|
|
|
}
|
|
@@ -726,10 +775,9 @@ static void receiver_timeout(void *parameter)
|
|
|
*/
|
|
|
static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_int32_t time)
|
|
|
{
|
|
|
+ DEF_RETURN_CODE(rc);
|
|
|
struct rt_thread *thread;
|
|
|
rt_ipc_msg_t msg_ret;
|
|
|
- register rt_base_t temp;
|
|
|
- rt_err_t ret;
|
|
|
void (*old_timeout_func)(void *) = 0;
|
|
|
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
|
@@ -739,102 +787,97 @@ static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t d
|
|
|
return -RT_EIO;
|
|
|
}
|
|
|
|
|
|
- temp = rt_hw_interrupt_disable();
|
|
|
+ rt_spin_lock(&ipc_ch_lock);
|
|
|
|
|
|
if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
|
|
|
{
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_EIO;
|
|
|
+ rc = -RT_EIO;
|
|
|
}
|
|
|
- if (ch->stat != RT_IPC_STAT_IDLE)
|
|
|
+ else if (ch->stat != RT_IPC_STAT_IDLE)
|
|
|
{
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_ERROR;
|
|
|
+ rc = -RT_ERROR;
|
|
|
}
|
|
|
-
|
|
|
- if (ch->wait_msg.next != &ch->wait_msg) /* there exist unhandled messages */
|
|
|
+ else
|
|
|
{
|
|
|
- msg_ret = rt_list_entry(ch->wait_msg.next, struct rt_ipc_msg, mlist);
|
|
|
- rt_list_remove(ch->wait_msg.next); /* remove the message from the channel */
|
|
|
- if (msg_ret->need_reply)
|
|
|
+ if (ch->wait_msg.next != &ch->wait_msg) /* there exist unhandled messages */
|
|
|
{
|
|
|
- RT_ASSERT(ch->wait_thread.next != &ch->wait_thread);
|
|
|
+ msg_ret = rt_list_entry(ch->wait_msg.next, struct rt_ipc_msg, mlist);
|
|
|
+ rt_list_remove(ch->wait_msg.next); /* remove the message from the channel */
|
|
|
+ if (msg_ret->need_reply)
|
|
|
+ {
|
|
|
+ RT_ASSERT(ch->wait_thread.next != &ch->wait_thread);
|
|
|
|
|
|
- thread = rt_list_entry(ch->wait_thread.next, struct rt_thread, tlist);
|
|
|
- rt_list_remove(ch->wait_thread.next);
|
|
|
- ch->reply = thread; /* record the waiting sender */
|
|
|
- ch->stat = RT_IPC_STAT_ACTIVE; /* no valid suspened receivers */
|
|
|
+ thread = rt_list_entry(ch->wait_thread.next, struct rt_thread, tlist);
|
|
|
+ rt_list_remove(ch->wait_thread.next);
|
|
|
+ ch->reply = thread; /* record the waiting sender */
|
|
|
+ ch->stat = RT_IPC_STAT_ACTIVE; /* no valid suspened receivers */
|
|
|
+ }
|
|
|
+ *data = msg_ret->msg; /* extract the transferred data */
|
|
|
+ if (data->type == RT_CHANNEL_FD)
|
|
|
+ {
|
|
|
+ data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
|
|
|
+ }
|
|
|
+ _ipc_msg_free(msg_ret); /* put back the message to kernel */
|
|
|
+ rc = RT_EOK;
|
|
|
}
|
|
|
- *data = msg_ret->msg; /* extract the transferred data */
|
|
|
- if (data->type == RT_CHANNEL_FD)
|
|
|
+ else if (time == 0)
|
|
|
{
|
|
|
- data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
|
|
|
+ rc = -RT_ETIMEOUT;
|
|
|
}
|
|
|
- _ipc_msg_free(msg_ret); /* put back the message to kernel */
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (time == 0)
|
|
|
+ else
|
|
|
{
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return -RT_ETIMEOUT;
|
|
|
- }
|
|
|
- /* no valid message, we must wait */
|
|
|
- thread = rt_thread_self();
|
|
|
+ /* no valid message, we must wait */
|
|
|
+ thread = rt_thread_self();
|
|
|
|
|
|
- ret = rt_channel_list_suspend(&ch->parent.suspend_thread, thread);
|
|
|
- if (ret != RT_EOK)
|
|
|
- {
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return ret;
|
|
|
- }
|
|
|
- rt_thread_wakeup_set(thread, wakeup_receiver, (void*)ch);
|
|
|
- ch->stat = RT_IPC_STAT_WAIT;/* no valid suspended senders */
|
|
|
- thread->error = RT_EOK;
|
|
|
- if (time > 0)
|
|
|
- {
|
|
|
- rt_timer_control(&(thread->thread_timer),
|
|
|
- RT_TIMER_CTRL_GET_FUNC,
|
|
|
- &old_timeout_func);
|
|
|
- rt_timer_control(&(thread->thread_timer),
|
|
|
- RT_TIMER_CTRL_SET_FUNC,
|
|
|
- receiver_timeout);
|
|
|
- /* reset the timeout of thread timer and start it */
|
|
|
- rt_timer_control(&(thread->thread_timer),
|
|
|
- RT_TIMER_CTRL_SET_TIME,
|
|
|
- &time);
|
|
|
- rt_timer_start(&(thread->thread_timer));
|
|
|
- }
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
+ rc = rt_channel_list_suspend(&ch->parent.suspend_thread, thread);
|
|
|
+ if (rc == RT_EOK)
|
|
|
+ {
|
|
|
+ rt_thread_wakeup_set(thread, wakeup_receiver, (void*)ch);
|
|
|
+ ch->stat = RT_IPC_STAT_WAIT;/* no valid suspended senders */
|
|
|
+ thread->error = RT_EOK;
|
|
|
+ if (time > 0)
|
|
|
+ {
|
|
|
+ rt_timer_control(&(thread->thread_timer),
|
|
|
+ RT_TIMER_CTRL_GET_FUNC,
|
|
|
+ &old_timeout_func);
|
|
|
+ rt_timer_control(&(thread->thread_timer),
|
|
|
+ RT_TIMER_CTRL_SET_FUNC,
|
|
|
+ receiver_timeout);
|
|
|
+ /* reset the timeout of thread timer and start it */
|
|
|
+ rt_timer_control(&(thread->thread_timer),
|
|
|
+ RT_TIMER_CTRL_SET_TIME,
|
|
|
+ &time);
|
|
|
+ rt_timer_start(&(thread->thread_timer));
|
|
|
+ }
|
|
|
+ rt_spin_unlock(&ipc_ch_lock);
|
|
|
|
|
|
- rt_schedule(); /* let the senders run */
|
|
|
+ rt_schedule(); /* let the senders run */
|
|
|
|
|
|
- temp = rt_hw_interrupt_disable();
|
|
|
- if (old_timeout_func)
|
|
|
- {
|
|
|
- rt_timer_control(&(thread->thread_timer),
|
|
|
- RT_TIMER_CTRL_SET_FUNC,
|
|
|
- old_timeout_func);
|
|
|
- }
|
|
|
- ret = thread->error;
|
|
|
- if ( ret != RT_EOK)
|
|
|
- {
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
- return ret;
|
|
|
- }
|
|
|
- /* If waked up, the received message has been store into the thread. */
|
|
|
- *data = ((rt_ipc_msg_t)(thread->msg_ret))->msg; /* extract data */
|
|
|
- if (data->type == RT_CHANNEL_FD)
|
|
|
- {
|
|
|
- data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
|
|
|
+ rt_spin_lock(&ipc_ch_lock);
|
|
|
+ if (old_timeout_func)
|
|
|
+ {
|
|
|
+ rt_timer_control(&(thread->thread_timer),
|
|
|
+ RT_TIMER_CTRL_SET_FUNC,
|
|
|
+ old_timeout_func);
|
|
|
+ }
|
|
|
+ rc = thread->error;
|
|
|
+ if (rc == RT_EOK)
|
|
|
+ {
|
|
|
+ /* If waked up, the received message has been store into the thread. */
|
|
|
+ *data = ((rt_ipc_msg_t)(thread->msg_ret))->msg; /* extract data */
|
|
|
+ if (data->type == RT_CHANNEL_FD)
|
|
|
+ {
|
|
|
+ data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
|
|
|
+ }
|
|
|
+ _ipc_msg_free(thread->msg_ret); /* put back the message to kernel */
|
|
|
+ thread->msg_ret = RT_NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- _ipc_msg_free(thread->msg_ret); /* put back the message to kernel */
|
|
|
- thread->msg_ret = RT_NULL;
|
|
|
}
|
|
|
|
|
|
- rt_hw_interrupt_enable(temp);
|
|
|
-
|
|
|
- return RT_EOK;
|
|
|
+ rt_spin_unlock(&ipc_ch_lock);
|
|
|
+ RETURN(rc);
|
|
|
}
|
|
|
|
|
|
rt_err_t rt_raw_channel_recv(rt_channel_t ch, rt_channel_msg_t data)
|
|
@@ -950,9 +993,10 @@ static int channel_fops_poll(struct dfs_file *file, struct rt_pollreq *req)
|
|
|
static int channel_fops_close(struct dfs_file *file)
|
|
|
{
|
|
|
rt_channel_t ch;
|
|
|
- rt_base_t level;
|
|
|
+ RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
+
|
|
|
+ rt_spin_lock(&ipc_ch_lock);
|
|
|
|
|
|
- level = rt_hw_interrupt_disable();
|
|
|
ch = (rt_channel_t)file->vnode->data;
|
|
|
if (file->vnode->ref_count == 1)
|
|
|
{
|
|
@@ -960,8 +1004,8 @@ static int channel_fops_close(struct dfs_file *file)
|
|
|
if (ch->ref == 0)
|
|
|
{
|
|
|
/* wakeup all the suspended receivers and senders */
|
|
|
- rt_channel_list_resume_all(&ch->parent.suspend_thread);
|
|
|
- rt_channel_list_resume_all(&ch->wait_thread);
|
|
|
+ _channel_list_resume_all_locked(&ch->parent.suspend_thread);
|
|
|
+ _channel_list_resume_all_locked(&ch->wait_thread);
|
|
|
|
|
|
/* all ipc msg will lost */
|
|
|
rt_list_init(&ch->wait_msg);
|
|
@@ -969,7 +1013,8 @@ static int channel_fops_close(struct dfs_file *file)
|
|
|
rt_object_delete(&ch->parent.parent); /* release the IPC channel structure */
|
|
|
}
|
|
|
}
|
|
|
- rt_hw_interrupt_enable(level);
|
|
|
+
|
|
|
+ rt_spin_unlock(&ipc_ch_lock);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
@@ -1160,23 +1205,23 @@ rt_err_t rt_channel_peek(int fd, rt_channel_msg_t data)
|
|
|
return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, 0);
|
|
|
}
|
|
|
|
|
|
-#ifdef RT_USING_FINSH
|
|
|
static int list_channel(void)
|
|
|
{
|
|
|
- rt_base_t level;
|
|
|
rt_channel_t *channels;
|
|
|
rt_ubase_t index, count;
|
|
|
struct rt_object *object;
|
|
|
struct rt_list_node *node;
|
|
|
struct rt_object_information *information;
|
|
|
|
|
|
+ RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
+
|
|
|
const char* stat_strs[] = {"idle", "wait", "active"};
|
|
|
|
|
|
information = rt_object_get_information(RT_Object_Class_Channel);
|
|
|
RT_ASSERT(information != RT_NULL);
|
|
|
|
|
|
count = 0;
|
|
|
- level = rt_hw_interrupt_disable();
|
|
|
+ rt_spin_lock(&ipc_list_lock);
|
|
|
/* get the count of IPC channels */
|
|
|
for (node = information->object_list.next;
|
|
|
node != &(information->object_list);
|
|
@@ -1184,7 +1229,7 @@ static int list_channel(void)
|
|
|
{
|
|
|
count ++;
|
|
|
}
|
|
|
- rt_hw_interrupt_enable(level);
|
|
|
+ rt_spin_unlock(&ipc_list_lock);
|
|
|
|
|
|
if (count == 0) return 0;
|
|
|
|
|
@@ -1192,18 +1237,18 @@ static int list_channel(void)
|
|
|
if (channels == RT_NULL) return 0; /* out of memory */
|
|
|
|
|
|
index = 0;
|
|
|
- level = rt_hw_interrupt_disable();
|
|
|
+ rt_spin_lock(&ipc_list_lock);
|
|
|
/* retrieve pointer of IPC channels */
|
|
|
- for (node = information->object_list.next;
|
|
|
- node != &(information->object_list);
|
|
|
- node = node->next)
|
|
|
+ for (node = information->object_list.next;
|
|
|
+ count > 0 && node != &(information->object_list);
|
|
|
+ count--, node = node->next)
|
|
|
{
|
|
|
object = rt_list_entry(node, struct rt_object, list);
|
|
|
|
|
|
channels[index] = (rt_channel_t)object;
|
|
|
index ++;
|
|
|
}
|
|
|
- rt_hw_interrupt_enable(level);
|
|
|
+ rt_spin_unlock(&ipc_list_lock);
|
|
|
|
|
|
rt_kprintf(" channel state\n");
|
|
|
rt_kprintf("-------- -------\n");
|
|
@@ -1222,5 +1267,4 @@ static int list_channel(void)
|
|
|
return 0;
|
|
|
}
|
|
|
MSH_CMD_EXPORT(list_channel, list IPC channel information);
|
|
|
-#endif
|
|
|
|