testglobalmutex.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "testglobalmutex.h"
  17. #include "fspr_thread_proc.h"
  18. #include "fspr_global_mutex.h"
  19. #include "fspr_strings.h"
  20. #include "fspr_errno.h"
  21. #include "testutil.h"
  22. static void launch_child(abts_case *tc, fspr_lockmech_e mech,
  23. fspr_proc_t *proc, fspr_pool_t *p)
  24. {
  25. fspr_procattr_t *procattr;
  26. const char *args[3];
  27. fspr_status_t rv;
  28. rv = fspr_procattr_create(&procattr, p);
  29. APR_ASSERT_SUCCESS(tc, "Couldn't create procattr", rv);
  30. rv = fspr_procattr_io_set(procattr, APR_NO_PIPE, APR_NO_PIPE,
  31. APR_NO_PIPE);
  32. APR_ASSERT_SUCCESS(tc, "Couldn't set io in procattr", rv);
  33. rv = fspr_procattr_error_check_set(procattr, 1);
  34. APR_ASSERT_SUCCESS(tc, "Couldn't set error check in procattr", rv);
  35. args[0] = "globalmutexchild" EXTENSION;
  36. args[1] = (const char*)fspr_itoa(p, (int)mech);
  37. args[2] = NULL;
  38. rv = fspr_proc_create(proc, "./globalmutexchild" EXTENSION, args, NULL,
  39. procattr, p);
  40. APR_ASSERT_SUCCESS(tc, "Couldn't launch program", rv);
  41. }
  42. static int wait_child(abts_case *tc, fspr_proc_t *proc)
  43. {
  44. int exitcode;
  45. fspr_exit_why_e why;
  46. ABTS_ASSERT(tc, "Error waiting for child process",
  47. fspr_proc_wait(proc, &exitcode, &why, APR_WAIT) == APR_CHILD_DONE);
  48. ABTS_ASSERT(tc, "child didn't terminate normally", why == APR_PROC_EXIT);
  49. return exitcode;
  50. }
  51. /* return symbolic name for a locking meechanism */
  52. static const char *mutexname(fspr_lockmech_e mech)
  53. {
  54. switch (mech) {
  55. case APR_LOCK_FCNTL: return "fcntl";
  56. case APR_LOCK_FLOCK: return "flock";
  57. case APR_LOCK_SYSVSEM: return "sysvsem";
  58. case APR_LOCK_PROC_PTHREAD: return "proc_pthread";
  59. case APR_LOCK_POSIXSEM: return "posixsem";
  60. case APR_LOCK_DEFAULT: return "default";
  61. default: return "unknown";
  62. }
  63. }
  64. static void test_exclusive(abts_case *tc, void *data)
  65. {
  66. fspr_lockmech_e mech = *(fspr_lockmech_e *)data;
  67. fspr_proc_t p1, p2, p3, p4;
  68. fspr_status_t rv;
  69. fspr_global_mutex_t *global_lock;
  70. int x = 0;
  71. abts_log_message("lock mechanism is: ");
  72. abts_log_message(mutexname(mech));
  73. rv = fspr_global_mutex_create(&global_lock, LOCKNAME, mech, p);
  74. APR_ASSERT_SUCCESS(tc, "Error creating mutex", rv);
  75. launch_child(tc, mech, &p1, p);
  76. launch_child(tc, mech, &p2, p);
  77. launch_child(tc, mech, &p3, p);
  78. launch_child(tc, mech, &p4, p);
  79. x += wait_child(tc, &p1);
  80. x += wait_child(tc, &p2);
  81. x += wait_child(tc, &p3);
  82. x += wait_child(tc, &p4);
  83. if (x != MAX_COUNTER) {
  84. char buf[200];
  85. sprintf(buf, "global mutex '%s' failed: %d not %d",
  86. mutexname(mech), x, MAX_COUNTER);
  87. abts_fail(tc, buf, __LINE__);
  88. }
  89. }
  90. abts_suite *testglobalmutex(abts_suite *suite)
  91. {
  92. fspr_lockmech_e mech = APR_LOCK_DEFAULT;
  93. suite = ADD_SUITE(suite)
  94. abts_run_test(suite, test_exclusive, &mech);
  95. #if APR_HAS_POSIXSEM_SERIALIZE
  96. mech = APR_LOCK_POSIXSEM;
  97. abts_run_test(suite, test_exclusive, &mech);
  98. #endif
  99. #if APR_HAS_SYSVSEM_SERIALIZE
  100. mech = APR_LOCK_SYSVSEM;
  101. abts_run_test(suite, test_exclusive, &mech);
  102. #endif
  103. #if APR_HAS_PROC_PTHREAD_SERIALIZE
  104. mech = APR_LOCK_PROC_PTHREAD;
  105. abts_run_test(suite, test_exclusive, &mech);
  106. #endif
  107. #if APR_HAS_FCNTL_SERIALIZE
  108. mech = APR_LOCK_FCNTL;
  109. abts_run_test(suite, test_exclusive, &mech);
  110. #endif
  111. #if APR_HAS_FLOCK_SERIALIZE
  112. mech = APR_LOCK_FLOCK;
  113. abts_run_test(suite, test_exclusive, &mech);
  114. #endif
  115. return suite;
  116. }