rwlock6_t.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * rwlock6_t.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Check writer and reader locking with reader timeouts
  37. *
  38. * Depends on API functions:
  39. * pthread_rwlock_timedrdlock()
  40. * pthread_rwlock_wrlock()
  41. * pthread_rwlock_unlock()
  42. */
  43. #include "test.h"
  44. #include <sys/timeb.h>
  45. static pthread_rwlock_t rwlock1 = PTHREAD_RWLOCK_INITIALIZER;
  46. static int bankAccount = 0;
  47. void * wrfunc(void * arg)
  48. {
  49. assert(pthread_rwlock_wrlock(&rwlock1) == 0);
  50. Sleep(2000);
  51. bankAccount += 10;
  52. assert(pthread_rwlock_unlock(&rwlock1) == 0);
  53. return ((void *)(size_t)bankAccount);
  54. }
  55. void * rdfunc(void * arg)
  56. {
  57. int ba = -1;
  58. struct timespec abstime = { 0, 0 };
  59. PTW32_STRUCT_TIMEB currSysTime;
  60. const DWORD NANOSEC_PER_MILLISEC = 1000000;
  61. PTW32_FTIME(&currSysTime);
  62. abstime.tv_sec = (long)currSysTime.time;
  63. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  64. if ((int) (size_t)arg == 1)
  65. {
  66. abstime.tv_sec += 1;
  67. assert(pthread_rwlock_timedrdlock(&rwlock1, &abstime) == ETIMEDOUT);
  68. ba = 0;
  69. }
  70. else if ((int) (size_t)arg == 2)
  71. {
  72. abstime.tv_sec += 3;
  73. assert(pthread_rwlock_timedrdlock(&rwlock1, &abstime) == 0);
  74. ba = bankAccount;
  75. assert(pthread_rwlock_unlock(&rwlock1) == 0);
  76. }
  77. return ((void *)(size_t)ba);
  78. }
  79. int
  80. main()
  81. {
  82. pthread_t wrt1;
  83. pthread_t wrt2;
  84. pthread_t rdt1;
  85. pthread_t rdt2;
  86. void* wr1Result = (void*)0;
  87. void* wr2Result = (void*)0;
  88. void* rd1Result = (void*)0;
  89. void* rd2Result = (void*)0;
  90. bankAccount = 0;
  91. assert(pthread_create(&wrt1, NULL, wrfunc, NULL) == 0);
  92. Sleep(500);
  93. assert(pthread_create(&rdt1, NULL, rdfunc, (void *)(size_t)1) == 0);
  94. Sleep(500);
  95. assert(pthread_create(&wrt2, NULL, wrfunc, NULL) == 0);
  96. Sleep(500);
  97. assert(pthread_create(&rdt2, NULL, rdfunc, (void *)(size_t)2) == 0);
  98. assert(pthread_join(wrt1, &wr1Result) == 0);
  99. assert(pthread_join(rdt1, &rd1Result) == 0);
  100. assert(pthread_join(wrt2, &wr2Result) == 0);
  101. assert(pthread_join(rdt2, &rd2Result) == 0);
  102. assert((int)(size_t)wr1Result == 10);
  103. assert((int)(size_t)rd1Result == 0);
  104. assert((int)(size_t)wr2Result == 20);
  105. assert((int)(size_t)rd2Result == 20);
  106. return 0;
  107. }