log4cplus  2.0.0
queue.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //  Copyright (C) 2009-2015, Vaclav Haisman. All rights reserved.
00003 //
00004 //  Redistribution and use in source and binary forms, with or without modifica-
00005 //  tion, are permitted provided that the following conditions are met:
00006 //
00007 //  1. Redistributions of  source code must  retain the above copyright  notice,
00008 //     this list of conditions and the following disclaimer.
00009 //
00010 //  2. Redistributions in binary form must reproduce the above copyright notice,
00011 //     this list of conditions and the following disclaimer in the documentation
00012 //     and/or other materials provided with the distribution.
00013 //
00014 //  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00015 //  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00016 //  FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
00017 //  APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
00018 //  INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
00019 //  DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
00020 //  OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
00021 //  ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
00022 //  (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
00023 //  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024 
00025 #ifndef LOG4CPLUS_HELPERS_QUEUE_H
00026 #define LOG4CPLUS_HELPERS_QUEUE_H
00027 
00028 #include <log4cplus/config.hxx>
00029 
00030 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
00031 #pragma once
00032 #endif
00033 
00034 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
00035 
00036 #include <deque>
00037 #include <log4cplus/spi/loggingevent.h>
00038 #include <log4cplus/thread/threads.h>
00039 #include <log4cplus/thread/syncprims.h>
00040 
00041 
00042 namespace log4cplus { namespace thread {
00043 
00044 
00046 class LOG4CPLUS_EXPORT Queue
00047     : public virtual helpers::SharedObject
00048 {
00049 public:
00051     typedef unsigned flags_type;
00052 
00054     typedef std::deque<spi::InternalLoggingEvent> queue_storage_type;
00055 
00056     explicit Queue (unsigned len = 100);
00057     virtual ~Queue ();
00058 
00059     // Producers' methods.
00060 
00072     flags_type put_event (spi::InternalLoggingEvent const & ev);
00073 
00078     flags_type signal_exit (bool drain = true);
00079 
00080     // Consumer's methods.
00081 
00099     flags_type get_events (queue_storage_type * buf);
00100 
00102     enum Flags
00103     {
00106         EVENT       = 0x0001,
00107 
00110         QUEUE       = 0x0002,
00111 
00114         EXIT        = 0x0004,
00115 
00118         DRAIN       = 0x0008,
00119 
00121         ERROR_BIT   = 0x0010,
00122 
00125         ERROR_AFTER = 0x0020
00126     };
00127 
00128 protected:
00130     queue_storage_type queue;
00131 
00133     Mutex mutex;
00134 
00136     ManualResetEvent ev_consumer;
00137 
00139     Semaphore sem;
00140 
00142     flags_type flags;
00143 
00144 private:
00145     Queue (Queue const &);
00146     Queue & operator = (Queue const &);
00147 };
00148 
00149 
00150 typedef helpers::SharedObjectPtr<Queue> QueuePtr;
00151 
00152 
00153 } } // namespace log4cplus { namespace thread {
00154 
00155 
00156 #endif // LOG4CPLUS_SINGLE_THREADED
00157 
00158 #endif // LOG4CPLUS_HELPERS_QUEUE_H