"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "test/testprocmutex.c" between
apr-1.6.5.tar.bz2 and apr-1.7.0.tar.bz2

About: APR (Apache Portable Runtime) project offers software libraries that provide a predictable and consistent interface to underlying platform-specific implementations (APR core library).

testprocmutex.c  (apr-1.6.5.tar.bz2):testprocmutex.c  (apr-1.7.0.tar.bz2)
skipping to change at line 23 skipping to change at line 23
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include "apr_shm.h" #include "apr_shm.h"
#include "apr_thread_proc.h" #include "apr_thread_proc.h"
#include "apr_file_io.h" #include "apr_file_io.h"
#include "apr_proc_mutex.h" #include "apr_proc_mutex.h"
#include "apr_errno.h" #include "apr_errno.h"
#include "apr_general.h" #include "apr_general.h"
#include "apr_strings.h"
#include "apr_getopt.h" #include "apr_getopt.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "testutil.h" #include "testutil.h"
#if APR_HAS_FORK #if APR_HAS_FORK
#define MAX_ITER 200 #define MAX_ITER 200
#define CHILDREN 6 #define CHILDREN 6
#define MAX_COUNTER (MAX_ITER * CHILDREN) #define MAX_COUNTER (MAX_ITER * CHILDREN)
#define MAX_WAIT_USEC (1000*1000) #define MAX_WAIT_USEC (1000*1000)
static apr_proc_mutex_t *proc_lock; static apr_proc_mutex_t *proc_lock;
static volatile int *x; static volatile int *x;
typedef struct lockmech {
apr_lockmech_e num;
const char *name;
} lockmech_t;
/* a slower more racy way to implement (*x)++ */ /* a slower more racy way to implement (*x)++ */
static int increment(int n) static int increment(int n)
{ {
apr_sleep(1); apr_sleep(1);
return n+1; return n+1;
} }
static void make_child(abts_case *tc, int trylock, apr_proc_t **proc, apr_pool_t *p) static void make_child(abts_case *tc, int trylock, apr_proc_t **proc, apr_pool_t *p)
{ {
apr_status_t rv; apr_status_t rv;
skipping to change at line 71 skipping to change at line 77
* will happen. In this case, the bad thing is that if the mutex * will happen. In this case, the bad thing is that if the mutex
* is a semaphore, it will be destroyed before all of the processes * is a semaphore, it will be destroyed before all of the processes
* die. That means that the test will most likely fail. * die. That means that the test will most likely fail.
*/ */
apr_initialize(); apr_initialize();
if (apr_proc_mutex_child_init(&proc_lock, NULL, p)) if (apr_proc_mutex_child_init(&proc_lock, NULL, p))
exit(1); exit(1);
do { do {
if (trylock) { if (trylock > 0) {
int wait_usec = 0; int wait_usec = 0;
while ((rv = apr_proc_mutex_trylock(proc_lock))) { while ((rv = apr_proc_mutex_trylock(proc_lock))) {
if (!APR_STATUS_IS_EBUSY(rv)) if (!APR_STATUS_IS_EBUSY(rv))
exit(1); exit(1);
if (++wait_usec >= MAX_WAIT_USEC) if (++wait_usec >= MAX_WAIT_USEC)
exit(1); exit(1);
apr_sleep(1); apr_sleep(1);
} }
} }
else if (trylock < 0) {
int wait_usec = 0;
while ((rv = apr_proc_mutex_timedlock(proc_lock, 1))) {
if (!APR_STATUS_IS_TIMEUP(rv))
exit(1);
if (++wait_usec >= MAX_WAIT_USEC)
exit(1);
}
}
else { else {
if (apr_proc_mutex_lock(proc_lock)) if (apr_proc_mutex_lock(proc_lock))
exit(1); exit(1);
} }
i++; i++;
*x = increment(*x); *x = increment(*x);
if (apr_proc_mutex_unlock(proc_lock)) if (apr_proc_mutex_unlock(proc_lock))
exit(1); exit(1);
} while (i < MAX_ITER); } while (i < MAX_ITER);
skipping to change at line 111 skipping to change at line 127
int code; int code;
apr_exit_why_e why; apr_exit_why_e why;
apr_status_t rv; apr_status_t rv;
rv = apr_proc_wait(proc, &code, &why, APR_WAIT); rv = apr_proc_wait(proc, &code, &why, APR_WAIT);
ABTS_ASSERT(tc, "child did not terminate with success", ABTS_ASSERT(tc, "child did not terminate with success",
rv == APR_CHILD_DONE && why == APR_PROC_EXIT && code == 0); rv == APR_CHILD_DONE && why == APR_PROC_EXIT && code == 0);
} }
static void test_exclusive(abts_case *tc, const char *lockname, static void test_exclusive(abts_case *tc, const char *lockname,
apr_lockmech_e mech) lockmech_t *mech)
{ {
apr_proc_t *child[CHILDREN]; apr_proc_t *child[CHILDREN];
apr_status_t rv; apr_status_t rv;
int n; int n;
rv = apr_proc_mutex_create(&proc_lock, lockname, mech, p); rv = apr_proc_mutex_create(&proc_lock, lockname, mech->num, p);
APR_ASSERT_SUCCESS(tc, "create the mutex", rv); if (rv == APR_ENOTIMPL) {
if (rv != APR_SUCCESS) /* MacOS lacks TIMED implementation, so don't fail for ENOTIMPL */
fprintf(stderr, "method %s not implemented, ", mech->name);
return; return;
}
APR_ASSERT_SUCCESS(tc, "create the mutex", rv);
for (n = 0; n < CHILDREN; n++) for (n = 0; n < CHILDREN; n++)
make_child(tc, 0, &child[n], p); make_child(tc, 0, &child[n], p);
for (n = 0; n < CHILDREN; n++) for (n = 0; n < CHILDREN; n++)
await_child(tc, child[n]); await_child(tc, child[n]);
ABTS_ASSERT(tc, "Locks don't appear to work", *x == MAX_COUNTER); ABTS_ASSERT(tc, "Locks don't appear to work", *x == MAX_COUNTER);
rv = apr_proc_mutex_trylock(proc_lock); rv = apr_proc_mutex_trylock(proc_lock);
if (rv == APR_ENOTIMPL) { if (rv == APR_ENOTIMPL) {
ABTS_NOT_IMPL(tc, "apr_proc_mutex_trylock not implemented"); fprintf(stderr, "%s_trylock() not implemented, ", mech->name);
return; ABTS_ASSERT(tc, "Default timed trylock not implemented",
mech->num != APR_LOCK_DEFAULT &&
mech->num != APR_LOCK_DEFAULT_TIMED);
} }
APR_ASSERT_SUCCESS(tc, "check for trylock", rv); else {
APR_ASSERT_SUCCESS(tc, "check for trylock", rv);
rv = apr_proc_mutex_unlock(proc_lock); for (n = 0; n < 2; n++) {
APR_ASSERT_SUCCESS(tc, "unlock after trylock check", rv); rv = apr_proc_mutex_trylock(proc_lock);
/* Some mech (eg. flock or fcntl) may succeed when the
* lock is re-acquired in the same process.
*/
if (rv != APR_SUCCESS) {
ABTS_ASSERT(tc,
apr_psprintf(p, "%s_trylock() should be busy => %pm"
,
mech->name, &rv),
APR_STATUS_IS_EBUSY(rv));
}
}
*x = 0; rv = apr_proc_mutex_unlock(proc_lock);
APR_ASSERT_SUCCESS(tc, "unlock after trylock check", rv);
for (n = 0; n < CHILDREN; n++) *x = 0;
make_child(tc, 1, &child[n], p);
for (n = 0; n < CHILDREN; n++) for (n = 0; n < CHILDREN; n++)
await_child(tc, child[n]); make_child(tc, 1, &child[n], p);
for (n = 0; n < CHILDREN; n++)
await_child(tc, child[n]);
ABTS_ASSERT(tc, "Locks don't appear to work with trylock",
*x == MAX_COUNTER);
}
ABTS_ASSERT(tc, "Locks don't appear to work with trylock", #if APR_HAS_TIMEDLOCKS
*x == MAX_COUNTER); rv = apr_proc_mutex_timedlock(proc_lock, 1);
if (rv == APR_ENOTIMPL) {
fprintf(stderr, "%s_timedlock() not implemented, ", mech->name);
ABTS_ASSERT(tc, "Default timed timedlock not implemented",
mech->num != APR_LOCK_DEFAULT_TIMED);
}
else {
APR_ASSERT_SUCCESS(tc, "check for timedlock", rv);
for (n = 0; n < 2; n++) {
rv = apr_proc_mutex_timedlock(proc_lock, 1);
/* Some mech (eg. flock or fcntl) may succeed when the
* lock is re-acquired in the same process.
*/
if (rv != APR_SUCCESS) {
ABTS_ASSERT(tc,
apr_psprintf(p, "%s_timedlock() should time out => %
pm",
mech->name, &rv),
APR_STATUS_IS_TIMEUP(rv));
}
}
rv = apr_proc_mutex_unlock(proc_lock);
APR_ASSERT_SUCCESS(tc, "unlock after timedlock check", rv);
*x = 0;
for (n = 0; n < CHILDREN; n++)
make_child(tc, -1, &child[n], p);
for (n = 0; n < CHILDREN; n++)
await_child(tc, child[n]);
ABTS_ASSERT(tc, "Locks don't appear to work with timedlock",
*x == MAX_COUNTER);
}
#endif /* APR_HAS_TIMEDLOCKS */
} }
static void proc_mutex(abts_case *tc, void *data) static void proc_mutex(abts_case *tc, void *data)
{ {
apr_status_t rv; apr_status_t rv;
const char *shmname = "tpm.shm"; const char *shmname = "tpm.shm";
apr_shm_t *shm; apr_shm_t *shm;
apr_lockmech_e *mech = data;
/* Use anonymous shm if available. */ /* Use anonymous shm if available. */
rv = apr_shm_create(&shm, sizeof(int), NULL, p); rv = apr_shm_create(&shm, sizeof(int), NULL, p);
if (rv == APR_ENOTIMPL) { if (rv == APR_ENOTIMPL) {
apr_file_remove(shmname, p); apr_file_remove(shmname, p);
rv = apr_shm_create(&shm, sizeof(int), shmname, p); rv = apr_shm_create(&shm, sizeof(int), shmname, p);
} }
APR_ASSERT_SUCCESS(tc, "create shm segment", rv); APR_ASSERT_SUCCESS(tc, "create shm segment", rv);
if (rv != APR_SUCCESS) if (rv != APR_SUCCESS)
return; return;
x = apr_shm_baseaddr_get(shm); x = apr_shm_baseaddr_get(shm);
test_exclusive(tc, NULL, *mech); test_exclusive(tc, NULL, data);
rv = apr_shm_destroy(shm); rv = apr_shm_destroy(shm);
APR_ASSERT_SUCCESS(tc, "Error destroying shared memory block", rv); APR_ASSERT_SUCCESS(tc, "Error destroying shared memory block", rv);
} }
abts_suite *testprocmutex(abts_suite *suite) abts_suite *testprocmutex(abts_suite *suite)
{ {
apr_lockmech_e mech = APR_LOCK_DEFAULT; lockmech_t lockmechs[] = {
{APR_LOCK_DEFAULT, "default"}
suite = ADD_SUITE(suite) #if APR_HAS_FLOCK_SERIALIZE
abts_run_test(suite, proc_mutex, &mech); ,{APR_LOCK_FLOCK, "flock"}
#if APR_HAS_POSIXSEM_SERIALIZE
mech = APR_LOCK_POSIXSEM;
abts_run_test(suite, proc_mutex, &mech);
#endif #endif
#if APR_HAS_SYSVSEM_SERIALIZE #if APR_HAS_SYSVSEM_SERIALIZE
mech = APR_LOCK_SYSVSEM; ,{APR_LOCK_SYSVSEM, "sysvsem"}
abts_run_test(suite, proc_mutex, &mech);
#endif #endif
#if APR_HAS_PROC_PTHREAD_SERIALIZE #if APR_HAS_POSIXSEM_SERIALIZE
mech = APR_LOCK_PROC_PTHREAD; ,{APR_LOCK_POSIXSEM, "posix"}
abts_run_test(suite, proc_mutex, &mech);
#endif #endif
#if APR_HAS_FCNTL_SERIALIZE #if APR_HAS_FCNTL_SERIALIZE
mech = APR_LOCK_FCNTL; ,{APR_LOCK_FCNTL, "fcntl"}
abts_run_test(suite, proc_mutex, &mech);
#endif #endif
#if APR_HAS_FLOCK_SERIALIZE #if APR_HAS_PROC_PTHREAD_SERIALIZE
mech = APR_LOCK_FLOCK; ,{APR_LOCK_PROC_PTHREAD, "proc_pthread"}
abts_run_test(suite, proc_mutex, &mech);
#endif #endif
,{APR_LOCK_DEFAULT_TIMED, "default_timed"}
};
int i;
suite = ADD_SUITE(suite)
for (i = 0; i < sizeof(lockmechs) / sizeof(lockmechs[0]); i++) {
abts_run_test(suite, proc_mutex, &lockmechs[i]);
}
return suite; return suite;
} }
#else /* APR_HAS_FORK */ #else /* APR_HAS_FORK */
static void proc_mutex(abts_case *tc, void *data) static void proc_mutex(abts_case *tc, void *data)
{ {
ABTS_NOT_IMPL(tc, "APR lacks fork() support"); ABTS_NOT_IMPL(tc, "APR lacks fork() support");
} }
 End of changes. 23 change blocks. 
36 lines changed or deleted 112 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)