rwlockrank.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * RWLock rank tests
  6. */
  7. #include "nspr.h"
  8. #include "plgetopt.h"
  9. static int _debug_on;
  10. static PRRWLock *rwlock0;
  11. static PRRWLock *rwlock1;
  12. static PRRWLock *rwlock2;
  13. static void rwtest(void *args)
  14. {
  15. PR_RWLock_Rlock(rwlock1);
  16. PR_RWLock_Unlock(rwlock1);
  17. PR_RWLock_Rlock(rwlock1);
  18. PR_RWLock_Unlock(rwlock1);
  19. /* Test correct lock rank. */
  20. PR_RWLock_Rlock(rwlock1);
  21. PR_RWLock_Rlock(rwlock2);
  22. PR_RWLock_Unlock(rwlock2);
  23. PR_RWLock_Unlock(rwlock1);
  24. PR_RWLock_Rlock(rwlock1);
  25. PR_RWLock_Rlock(rwlock2);
  26. PR_RWLock_Unlock(rwlock1);
  27. PR_RWLock_Unlock(rwlock2);
  28. PR_RWLock_Rlock(rwlock1);
  29. PR_RWLock_Rlock(rwlock0);
  30. PR_RWLock_Rlock(rwlock2);
  31. PR_RWLock_Unlock(rwlock2);
  32. PR_RWLock_Unlock(rwlock0);
  33. PR_RWLock_Unlock(rwlock1);
  34. #if 0
  35. /* Test incorrect lock rank. */
  36. PR_RWLock_Rlock(rwlock2);
  37. PR_RWLock_Rlock(rwlock1);
  38. PR_RWLock_Unlock(rwlock1);
  39. PR_RWLock_Unlock(rwlock2);
  40. PR_RWLock_Rlock(rwlock2);
  41. PR_RWLock_Rlock(rwlock0);
  42. PR_RWLock_Rlock(rwlock1);
  43. PR_RWLock_Unlock(rwlock1);
  44. PR_RWLock_Unlock(rwlock0);
  45. PR_RWLock_Unlock(rwlock2);
  46. #endif
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. PRStatus rc;
  51. PRThread *thread;
  52. PLOptStatus os;
  53. PLOptState *opt = PL_CreateOptState(argc, argv, "d");
  54. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
  55. if (PL_OPT_BAD == os) {
  56. continue;
  57. }
  58. switch (opt->option) {
  59. case 'd': /* debug mode */
  60. _debug_on = 1;
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. PL_DestroyOptState(opt);
  67. rwlock0 = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "Lock 0");
  68. if (rwlock0 == NULL) {
  69. fprintf(stderr, "PR_NewRWLock failed - error %d\n",
  70. (int)PR_GetError());
  71. return 1;
  72. }
  73. rwlock1 = PR_NewRWLock(1, "Lock 1");
  74. if (rwlock1 == NULL) {
  75. fprintf(stderr, "PR_NewRWLock failed - error %d\n",
  76. (int)PR_GetError());
  77. return 1;
  78. }
  79. rwlock2 = PR_NewRWLock(2, "Lock 2");
  80. if (rwlock2 == NULL) {
  81. fprintf(stderr, "PR_NewRWLock failed - error %d\n",
  82. (int)PR_GetError());
  83. return 1;
  84. }
  85. thread = PR_CreateThread(PR_USER_THREAD, rwtest, NULL, PR_PRIORITY_NORMAL,
  86. PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  87. if (thread == NULL) {
  88. fprintf(stderr, "PR_CreateThread failed - error %d\n",
  89. (int)PR_GetError());
  90. PR_ProcessExit(2);
  91. }
  92. if (_debug_on) {
  93. printf("%s: created thread = %p\n", argv[0], thread);
  94. }
  95. rc = PR_JoinThread(thread);
  96. PR_ASSERT(rc == PR_SUCCESS);
  97. PR_DestroyRWLock(rwlock0);
  98. rwlock0 = NULL;
  99. PR_DestroyRWLock(rwlock1);
  100. rwlock1 = NULL;
  101. PR_DestroyRWLock(rwlock2);
  102. rwlock2 = NULL;
  103. printf("PASS\n");
  104. return 0;
  105. }