Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
semaphore.c File Reference
#include "gwlib/gwlib.h"

Go to the source code of this file.

Data Structures

struct  Semaphore
 

Functions

Semaphoresemaphore_create (long n)
 
void semaphore_destroy (Semaphore *semaphore)
 
void semaphore_up (Semaphore *semaphore)
 
void semaphore_down (Semaphore *semaphore)
 
long semaphore_getvalue (Semaphore *semaphore)
 

Function Documentation

Semaphore* semaphore_create ( long  n)

Definition at line 81 of file semaphore.c.

References gwlist_add_producer(), gwlist_create, gwlist_produce(), Semaphore::list, and panic.

Referenced by httpd_emu_create(), init_smsbox(), run_smsbox(), smpp_emu(), smsc_emu_create(), and smsc_http_create().

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 }
void gwlist_produce(List *list, void *item)
Definition: list.c:411
List * list
Definition: semaphore.c:76
#define panic
Definition: log.h:87
#define gwlist_create()
Definition: list.h:136
void gwlist_add_producer(List *list)
Definition: list.c:383
void semaphore_destroy ( Semaphore semaphore)

Definition at line 104 of file semaphore.c.

References gwlist_destroy(), Semaphore::list, and panic.

Referenced by conndata_destroy(), httpd_emu_create(), main(), run_smsbox(), smpp_emu(), and smsc_emu_create().

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 }
List * list
Definition: semaphore.c:76
#define panic
Definition: log.h:87
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
void semaphore_down ( Semaphore semaphore)

Definition at line 132 of file semaphore.c.

References gw_assert(), gwlist_consume(), and Semaphore::list.

Referenced by boxc_sent_push(), httpd_emu_create(), httpsmsc_sender(), obey_request(), smpp_emu_writer(), and smsc_emu_create().

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 }
List * list
Definition: semaphore.c:76
gw_assert(wtls_machine->packet_to_send!=NULL)
void * gwlist_consume(List *list)
Definition: list.c:427
long semaphore_getvalue ( Semaphore semaphore)

Definition at line 143 of file semaphore.c.

References gw_assert(), gwlist_len(), Semaphore::list, and panic.

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 }
long gwlist_len(List *list)
Definition: list.c:166
List * list
Definition: semaphore.c:76
gw_assert(wtls_machine->packet_to_send!=NULL)
#define panic
Definition: log.h:87
void semaphore_up ( Semaphore semaphore)

Definition at line 118 of file semaphore.c.

References error(), gw_assert(), gwlist_produce(), and Semaphore::list.

Referenced by boxc_sent_pop(), httpd_emu(), httpsmsc_send_cb(), httpsmsc_sender(), obey_request(), run_smsbox(), smpp_emu(), smpp_emu_handle_pdu(), and url_result_thread().

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 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void gwlist_produce(List *list, void *item)
Definition: list.c:411
List * list
Definition: semaphore.c:76
gw_assert(wtls_machine->packet_to_send!=NULL)
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.