Blame view

RIOT/tests/pthread_rwlock/main.c 3.7 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  /*
   * Copyright (C) 2014  René Kijewski  <rene.kijewski@fu-berlin.de>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  
  /**
   * @ingroup tests
   * @{
   *
   * @file
   * @brief       Test rwlock implementation.
   *
   * @author      René Kijewski <rene.kijewski@fu-berlin.de>
   *
   * @}
   */
  
  #include <pthread.h>
  #include <stdio.h>
  
  #include "random.h"
  #include "sched.h"
  #include "thread.h"
  #include "xtimer.h"
  
  #define NUM_READERS_HIGH 2
  #define NUM_READERS_LOW 3
  
  #define NUM_WRITERS_HIGH 1
  #define NUM_WRITERS_LOW 2
  
  #define NUM_READERS (NUM_READERS_HIGH + NUM_READERS_LOW)
  #define NUM_WRITERS (NUM_WRITERS_HIGH + NUM_WRITERS_LOW)
  #define NUM_CHILDREN (NUM_READERS + NUM_WRITERS)
  
  #define NUM_ITERATIONS 5
  
  #define RAND_SEED 0xC0FFEE
  
  static pthread_rwlock_t rwlock;
  static volatile unsigned counter;
  
  #define PRINTF(FMT, ...) \
      printf("%c%" PRIkernel_pid " (prio=%u): " FMT "\n", __func__[0], sched_active_pid, sched_active_thread->priority, __VA_ARGS__)
  
  static void do_sleep(int factor)
  {
      uint32_t timeout_us = (random_uint32() % 100000) * factor;
      /* PRINTF("sleep for % 8i µs.", timeout_us); */
      xtimer_usleep(timeout_us);
  }
  
  static void *writer(void *arg)
  {
      (void) arg;
      /* PRINTF("%s", "start"); */
      for (int i = 0; i < NUM_ITERATIONS; ++i) {
          pthread_rwlock_wrlock(&rwlock);
          unsigned cur = ++counter;
          do_sleep(3); /* simulate time that it takes to write the value */
          PRINTF("%i: write -> %2u (correct = %u)", i, cur, cur == counter);
          pthread_rwlock_unlock(&rwlock);
          do_sleep(2);
      }
      /* PRINTF("%s", "done"); */
      return NULL;
  }
  
  static void *reader(void *arg)
  {
      (void) arg;
      /* PRINTF("%s", "start"); */
      for (int i = 0; i < NUM_ITERATIONS; ++i) {
          pthread_rwlock_rdlock(&rwlock);
          unsigned cur = counter;
          do_sleep(1); /* simulate time that it takes to read the value */
          PRINTF("%i: read  <- %2u (correct = %u)", i, cur, cur == counter);
          pthread_rwlock_unlock(&rwlock);
          do_sleep(1);
      }
      /* PRINTF("%s", "done"); */
      return NULL;
  }
  
  int main(void)
  {
      static char stacks[NUM_CHILDREN][THREAD_STACKSIZE_MAIN];
  
      puts("Main start.");
  
      for (unsigned i = 0; i < NUM_CHILDREN; ++i) {
          int prio;
          void *(*fun)(void *);
          const char *name;
  
          if (i < NUM_READERS) {
              if (i < NUM_READERS_HIGH) {
                  prio = THREAD_PRIORITY_MAIN + 1;
              }
              else {
                  prio = THREAD_PRIORITY_MAIN + 2;
              }
              fun = reader;
              name = "reader";
          }
          else {
              if (i - NUM_READERS < NUM_WRITERS_HIGH) {
                  prio = THREAD_PRIORITY_MAIN + 1;
              }
              else {
                  prio = THREAD_PRIORITY_MAIN + 2;
              }
              fun = writer;
              name = "writer";
          }
  
          thread_create(stacks[i], sizeof(stacks[i]),
                        prio, THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
                        fun, NULL, name);
      }
  
      puts("Main done.");
      return 0;
  }