S3FC project page S3FC home page

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

s3_fifo_base.cc

Go to the documentation of this file.
00001 /*
00002  * Stone Three Foundation Class (s3fc) provides a number of utility classes.
00003  * Copyright (C) 2001 by Stone Three Signal Processing (Pty) Ltd.
00004  *
00005  * Authored by Stone Three Signal Processing (Pty) Ltd.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  * 
00021  * Please see the file 'COPYING' in the source root directory.
00022  */
00023 
00033 #include <s3fc/s3_fifo_base.h>
00034 #include <s3fc/s3_macros.h>
00035 #include <s3fc/s3_exception.h>
00036 #include <sstream>
00037 #include <algorithm>
00038 
00039 //
00040 void s3_subscribable_fifo::subscribe_producer(const s3_semaphore& sem)
00041 {
00042    notification_lock.lock();
00043    // test precondition
00044    if ( std::find(producers.begin(), producers.end(), &sem) != producers.end() )
00045    {
00046       // semaphore already subscribed
00047       notification_lock.unlock();
00048       std::stringstream str;
00049       str << "Semaphore at  [" << &sem << "] already subscribed to "
00050      << "producer notification list";
00051       throw s3_generic_exception("s3_subscribable_fifo::"
00052              "subscribe_producer", str.str());
00053    }
00054    // add to list
00055    producers.push_back(const_cast<s3_semaphore*>(&sem));
00056    notification_lock.unlock();
00057 }
00058 
00059 //
00060 void s3_subscribable_fifo::unsubscribe_producer(const s3_semaphore& sem)
00061 {
00062 
00063    notification_lock.lock();
00064    // test precondition
00065    if ( std::find(producers.begin(), producers.end(), &sem) == producers.end() )
00066    {
00067       // semaphore not subscribed
00068       notification_lock.unlock();
00069       std::stringstream str;
00070       str << "Semaphore at  [" << &sem << "] not subscribed to "
00071      << "producer notification list";
00072       throw s3_generic_exception("s3_subscribable_fifo::"
00073              "unsubscribe_producer", str.str());
00074    }
00075    // remove from list
00076    producers.erase(std::find(producers.begin(), producers.end(), &sem));
00077    notification_lock.unlock();
00078 }
00079 
00080 //
00081 bool s3_subscribable_fifo::is_subscribed_producer(const s3_semaphore& sem)
00082 {
00083    bool retval;
00084    notification_lock.lock();
00085    retval = std::find(producers.begin(),
00086             producers.end(), &sem) != producers.end();
00087    notification_lock.unlock();
00088    return retval;
00089 }
00090 
00091 //
00092 void s3_subscribable_fifo::subscribe_consumer(const s3_semaphore& sem)
00093 {
00094    notification_lock.lock();
00095    // test precondition
00096    if ( std::find(consumers.begin(), consumers.end(), &sem) != consumers.end() )
00097    {
00098       // semaphore already subscribed
00099       notification_lock.unlock();
00100       std::stringstream str;
00101       str << "Semaphore at  [" << &sem << "] already subscribed to "
00102      << "consumer notification list";
00103       throw s3_generic_exception("s3_subscribable_fifo::"
00104              "subscribe_consumer", str.str());
00105    }
00106    // add to list
00107    consumers.push_back(const_cast<s3_semaphore*>(&sem));
00108    notification_lock.unlock();
00109 }
00110 
00111 //
00112 void s3_subscribable_fifo::unsubscribe_consumer(const s3_semaphore& sem)
00113 {
00114    notification_lock.lock();
00115    // test precondition
00116    if ( std::find(consumers.begin(), consumers.end(), &sem) == consumers.end() )
00117    {
00118       // semaphore not subscribed
00119       notification_lock.unlock();
00120       std::stringstream str;
00121       str << "Semaphore at  [" << &sem << "] not subscribed to "
00122      << "consumer notification list";
00123       throw s3_generic_exception("s3_subscribable_fifo::"
00124              "unsubscribe_consumer", str.str());
00125    }
00126    // remove from list
00127    consumers.erase(std::find(consumers.begin(), consumers.end(), &sem));
00128    notification_lock.unlock();
00129 }
00130 
00131 //
00132 bool s3_subscribable_fifo::is_subscribed_consumer(const s3_semaphore& sem)
00133 {
00134    bool retval;
00135    notification_lock.lock();
00136    retval = std::find(consumers.begin(), consumers.end(), &sem) !=
00137        consumers.end();
00138    notification_lock.unlock();
00139    return retval;
00140 }
00141 
00142 
00143 //
00144 void s3_subscribable_fifo::notify_producers() const
00145 {
00146    notification_lock.lock();   
00147    for (T_ConstIterator ptr = producers.begin(); 
00148    ptr != producers.end();
00149    ptr++)
00150    {
00151       (*ptr)->post();
00152    }
00153    notification_lock.unlock();      
00154 }
00155 
00156 //
00157 void s3_subscribable_fifo::notify_consumers() const
00158 {
00159    notification_lock.lock();   
00160    for (T_ConstIterator ptr = consumers.begin(); 
00161    ptr != consumers.end();
00162    ptr++)
00163    {
00164       (*ptr)->post();
00165    }
00166    notification_lock.unlock();      
00167 }

Send comments to: s3fc@stonethree.com SourceForge Logo