Blame view

RIOT/tests/pthread_barrier/main.c 1.54 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
  /*
   * Copyright (C) 2014 Freie Universität Berlin
   *
   * 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 pthread_barrier test
   *
   * @author René Kijewski <rene.kijewski@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <inttypes.h>
  
  #include "pthread.h"
  #include "random.h"
  #include "xtimer.h"
  
  #define NUM_CHILDREN 4
  #define NUM_ITERATIONS 5
  
  #define RAND_SEED 0xC0FFEE
  
  static pthread_barrier_t barrier;
  
  static void *run(void *id_)
  {
      int id = (intptr_t) id_ + 1;
      printf("Start %i\n", id);
  
      for (int i = 1; i <= NUM_ITERATIONS; ++i) {
          if (id == NUM_CHILDREN) {
              puts("\n======================================\n");
          }
          pthread_barrier_wait(&barrier);
  
          uint32_t timeout_us = random_uint32() % 2500000;
          printf("Child %i sleeps for %8" PRIu32 " µs.\n", id, timeout_us);
          xtimer_usleep(timeout_us);
      }
  
      printf("Done %i\n", id);
      return NULL;
  }
  
  int main(void)
  {
      random_init(RAND_SEED);
  
      puts("Start.\n");
      pthread_barrier_init(&barrier, NULL, NUM_CHILDREN);
  
      pthread_t children[NUM_CHILDREN];
      for (int i = 0; i < NUM_CHILDREN; ++i) {
          pthread_create(&children[i], NULL, run, (void *) (intptr_t) i);
      }
  
      for (int i = 0; i < NUM_CHILDREN; ++i) {
          void *void_;
          pthread_join(children[i], &void_);
      }
  
      pthread_barrier_destroy(&barrier);
      puts("\nDone.");
      return 0;
  }