rwlock6_t2.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * rwlock6_t2.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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Check writer and reader timeouts.
  37. *
  38. * Depends on API functions:
  39. * pthread_rwlock_timedrdlock()
  40. * pthread_rwlock_timedwrlock()
  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. struct timespec abstime = { 0, 0 };
  48. void * wrfunc(void * arg)
  49. {
  50. int result;
  51. result = pthread_rwlock_timedwrlock(&rwlock1, &abstime);
  52. if ((int) (size_t)arg == 1)
  53. {
  54. assert(result == 0);
  55. Sleep(2000);
  56. bankAccount += 10;
  57. assert(pthread_rwlock_unlock(&rwlock1) == 0);
  58. return ((void *)(size_t)bankAccount);
  59. }
  60. else if ((int) (size_t)arg == 2)
  61. {
  62. assert(result == ETIMEDOUT);
  63. return ((void *) 100);
  64. }
  65. return ((void *)(size_t)-1);
  66. }
  67. void * rdfunc(void * arg)
  68. {
  69. int ba = 0;
  70. assert(pthread_rwlock_timedrdlock(&rwlock1, &abstime) == ETIMEDOUT);
  71. return ((void *)(size_t)ba);
  72. }
  73. int
  74. main()
  75. {
  76. pthread_t wrt1;
  77. pthread_t wrt2;
  78. pthread_t rdt;
  79. void* wr1Result = (void*)0;
  80. void* wr2Result = (void*)0;
  81. void* rdResult = (void*)0;
  82. PTW32_STRUCT_TIMEB currSysTime;
  83. const DWORD NANOSEC_PER_MILLISEC = 1000000;
  84. PTW32_FTIME(&currSysTime);
  85. abstime.tv_sec = (long)currSysTime.time;
  86. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  87. abstime.tv_sec += 1;
  88. bankAccount = 0;
  89. assert(pthread_create(&wrt1, NULL, wrfunc, (void *)(size_t)1) == 0);
  90. Sleep(100);
  91. assert(pthread_create(&rdt, NULL, rdfunc, NULL) == 0);
  92. Sleep(100);
  93. assert(pthread_create(&wrt2, NULL, wrfunc, (void *)(size_t)2) == 0);
  94. assert(pthread_join(wrt1, &wr1Result) == 0);
  95. assert(pthread_join(rdt, &rdResult) == 0);
  96. assert(pthread_join(wrt2, &wr2Result) == 0);
  97. assert((int)(size_t)wr1Result == 10);
  98. assert((int)(size_t)rdResult == 0);
  99. assert((int)(size_t)wr2Result == 100);
  100. return 0;
  101. }