Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
thread.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 #include <string.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <errno.h>
62 #include <unistd.h>
63 
64 #include "gwlib/gwlib.h"
65 
66 
67 #ifdef MUTEX_STATS
68 Mutex *mutex_create_measured(Mutex *mutex, char *filename, int lineno)
69 {
70  mutex->filename = filename;
71  mutex->lineno = lineno;
72  mutex->locks = 0;
73  mutex->collisions = 0;
74  return mutex;
75 }
76 #endif
77 
79 {
80  Mutex *mutex;
81 
82  mutex = gw_malloc(sizeof(Mutex));
83  pthread_mutex_init(&mutex->mutex, NULL);
84  mutex->owner = -1;
85  mutex->dynamic = 1;
86  return mutex;
87 }
88 
90 {
91  pthread_mutex_init(&mutex->mutex, NULL);
92  mutex->owner = -1;
93  mutex->dynamic = 0;
94  return mutex;
95 }
96 
98 {
99  int ret;
100 
101  if (mutex == NULL)
102  return;
103 
104 #ifdef MUTEX_STATS
105  if (mutex->locks > 0 || mutex->collisions > 0) {
106  info(0, "Mutex %s:%d: %ld locks, %ld collisions.",
107  mutex->filename, mutex->lineno,
108  mutex->locks, mutex->collisions);
109  }
110 #endif
111 
112  if ((ret = pthread_mutex_destroy(&mutex->mutex)) != 0)
113  panic(ret, "Attempt to destroy locked mutex!");
114 
115  if (mutex->dynamic == 0)
116  return;
117  gw_free(mutex);
118 }
119 
120 
121 void mutex_lock_real(Mutex *mutex, char *file, int line, const char *func)
122 {
123  int ret;
124 
125  gw_assert(mutex != NULL);
126 
127 #ifdef MUTEX_STATS
128  ret = pthread_mutex_trylock(&mutex->mutex);
129  if (ret != 0) {
130  ret = pthread_mutex_lock(&mutex->mutex);
131  mutex->collisions++;
132  }
133  mutex->locks++;
134 #else
135  ret = pthread_mutex_lock(&mutex->mutex);
136 #endif
137  if (ret != 0)
138  panic(0, "%s:%ld: %s: Mutex failure! (Called from %s:%ld:%s.)", \
139  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
140  if (mutex->owner == gwthread_self())
141  panic(0, "%s:%ld: %s: Managed to lock the mutex twice! (Called from %s:%ld:%s.)", \
142  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
143  mutex->owner = gwthread_self();
144 }
145 
146 int mutex_unlock_real(Mutex *mutex, char *file, int line, const char *func)
147 {
148  int ret;
149 
150  if (mutex == NULL) {
151  error(0, "%s:%ld: %s: Trying to unlock a NULL mutex! (Called from %s:%ld:%s.)", \
152  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
153  return -1;
154  }
155  gw_assert(mutex != NULL);
156  mutex->owner = -1;
157  ret = pthread_mutex_unlock(&mutex->mutex);
158  if (ret != 0)
159  panic(0, "%s:%ld: %s: Mutex failure! (Called from %s:%ld:%s.)", \
160  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
161 
162  return ret;
163 }
164 
165 int mutex_trylock_real(Mutex *mutex, const char *file, int line, const char *func)
166 {
167  int ret;
168 
169  if (mutex == NULL) {
170  error(0, "%s:%ld: %s: Trying to lock a NULL mutex! (Called from %s:%ld:%s.)", \
171  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
172  return -1;
173  }
174 
175  ret = pthread_mutex_trylock(&mutex->mutex);
176  if (ret == 0) {
177  if (mutex->owner == gwthread_self())
178  panic(0, "%s:%ld: %s: Managed to lock the mutex twice! (Called from %s:%ld:%s.)", \
179  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
180 
181  mutex->owner = gwthread_self();
182  }
183  else if (ret == EINVAL)
184  panic(0, "%s:%ld: %s: Mutex failure! (Called from %s:%ld:%s.)", \
185  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
186 
187  return ret;
188 }
189 
190 
void error(int err, const char *fmt,...)
Definition: log.c:612
void info(int err, const char *fmt,...)
Definition: log.c:636
long gwthread_self(void)
int dynamic
Definition: thread.h:79
int mutex_unlock_real(Mutex *mutex, char *file, int line, const char *func)
Definition: thread.c:146
FILE * file
Definition: log.c:133
Mutex * mutex_create_real(void)
Definition: thread.c:78
Mutex * mutex_init_static_real(Mutex *mutex)
Definition: thread.c:89
Mutex * mutex
Definition: fakewap.c:238
pthread_mutex_t mutex
Definition: thread.h:77
char filename[FILENAME_MAX+1]
Definition: log.c:135
gw_assert(wtls_machine->packet_to_send!=NULL)
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
void mutex_lock_real(Mutex *mutex, char *file, int line, const char *func)
Definition: thread.c:121
#define panic
Definition: log.h:87
Definition: thread.h:76
long owner
Definition: thread.h:78
int mutex_trylock_real(Mutex *mutex, const char *file, int line, const char *func)
Definition: thread.c:165
Mutex * mutex_create_measured(Mutex *mutex, char *filename, int lineno)
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.