pte_threadDestroy.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * pte_threadDestroy.c
  3. *
  4. * Description:
  5. * This translation unit implements routines which are private to
  6. * the implementation and may be used throughout it.
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  11. * Copyright(C) 2008 Jason Schmidlapp
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. *
  16. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  17. * Copyright(C) 1998 John E. Bossom
  18. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  19. *
  20. * Contact Email: [email protected]
  21. *
  22. * The original list of contributors to the Pthreads-win32 project
  23. * is contained in the file CONTRIBUTORS.ptw32 included with the
  24. * source code distribution. The list can also be seen at the
  25. * following World Wide Web location:
  26. * http://sources.redhat.com/pthreads-win32/contributors.html
  27. *
  28. * This library is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU Lesser General Public
  30. * License as published by the Free Software Foundation; either
  31. * version 2 of the License, or (at your option) any later version.
  32. *
  33. * This library is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public
  39. * License along with this library in the file COPYING.LIB;
  40. * if not, write to the Free Software Foundation, Inc.,
  41. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "pthread.h"
  47. #include "implement.h"
  48. static void
  49. pte_threadDestroyCommon (pthread_t thread, unsigned char shouldThreadExit)
  50. {
  51. pte_thread_t * tp = (pte_thread_t *) thread;
  52. pte_thread_t threadCopy;
  53. if (tp != NULL)
  54. {
  55. /*
  56. * Copy thread state so that the thread can be atomically NULLed.
  57. */
  58. memcpy (&threadCopy, tp, sizeof (threadCopy));
  59. /*
  60. * Thread ID structs are never freed. They're NULLed and reused.
  61. * This also sets the thread to PThreadStateInitial (invalid).
  62. */
  63. pte_threadReusePush (thread);
  64. (void) pthread_mutex_destroy(&threadCopy.cancelLock);
  65. (void) pthread_mutex_destroy(&threadCopy.threadLock);
  66. if (threadCopy.threadId != 0)
  67. {
  68. if (shouldThreadExit)
  69. {
  70. pte_osThreadExitAndDelete(threadCopy.threadId);
  71. }
  72. else
  73. {
  74. pte_osThreadDelete(threadCopy.threadId);
  75. }
  76. }
  77. }
  78. } /* pte_threadDestroy */
  79. void pte_threadDestroy (pthread_t thread)
  80. {
  81. pte_threadDestroyCommon(thread,0);
  82. }
  83. void pte_threadExitAndDestroy (pthread_t thread)
  84. {
  85. pte_threadDestroyCommon(thread,1);
  86. }