Blame view

RIOT/tests/pthread_cleanup/main.c 1.83 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
  /*
   * 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 test application
   *
   * @author René Kijewski <rene.kijewski@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include "pthread.h"
  
  #define RET_EXIT ((void *) 1234)
  #define RET_FAIL ((void *) 5678)
  
  static void cleanup(void *arg)
  {
      printf("Cleanup: <%s>\n", (const char *) arg);
  }
  
  static void *run(void *unused) {
      (void) unused;
  
      /* indentation for visibility */
      puts("<SCOPE 0>");
      pthread_cleanup_push(cleanup, "1");
          puts("<SCOPE 1>");
          pthread_cleanup_push(cleanup, "2");
              puts("<SCOPE 2>");
              pthread_cleanup_push(cleanup, "3");
                  puts("<SCOPE 3>");
                  pthread_cleanup_push(cleanup, "4");
                      puts("<SCOPE 4>");
                      pthread_cleanup_push(cleanup, "5");
                          puts("<SCOPE 5 />");
                      pthread_cleanup_pop(1);
                      puts("</SCOPE 4>");
                  pthread_cleanup_pop(0); /* cleanup 4 should not be executed */
                  puts("</SCOPE 3>");
              pthread_cleanup_pop(1);
              pthread_exit(RET_EXIT);
              puts("/<SCOPE 2>"); /* thread exited, should not be printed */
          pthread_cleanup_pop(0); /* should be printed nevertheless */
          puts("</SCOPE 1>");
      pthread_cleanup_pop(1);
      puts("</SCOPE 0>");
  
      return RET_FAIL;
  }
  
  int main(void) {
      puts("Start.");
  
      pthread_t th_id;
      pthread_create(&th_id, NULL, run, NULL);
  
      void *res;
      pthread_join(th_id, (void **) &res);
  
      printf("Result: %u\n", (int) (intptr_t) res);
      puts("Done.");
      return 0;
  }