mutex8.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * mutex8.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. * Test the default (type not set) 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_mutex_lock()
  33. * pthread_mutex_timedlock()
  34. * pthread_mutex_unlock()
  35. */
  36. #include "test.h"
  37. #include <sys/timeb.h>
  38. static int lockCount = 0;
  39. static pthread_mutex_t mutex;
  40. void * locker(void * arg)
  41. {
  42. struct timespec abstime = { 0, 0 };
  43. PTW32_STRUCT_TIMEB currSysTime;
  44. const DWORD NANOSEC_PER_MILLISEC = 1000000;
  45. PTW32_FTIME(&currSysTime);
  46. abstime.tv_sec = (long)currSysTime.time;
  47. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  48. abstime.tv_sec += 1;
  49. assert(pthread_mutex_timedlock(&mutex, &abstime) == ETIMEDOUT);
  50. lockCount++;
  51. return 0;
  52. }
  53. int
  54. main()
  55. {
  56. pthread_t t;
  57. assert(pthread_mutex_init(&mutex, NULL) == 0);
  58. assert(pthread_mutex_lock(&mutex) == 0);
  59. assert(pthread_create(&t, NULL, locker, NULL) == 0);
  60. Sleep(2000);
  61. assert(lockCount == 1);
  62. assert(pthread_mutex_unlock(&mutex) == 0);
  63. return 0;
  64. }