Blame view

RIOT/tests/thread_cooperation/main.c 2.29 KB
a752c7ab   elopes   add first test an...
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
98
99
100
101
102
103
104
  /*
   * Copyright (C) 2014 Hamburg University of Applied Sciences
   *
   * 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 riot thread test application
   *
   * @author Raphael Hiesgen <raphael.hiesgen@haw-hamburg.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <inttypes.h>
  
  #include "thread.h"
  #include "mutex.h"
  
  #define PROBLEM 12
  
  mutex_t mtx = MUTEX_INIT;
  
  volatile uint32_t storage = 1;
  kernel_pid_t main_id = KERNEL_PID_UNDEF;
  kernel_pid_t ths[PROBLEM];
  char stacks[PROBLEM][THREAD_STACKSIZE_MAIN];
  
  void *run(void *arg)
  {
      (void) arg;
  
      kernel_pid_t me = thread_getpid();
      printf("I am alive (%d)\n", me);
      msg_t m;
      msg_receive(&m);
      printf("Thread %d has arg %" PRIu32 "\n", me, m.content.value);
  
      mutex_lock(&mtx);
  
      storage *= m.content.value;
      mutex_unlock(&mtx);
  
      msg_t final;
      final.content.value = me;
      int err = msg_send(&final, main_id);
  
      if (err < 0) {
          printf("[!!!] Failed to send message from %d to main\n", me);
      }
  
      return NULL;
  }
  
  int main(void)
  {
      main_id = thread_getpid();
  
      printf("Problem: %d\n", PROBLEM);
  
      msg_t args[PROBLEM];
  
      for (int i = 0; i < PROBLEM; ++i) {
          printf("Creating thread with arg %d\n", (i + 1));
          ths[i] = thread_create(stacks[i], sizeof(stacks[i]),
                                 THREAD_PRIORITY_MAIN - 1,
                                 THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
                                 run, NULL, "thread");
  
          if (ths[i] < 0)  {
              printf("[!!!] Creating thread failed.\n");
          }
          else {
              args[i].content.value = i + 1;
  
              int err = msg_send(&args[i], ths[i]);
              if (err < 0) {
                  printf("[!!!] Sending message to thread %d failed\n", ths[i]);
              }
          }
      }
  
      for (int i = 0; i < PROBLEM; ++i) {
          msg_t msg;
          msg_receive(&msg);
          printf("Reveiced message %d from thread %" PRIu32 "\n", i, msg.content.value);
      }
  
      printf("Factorial: %"PRIu32"\n", storage);
  
      if (storage != 479001600LU) {
          puts("[!!!] Error, expected: 12!= 479001600.");
      }
  
      puts("finished");
      return 0;
  }