Blame view

RIOT/tests/thread_msg/main.c 2.22 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  /*
   * Copyright (C) 2013 Christian Mehlis <mehlis@inf.fu-berlin.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup tests
   * @{
   *
   * @file
   * @brief Thread test application
   *
   * @author Christian Mehlis <mehlis@inf.fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <inttypes.h>
  
  #include "thread.h"
  #include "msg.h"
  
  char t1_stack[THREAD_STACKSIZE_MAIN];
  char t2_stack[THREAD_STACKSIZE_MAIN];
  char t3_stack[THREAD_STACKSIZE_MAIN];
  
  kernel_pid_t p1, p2, p3;
  
  void *thread1(void *arg)
  {
      (void) arg;
      puts("THREAD 1 start\n");
  
      for (int i = 0; i < 3; ++i) {
          msg_t msg, reply;
  
          msg_receive(&msg);
          printf("T1 recv: %" PRIu32 "(i=%d)\n", msg.content.value, i);
  
          msg.content.value = i;
          msg_send_receive(&msg, &reply, p2);
          printf("T1 got reply: %" PRIu32 " (i=%d)\n", reply.content.value, i);
      }
  
      puts("THREAD 1 end\n");
      return NULL;
  }
  
  void *thread2(void *arg)
  {
      (void) arg;
      puts("THREAD 2\n");
  
      for (int i = 0;; ++i) {
          msg_t msg, reply;
  
          msg_receive(&msg);
          printf("T2 got %" PRIu32 " (i=%d)\n", msg.content.value, i);
          reply.content.value = msg.content.value;
          msg_reply(&msg, &reply);
      }
  
      return NULL;
  }
  
  void *thread3(void *arg)
  {
      (void) arg;
      puts("THREAD 3\n");
  
      for (int i = 0;; ++i) {
          msg_t msg;
          msg.content.value = i;
          printf("T3 i=%d\n", i);
          msg_send(&msg, p1);
      }
      return NULL;
  }
  
  int main(void)
  {
      p1 = thread_create(t1_stack, sizeof(t1_stack), THREAD_PRIORITY_MAIN - 1,
                         THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
                         thread1, NULL, "nr1");
      p2 = thread_create(t2_stack, sizeof(t2_stack), THREAD_PRIORITY_MAIN - 1,
                         THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
                         thread2, NULL, "nr2");
      p3 = thread_create(t3_stack, sizeof(t3_stack), THREAD_PRIORITY_MAIN - 1,
                         THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
                         thread3, NULL, "nr3");
      puts("THREADS CREATED\n");
      return 0;
  }