Kannel: Open Source WAP and SMS gateway  svn-r5335
gw-semaphore.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  * semaphore.c - implementation of semaphores
59  *
60  * Lars Wirzenius
61  * Alexander Malysh <a.malysh@centrium.de>, 2004
62  */
63 
64 
65 #include "gwlib/gwlib.h"
66 
67 #ifdef HAVE_SEMAPHORE
68 #include <semaphore.h>
69 #include <errno.h>
70 #endif
71 
72 struct Semaphore {
73 #ifdef HAVE_SEMAPHORE
74  sem_t sem;
75 #else
77 #endif
78 };
79 
80 
82 {
83  Semaphore *semaphore;
84 #ifndef HAVE_SEMAPHORE
85  static char item;
86 #endif
87 
88  semaphore = gw_malloc(sizeof(*semaphore));
89 
90 #ifdef HAVE_SEMAPHORE
91  if (sem_init(&semaphore->sem, 0, (unsigned int) n) != 0)
92  panic(errno, "Could not initialize semaphore.");
93 #else
94  semaphore->list = gwlist_create();
95  gwlist_add_producer(semaphore->list);
96  while (n-- > 0)
97  gwlist_produce(semaphore->list, &item);
98 #endif
99 
100  return semaphore;
101 }
102 
103 
104 void semaphore_destroy(Semaphore *semaphore)
105 {
106  if (semaphore != NULL) {
107 #ifdef HAVE_SEMAPHORE
108  if (sem_destroy(&semaphore->sem) != 0)
109  panic(errno, "Destroying semaphore while some threads are waiting.");
110 #else
111  gwlist_destroy(semaphore->list, NULL);
112 #endif
113  gw_free(semaphore);
114  }
115 }
116 
117 
118 void semaphore_up(Semaphore *semaphore)
119 {
120 #ifndef HAVE_SEMAPHORE
121  static char item;
122  gw_assert(semaphore != NULL);
123  gwlist_produce(semaphore->list, &item);
124 #else
125  gw_assert(semaphore != NULL);
126  if (sem_post(&semaphore->sem) != 0)
127  error(errno, "Value for semaphore is out of range.");
128 #endif
129 }
130 
131 
132 void semaphore_down(Semaphore *semaphore)
133 {
134  gw_assert(semaphore != NULL);
135 #ifdef HAVE_SEMAPHORE
136  sem_wait(&semaphore->sem);
137 #else
138  gwlist_consume(semaphore->list);
139 #endif
140 }
141 
142 
144 {
145  gw_assert(semaphore != NULL);
146 #ifdef HAVE_SEMAPHORE
147  {
148  int val;
149  if (sem_getvalue(&semaphore->sem, &val) != 0)
150  panic(errno, "Could not get semaphore value.");
151  return val;
152  }
153 #else
154  return gwlist_len(semaphore->list);
155 #endif
156 }
157 
void error(int err, const char *fmt,...)
Definition: log.c:648
long semaphore_getvalue(Semaphore *semaphore)
Definition: gw-semaphore.c:143
gw_assert(wtls_machine->packet_to_send !=NULL)
void semaphore_destroy(Semaphore *semaphore)
Definition: gw-semaphore.c:104
void gwlist_produce(List *list, void *item)
Definition: list.c:411
long gwlist_len(List *list)
Definition: list.c:166
void semaphore_down(Semaphore *semaphore)
Definition: gw-semaphore.c:132
List * list
Definition: gw-semaphore.c:76
void semaphore_up(Semaphore *semaphore)
Definition: gw-semaphore.c:118
void * gwlist_consume(List *list)
Definition: list.c:427
#define panic
Definition: log.h:87
#define gwlist_create()
Definition: list.h:136
void gwlist_add_producer(List *list)
Definition: list.c:383
Semaphore * semaphore_create(long n)
Definition: gw-semaphore.c:81
Definition: list.c:102
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.