mutex8.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * mutex8.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. * Test the default (type not set) 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_mutex_lock()
  39. * pthread_mutex_timedlock()
  40. * pthread_mutex_unlock()
  41. */
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include "test.h"
  45. static int lockCount = 0;
  46. static pthread_mutex_t mutex;
  47. static void * locker(void * arg)
  48. {
  49. struct timespec abstime =
  50. {
  51. 0, 0
  52. };
  53. struct timeb currSysTime;
  54. const unsigned long NANOSEC_PER_MILLISEC = 1000000;
  55. _ftime(&currSysTime);
  56. currSysTime.time += 1; // wait for one seconds
  57. abstime.tv_sec = currSysTime.time;
  58. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  59. assert(pthread_mutex_timedlock(&mutex, &abstime) == ETIMEDOUT);
  60. lockCount++;
  61. return 0;
  62. }
  63. int
  64. pthread_test_mutex8()
  65. {
  66. pthread_t t;
  67. lockCount = 0;
  68. assert(pthread_mutex_init(&mutex, NULL) == 0);
  69. assert(pthread_mutex_lock(&mutex) == 0);
  70. assert(pthread_create(&t, NULL, locker, NULL) == 0);
  71. pte_osThreadSleep(2000);
  72. assert(lockCount == 1);
  73. assert(pthread_mutex_unlock(&mutex) == 0);
  74. assert(pthread_join(t,NULL) == 0);
  75. assert(pthread_mutex_destroy(&mutex) == 0);
  76. return 0;
  77. }