testthread.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "fspr_thread_proc.h"
  17. #include "fspr_errno.h"
  18. #include "fspr_general.h"
  19. #include "errno.h"
  20. #include "fspr_time.h"
  21. #include "testutil.h"
  22. #if APR_HAS_THREADS
  23. static fspr_thread_mutex_t *thread_lock;
  24. static fspr_thread_once_t *control = NULL;
  25. static int x = 0;
  26. static int value = 0;
  27. static fspr_thread_t *t1;
  28. static fspr_thread_t *t2;
  29. static fspr_thread_t *t3;
  30. static fspr_thread_t *t4;
  31. /* just some made up number to check on later */
  32. static fspr_status_t exit_ret_val = 123;
  33. static void init_func(void)
  34. {
  35. value++;
  36. }
  37. static void * APR_THREAD_FUNC thread_func1(fspr_thread_t *thd, void *data)
  38. {
  39. int i;
  40. fspr_thread_once(control, init_func);
  41. for (i = 0; i < 10000; i++) {
  42. fspr_thread_mutex_lock(thread_lock);
  43. x++;
  44. fspr_thread_mutex_unlock(thread_lock);
  45. }
  46. fspr_thread_exit(thd, exit_ret_val);
  47. return NULL;
  48. }
  49. static void thread_init(abts_case *tc, void *data)
  50. {
  51. fspr_status_t rv;
  52. rv = fspr_thread_once_init(&control, p);
  53. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  54. rv = fspr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p);
  55. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  56. }
  57. static void create_threads(abts_case *tc, void *data)
  58. {
  59. fspr_status_t rv;
  60. rv = fspr_thread_create(&t1, NULL, thread_func1, NULL, p);
  61. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  62. rv = fspr_thread_create(&t2, NULL, thread_func1, NULL, p);
  63. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  64. rv = fspr_thread_create(&t3, NULL, thread_func1, NULL, p);
  65. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  66. rv = fspr_thread_create(&t4, NULL, thread_func1, NULL, p);
  67. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  68. }
  69. static void join_threads(abts_case *tc, void *data)
  70. {
  71. fspr_status_t s;
  72. fspr_thread_join(&s, t1);
  73. ABTS_INT_EQUAL(tc, exit_ret_val, s);
  74. fspr_thread_join(&s, t2);
  75. ABTS_INT_EQUAL(tc, exit_ret_val, s);
  76. fspr_thread_join(&s, t3);
  77. ABTS_INT_EQUAL(tc, exit_ret_val, s);
  78. fspr_thread_join(&s, t4);
  79. ABTS_INT_EQUAL(tc, exit_ret_val, s);
  80. }
  81. static void check_locks(abts_case *tc, void *data)
  82. {
  83. ABTS_INT_EQUAL(tc, 40000, x);
  84. }
  85. static void check_thread_once(abts_case *tc, void *data)
  86. {
  87. ABTS_INT_EQUAL(tc, 1, value);
  88. }
  89. #else
  90. static void threads_not_impl(abts_case *tc, void *data)
  91. {
  92. ABTS_NOT_IMPL(tc, "Threads not implemented on this platform");
  93. }
  94. #endif
  95. abts_suite *testthread(abts_suite *suite)
  96. {
  97. suite = ADD_SUITE(suite)
  98. #if !APR_HAS_THREADS
  99. abts_run_test(suite, threads_not_impl, NULL);
  100. #else
  101. abts_run_test(suite, thread_init, NULL);
  102. abts_run_test(suite, create_threads, NULL);
  103. abts_run_test(suite, join_threads, NULL);
  104. abts_run_test(suite, check_locks, NULL);
  105. abts_run_test(suite, check_thread_once, NULL);
  106. #endif
  107. return suite;
  108. }