Kannel: Open Source WAP and SMS gateway  svn-r5335
thread.c File Reference
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include "gwlib/gwlib.h"

Go to the source code of this file.

Functions

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)
 

Function Documentation

◆ mutex_create_real()

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

◆ mutex_destroy()

void mutex_destroy ( Mutex mutex)

Definition at line 97 of file thread.c.

References Mutex::dynamic, info(), Mutex::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(), gw_timerset_elapsed_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:672
int dynamic
Definition: thread.h:79
Mutex * mutex
Definition: fakewap.c:238
pthread_mutex_t mutex
Definition: thread.h:77
#define panic
Definition: log.h:87

◆ mutex_init_static_real()

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

◆ mutex_lock_real()

void mutex_lock_real ( Mutex mutex,
char *  file,
int  line,
const char *  func 
)

Definition at line 121 of file thread.c.

References file, gw_assert(), gwthread_self(), Mutex::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);
144 }
long gwthread_self(void)
gw_assert(wtls_machine->packet_to_send !=NULL)
FILE * file
Definition: log.c:169
Mutex * mutex
Definition: fakewap.c:238
pthread_mutex_t mutex
Definition: thread.h:77
#define panic
Definition: log.h:87
long owner
Definition: thread.h:78

◆ mutex_trylock_real()

int mutex_trylock_real ( Mutex mutex,
const char *  file,
int  line,
const char *  func 
)

Definition at line 165 of file thread.c.

References error(), file, gwthread_self(), Mutex::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 
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:648
long gwthread_self(void)
FILE * file
Definition: log.c:169
Mutex * mutex
Definition: fakewap.c:238
pthread_mutex_t mutex
Definition: thread.h:77
#define panic
Definition: log.h:87
long owner
Definition: thread.h:78

◆ mutex_unlock_real()

int mutex_unlock_real ( Mutex mutex,
char *  file,
int  line,
const char *  func 
)

Definition at line 146 of file thread.c.

References error(), file, gw_assert(), Mutex::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:648
gw_assert(wtls_machine->packet_to_send !=NULL)
FILE * file
Definition: log.c:169
Mutex * mutex
Definition: fakewap.c:238
pthread_mutex_t mutex
Definition: thread.h:77
#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.