summaryrefslogtreecommitdiffstats
path: root/util/test/util/test_thread.c
blob: 4bddd7fcc02624dc32608ea5a9266c52df5d075c (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <axutil_error_default.h>
#include <axutil_log.h>
#include <axutil_log_default.h>
#include <axutil_allocator.h>
#include <axutil_utils.h>
#include "test_thread.h"
#include <cut_defs.h>

const axutil_env_t *env = NULL;
static axutil_thread_mutex_t *thread_lock = NULL;
static axutil_thread_once_t *control = NULL;
static int x = 0;
static int value = 0;
static int param_data;

static axutil_thread_t *t1 = NULL;
static axutil_thread_t *t2 = NULL;

/*for detach tests*/
static axutil_thread_t *t3 = NULL;
static axutil_thread_t *t4 = NULL;

void
init_func(
    void)
{
    value++;
}

void
test_thread_init(
    const axutil_env_t * env)
{
    axutil_allocator_t *allocator = NULL;

    allocator = env->allocator;

    control = axutil_thread_once_init(allocator);
    CUT_ASSERT_PTR_NOT_EQUAL(control, NULL, 0);

    thread_lock =
        axutil_thread_mutex_create(allocator, AXIS2_THREAD_MUTEX_DEFAULT);
    CUT_ASSERT_PTR_NOT_EQUAL(thread_lock, NULL, 0);
    /* To avoid warning of not using cut_str_equal */
    CUT_ASSERT_STR_EQUAL("", "", 0);

}

void *AXIS2_CALL
test_function(
    axutil_thread_t * td,
    void *param)
{
    int i  = *((int *) param);
    printf("thread data = %d \n", i);
	param_data = i;
	
    axutil_thread_once(control, init_func);
    CUT_ASSERT(value==1);
    axutil_thread_mutex_lock(thread_lock);
    printf("x = %d \n", ++x);
    axutil_thread_mutex_unlock(thread_lock);

    /*axutil_thread_exit(td, env->allocator); */

    return (void *) 1;
}

void
test_axutil_thread_create(
    const axutil_env_t * env)
{
    axis2_status_t rv = AXIS2_FAILURE;
    axutil_allocator_t *allocator = NULL;
    int *i = NULL,
        *j = NULL;

    allocator = env->allocator;
    i = AXIS2_MALLOC(allocator, sizeof(int));
    *i = 5;
    param_data = -1;
    t1 = axutil_thread_create(allocator, NULL, test_function, (void *) i);
    CUT_ASSERT_PTR_NOT_EQUAL(t1, NULL, 0);
    AXIS2_SLEEP(1);
    CUT_ASSERT_INT_EQUAL(param_data, *i, 0);


    j = AXIS2_MALLOC(allocator, sizeof(int));
    *j = 25;
    param_data = -1;
    t2 = axutil_thread_create(allocator, NULL, test_function, (void *) j);
    CUT_ASSERT_PTR_NOT_EQUAL(t2, NULL, 0);
    AXIS2_SLEEP(1);
    CUT_ASSERT_INT_EQUAL(param_data, *j, 0);

    rv = axutil_thread_join(t1);
    CUT_ASSERT_INT_EQUAL(rv, AXIS2_SUCCESS, 0);

    rv = axutil_thread_join(t2);
    CUT_ASSERT_INT_EQUAL(rv, AXIS2_SUCCESS, 0);

}

void *AXIS2_CALL
test_function2(
    axutil_thread_t * td,
    void *param)
{
    printf("thread \n");
    return (void *) 1;
}

void
test_axutil_thread_detach(
    const axutil_env_t * env)
{
    axutil_threadattr_t *attr = NULL;
    axutil_allocator_t *allocator = NULL;
    axis2_status_t rv = AXIS2_FAILURE;

    allocator = env->allocator;
    attr = axutil_threadattr_create(allocator);
    CUT_ASSERT_PTR_NOT_EQUAL(attr, NULL, 1);
    rv = axutil_threadattr_detach_set(attr, 1);
    CUT_ASSERT_INT_EQUAL(rv, AXIS2_SUCCESS, 1);
    t3 = axutil_thread_create(allocator, attr, test_function2, NULL);
    CUT_ASSERT_PTR_NOT_EQUAL(t3, NULL, 1);

    /*
     * thread is already detached - should return AXIS2_FAILURE
     */
    rv = axutil_thread_detach(t3);
    CUT_ASSERT_INT_EQUAL(rv, AXIS2_FAILURE, 1);
 }

void
test_axutil_thread_detach2(
    const axutil_env_t * env)
{
    axutil_threadattr_t *attr = NULL;
    axutil_allocator_t *allocator = NULL;
    axis2_status_t rv = AXIS2_FAILURE;

    allocator = env->allocator;
    attr = axutil_threadattr_create(allocator);
    CUT_ASSERT_PTR_NOT_EQUAL(attr, NULL, 1);

    t4 = axutil_thread_create(allocator, attr, test_function2, NULL);
    CUT_ASSERT_PTR_NOT_EQUAL(t4, NULL, 1);
    /*
     * thread is not detached yet - should return AXIS2_SUCCESS
     */
    rv = axutil_thread_detach(t4);
    CUT_ASSERT_INT_EQUAL(rv, AXIS2_SUCCESS, 1);
}

void
check_locks(
    )
{
    CUT_ASSERT_INT_EQUAL(x, 2, 0);
}

void
check_thread_once(
    )
{
    CUT_ASSERT_INT_EQUAL(value, 1, 0);
}

void
run_test_thread(
    const axutil_env_t * env)
{
    test_thread_init(env);
    test_axutil_thread_create(env);
    check_locks();
    check_thread_once();
    test_axutil_thread_detach(env);
    test_axutil_thread_detach2(env);

#if defined (WIN32)
    Sleep(1000);                /*to give time for detached threads to execute */
#else
    sleep(2);
#endif

    axutil_thread_mutex_destroy(thread_lock);
}

int
main(
    void)
{
    axutil_env_t *env = cut_setup_env("util thread");
	CUT_ASSERT(env != NULL);
	if (env) {
       run_test_thread(env);
       axutil_env_free(env);
    }
    CUT_RETURN_ON_FAILURE(-1);
    return 0;
}