mutex8r.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * mutex8r.c
  3. *
  4. *
  5. * Based upon 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * --------------------------------------------------------------------------
  26. *
  27. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  28. * Copyright(C) 2008 Jason Schmidlapp
  29. *
  30. * Contact Email: [email protected]
  31. *
  32. *
  33. * Tests PTHREAD_MUTEX_RECURSIVE mutex type exercising timedlock.
  34. * Thread locks mutex, another thread timedlocks the mutex.
  35. * Timed thread should timeout.
  36. *
  37. * Depends on API functions:
  38. * pthread_create()
  39. * pthread_mutexattr_init()
  40. * pthread_mutexattr_destroy()
  41. * pthread_mutexattr_settype()
  42. * pthread_mutexattr_gettype()
  43. * pthread_mutex_init()
  44. * pthread_mutex_destroy()
  45. * pthread_mutex_lock()
  46. * pthread_mutex_timedlock()
  47. * pthread_mutex_unlock()
  48. */
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include "test.h"
  52. static int lockCount = 0;
  53. static pthread_mutex_t mutex;
  54. static pthread_mutexattr_t mxAttr;
  55. static void * locker(void * arg)
  56. {
  57. struct timespec abstime =
  58. {
  59. 0, 0
  60. };
  61. struct _timeb currSysTime;
  62. const unsigned int NANOSEC_PER_MILLISEC = 1000000;
  63. _ftime(&currSysTime);
  64. abstime.tv_sec = currSysTime.time;
  65. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  66. abstime.tv_sec += 1;
  67. assert(pthread_mutex_timedlock(&mutex, &abstime) == ETIMEDOUT);
  68. lockCount++;
  69. return 0;
  70. }
  71. int
  72. pthread_test_mutex8r()
  73. {
  74. pthread_t t;
  75. int mxType = -1;
  76. lockCount = 0;
  77. assert(pthread_mutexattr_init(&mxAttr) == 0);
  78. assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_RECURSIVE) == 0);
  79. assert(pthread_mutexattr_gettype(&mxAttr, &mxType) == 0);
  80. assert(mxType == PTHREAD_MUTEX_RECURSIVE);
  81. assert(pthread_mutex_init(&mutex, &mxAttr) == 0);
  82. assert(pthread_mutex_lock(&mutex) == 0);
  83. assert(pthread_create(&t, NULL, locker, NULL) == 0);
  84. pte_osThreadSleep(2000);
  85. assert(lockCount == 1);
  86. assert(pthread_mutex_unlock(&mutex) == 0);
  87. assert(pthread_join(t,NULL) == 0);
  88. assert(pthread_mutex_destroy(&mutex) == 0);
  89. return 0;
  90. }