Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
gw-rwlock.c File Reference
#include "gw-config.h"
#include "gwlib.h"
#include "gw-rwlock.h"

Go to the source code of this file.

Macros

#define DEBUG   0
 
#define RWDEBUG(str, lev, frm, args...)   do{}while(0)
 

Functions

RWLockgw_rwlock_create (void)
 
void gw_rwlock_init_static (RWLock *lock)
 
void gw_rwlock_destroy (RWLock *lock)
 
int gw_rwlock_rdlock (RWLock *lock)
 
int gw_rwlock_unlock (RWLock *lock)
 
int gw_rwlock_wrlock (RWLock *lock)
 

Macro Definition Documentation

#define DEBUG   0

Definition at line 69 of file gw-rwlock.c.

#define RWDEBUG (   str,
  lev,
  frm,
  args... 
)    do{}while(0)

Definition at line 73 of file gw-rwlock.c.

Referenced by gw_rwlock_rdlock(), gw_rwlock_unlock(), and gw_rwlock_wrlock().

Function Documentation

RWLock* gw_rwlock_create ( void  )

Definition at line 77 of file gw-rwlock.c.

References RWLock::dynamic, gwlist_create, panic, RWLock::rwlock, and RWLock::writer.

Referenced by load_create_real(), and smsbox_start().

78 {
79  RWLock *ret = gw_malloc(sizeof(*ret));
80 #ifdef HAVE_PTHREAD_RWLOCK
81  int rc = pthread_rwlock_init(&ret->rwlock, NULL);
82  if (rc != 0)
83  panic(rc, "Initialization of RWLock failed.");
84 #else
85  ret->writer = -1;
86  ret->rwlock = gwlist_create();
87  if (ret->rwlock == NULL)
88  panic(0, "Initialization of RWLock failed.");
89 #endif
90  ret->dynamic = 1;
91 
92  return ret;
93 }
long writer
Definition: gw-rwlock.h:78
int dynamic
Definition: gw-rwlock.h:80
List * rwlock
Definition: gw-rwlock.h:77
#define panic
Definition: log.h:87
#define gwlist_create()
Definition: list.h:136
void gw_rwlock_destroy ( RWLock lock)

Definition at line 112 of file gw-rwlock.c.

References RWLock::dynamic, gwlist_destroy(), panic, and RWLock::rwlock.

Referenced by dlr_mem_shutdown(), load_destroy(), log_shutdown(), smsboxc_run(), and smsc2_cleanup().

113 {
114 #ifdef HAVE_PTHREAD_RWLOCK
115  int ret;
116 #endif
117 
118  if (!lock)
119  return;
120 
121 #ifdef HAVE_PTHREAD_RWLOCK
122  ret = pthread_rwlock_destroy(&lock->rwlock);
123  if (ret != 0)
124  panic(ret, "Attempt to destroy locked rwlock.");
125 #else
126  gwlist_destroy(lock->rwlock, NULL);
127 #endif
128 
129  if (lock->dynamic)
130  gw_free(lock);
131 }
int dynamic
Definition: gw-rwlock.h:80
List * rwlock
Definition: gw-rwlock.h:77
#define panic
Definition: log.h:87
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
void gw_rwlock_init_static ( RWLock lock)

Definition at line 96 of file gw-rwlock.c.

References RWLock::dynamic, gwlist_create, panic, RWLock::rwlock, and RWLock::writer.

Referenced by dlr_init_mem(), log_init(), and smsc2_start().

97 {
98 #ifdef HAVE_PTHREAD_RWLOCK
99  int rc = pthread_rwlock_init(&lock->rwlock, NULL);
100  if (rc != 0)
101  panic(rc, "Initialization of RWLock failed.");
102 #else
103  lock->writer = -1;
104  lock->rwlock = gwlist_create();
105  if (lock->rwlock == NULL)
106  panic(0, "Initialization of RWLock failed.");
107 #endif
108  lock->dynamic = 0;
109 }
long writer
Definition: gw-rwlock.h:78
int dynamic
Definition: gw-rwlock.h:80
List * rwlock
Definition: gw-rwlock.h:77
#define panic
Definition: log.h:87
#define gwlist_create()
Definition: list.h:136
int gw_rwlock_rdlock ( RWLock lock)

Definition at line 134 of file gw-rwlock.c.

References gw_assert(), gwlist_add_producer(), gwlist_lock(), gwlist_unlock(), panic, RWDEBUG, and RWLock::rwlock.

Referenced by bb_smscconn_receive(), boxc_status(), concat_handling_clear_old_parts(), dlr_mem_get(), load_get(), load_len(), route_incoming_to_boxc(), sms_to_smsboxes(), smsc2_resume(), smsc2_rout(), smsc2_shutdown(), smsc2_status(), smsc2_stop_smsc(), and smsc2_suspend().

135 {
136  int ret = 0;
137  gw_assert(lock != NULL);
138 
139 #ifdef HAVE_PTHREAD_RWLOCK
140  ret = pthread_rwlock_rdlock(&lock->rwlock);
141  if (ret != 0) {
142  panic(ret, "Error while pthread_rwlock_rdlock.");
143  }
144 #else
145  gwlist_lock(lock->rwlock);
147  gwlist_unlock(lock->rwlock);
148  RWDEBUG("", 0, "------------ gw_rwlock_rdlock(%p) ----------", lock);
149 #endif
150 
151  return ret;
152 }
List * rwlock
Definition: gw-rwlock.h:77
#define RWDEBUG(str, lev, frm, args...)
Definition: gw-rwlock.c:73
void gwlist_unlock(List *list)
Definition: list.c:354
gw_assert(wtls_machine->packet_to_send!=NULL)
void gwlist_lock(List *list)
Definition: list.c:347
#define panic
Definition: log.h:87
void gwlist_add_producer(List *list)
Definition: list.c:383
int gw_rwlock_unlock ( RWLock lock)

Definition at line 155 of file gw-rwlock.c.

References gw_assert(), gwlist_remove_producer(), gwlist_unlock(), gwthread_self(), panic, RWDEBUG, RWLock::rwlock, and RWLock::writer.

Referenced by bb_smscconn_receive(), boxc_status(), concat_handling_clear_old_parts(), dlr_mem_add(), dlr_mem_flush(), dlr_mem_get(), dlr_mem_remove(), dlr_mem_shutdown(), load_add_interval(), load_get(), load_increase_with(), load_len(), log_close_all(), log_open(), log_reopen(), route_incoming_to_boxc(), run_smsbox(), sms_to_smsboxes(), smsc2_add_smsc(), smsc2_cleanup(), smsc2_graceful_restart(), smsc2_reload_lists(), smsc2_remove_smsc(), smsc2_restart_smsc(), smsc2_resume(), smsc2_rout(), smsc2_shutdown(), smsc2_status(), smsc2_stop_smsc(), and smsc2_suspend().

156 {
157  int ret = 0;
158  gw_assert(lock != NULL);
159 
160 #ifdef HAVE_PTHREAD_RWLOCK
161  ret = pthread_rwlock_unlock(&lock->rwlock);
162  if (ret != 0)
163  panic(ret, "Error while gw_rwlock_unlock.");
164 #else
165  RWDEBUG("", 0, "------------ gw_rwlock_unlock(%p) ----------", lock);
166  if (lock->writer == gwthread_self()) {
167  lock->writer = -1;
168  gwlist_unlock(lock->rwlock);
169  } else
171 #endif
172 
173  return ret;
174 }
long gwthread_self(void)
long writer
Definition: gw-rwlock.h:78
List * rwlock
Definition: gw-rwlock.h:77
#define RWDEBUG(str, lev, frm, args...)
Definition: gw-rwlock.c:73
void gwlist_unlock(List *list)
Definition: list.c:354
void gwlist_remove_producer(List *list)
Definition: list.c:401
gw_assert(wtls_machine->packet_to_send!=NULL)
#define panic
Definition: log.h:87
int gw_rwlock_wrlock ( RWLock lock)

Definition at line 177 of file gw-rwlock.c.

References gw_assert(), gwlist_consume(), gwlist_lock(), gwlist_producer_count(), gwthread_self(), panic, RWDEBUG, RWLock::rwlock, and RWLock::writer.

Referenced by dlr_mem_add(), dlr_mem_flush(), dlr_mem_remove(), dlr_mem_shutdown(), load_add_interval(), load_increase_with(), log_close_all(), log_open(), log_reopen(), run_smsbox(), smsc2_add_smsc(), smsc2_cleanup(), smsc2_graceful_restart(), smsc2_reload_lists(), smsc2_remove_smsc(), and smsc2_restart_smsc().

178 {
179  int ret = 0;
180  gw_assert(lock != NULL);
181 
182 #ifdef HAVE_PTHREAD_RWLOCK
183  ret = pthread_rwlock_wrlock(&lock->rwlock);
184  if (ret != 0)
185  panic(ret, "Error while pthread_rwlock_wrlock.");
186 #else
187  RWDEBUG("", 0, "------------ gw_rwlock_wrlock(%p) ----------", lock);
188  gwlist_lock(lock->rwlock);
189  RWDEBUG("", 0, "------------ gw_rwlock_wrlock(%p) producers=%d", lock, gwlist_producer_count(lock->rwlock));
190  /* wait for reader */
191  gwlist_consume(lock->rwlock);
192  lock->writer = gwthread_self();
193 #endif
194 
195  return ret;
196 }
long gwthread_self(void)
long writer
Definition: gw-rwlock.h:78
List * rwlock
Definition: gw-rwlock.h:77
#define RWDEBUG(str, lev, frm, args...)
Definition: gw-rwlock.c:73
gw_assert(wtls_machine->packet_to_send!=NULL)
void gwlist_lock(List *list)
Definition: list.c:347
void * gwlist_consume(List *list)
Definition: list.c:427
#define panic
Definition: log.h:87
int gwlist_producer_count(List *list)
Definition: list.c:391
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.