Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:36

0001 /*
0002  * Copyright (c) 2015 embedded brains GmbH & Co. KG
0003  *
0004  * The license and distribution terms for this file may be
0005  * found in the file LICENSE in this distribution or at
0006  * http://www.rtems.com/license/LICENSE.
0007  */
0008 
0009 #ifdef HAVE_CONFIG_H
0010 #include "config.h"
0011 #endif
0012 
0013 #include <assert.h>
0014 #include <stdio.h>
0015 #include <pthread.h>
0016 
0017 static int step;
0018 
0019 static void destroy(void *arg)
0020 {
0021   assert(step == 2);
0022   step = 3;
0023   printf("destroy\n");
0024 }
0025 
0026 static void cleanup(void *arg)
0027 {
0028   assert(step == 1);
0029   step = 2;
0030   printf("cleanup\n");
0031 }
0032 
0033 static void *task(void *arg)
0034 {
0035   pthread_key_t key;
0036   int eno;
0037 
0038   eno = pthread_key_create(&key, destroy);
0039   assert(eno == 0);
0040 
0041   pthread_cleanup_push(cleanup, NULL);
0042 
0043   eno = pthread_setspecific(key, &key);
0044   assert(eno == 0);
0045 
0046   assert(step == 0);
0047   step = 1;
0048 
0049   pthread_exit(NULL);
0050   pthread_cleanup_pop(0);
0051 
0052   return NULL;
0053 }
0054 
0055 int main(int argc, char **argv)
0056 {
0057   pthread_t t;
0058   int eno;
0059 
0060   eno = pthread_create(&t, NULL, task, NULL);
0061   assert(eno == 0);
0062 
0063   eno = pthread_join(t, NULL);
0064   assert(eno == 0);
0065 
0066   return 0;
0067 }