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

semaphore.c

Go to the documentation of this file.
00001 /* ==================================================================== 
00002  * The Kannel Software License, Version 1.0 
00003  * 
00004  * Copyright (c) 2001-2008 Kannel Group  
00005  * Copyright (c) 1998-2001 WapIT Ltd.   
00006  * All rights reserved. 
00007  * 
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions 
00010  * are met: 
00011  * 
00012  * 1. Redistributions of source code must retain the above copyright 
00013  *    notice, this list of conditions and the following disclaimer. 
00014  * 
00015  * 2. Redistributions in binary form must reproduce the above copyright 
00016  *    notice, this list of conditions and the following disclaimer in 
00017  *    the documentation and/or other materials provided with the 
00018  *    distribution. 
00019  * 
00020  * 3. The end-user documentation included with the redistribution, 
00021  *    if any, must include the following acknowledgment: 
00022  *       "This product includes software developed by the 
00023  *        Kannel Group (http://www.kannel.org/)." 
00024  *    Alternately, this acknowledgment may appear in the software itself, 
00025  *    if and wherever such third-party acknowledgments normally appear. 
00026  * 
00027  * 4. The names "Kannel" and "Kannel Group" must not be used to 
00028  *    endorse or promote products derived from this software without 
00029  *    prior written permission. For written permission, please  
00030  *    contact org@kannel.org. 
00031  * 
00032  * 5. Products derived from this software may not be called "Kannel", 
00033  *    nor may "Kannel" appear in their name, without prior written 
00034  *    permission of the Kannel Group. 
00035  * 
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00039  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
00040  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
00041  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
00042  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
00043  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
00044  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
00045  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
00046  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00047  * ==================================================================== 
00048  * 
00049  * This software consists of voluntary contributions made by many 
00050  * individuals on behalf of the Kannel Group.  For more information on  
00051  * the Kannel Group, please see <http://www.kannel.org/>. 
00052  * 
00053  * Portions of this software are based upon software originally written at  
00054  * WapIT Ltd., Helsinki, Finland for the Kannel project.  
00055  */ 
00056 
00057 /*
00058  * semaphore.c - implementation of semaphores
00059  *
00060  * Lars Wirzenius
00061  * Alexander Malysh <a.malysh@centrium.de>, 2004
00062  */
00063 
00064 
00065 #include "gwlib/gwlib.h"
00066 
00067 #ifdef HAVE_SEMAPHORE
00068 #include <semaphore.h>
00069 #include <errno.h>
00070 #endif
00071 
00072 struct Semaphore {
00073 #ifdef HAVE_SEMAPHORE
00074     sem_t sem;
00075 #else
00076     List *list;
00077 #endif
00078 };
00079 
00080 
00081 Semaphore *semaphore_create(long n)
00082 {
00083     Semaphore *semaphore;
00084 #ifndef HAVE_SEMAPHORE
00085     static char item;
00086 #endif
00087     
00088     semaphore = gw_malloc(sizeof(*semaphore));
00089 
00090 #ifdef HAVE_SEMAPHORE
00091     if (sem_init(&semaphore->sem, 0, (unsigned int) n) != 0)
00092         panic(errno, "Could not initialize semaphore.");
00093 #else
00094     semaphore->list = gwlist_create();
00095     gwlist_add_producer(semaphore->list);
00096     while (n-- > 0)
00097     gwlist_produce(semaphore->list, &item);
00098 #endif
00099 
00100     return semaphore;
00101 }
00102 
00103 
00104 void semaphore_destroy(Semaphore *semaphore)
00105 {
00106     if (semaphore != NULL) {
00107 #ifdef HAVE_SEMAPHORE
00108         if (sem_destroy(&semaphore->sem) != 0)
00109             panic(errno, "Destroying semaphore while some threads are waiting.");
00110 #else
00111     gwlist_destroy(semaphore->list, NULL);
00112 #endif
00113     gw_free(semaphore);
00114     }
00115 }
00116 
00117 
00118 void semaphore_up(Semaphore *semaphore)
00119 {
00120 #ifndef HAVE_SEMAPHORE
00121     static char item;
00122     gw_assert(semaphore != NULL);
00123     gwlist_produce(semaphore->list, &item);
00124 #else
00125     gw_assert(semaphore != NULL);
00126     if (sem_post(&semaphore->sem) != 0)
00127         error(errno, "Value for semaphore is out of range.");
00128 #endif
00129 }
00130 
00131 
00132 void semaphore_down(Semaphore *semaphore)
00133 {
00134     gw_assert(semaphore != NULL);
00135 #ifdef HAVE_SEMAPHORE
00136     sem_wait(&semaphore->sem);
00137 #else
00138     gwlist_consume(semaphore->list);
00139 #endif
00140 }
00141 
00142 
00143 long semaphore_getvalue(Semaphore *semaphore)
00144 {
00145     gw_assert(semaphore != NULL);
00146 #ifdef HAVE_SEMAPHORE
00147     {
00148         int val;
00149         if (sem_getvalue(&semaphore->sem, &val) != 0)
00150             panic(errno, "Could not get semaphore value.");
00151         return val;
00152     }
00153 #else
00154     return gwlist_len(semaphore->list);
00155 #endif
00156 }
00157 
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.