Blame view

RIOT/tests/pthread_tls/main.c 3.59 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
138
  /*
   * Copyright (C) 2014 Hamburg University of Applied Sciences (HAW)
   *
   * 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 tls test application
   *
   * @author Martin Landsmann <martin.landsmann@haw-hamburg.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include "pthread.h"
  
  #define NUMBER_OF_TLS (20)
  
  void *run(void *parameter)
  {
      pthread_key_t aKeys[NUMBER_OF_TLS];
      int aTLS_values[NUMBER_OF_TLS];
  
      (void)parameter;
  
      printf("\n-= TEST 1 - create %d tls with sequencial values 0...%d =-\n", NUMBER_OF_TLS, NUMBER_OF_TLS - 1);
  
      for (int i = 0; i < NUMBER_OF_TLS; ++i) {
          aTLS_values[i] = i;
          pthread_key_create(&(aKeys[i]), NULL);
          pthread_setspecific(aKeys[i], &aTLS_values[i]);
      }
  
      printf("now rise sequencial by one values 1...%d\n", NUMBER_OF_TLS);
  
      for (int i = 0; i < NUMBER_OF_TLS; ++i) {
          aTLS_values[i]++;
      }
  
      printf("pick deliberate storage (key[3]:%d) and change the value\n", (int)aKeys[3]);
      void *val = pthread_getspecific(aKeys[3]);
      *((int *)val) = 42;
  
      printf("show tls values:\n");
  
      for (int i = 0; i < NUMBER_OF_TLS; ++i) {
          void *val = pthread_getspecific(aKeys[i]);
          int x = *(int *)val;
          printf("key[%d]: %d, val: %d\n",i, (int)aKeys[i], x);
      }
  
      printf("\n -= TEST 2 - delete deliberate key (key[5]:%d) =-\n", (int)aKeys[5]);
      pthread_key_delete(aKeys[5]);
  
      printf("show tls values:\n");
  
      for (int i = 0; i < NUMBER_OF_TLS; ++i) {
          void *val = pthread_getspecific(aKeys[i]);
  
          if (val != NULL) {
              int x = *(int *)val;
              printf("key[%d]: %d, val: %d\n",i, (int)aKeys[i], x);
          }
      }
  
      printf("\n-= TEST 3 - create new tls =-\n");
      int new_val = 99;
      pthread_key_t new_key;
      pthread_key_create(&new_key, NULL);
      pthread_setspecific(new_key, &new_val);
  
      printf("added new tls, key: %d, val: %d\n", (int)new_key, new_val);
      printf("show tls values:\n");
  
      for (int i = 0; i < NUMBER_OF_TLS; ++i) {
          void *val = pthread_getspecific(aKeys[i]);
  
          if (val != NULL) {
              int x = *(int *)val;
              printf("key[%d]: %d, val: %d\n",i, (int)aKeys[i], x);
          }
      }
  
      printf("\n-= TEST 4 - delete all keys =-\n");
  
      for (int i = 0; i < NUMBER_OF_TLS; ++i) {
          pthread_key_delete(aKeys[i]);
      }
  
      printf("show tls values:\n");
  
      for (int i = 0; i < NUMBER_OF_TLS; ++i) {
          void *val = pthread_getspecific(aKeys[i]);
  
          if (val != NULL) {
              int x = *(int *)val;
              printf("key[%d]: %d, val: %d\n",i, (int)aKeys[i], x);
          }
      }
  
      printf("\n-= TEST 5 - try delete non-existing key =-\n");
      printf("try to delete returns: %d\n", pthread_key_delete((pthread_key_t)99));
  
      printf("\n-= TEST 6 - add key and delete without a tls =-\n");
      pthread_key_create(&new_key, NULL);
      printf("crated key: %d\n", (int)new_key);
      printf("try to delete returns: %d\n", pthread_key_delete(new_key));
  
      printf("\n-= TEST 7 - add key without tls =-\n");
      pthread_key_create(&new_key, NULL);
      printf("crated key: %d\n", (int)new_key);
      void* test_7_val = pthread_getspecific(new_key);
      printf("test_7_val: %p\n", test_7_val);
  
  
      return NULL;
  }
  
  int main(void)
  {
      pthread_t th_id;
      pthread_attr_t th_attr;
  
      pthread_attr_init(&th_attr);
      pthread_create(&th_id, &th_attr, run, NULL);
  
      size_t res;
      pthread_join(th_id, (void **) &res);
      puts("\ntls tests finished.");
      return 0;
  }