hmutex_test.c 3.0 KB

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