rwlock6.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * rwlock6.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
  37. *
  38. * Depends on API functions:
  39. * pthread_rwlock_rdlock()
  40. * pthread_rwlock_wrlock()
  41. * pthread_rwlock_unlock()
  42. */
  43. #include "test.h"
  44. static pthread_rwlock_t rwlock1 = PTHREAD_RWLOCK_INITIALIZER;
  45. static int bankAccount = 0;
  46. void * wrfunc(void * arg)
  47. {
  48. int ba;
  49. assert(pthread_rwlock_wrlock(&rwlock1) == 0);
  50. Sleep(200);
  51. bankAccount += 10;
  52. ba = bankAccount;
  53. assert(pthread_rwlock_unlock(&rwlock1) == 0);
  54. return ((void *)(size_t)ba);
  55. }
  56. void * rdfunc(void * arg)
  57. {
  58. int ba;
  59. assert(pthread_rwlock_rdlock(&rwlock1) == 0);
  60. ba = bankAccount;
  61. assert(pthread_rwlock_unlock(&rwlock1) == 0);
  62. return ((void *)(size_t)ba);
  63. }
  64. int
  65. main()
  66. {
  67. pthread_t wrt1;
  68. pthread_t wrt2;
  69. pthread_t rdt;
  70. void* wr1Result = (void*)0;
  71. void* wr2Result = (void*)0;
  72. void* rdResult = (void*)0;
  73. bankAccount = 0;
  74. assert(pthread_create(&wrt1, NULL, wrfunc, NULL) == 0);
  75. Sleep(50);
  76. assert(pthread_create(&rdt, NULL, rdfunc, NULL) == 0);
  77. Sleep(50);
  78. assert(pthread_create(&wrt2, NULL, wrfunc, NULL) == 0);
  79. assert(pthread_join(wrt1, &wr1Result) == 0);
  80. assert(pthread_join(rdt, &rdResult) == 0);
  81. assert(pthread_join(wrt2, &wr2Result) == 0);
  82. assert((int)(size_t)wr1Result == 10);
  83. assert((int)(size_t)rdResult == 10);
  84. assert((int)(size_t)wr2Result == 20);
  85. return 0;
  86. }