00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00044 if ( std::find(producers.begin(), producers.end(), &sem) != producers.end() )
00045 {
00046
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
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
00065 if ( std::find(producers.begin(), producers.end(), &sem) == producers.end() )
00066 {
00067
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
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
00096 if ( std::find(consumers.begin(), consumers.end(), &sem) != consumers.end() )
00097 {
00098
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
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
00116 if ( std::find(consumers.begin(), consumers.end(), &sem) == consumers.end() )
00117 {
00118
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
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 }