pthread_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <pthread.h>
  2. #include <finsh.h>
  3. #define _die_(x) do { rt_kprintf(x); RT_ASSERT(0); } while (0)
  4. #define pr(x) do { rt_kprintf(x); } while (0)
  5. #define sleep(n) rt_thread_sleep((n * RT_TICK_PER_SECOND)/1000)
  6. #define alarm(n)
  7. /* (0) once test */
  8. void test0_ok() { pr("(once called) "); }
  9. void test0_failed() { _die_("failed...\n"); }
  10. void pth_t0() {
  11. pthread_once_t v_once=PTHREAD_ONCE_INIT;
  12. pr("\nTEST 0: once test:\n\n");
  13. pr("testing once function... ");
  14. pthread_once(&v_once,test0_ok);
  15. pthread_once(&v_once,test0_failed);
  16. pr("OK.\n");
  17. }
  18. FINSH_FUNCTION_EXPORT(pth_t0, pthread testcase0);
  19. /* (1) mutex tests */
  20. void test_rec_mutex() {
  21. pthread_mutex_t tm;
  22. pthread_mutexattr_t ta;
  23. pthread_mutexattr_settype(&ta, PTHREAD_MUTEX_RECURSIVE);
  24. pthread_mutex_init(&tm, &ta);
  25. pr("testing recursive mutex... ");
  26. alarm(5);
  27. if (pthread_mutex_lock(&tm) != 0) _die_("failed... mutex_lock on unused rec-mutex (c=0)...\n");
  28. if (tm.lock.owner!=pthread_self()) _die_("failed.. wrong owner....\n");
  29. if (tm.lock.hold!=1) _die_("failed... wrong counting (c!=1)....\n");
  30. if (pthread_mutex_lock(&tm) != 0) _die_("failed... mutex_lock on taken rec-mutex (c=1)...\n");
  31. if (tm.lock.hold!=2) _die_("failed... wrong counting (c!=2)....\n");
  32. if (pthread_mutex_lock(&tm) != 0) _die_("failed... mutex_lock on taken rec-mutex (c=2)...\n");
  33. if (tm.lock.hold!=3) _die_("failed... wrong counting (c!=3)....\n");
  34. if (pthread_mutex_unlock(&tm) != 0) _die_("failed... mutex_unlock on taken rec-mutex (c=3)...\n");
  35. if (tm.lock.hold!=2) _die_("failed... wrong counting (c!=2)....\n");
  36. if (tm.lock.owner==0) _die_("failed... mutex has no owner?!?!\n");
  37. if (pthread_mutex_unlock(&tm) != 0) _die_("failed... mutex_unlock on taken rec-mutex (c=2)...\n");
  38. if (tm.lock.hold!=1) _die_("failed... wrong counting (c!=1)....\n");
  39. if (tm.lock.owner==0) _die_("failed... mutex has no owner?!?!\n");
  40. if (pthread_mutex_unlock(&tm) != 0) _die_("failed... mutex_unlock on taken rec-mutex (c=1)...\n");
  41. if (tm.lock.hold!=0) _die_("failed... wrong counting (c!=0)....\n");
  42. if (tm.lock.owner!=0) _die_("failed... mutex still owned ?!?!\n");
  43. if (pthread_mutex_unlock(&tm) != 0) _die_("failed... mutex_unlock on free rec-mutex (c=0)...\n");
  44. alarm(0);
  45. pr("OK.\n");
  46. }
  47. void test_err_mutex() {
  48. pthread_mutex_t tm;
  49. pthread_mutexattr_t ta;
  50. pthread_mutexattr_settype(&ta, PTHREAD_MUTEX_ERRORCHECK);
  51. pthread_mutex_init(&tm, &ta);
  52. pr("testing errorcheck mutex... ");
  53. alarm(5);
  54. if (pthread_mutex_lock(&tm) != 0) _die_("failed... mutex_lock on unused errchk-mutex...\n");
  55. if (tm.lock.owner!=pthread_self()) _die_("failed.. wrong owner....\n");
  56. if (pthread_mutex_lock(&tm) != EDEADLK) _die_("failed... mutex_lock on taken errchk-mutex...\n");
  57. if (pthread_mutex_unlock(&tm) != 0) _die_("failed... mutex_unlock on taken errchk-mutex...\n");
  58. if (tm.lock.owner!=0) _die_("failed... mutex still owned ?!?!\n");
  59. if (pthread_mutex_unlock(&tm) != EPERM) _die_("failed... mutex_unlock on free errchk-mutex...\n");
  60. alarm(0);
  61. pr("OK.\n");
  62. }
  63. void pth_t1() {
  64. pr("\nTEST 1: mutex test:\n\n");
  65. test_rec_mutex();
  66. test_err_mutex();
  67. }
  68. FINSH_FUNCTION_EXPORT(pth_t1, pthread testcase0);
  69. void* thread(void*arg)
  70. {
  71. if (0) { arg=0; }
  72. pr("(thread created) ");
  73. sleep(1);
  74. pr("(thread exit) ");
  75. return 0;
  76. }
  77. void test_thread() {
  78. pthread_t t;
  79. pr("testing basic thread creation and join... ");
  80. if ((pthread_create(&t,0,thread,0))!=0) _die_("failed...\n");
  81. if (pthread_join(t,0) != 0) _die_("failed... joining thread\n");
  82. pr("OK.\n");
  83. }
  84. void test_thread_join_detached() {
  85. pthread_t t;
  86. pthread_attr_t attr;
  87. pr("testing for failing join of a detached thread... ");
  88. pthread_attr_init(&attr);
  89. pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
  90. if ((pthread_create(&t,&attr,thread,0))!=0) _die_("failed...\n");
  91. if (pthread_join(t,0) == 0) _die_("failed... I had joined a detached thread !\n");
  92. sleep(2);
  93. pr("OK.\n");
  94. }
  95. static char alt_stack[4096];
  96. void test_thread_alt_stack() {
  97. pthread_t t;
  98. pthread_attr_t attr;
  99. pr("testing alternate thread stack... ");
  100. pthread_attr_init(&attr);
  101. pthread_attr_setstacksize(&attr,sizeof(alt_stack));
  102. if ((pthread_create(&t,&attr,thread,0))!=0) _die_("failed... creating thread\n");
  103. if (pthread_join(t,0) != 0) _die_("failed... joining thread\n");
  104. pthread_attr_setstackaddr(&attr,alt_stack);
  105. if ((pthread_create(&t,&attr,thread,0))!=0) _die_("failed... creating thread\n");
  106. if (pthread_join(t,0) != 0) _die_("failed... joining thread\n");
  107. pr("OK.\n");
  108. }
  109. void pth_t2()
  110. {
  111. pr("\nTEST 2: thread creation & attributes:\n\n");
  112. test_thread();
  113. test_thread_join_detached();
  114. test_thread_alt_stack();
  115. }
  116. FINSH_FUNCTION_EXPORT(pth_t2, pthread testcase1);