summaryrefslogtreecommitdiff
path: root/srclib/apr/test/testglobalmutex.c
blob: ccd7d7bea50fb49bd8c24a428560c565898c88b5 (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
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 * applicable.
 *
 * Licensed 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 "testglobalmutex.h"
#include "apr_thread_proc.h"
#include "apr_global_mutex.h"
#include "apr_strings.h"
#include "apr_errno.h"
#include "testutil.h"

static void launch_child(abts_case *tc, apr_lockmech_e mech,
                         apr_proc_t *proc, apr_pool_t *p)
{
    apr_procattr_t *procattr;
    const char *args[3];
    apr_status_t rv;

    rv = apr_procattr_create(&procattr, p);
    APR_ASSERT_SUCCESS(tc, "Couldn't create procattr", rv);

    rv = apr_procattr_io_set(procattr, APR_NO_PIPE, APR_NO_PIPE,
            APR_NO_PIPE);
    APR_ASSERT_SUCCESS(tc, "Couldn't set io in procattr", rv);

    rv = apr_procattr_error_check_set(procattr, 1);
    APR_ASSERT_SUCCESS(tc, "Couldn't set error check in procattr", rv);

    args[0] = "globalmutexchild" EXTENSION;
    args[1] = (const char*)apr_itoa(p, (int)mech);
    args[2] = NULL;
    rv = apr_proc_create(proc, "./globalmutexchild" EXTENSION, args, NULL,
            procattr, p);
    APR_ASSERT_SUCCESS(tc, "Couldn't launch program", rv);
}

static int wait_child(abts_case *tc, apr_proc_t *proc)
{
    int exitcode;
    apr_exit_why_e why;

    ABTS_ASSERT(tc, "Error waiting for child process",
            apr_proc_wait(proc, &exitcode, &why, APR_WAIT) == APR_CHILD_DONE);

    ABTS_ASSERT(tc, "child didn't terminate normally", why == APR_PROC_EXIT);
    return exitcode;
}

/* return symbolic name for a locking meechanism */
static const char *mutexname(apr_lockmech_e mech)
{
    switch (mech) {
    case APR_LOCK_FCNTL: return "fcntl";
    case APR_LOCK_FLOCK: return "flock";
    case APR_LOCK_SYSVSEM: return "sysvsem";
    case APR_LOCK_PROC_PTHREAD: return "proc_pthread";
    case APR_LOCK_POSIXSEM: return "posixsem";
    case APR_LOCK_DEFAULT: return "default";
    default: return "unknown";
    }
}

static void test_exclusive(abts_case *tc, void *data)
{
    apr_lockmech_e mech = *(apr_lockmech_e *)data;
    apr_proc_t p1, p2, p3, p4;
    apr_status_t rv;
    apr_global_mutex_t *global_lock;
    int x = 0;
    abts_log_message("lock mechanism is: ");
    abts_log_message(mutexname(mech));
 
    rv = apr_global_mutex_create(&global_lock, LOCKNAME, mech, p);
    APR_ASSERT_SUCCESS(tc, "Error creating mutex", rv);

    launch_child(tc, mech, &p1, p);
    launch_child(tc, mech, &p2, p);
    launch_child(tc, mech, &p3, p);
    launch_child(tc, mech, &p4, p);
 
    x += wait_child(tc, &p1);
    x += wait_child(tc, &p2);
    x += wait_child(tc, &p3);
    x += wait_child(tc, &p4);

    if (x != MAX_COUNTER) {
        char buf[200];
        sprintf(buf, "global mutex '%s' failed: %d not %d",
                mutexname(mech), x, MAX_COUNTER);
        abts_fail(tc, buf, __LINE__);
    }
}

abts_suite *testglobalmutex(abts_suite *suite)
{
    apr_lockmech_e mech = APR_LOCK_DEFAULT;

    suite = ADD_SUITE(suite)
    abts_run_test(suite, test_exclusive, &mech);
#if APR_HAS_POSIXSEM_SERIALIZE
    mech = APR_LOCK_POSIXSEM;
    abts_run_test(suite, test_exclusive, &mech);
#endif
#if APR_HAS_SYSVSEM_SERIALIZE
    mech = APR_LOCK_SYSVSEM;
    abts_run_test(suite, test_exclusive, &mech);
#endif
#if APR_HAS_PROC_PTHREAD_SERIALIZE
    mech = APR_LOCK_PROC_PTHREAD;
    abts_run_test(suite, test_exclusive, &mech);
#endif
#if APR_HAS_FCNTL_SERIALIZE
    mech = APR_LOCK_FCNTL;
    abts_run_test(suite, test_exclusive, &mech);
#endif
#if APR_HAS_FLOCK_SERIALIZE
    mech = APR_LOCK_FLOCK;
    abts_run_test(suite, test_exclusive, &mech);
#endif

    return suite;
}