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