mutex8r.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * mutex8r.c
  3. *
  4. *
  5. * Pthreads-win32 - POSIX Threads Library for Win32
  6. * Copyright (C) 1998 Ben Elliston and Ross Johnson
  7. * Copyright (C) 1999,2000,2001 Ross Johnson
  8. *
  9. * Contact Email: [email protected]
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. *
  25. * --------------------------------------------------------------------------
  26. *
  27. * Tests PTHREAD_MUTEX_RECURSIVE mutex type exercising timedlock.
  28. * Thread locks mutex, another thread timedlocks the mutex.
  29. * Timed thread should timeout.
  30. *
  31. * Depends on API functions:
  32. * pthread_create()
  33. * pthread_mutexattr_init()
  34. * pthread_mutexattr_destroy()
  35. * pthread_mutexattr_settype()
  36. * pthread_mutexattr_gettype()
  37. * pthread_mutex_init()
  38. * pthread_mutex_destroy()
  39. * pthread_mutex_lock()
  40. * pthread_mutex_timedlock()
  41. * pthread_mutex_unlock()
  42. */
  43. #include "test.h"
  44. #include <sys/timeb.h>
  45. static int lockCount;
  46. static pthread_mutex_t mutex;
  47. static pthread_mutexattr_t mxAttr;
  48. void * locker(void * arg)
  49. {
  50. struct timespec abstime = { 0, 0 };
  51. PTW32_STRUCT_TIMEB currSysTime;
  52. const DWORD NANOSEC_PER_MILLISEC = 1000000;
  53. PTW32_FTIME(&currSysTime);
  54. abstime.tv_sec = (long)currSysTime.time;
  55. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  56. abstime.tv_sec += 1;
  57. assert(pthread_mutex_timedlock(&mutex, &abstime) == ETIMEDOUT);
  58. lockCount++;
  59. return 0;
  60. }
  61. int
  62. main()
  63. {
  64. pthread_t t;
  65. int mxType = -1;
  66. assert(pthread_mutexattr_init(&mxAttr) == 0);
  67. BEGIN_MUTEX_STALLED_ROBUST(mxAttr)
  68. lockCount = 0;
  69. assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_RECURSIVE) == 0);
  70. assert(pthread_mutexattr_gettype(&mxAttr, &mxType) == 0);
  71. assert(mxType == PTHREAD_MUTEX_RECURSIVE);
  72. assert(pthread_mutex_init(&mutex, &mxAttr) == 0);
  73. assert(pthread_mutex_lock(&mutex) == 0);
  74. assert(pthread_create(&t, NULL, locker, NULL) == 0);
  75. Sleep(2000);
  76. assert(lockCount == 1);
  77. assert(pthread_mutex_unlock(&mutex) == 0);
  78. END_MUTEX_STALLED_ROBUST(mxAttr)
  79. return 0;
  80. }