Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

thread.c File Reference

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include "gwlib/gwlib.h"

Include dependency graph for thread.c:

Include dependency graph

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* mutex_create_real void   ) 
 

Definition at line 78 of file thread.c.

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

00079 {
00080     Mutex *mutex;
00081 
00082     mutex = gw_malloc(sizeof(Mutex));
00083     pthread_mutex_init(&mutex->mutex, NULL);
00084     mutex->owner = -1;
00085     mutex->dynamic = 1;
00086     return mutex;
00087 }

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(), conn_destroy(), conn_pool_shutdown(), counter_destroy(), dict_destroy(), gw_check_shutdown(), gw_prioqueue_destroy(), gwlib_protected_shutdown(), gwlist_destroy(), main(), octstr_shutdown(), openssl_shutdown_locks(), port_shutdown(), proxy_shutdown(), radius_acct_shutdown(), server_shutdown(), shutdown_concat_handler(), smscconn_destroy(), smscenter_destruct(), smscwrapper_destroy(), store_dumper(), and timers_shutdown().

00098 {
00099     int ret;
00100 
00101     if (mutex == NULL)
00102         return;
00103 
00104 #ifdef MUTEX_STATS
00105     if (mutex->locks > 0 || mutex->collisions > 0) {
00106         info(0, "Mutex %s:%d: %ld locks, %ld collisions.",
00107              mutex->filename, mutex->lineno,
00108              mutex->locks, mutex->collisions);
00109     }
00110 #endif
00111 
00112     if ((ret = pthread_mutex_destroy(&mutex->mutex)) != 0)
00113         panic(ret, "Attempt to destroy locked mutex!");
00114 
00115     if (mutex->dynamic == 0)
00116         return;
00117     gw_free(mutex);
00118 }

Here is the call graph for this function:

Mutex* mutex_init_static_real Mutex mutex  ) 
 

Definition at line 89 of file thread.c.

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

00090 {
00091     pthread_mutex_init(&mutex->mutex, NULL);
00092     mutex->owner = -1;
00093     mutex->dynamic = 0;
00094     return mutex;
00095 }

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.

00122 {
00123     int ret;
00124 
00125     gw_assert(mutex != NULL);
00126 
00127 #ifdef MUTEX_STATS
00128     ret = pthread_mutex_trylock(&mutex->mutex);
00129     if (ret != 0) {
00130         ret = pthread_mutex_lock(&mutex->mutex);
00131         mutex->collisions++;
00132     }
00133     mutex->locks++;
00134 #else
00135     ret = pthread_mutex_lock(&mutex->mutex);
00136 #endif
00137     if (ret != 0)
00138         panic(0, "%s:%ld: %s: Mutex failure! (Called from %s:%ld:%s.)", \
00139                  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
00140     if (mutex->owner == gwthread_self())
00141         panic(0, "%s:%ld: %s: Managed to lock the mutex twice! (Called from %s:%ld:%s.)", \
00142                  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
00143     mutex->owner = gwthread_self();
00144 }

Here is the call graph for this function:

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.

00166 {
00167     int ret;
00168 
00169     if (mutex == NULL) {
00170         error(0, "%s:%ld: %s: Trying to lock a NULL mutex! (Called from %s:%ld:%s.)", \
00171                  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
00172         return -1;
00173     }
00174 
00175     ret = pthread_mutex_trylock(&mutex->mutex);
00176     if (ret == 0) {
00177         if (mutex->owner == gwthread_self())
00178             panic(0, "%s:%ld: %s: Managed to lock the mutex twice! (Called from %s:%ld:%s.)", \
00179                  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
00180 
00181         mutex->owner = gwthread_self();
00182     }
00183     else if (ret == EINVAL)
00184         panic(0, "%s:%ld: %s: Mutex failure! (Called from %s:%ld:%s.)", \
00185                  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
00186 
00187     return ret;
00188 }

Here is the call graph for this function:

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.

00147 {
00148      int ret;
00149     
00150     if (mutex == NULL) {
00151         error(0, "%s:%ld: %s: Trying to unlock a NULL mutex! (Called from %s:%ld:%s.)", \
00152                  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
00153        return -1;
00154     }
00155     gw_assert(mutex != NULL);
00156     mutex->owner = -1;
00157     ret = pthread_mutex_unlock(&mutex->mutex);
00158     if (ret != 0)
00159         panic(0, "%s:%ld: %s: Mutex failure! (Called from %s:%ld:%s.)", \
00160                  __FILE__, (long) __LINE__, __func__, file, (long) line, func);
00161 
00162     return ret;
00163 }

Here is the call graph for this function:

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.