1 /* 2 * pthread_mutex_destroy.c 3 * 4 * Description: 5 * This translation unit implements mutual exclusion (mutex) primitives. 6 * 7 * -------------------------------------------------------------------------- 8 * 9 * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems 10 * Copyright(C) 2008 Jason Schmidlapp 11 * 12 * Contact Email: jschmidlapp@users.sourceforge.net 13 * 14 * 15 * Based upon Pthreads-win32 - POSIX Threads Library for Win32 16 * Copyright(C) 1998 John E. Bossom 17 * Copyright(C) 1999,2005 Pthreads-win32 contributors 18 * 19 * Contact Email: rpj@callisto.canberra.edu.au 20 * 21 * The original list of contributors to the Pthreads-win32 project 22 * is contained in the file CONTRIBUTORS.ptw32 included with the 23 * source code distribution. The list can also be seen at the 24 * following World Wide Web location: 25 * http://sources.redhat.com/pthreads-win32/contributors.html 26 * 27 * This library is free software; you can redistribute it and/or 28 * modify it under the terms of the GNU Lesser General Public 29 * License as published by the Free Software Foundation; either 30 * version 2 of the License, or (at your option) any later version. 31 * 32 * This library is distributed in the hope that it will be useful, 33 * but WITHOUT ANY WARRANTY; without even the implied warranty of 34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 35 * Lesser General Public License for more details. 36 * 37 * You should have received a copy of the GNU Lesser General Public 38 * License along with this library in the file COPYING.LIB; 39 * if not, write to the Free Software Foundation, Inc., 40 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 41 */ 42 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 47 #include "pthread.h" 48 #include "implement.h" 49 50 51 int 52 pthread_mutex_destroy (pthread_mutex_t * mutex) 53 { 54 int result = 0; 55 pthread_mutex_t mx; 56 57 /* 58 * Let the system deal with invalid pointers. 59 */ 60 61 /* 62 * Check to see if we have something to delete. 63 */ 64 if (*mutex < PTHREAD_ERRORCHECK_MUTEX_INITIALIZER) 65 { 66 mx = *mutex; 67 68 result = pthread_mutex_trylock (&mx); 69 70 /* 71 * If trylock succeeded and the mutex is not recursively locked it 72 * can be destroyed. 73 */ 74 if (result == 0) 75 { 76 if (mx->kind != PTHREAD_MUTEX_RECURSIVE || 1 == mx->recursive_count) 77 { 78 /* 79 * FIXME!!! 80 * The mutex isn't held by another thread but we could still 81 * be too late invalidating the mutex below since another thread 82 * may already have entered mutex_lock and the check for a valid 83 * *mutex != NULL. 84 * 85 * Note that this would be an unusual situation because it is not 86 * common that mutexes are destroyed while they are still in 87 * use by other threads. 88 */ 89 *mutex = NULL; 90 91 result = pthread_mutex_unlock (&mx); 92 93 if (result == 0) 94 { 95 pte_osSemaphoreDelete(mx->handle); 96 97 free(mx); 98 99 } 100 else 101 { 102 /* 103 * Restore the mutex before we return the error. 104 */ 105 *mutex = mx; 106 } 107 } 108 else /* mx->recursive_count > 1 */ 109 { 110 /* 111 * The mutex must be recursive and already locked by us (this thread). 112 */ 113 mx->recursive_count--; /* Undo effect of pthread_mutex_trylock() above */ 114 result = EBUSY; 115 } 116 } 117 } 118 else 119 { 120 /* 121 * See notes in pte_mutex_check_need_init() above also. 122 */ 123 124 pte_osMutexLock (pte_mutex_test_init_lock); 125 126 127 /* 128 * Check again. 129 */ 130 if (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER) 131 { 132 /* 133 * This is all we need to do to destroy a statically 134 * initialised mutex that has not yet been used (initialised). 135 * If we get to here, another thread 136 * waiting to initialise this mutex will get an EINVAL. 137 */ 138 *mutex = NULL; 139 } 140 else 141 { 142 /* 143 * The mutex has been initialised while we were waiting 144 * so assume it's in use. 145 */ 146 result = EBUSY; 147 } 148 149 pte_osMutexUnlock(pte_mutex_test_init_lock); 150 151 } 152 153 return (result); 154 } 155