1 /* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 #include "thr_mutex.h"
29 #include "my_thread_local.h"
30 
31 #if defined(SAFE_MUTEX)
32 /* This makes a wrapper for mutex handling to make it easier to debug mutex */
33 
34 static my_bool safe_mutex_inited= FALSE;
35 
36 /**
37   While it looks like this function is pointless, it makes it possible to
38   catch usage of global static mutexes. Since the order of construction of
39   global objects in different compilation units is undefined, this is
40   quite useful.
41 */
safe_mutex_global_init(void)42 void safe_mutex_global_init(void)
43 {
44   safe_mutex_inited= TRUE;
45 }
46 
47 
safe_mutex_init(my_mutex_t * mp,const native_mutexattr_t * attr,const char * file,uint line)48 int safe_mutex_init(my_mutex_t *mp, const native_mutexattr_t *attr,
49 		    const char *file, uint line)
50 {
51   DBUG_ASSERT(safe_mutex_inited);
52   memset(mp, 0, sizeof(*mp));
53   native_mutex_init(&mp->global,MY_MUTEX_INIT_ERRCHK);
54   native_mutex_init(&mp->mutex,attr);
55   /* Mark that mutex is initialized */
56   mp->file= file;
57   mp->line= line;
58   return 0;
59 }
60 
61 
safe_mutex_lock(my_mutex_t * mp,my_bool try_lock,const char * file,uint line)62 int safe_mutex_lock(my_mutex_t *mp, my_bool try_lock,
63                     const char *file, uint line)
64 {
65   int error;
66   native_mutex_lock(&mp->global);
67   if (!mp->file)
68   {
69     native_mutex_unlock(&mp->global);
70     fprintf(stderr,
71 	    "safe_mutex: Trying to lock uninitialized mutex at %s, line %d\n",
72 	    file, line);
73     fflush(stderr);
74     abort();
75   }
76 
77   if (mp->count > 0)
78   {
79     if (try_lock)
80     {
81       native_mutex_unlock(&mp->global);
82       return EBUSY;
83     }
84     else if (my_thread_equal(my_thread_self(),mp->thread))
85     {
86 #ifndef DBUG_OFF
87       fprintf(stderr,
88               "safe_mutex: Trying to lock mutex at %s, line %d, when the"
89               " mutex was already locked at %s, line %d in thread T@%u\n",
90               file, line, mp->file, mp->line, my_thread_var_id());
91       fflush(stderr);
92 #endif
93       abort();
94     }
95   }
96   native_mutex_unlock(&mp->global);
97 
98   /*
99     If we are imitating trylock(), we need to take special
100     precautions.
101 
102     - We cannot use pthread_mutex_lock() only since another thread can
103       overtake this thread and take the lock before this thread
104       causing pthread_mutex_trylock() to hang. In this case, we should
105       just return EBUSY. Hence, we use pthread_mutex_trylock() to be
106       able to return immediately.
107 
108     - We cannot just use trylock() and continue execution below, since
109       this would generate an error and abort execution if the thread
110       was overtaken and trylock() returned EBUSY . In this case, we
111       instead just return EBUSY, since this is the expected behaviour
112       of trylock().
113    */
114   if (try_lock)
115   {
116     error= native_mutex_trylock(&mp->mutex);
117     if (error == EBUSY)
118       return error;
119   }
120   else
121     error= native_mutex_lock(&mp->mutex);
122 
123   if (error || (error=native_mutex_lock(&mp->global)))
124   {
125     fprintf(stderr,"Got error %d when trying to lock mutex at %s, line %d\n",
126 	    error, file, line);
127     fflush(stderr);
128     abort();
129   }
130   mp->thread= my_thread_self();
131   if (mp->count++)
132   {
133     fprintf(stderr,"safe_mutex: Error in thread libray: Got mutex at %s, \
134 line %d more than 1 time\n", file,line);
135     fflush(stderr);
136     abort();
137   }
138   mp->file= file;
139   mp->line=line;
140   native_mutex_unlock(&mp->global);
141   return error;
142 }
143 
144 
safe_mutex_unlock(my_mutex_t * mp,const char * file,uint line)145 int safe_mutex_unlock(my_mutex_t *mp, const char *file, uint line)
146 {
147   int error;
148   native_mutex_lock(&mp->global);
149   if (mp->count == 0)
150   {
151     fprintf(stderr,"safe_mutex: Trying to unlock mutex that wasn't locked at %s, line %d\n            Last used at %s, line: %d\n",
152 	    file,line,mp->file ? mp->file : "",mp->line);
153     fflush(stderr);
154     abort();
155   }
156   if (!my_thread_equal(my_thread_self(),mp->thread))
157   {
158     fprintf(stderr,"safe_mutex: Trying to unlock mutex at %s, line %d  that was locked by another thread at: %s, line: %d\n",
159 	    file,line,mp->file,mp->line);
160     fflush(stderr);
161     abort();
162   }
163   mp->thread= 0;
164   mp->count--;
165   error=native_mutex_unlock(&mp->mutex);
166   if (error)
167   {
168     fprintf(stderr,"safe_mutex: Got error: %d (%d) when trying to unlock mutex at %s, line %d\n", error, errno, file, line);
169     fflush(stderr);
170     abort();
171   }
172   native_mutex_unlock(&mp->global);
173   return error;
174 }
175 
176 
safe_mutex_destroy(my_mutex_t * mp,const char * file,uint line)177 int safe_mutex_destroy(my_mutex_t *mp, const char *file, uint line)
178 {
179   int error=0;
180   native_mutex_lock(&mp->global);
181   if (!mp->file)
182   {
183     native_mutex_unlock(&mp->global);
184     fprintf(stderr,
185 	    "safe_mutex: Trying to destroy uninitialized mutex at %s, line %d\n",
186 	    file, line);
187     fflush(stderr);
188     abort();
189   }
190   if (mp->count != 0)
191   {
192     native_mutex_unlock(&mp->global);
193     fprintf(stderr,"safe_mutex: Trying to destroy a mutex that was locked at %s, line %d at %s, line %d\n",
194 	    mp->file,mp->line, file, line);
195     fflush(stderr);
196     abort();
197   }
198   native_mutex_unlock(&mp->global);
199   if (native_mutex_destroy(&mp->global))
200     error=1;
201   if (native_mutex_destroy(&mp->mutex))
202     error=1;
203   mp->file= 0;					/* Mark destroyed */
204   return error;
205 }
206 
207 #endif /* SAFE_MUTEX */
208