test_pthread_cleanup_push.c revision e16cb84e2324f05334d18dcf5956f20f44262b62
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#include <pthread.h>
29#include <stdio.h>
30#include <stdlib.h>
31
32
33#define MAGIC1 0xcafebabeU
34#define MAGIC2 0x8badf00dU
35#define MAGIC3 0x12345667U
36
37static int g_ok1 = 0;
38static int g_ok2 = 0;
39static int g_ok3 = 0;
40
41static void
42cleanup1( void* arg )
43{
44    if ((unsigned)arg != MAGIC1)
45        g_ok1 = -1;
46    else
47        g_ok1 = +1;
48}
49
50static void
51cleanup2( void* arg )
52{
53    if ((unsigned)arg != MAGIC2) {
54        g_ok2 = -1;
55    } else
56        g_ok2 = +1;
57}
58
59static void
60cleanup3( void* arg )
61{
62    if ((unsigned)arg != MAGIC3)
63        g_ok3 = -1;
64    else
65        g_ok3 = +1;
66}
67
68
69static void*
70thread1_func( void* arg )
71{
72    pthread_cleanup_push( cleanup1, (void*)MAGIC1 );
73    pthread_cleanup_push( cleanup2, (void*)MAGIC2 );
74    pthread_cleanup_push( cleanup3, (void*)MAGIC3 );
75
76    if (arg != NULL)
77        pthread_exit(0);
78
79    pthread_cleanup_pop(0);
80    pthread_cleanup_pop(1);
81    pthread_cleanup_pop(1);
82
83    return NULL;
84}
85
86static int test( int do_exit )
87{
88    pthread_t t;
89
90    pthread_create( &t, NULL, thread1_func, (void*)do_exit );
91    pthread_join( t, NULL );
92
93    if (g_ok1 != +1) {
94        if (g_ok1 == 0) {
95            fprintf(stderr, "cleanup1 not called !!\n");
96        } else {
97            fprintf(stderr, "cleanup1 called with wrong argument\n" );
98        }
99        exit(1);
100    }
101    else if (g_ok2 != +1) {
102        if (g_ok2 == 0)
103            fprintf(stderr, "cleanup2 not called !!\n");
104        else
105            fprintf(stderr, "cleanup2 called with wrong argument\n");
106        exit(2);
107    }
108    else if (do_exit && g_ok3 != +1) {
109        if (g_ok3 == 0) {
110            fprintf(stderr, "cleanup3 not called !!\n");
111        } else {
112            fprintf(stderr, "cleanup3 called with bad argument !!\n");
113        }
114        exit(3);
115    }
116    else if (!do_exit && g_ok3 != 0) {
117        if (g_ok3 == 1) {
118            fprintf(stderr, "cleanup3 wrongly called !!\n");
119        } else {
120            fprintf(stderr, "cleanup3 wrongly called with bad argument !!\n");
121        }
122        exit(3);
123    }
124
125    return 0;
126}
127
128int main( void )
129{
130    test(0);
131    test(1);
132    printf("OK\n");
133    return 0;
134}
135