NuttX的学习笔记 11

板子可以跑NuttX了,继续研究ostest
之前是在讲消息队列的,那么这节就把ostest里面相关消息队列的代码分析一遍。
先从ostest_main()开始。

apps\examples\ostest\ostest_main():mqueue_test()

int ostest_main(int argc, FAR char *argv[])
{
  int result;

  /* Verify that stdio works first */
  stdio_test();

  /* Set up some environment variables */
  printf("ostest_main: putenv(%s)\n", g_putenv_value);
  putenv(g_putenv_value);                   /* Varaible1=BadValue3 */
  printf("ostest_main: setenv(%s, %s, TRUE)\n", g_var1_name, g_var1_value);
  setenv(g_var1_name, g_var1_value, TRUE);  /* Variable1=GoodValue1 */

  printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var2_name, g_bad_value1);
  setenv(g_var2_name, g_bad_value1, FALSE); /* Variable2=BadValue1 */
  printf("ostest_main: setenv(%s, %s, TRUE)\n", g_var2_name, g_var2_value);
  setenv(g_var2_name, g_var2_value, TRUE);  /* Variable2=GoodValue2 */

  printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var3_name, g_var3_name);
  setenv(g_var3_name, g_var3_value, FALSE); /* Variable3=GoodValue3 */
  printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var3_name, g_var3_name);
  setenv(g_var3_name, g_bad_value2, FALSE); /* Variable3=GoodValue3 */
  show_environment(true, true, true);

  /* Verify that we can spawn a new task */
  result = task_create("ostest", PRIORITY, STACKSIZE, user_main,
                       (FAR char * const *)g_argv);
  if (result == ERROR)
    {
      printf("ostest_main: ERROR Failed to start user_main\n");
      ostest_result = ERROR;
    }
  else
    {
      printf("ostest_main: Started user_main at PID=%d\n", result);

      /* Wait for the test to complete to get the test result */
      if (waitpid(result, &ostest_result, 0) != result)
        {
          printf("ostest_main: ERROR Failed to wait for user_main to terminate\n");
          ostest_result = ERROR;
        }
    }
  printf("ostest_main: Exiting with status %d\n", ostest_result);
  return ostest_result;
}

这里它创建了一个任务:user_main(),再来看一下这个函数。
从上往下,找到了关于消息队列的函数

      /* Verify pthreads and message queues */

      printf("\nuser_main: message queue test\n");
      mqueue_test();
      check_test_memory_usage();

跳转进去。

apps\examples\ostest\mqueue.c:mqueue_test()

void mqueue_test(void)
{
  pthread_t sender;         // 发送线程
  pthread_t receiver;       // 接收线程
  void *result;
  pthread_attr_t attr;
  struct sched_111param sparam;
  FAR void *expected;
  int prio_min;
  int prio_max;
  int prio_mid;
  int status;

  /* Reset globals for the beginning of the test */

  g_send_mqfd = NULL;
  g_recv_mqfd = NULL;

  /* Start the sending thread at higher priority */

  printf("mqueue_test: Starting receiver\n");
  status = pthread_attr_init(&attr);
  if (status != 0)
    {
      printf("mqueue_test: pthread_attr_init failed, status=%d\n", status);
    }

  status = pthread_attr_setstacksize(&attr, STACKSIZE);
  if (status != 0)
    {
      printf("mqueue_test: pthread_attr_setstacksize failed, status=%d\n", status);
    }

  prio_min = sched_get_priority_min(SCHED_FIFO);
  prio_max = sched_get_priority_max(SCHED_FIFO);
  prio_mid = (prio_min + prio_max) / 2;

  sparam.sched_priority = prio_mid;
  status = pthread_attr_setschedparam(&attr,&sparam);
  if (status != OK)
    {
      printf("mqueue_test: pthread_attr_setschedparam failed, status=%d\n", status);
    }
  else
    {
      printf("mqueue_test: Set receiver priority to %d\n", sparam.sched_priority);
    }

  status = pthread_create(&receiver, &attr, receiver_thread, NULL);
  if (status != 0)
    {
      printf("mqueue_test: pthread_create failed, status=%d\n", status);
    }

  /* Start the sending thread at lower priority */

  printf("mqueue_test: Starting sender\n");
  status = pthread_attr_init(&attr);
  if (status != 0)
    {
      printf("mqueue_test: pthread_attr_init failed, status=%d\n", status);
    }

  status = pthread_attr_setstacksize(&attr, STACKSIZE);
  if (status != 0)
    {
      printf("mqueue_test: pthread_attr_setstacksize failed, status=%d\n",
             status);
    }

  sparam.sched_priority = (prio_min + prio_mid) / 2;
  status = pthread_attr_setschedparam(&attr,&sparam);
  if (status != OK)
    {
      printf("mqueue_test: pthread_attr_setschedparam failed, status=%d\n",
             status);
    }
  else
    {
      printf("mqueue_test: Set sender thread priority to %d\n",
             sparam.sched_priority);
    }

  status = pthread_create(&sender, &attr, sender_thread, NULL);
  if (status != 0)
    {
      printf("mqueue_test: pthread_create failed, status=%d\n", status);
    }

  printf("mqueue_test: Waiting for sender to complete\n");
  pthread_join(sender, &result);
  if (result != (FAR void *)0)
    {
      printf("mqueue_test: ERROR sender thread exited with %d errors\n",
             (int)((intptr_t)result));
    }

  /* Wake up the receiver thread with a signal */

  printf("mqueue_test: Killing receiver\n");
  pthread_kill(receiver, 9);

  /* Wait a bit to see if the thread exits on its own */

  usleep(HALF_SECOND_USEC_USEC);

  /* Then cancel the thread and see if it did */

  printf("mqueue_test: Canceling receiver\n");

  expected = PTHREAD_CANCELED;
  status = pthread_cancel(receiver);
  if (status == ESRCH)
    {
      printf("mqueue_test: receiver has already terminated\n");
      expected = (FAR void *)0;
    }

  /* Check the result.  If the pthread was canceled, PTHREAD_CANCELED is the
   * correct result.  Zero might be returned if the thread ran to completion
   * before it was canceled.
   */

  pthread_join(receiver, &result);
  if (result != expected)
    {
      printf("mqueue_test: ERROR receiver thread should have exited with %p\n",
             expected);
      printf("             ERROR Instead exited with nerrors=%d\n",
             (int)((intptr_t)result));
    }

  /* Message queues are global resources and persist for the life the
   * task group.  The message queue opened by the sender_thread must be closed
   * since the sender pthread may have been canceled and may have left the
   * message queue open.
   */

  if (result == PTHREAD_CANCELED && g_recv_mqfd)
    {
      if (mq_close(g_recv_mqfd) < 0)
        {
          printf("mqueue_test: ERROR mq_close failed\n");
        }
    }
  else if (result != PTHREAD_CANCELED && g_recv_mqfd)
    {
      printf("mqueue_test: ERROR send mqd_t left open\n");
      if (mq_close(g_recv_mqfd) < 0)
        {
          printf("mqueue_test: ERROR mq_close failed\n");
        }
    }

  /* Make sure that the receive queue is closed as well */

  if (g_send_mqfd)
    {
      printf("mqueue_test: ERROR receiver mqd_t left open\n");
      if (mq_close(g_send_mqfd) < 0)
        {
          printf("sender_thread: ERROR mq_close failed\n");
        }
    }

  /* Destroy the message queue */

  if (mq_unlink("mqueue") < 0)
    {
      printf("mqueue_test: ERROR mq_unlink failed\n");
    }
}

整体来说这个任务做了什么呢:
1. 启动一个中优先级的sending thread
2. 启动一个中低优先级的receiver thread
3. 使用pthread_join()等待sending thread停止
4. 给receiver thread发送信号
5. 等待 500000 us
6. 取消receiver thread
7. 检查receiver thread结束时返回的结果
8. 关闭并删除消息队列

看起来最想了解的消息队列主要部分不在这里。消息队列相关的代码被分成了两部分,先从发送看起吧。

apps\examples\ostest\mqueue.c:sender_thread()

static void *sender_thread(void *arg)
{
  char msg_buffer[TEST_MSGLEN]; 
  struct mq_attr attr;
  int status = 0;
  int nerrors = 0;
  int i;

  printf("sender_thread: Starting\n");

  /* Fill in attributes for message queue */

  attr.mq_maxmsg  = 20;         // 最大消息条数
  attr.mq_msgsize = TEST_MSGLEN;    // 最大消息长度
  attr.mq_flags   = 0;

这部分定义了队列的属性。


  g_send_mqfd = mq_open("mqueue", O_WRONLY|O_CREAT, 0666, &attr);
  if (g_send_mqfd == (mqd_t)-1)
    {
      printf("sender_thread: ERROR mq_open failed\n");
      pthread_exit((pthread_addr_t)1);
    }

这里就出现了消息队列的函数mq_open(),详细参考==>mq_open(),这里设置了队列名称为mqueue,写标志位O_WRONLY和不存在创建标志位O_CREAT。同时,启用O_CREAT后需要再填写两个参数,一个是权限,0666按照我之前在文件中找出来的定义来看,只能是(0)(4+2)(4+2)(4+2),也就是rw-rw-rw,也就是说,这个队列是全员可读写的。
最后的参数是前面定义好的队列属性。

  memcpy(msg_buffer, TEST_MESSAGE, TEST_MSGLEN);

把字符串 "This is a test and only a test" 拷贝到一个缓冲器里,其实就是一个变量里。

  for (i = 0; i < TEST_SEND_NMSGS; i++)
    {
      status = mq_send(g_send_mqfd, msg_buffer, TEST_MSGLEN, 42);
      if (status < 0)
        {
          printf("sender_thread: ERROR mq_send failure=%d on msg %d\n", status, i);
          nerrors++;
        }
      else
        {
          printf("sender_thread: mq_send succeeded on msg %d\n", i);
        }
    }

循环10次发送消息,这里用到了==> mq_send() <==函数。这里标记一下这个优先级的东西。我猜想这里随意发送优先级不同的消息,接收端接收到的应该是排号序的。去代码里验证。mq_send 调用了nxmq_send传递了参数prio。而nxmq_send则是调用了nxmq_do_send
nxmq_do_send里有写:

nuttx\fs\mqueue\mq_open.c:mq_open()

  for (prev = NULL, next = (FAR struct mqueue_msg_s *)msgq->msglist.head;
       next && prio <= next->priority;
       prev = next, next = next->next);

循环执行到next指针指向的消息优先级比他大或者不存在时停止。此时将该消息插在指针prev后面。具体的实现方法还得往里走。再往里我就不记录了,有点多。。基本涉及到的函数都是sq开头的,位置:nuttx\libs\libc\queue这里还有dq开头的一些文件,看起来是双向链表。总之,消息最后是会按照优先级排序的。不会存在同优先级覆盖问题。

  if (mq_close(g_send_mqfd) < 0)
    {
      printf("sender_thread: ERROR mq_close failed\n");
    }
  else
    {
      g_send_mqfd = NULL;
    }

这里是关闭消息列表了==>mq_close<==。

  printf("sender_thread: returning nerrors=%d\n", nerrors);
  return (pthread_addr_t)((uintptr_t)nerrors);
}

最后返回了10

再来看另一个线程

apps\examples\ostest\mqueue.c:receiver_thread()

static void *receiver_thread(void *arg)
{
  char msg_buffer[TEST_MSGLEN];
  struct mq_attr attr;
  int nbytes;
  int nerrors = 0;
  int i;

  printf("receiver_thread: Starting\n");

  /* Fill in attributes for message queue */

  attr.mq_maxmsg  = 20;
  attr.mq_msgsize = TEST_MSGLEN;
  attr.mq_flags   = 0;

这部分定义了队列的属性。

   g_recv_mqfd = mq_open("mqueue", O_RDONLY|O_CREAT, 0666, &attr);
   if (g_recv_mqfd < 0)
     {
       printf("receiver_thread: ERROR mq_open failed\n");
       pthread_exit((pthread_addr_t)1);
     }

这一段使用了==>mq_open<==,和sender_thread()中的mq_open是一样的。


   for (i = 0; i < TEST_RECEIVE_NMSGS; i++)
    {
      memset(msg_buffer, 0xaa, TEST_MSGLEN);
      nbytes = mq_receive(g_recv_mqfd, msg_buffer, TEST_MSGLEN, 0);
      if (nbytes < 0)
        {
          /* mq_receive failed.  If the error is because of EINTR then
           * it is not a failure.
           */

          if (errno != EINTR)
            {
              printf("receiver_thread: ERROR mq_receive failure on msg %d, errno=%d\n", i, errno);
              nerrors++;
            }
          else
            {
              printf("receiver_thread: mq_receive interrupted!\n");
            }
        }
      else if (nbytes != TEST_MSGLEN)
        {
          printf("receiver_thread: mq_receive return bad size %d on msg %d\n", nbytes, i);
          nerrors++;
        }
      else if (memcmp(TEST_MESSAGE, msg_buffer, nbytes) != 0)
        {
          int j;

          printf("receiver_thread: mq_receive returned corrupt message on msg %d\n", i);
          printf("receiver_thread:                  i  Expected Received\n");

          for (j = 0; j < TEST_MSGLEN-1; j++)
            {
              if (isprint(msg_buffer[j]))
                {
                 printf("receiver_thread:                  %2d %02x (%c) %02x (%c)\n",
                         j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j], msg_buffer[j]);
                }
              else
                {
                  printf("receiver_thread:                  %2d %02x (%c) %02x\n",
                         j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j]);
                }
            }

          printf("receiver_thread:                  %2d 00      %02x\n",
                  j, msg_buffer[j]);
        }
      else
        {
          printf("receiver_thread: mq_receive succeeded on msg %d\n", i);
        }
    }

接收11次数据,每次都会检查数据是否正常。最后一次会失败,因为没有数据可以读取了。所以会
printf("receiver_thread: mq_receive interrupted!\n");


  if (mq_close(g_recv_mqfd) < 0)
    {
      printf("receiver_thread: ERROR mq_close failed\n");
      nerrors++;
    }
  else
    {
      g_recv_mqfd = NULL;
    }

关闭消息队列,同sender_thread

  printf("receiver_thread: returning nerrors=%d\n", nerrors);
  pthread_exit((pthread_addr_t)((uintptr_t)nerrors));
  return (pthread_addr_t)((uintptr_t)nerrors);
}

最后返回0代表正常。这么一句呢。这里不得不提到之前ostest任务中的pthread_join,这里的pthread_exit就是为了让那里停止等待。顺便mq_timedreceive也有,和这篇差不多,就不看了。就到这里吧。


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值