Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
thread.h File Reference
#include "gw-config.h"
#include <pthread.h>

Go to the source code of this file.

Data Structures

struct  Mutex
 

Macros

#define mutex_create()   gw_claim_area(mutex_create_real())
 
#define mutex_init_static(mutex)   mutex_init_static_real(mutex)
 
#define mutex_lock(m)   mutex_lock_real(m, __FILE__, __LINE__, __func__)
 
#define mutex_unlock(m)   mutex_unlock_real(m, __FILE__, __LINE__, __func__)
 
#define mutex_trylock(m)   mutex_trylock_real(m, __FILE__, __LINE__, __func__)
 

Functions

Mutexmutex_create_measured (Mutex *mutex, char *filename, int lineno)
 
Mutexmutex_create_real (void)
 
Mutexmutex_init_static_real (Mutex *mutex)
 
void mutex_destroy (Mutex *mutex)
 
void mutex_lock_real (Mutex *mutex, char *file, int line, const char *func)
 
int mutex_unlock_real (Mutex *mutex, char *file, int line, const char *func)
 
int mutex_trylock_real (Mutex *mutex, const char *file, int line, const char *func)
 

Macro Definition Documentation

#define mutex_init_static (   mutex)    mutex_init_static_real(mutex)

Definition at line 115 of file thread.h.

Referenced by gw_check_init_mem(), gwlib_protected_init(), and octstr_init().

#define mutex_trylock (   m)    mutex_trylock_real(m, __FILE__, __LINE__, __func__)

Definition at line 144 of file thread.h.

Function Documentation

Mutex* mutex_create_measured ( Mutex mutex,
char *  filename,
int  lineno 
)
Mutex* mutex_create_real ( void  )

Definition at line 78 of file thread.c.

References Mutex::dynamic, Mutex::mutex, mutex, and Mutex::owner.

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 }
int dynamic
Definition: thread.h:79
Mutex * mutex
Definition: fakewap.c:238
pthread_mutex_t mutex
Definition: thread.h:77
Definition: thread.h:76
long owner
Definition: thread.h:78
void mutex_destroy ( Mutex mutex)

Definition at line 97 of file thread.c.

References Mutex::dynamic, info(), Mutex::mutex, and panic.

Referenced by bearerbox_address_destroy(), client_shutdown(), concat_handling_cleanup(), conn_destroy(), conn_pool_shutdown(), counter_destroy(), dict_destroy(), gw_check_shutdown(), gw_prioqueue_destroy(), gw_timerset_destroy(), gwlib_protected_shutdown(), gwlist_destroy(), main(), octstr_shutdown(), port_shutdown(), proxy_shutdown(), radius_acct_shutdown(), server_shutdown(), smscconn_destroy(), smscenter_destruct(), smscwrapper_destroy(), store_dumper(), and timers_shutdown().

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 }
void info(int err, const char *fmt,...)
Definition: log.c:636
int dynamic
Definition: thread.h:79
pthread_mutex_t mutex
Definition: thread.h:77
#define panic
Definition: log.h:87
Mutex* mutex_init_static_real ( Mutex mutex)

Definition at line 89 of file thread.c.

References Mutex::dynamic, Mutex::mutex, mutex, and Mutex::owner.

90 {
91  pthread_mutex_init(&mutex->mutex, NULL);
92  mutex->owner = -1;
93  mutex->dynamic = 0;
94  return mutex;
95 }
int dynamic
Definition: thread.h:79
Mutex * mutex
Definition: fakewap.c:238
pthread_mutex_t mutex
Definition: thread.h:77
long owner
Definition: thread.h:78
void mutex_lock_real ( Mutex mutex,
char *  file,
int  line,
const char *  func 
)

Definition at line 121 of file thread.c.

References gw_assert(), gwthread_self(), Mutex::mutex, Mutex::owner, and panic.

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 }
long gwthread_self(void)
FILE * file
Definition: log.c:133
pthread_mutex_t mutex
Definition: thread.h:77
gw_assert(wtls_machine->packet_to_send!=NULL)
#define panic
Definition: log.h:87
long owner
Definition: thread.h:78
int mutex_trylock_real ( Mutex mutex,
const char *  file,
int  line,
const char *  func 
)

Definition at line 165 of file thread.c.

References error(), gwthread_self(), Mutex::mutex, Mutex::owner, and panic.

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 }
void error(int err, const char *fmt,...)
Definition: log.c:612
long gwthread_self(void)
FILE * file
Definition: log.c:133
pthread_mutex_t mutex
Definition: thread.h:77
#define panic
Definition: log.h:87
long owner
Definition: thread.h:78
int mutex_unlock_real ( Mutex mutex,
char *  file,
int  line,
const char *  func 
)

Definition at line 146 of file thread.c.

References error(), gw_assert(), Mutex::mutex, Mutex::owner, and panic.

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 }
void error(int err, const char *fmt,...)
Definition: log.c:612
FILE * file
Definition: log.c:133
pthread_mutex_t mutex
Definition: thread.h:77
gw_assert(wtls_machine->packet_to_send!=NULL)
#define panic
Definition: log.h:87
long owner
Definition: thread.h:78
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.