hmutex_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "hthread.h"
  2. #include "hmutex.h"
  3. #include <stdio.h>
  4. #include <time.h>
  5. void once_print() {
  6. printf("exec once\n");
  7. }
  8. HTHREAD_ROUTINE(test_once) {
  9. honce_t once = HONCE_INIT;
  10. for (int i = 0; i < 10; ++i) {
  11. honce(&once, once_print);
  12. }
  13. printf("honce test OK!\n");
  14. return 0;
  15. }
  16. HTHREAD_ROUTINE(test_mutex) {
  17. hmutex_t mutex;
  18. hmutex_init(&mutex);
  19. hmutex_lock(&mutex);
  20. hmutex_unlock(&mutex);
  21. hmutex_destroy(&mutex);
  22. printf("hmutex test OK!\n");
  23. return 0;
  24. }
  25. #if HAVE_PTHREAD_SPIN_LOCK
  26. HTHREAD_ROUTINE(test_spinlock) {
  27. hspinlock_t spin;
  28. hspinlock_init(&spin);
  29. hspinlock_lock(&spin);
  30. hspinlock_unlock(&spin);
  31. hspinlock_destroy(&spin);
  32. printf("hspinlock test OK!\n");
  33. return 0;
  34. }
  35. #endif
  36. HTHREAD_ROUTINE(test_rwlock) {
  37. hrwlock_t rwlock;
  38. hrwlock_init(&rwlock);
  39. hrwlock_rdlock(&rwlock);
  40. hrwlock_rdunlock(&rwlock);
  41. hrwlock_wrlock(&rwlock);
  42. hrwlock_wrunlock(&rwlock);
  43. hrwlock_destroy(&rwlock);
  44. printf("hrwlock test OK!\n");
  45. return 0;
  46. }
  47. #if HAVE_PTHREAD_MUTEX_TIMEDLOCK
  48. HTHREAD_ROUTINE(test_timed_mutex) {
  49. htimed_mutex_t mutex;
  50. htimed_mutex_init(&mutex);
  51. htimed_mutex_lock(&mutex);
  52. time_t start_time = time(NULL);
  53. htimed_mutex_lock_for(&mutex, 3000);
  54. time_t end_time = time(NULL);
  55. htimed_mutex_unlock(&mutex);
  56. htimed_mutex_destroy(&mutex);
  57. printf("htimed_mutex_lock_for %zds\n", end_time - start_time);
  58. printf("htimed_mutex test OK!\n");
  59. return 0;
  60. }
  61. #endif
  62. HTHREAD_ROUTINE(test_condvar) {
  63. hmutex_t mutex;
  64. hmutex_init(&mutex);
  65. hcondvar_t cv;
  66. hcondvar_init(&cv);
  67. hmutex_lock(&mutex);
  68. hcondvar_signal(&cv);
  69. hcondvar_broadcast(&cv);
  70. time_t start_time = time(NULL);
  71. hcondvar_wait_for(&cv, &mutex, 3000);
  72. time_t end_time = time(NULL);
  73. printf("hcondvar_wait_for %zds\n", end_time - start_time);
  74. hmutex_unlock(&mutex);
  75. hmutex_destroy(&mutex);
  76. hcondvar_destroy(&cv);
  77. printf("hcondvar test OK!\n");
  78. return 0;
  79. }
  80. int main(int argc, char* argv[]) {
  81. hthread_t thread_once = hthread_create(test_once, NULL);
  82. hthread_t thread_mutex = hthread_create(test_mutex, NULL);
  83. #if HAVE_PTHREAD_SPIN_LOCK
  84. hthread_t thread_spinlock = hthread_create(test_spinlock, NULL);
  85. #endif
  86. hthread_t thread_rwlock = hthread_create(test_rwlock, NULL);
  87. #if HAVE_PTHREAD_MUTEX_TIMEDLOCK
  88. hthread_t thread_timed_mutex = hthread_create(test_timed_mutex, NULL);
  89. #endif
  90. hthread_t thread_condvar = hthread_create(test_condvar, NULL);
  91. hthread_join(thread_once);
  92. hthread_join(thread_mutex);
  93. #if HAVE_PTHREAD_SPIN_LOCK
  94. hthread_join(thread_spinlock);
  95. #endif
  96. hthread_join(thread_rwlock);
  97. #if HAVE_PTHREAD_MUTEX_TIMEDLOCK
  98. hthread_join(thread_timed_mutex);
  99. #endif
  100. hthread_join(thread_condvar);
  101. printf("hthread test OK!\n");
  102. return 0;
  103. }