FifoBase-win32.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:9k
- /* ====================================================================
- * The Vovida Software License, Version 1.0
- *
- * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The names "VOCAL", "Vovida Open Communication Application Library",
- * and "Vovida Open Communication Application Library (VOCAL)" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact vocal@vovida.org.
- *
- * 4. Products derived from this software may not be called "VOCAL", nor
- * may "VOCAL" appear in their name, without prior written
- * permission of Vovida Networks, Inc.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
- * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
- * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * ====================================================================
- *
- * This software consists of voluntary contributions made by Vovida
- * Networks, Inc. and many individuals on behalf of Vovida Networks,
- * Inc. For more information on Vovida Networks, Inc., please see
- * <http://www.vovida.org/>.
- *
- */
- #ifdef WIN32
- static const char* const FifoBase_win32_cxx_Version =
- "$Id: FifoBase-win32.cxx,v 1.2 2001/06/29 20:45:34 bko Exp $";
- #include "Lock.hxx"
- //#include "FifoBase.h"
- using namespace Vocal;
- using namespace Vocal::TimeAndDate;
- using Process::Lock;
- template < class Msg >
- FifoBase < Msg > ::FifoBase()
- : shutdown_(false)
- {}
- template < class Msg >
- FifoBase < Msg > ::~FifoBase()
- {}
- template < class Msg >
- void
- FifoBase < Msg > ::add(const Msg & msg)
- {
- Lock lock(mutex_);
- (void)lock;
- if ( shutdown_ )
- {
- return ;
- }
- fifo_.push_back(new Msg(msg));
- try
- {
- wakeup();
- }
- catch ( VException & )
- {
- assert(0);
- }
- }
- template < class Msg >
- FifoBase < Msg > ::EventId
- FifoBase < Msg > ::addDelayMs(
- const Msg & msg,
- const milliseconds_t relativeTimeout__
- )
- {
- Lock lock(mutex_);
- (void)lock;
- if ( shutdown_ )
- {
- return ( 0 );
- }
- milliseconds_t relativeTimeout = relativeTimeout__;
- if ( relativeTimeout < 0 )
- {
- relativeTimeout = 0;
- }
- TimerEntryId newId = timer_.add(new Msg(msg), relativeTimeout);
- TimerEntryId firstId = timer_.getFirstTimerEntryId();
- // If we insert the new message at the front of the timer list,
- // we need to wakeup wait() since we have a timed message that
- // expires sooner than the current, or if we didn't have a timer
- // to begin with.
- //
- if ( firstId == newId )
- {
- try
- {
- wakeup();
- }
- catch ( VException & )
- {
- assert(0);
- }
- }
- return ( newId );
- }
- template < class Msg >
- void
- FifoBase < Msg > ::cancel(EventId id)
- {
- if ( id == 0 )
- {
- return ;
- }
- Lock lock(mutex_);
- (void)lock;
- if ( !timer_.cancel(id) )
- {
- // If the timer didn't hold the message to cancel, walk through
- // the queue and see if we have it there.
- //
- for ( MessageContainer::iterator it = fifo_.begin();
- it != fifo_.end();
- it++
- )
- {
- // Assertion: The id for a timed event is just the
- // address of the memory for the message. If this isn't
- // true in TimerEntry, then we have problems.
- //
- if ( id == (EventId)((*it).operator->()) )
- {
- fifo_.erase(it);
- return ;
- }
- }
- }
- }
- template < class Msg >
- int
- FifoBase < Msg > ::block(milliseconds_t relativeTimeout)
- throw ( VException )
- {
- Lock lock(mutex_);
- (void)lock;
- if ( messageAvailableNoLock() )
- {
- return ( 1 );
- }
- return ( blockNoLock(relativeTimeout) );
- }
- template < class Msg >
- Msg
- FifoBase < Msg > ::getNext()
- throw ( VException )
- {
- Lock lock(mutex_);
- (void)lock;
- // Wait while there are no messages available.
- //
- while ( !messageAvailableNoLock() )
- {
- blockNoLock();
- }
- // Move expired timers into fifo.
- //
- while ( timer_.messageAvailable() )
- {
- try
- {
- fifo_.push_back(timer_.getMessage());
- }
- catch ( ... )
- {
- assert(0);
- }
- }
- // Return the first message on the fifo.
- //
- Msg firstMessage = *(fifo_.front());
- fifo_.pop_front();
- return ( firstMessage );
- }
- template < class Msg >
- unsigned int
- FifoBase < Msg > ::size() const
- {
- Lock lock(mutex_);
- (void)lock;
- return ( fifo_.size() + timer_.size() );
- }
- template < class Msg >
- bool
- FifoBase < Msg > ::messageAvailable()
- {
- Lock lock(mutex_);
- (void)lock;
- return ( messageAvailableNoLock() );
- }
- template < class Msg >
- void
- FifoBase < Msg > ::shutdown()
- {
- Lock lock(mutex_);
- (void)lock;
- shutdown_ = true;
- }
- template < class Msg >
- bool
- FifoBase < Msg > ::operator==(const FifoBase & src) const
- {
- // Since each oberver is unique, we can compare addresses.
- //
- return ( this == &src );
- }
- template < class Msg >
- bool
- FifoBase < Msg > ::operator!=(const FifoBase & src) const
- {
- return ( this != &src );
- }
- template < class Msg >
- bool
- FifoBase < Msg > ::operator<(const FifoBase & src) const
- {
- return ( this < &src );
- }
- template < class Msg >
- bool
- FifoBase < Msg > ::operator<=(const FifoBase & src) const
- {
- return ( this <= &src );
- }
- template < class Msg >
- bool
- FifoBase < Msg > ::operator>(const FifoBase & src) const
- {
- return ( this > &src );
- }
- template < class Msg >
- bool
- FifoBase < Msg > ::operator>=(const FifoBase & src) const
- {
- return ( this >= &src );
- }
- template < class Msg >
- bool
- FifoBase < Msg > ::messageAvailableNoLock()
- {
- return ( fifo_.size() > 0 || timer_.messageAvailable() );
- }
- template < class Msg >
- int
- FifoBase < Msg > ::blockNoLock(milliseconds_t relativeTimeout)
- throw ( VException )
- {
- // Use the shortest timeout value between the given relativeTimout
- // and the timer container's timeout, remembering that infinite timeout
- // is specified by a negative number.
- //
- milliseconds_t timerTimeout = timer_.getTimeout(),
- timeout;
- // If timerTimeout is infinite, relativeTimeout can only be the
- // same or shorter.
- //
- if ( timerTimeout < 0 )
- {
- timeout = relativeTimeout;
- }
- // If relativeTimeout is infinite, timerTimeout can only be the
- // same or shorter.
- //
- else if ( relativeTimeout < 0 )
- {
- timeout = timerTimeout;
- }
- // Both are positive timeout values. Take the shortest in duration.
- else
- {
- timeout = relativeTimeout < timerTimeout
- ? relativeTimeout
- : timerTimeout;
- }
- // Wait for an event. A timer expiry or signal will return a 0 here.
- //
- int numberActive = wait(timeout);
- if ( numberActive > 0 )
- {
- return ( numberActive );
- }
- // See if a timer has expired. If it has expired, return 1 to indicate
- // a message is available from the timer.
- //
- if ( messageAvailableNoLock() )
- {
- return ( 1 );
- }
- // To get here, either a signal woke us up, or the we used the
- // relativeTimeout value, meaning that a message isn't available from
- // the timer container.
- //
- return ( 0 );
- }
- #endif // WIN32