Kannel: Open Source WAP and SMS gateway  svn-r5335
gw-rwlock.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 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 /*
58  * gw-rwlock.c: Implements Reader/Writer Lock.
59  * If pthread_rwlock_XXX functions are present then those will be used;
60  * otherwise emulation with mutexes/condition should be done.
61  *
62  * Alexander Malysh <amalysh@kannel.org>, initial version 2004
63  */
64 
65 #include "gw-config.h"
66 #include "gwlib.h"
67 #include "gw-rwlock.h"
68 
69 #define DEBUG 0
70 #if DEBUG
71  #define RWDEBUG(str,lev,frm,args...) debug(str, lev, frm, ## args)
72 #else
73  #define RWDEBUG(str,lev,frm,args...) do{}while(0)
74 #endif
75 
76 
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 }
94 
95 
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 }
110 
111 
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 }
132 
133 
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);
146  gwlist_add_producer(lock->rwlock);
147  gwlist_unlock(lock->rwlock);
148  RWDEBUG("", 0, "------------ gw_rwlock_rdlock(%p) ----------", lock);
149 #endif
150 
151  return ret;
152 }
153 
154 
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
170  gwlist_remove_producer(lock->rwlock);
171 #endif
172 
173  return ret;
174 }
175 
176 
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
int dynamic
Definition: gw-rwlock.h:80
gw_assert(wtls_machine->packet_to_send !=NULL)
void gw_rwlock_destroy(RWLock *lock)
Definition: gw-rwlock.c:112
int gw_rwlock_wrlock(RWLock *lock)
Definition: gw-rwlock.c:177
RWLock * gw_rwlock_create(void)
Definition: gw-rwlock.c:77
#define lock(c)
Definition: counter.c:89
int gw_rwlock_rdlock(RWLock *lock)
Definition: gw-rwlock.c:134
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
int gw_rwlock_unlock(RWLock *lock)
Definition: gw-rwlock.c:155
void gwlist_lock(List *list)
Definition: list.c:347
void * gwlist_consume(List *list)
Definition: list.c:427
#define panic
Definition: log.h:87
#define gwlist_create()
Definition: list.h:136
void gw_rwlock_init_static(RWLock *lock)
Definition: gw-rwlock.c:96
void gwlist_add_producer(List *list)
Definition: list.c:383
int gwlist_producer_count(List *list)
Definition: list.c:391
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.