hmutex_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. HTHREAD_ROUTINE(test_spinlock) {
  26. hspinlock_t spin;
  27. hspinlock_init(&spin);
  28. hspinlock_lock(&spin);
  29. hspinlock_unlock(&spin);
  30. hspinlock_destroy(&spin);
  31. printf("hspinlock test OK!\n");
  32. return 0;
  33. }
  34. HTHREAD_ROUTINE(test_rwlock) {
  35. hrwlock_t rwlock;
  36. hrwlock_init(&rwlock);
  37. hrwlock_rdlock(&rwlock);
  38. hrwlock_rdunlock(&rwlock);
  39. hrwlock_wrlock(&rwlock);
  40. hrwlock_wrunlock(&rwlock);
  41. hrwlock_destroy(&rwlock);
  42. printf("hrwlock test OK!\n");
  43. return 0;
  44. }
  45. HTHREAD_ROUTINE(test_timed_mutex) {
  46. htimed_mutex_t mutex;
  47. htimed_mutex_init(&mutex);
  48. htimed_mutex_lock(&mutex);
  49. time_t start_time = time(NULL);
  50. htimed_mutex_lock_for(&mutex, 3000);
  51. time_t end_time = time(NULL);
  52. htimed_mutex_unlock(&mutex);
  53. htimed_mutex_destroy(&mutex);
  54. printf("htimed_mutex_lock_for %zds\n", end_time - start_time);
  55. printf("htimed_mutex test OK!\n");
  56. return 0;
  57. }
  58. HTHREAD_ROUTINE(test_condvar) {
  59. hmutex_t mutex;
  60. hmutex_init(&mutex);
  61. hcondvar_t cv;
  62. hcondvar_init(&cv);
  63. hmutex_lock(&mutex);
  64. hcondvar_signal(&cv);
  65. hcondvar_broadcast(&cv);
  66. time_t start_time = time(NULL);
  67. hcondvar_wait_for(&cv, &mutex, 3000);
  68. time_t end_time = time(NULL);
  69. printf("hcondvar_wait_for %zds\n", end_time - start_time);
  70. hmutex_unlock(&mutex);
  71. hmutex_destroy(&mutex);
  72. hcondvar_destroy(&cv);
  73. printf("hcondvar test OK!\n");
  74. return 0;
  75. }
  76. HTHREAD_ROUTINE(test_sem) {
  77. hsem_t sem;
  78. hsem_init(&sem, 10);
  79. for (int i = 0; i < 10; ++i) {
  80. hsem_wait(&sem);
  81. }
  82. hsem_post(&sem);
  83. hsem_wait(&sem);
  84. time_t start_time = time(NULL);
  85. hsem_wait_for(&sem, 3000);
  86. time_t end_time = time(NULL);
  87. printf("hsem_wait_for %zds\n", end_time - start_time);
  88. hsem_destroy(&sem);
  89. printf("hsem test OK!\n");
  90. return 0;
  91. }
  92. int main(int argc, char* argv[]) {
  93. hthread_t thread_once = hthread_create(test_once, NULL);
  94. hthread_t thread_mutex = hthread_create(test_mutex, NULL);
  95. hthread_t thread_spinlock = hthread_create(test_spinlock, NULL);
  96. hthread_t thread_rwlock = hthread_create(test_rwlock, NULL);
  97. hthread_t thread_timed_mutex = hthread_create(test_timed_mutex, NULL);
  98. hthread_t thread_condvar = hthread_create(test_condvar, NULL);
  99. hthread_t thread_sem = hthread_create(test_sem, NULL);
  100. hthread_join(thread_once);
  101. hthread_join(thread_mutex);
  102. hthread_join(thread_spinlock);
  103. hthread_join(thread_rwlock);
  104. hthread_join(thread_timed_mutex);
  105. hthread_join(thread_condvar);
  106. hthread_join(thread_sem);
  107. printf("hthread test OK!\n");
  108. return 0;
  109. }