1
0

hmutex_test.c 3.5 KB

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