S3FC project page S3FC home page

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

s3_periodic_notifier.h

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 
00037 #ifndef S3_PERIODIC_NOTIFIER_H
00038 #define S3_PERIODIC_NOTIFIER_H
00039 
00080 #include <s3fc/s3_thread_base.h>
00081 #include <s3fc/s3_mutex.h>
00082 #include <s3fc/s3_semaphore.h>
00083 #include <map>
00084 #include <list>
00085 
00086 class s3_periodic_notifier 
00087 {
00088   public:
00092    typedef unsigned int period_t;
00093   protected:
00098    class event_loop : protected s3_thread_base
00099    {
00100      private:
00101       typedef s3_periodic_notifier* notifier_id_t;
00102       typedef std::map<s3_periodic_notifier*, period_t> list_t;
00103       // each connected client has an entry here - indexed on client ptr
00104       std::map<s3_periodic_notifier*, period_t> client_list;
00105       s3_mutex state_lock;
00106      public:
00107       // default constructor - start the main loop
00108       event_loop();
00109       // destructor - stop the main loop and wait for it to terminate
00110       ~event_loop();
00111       // add client to client_list
00112       // @pre is_client == false
00113       void add_client(s3_periodic_notifier* n_id);
00114       // remove client from client_list
00115       // @pre is_client == true
00116       void remove_client(s3_periodic_notifier* n_id);
00117       // @pre is_client == true
00118       void set_period(s3_periodic_notifier* id, 
00119             const period_t& n_period);
00120       // return true if there are clients in the client_list
00121       bool clients() const;
00122      private:
00123       // true if n_id is in client_list
00124       // @note This should always be called with the state_lock held.
00125       bool is_client(s3_periodic_notifier* n_id);
00126       // 
00127       virtual void main_loop();
00128    };
00129    friend class event_loop;
00135    static event_loop* ev_loop;
00139    static s3_mutex ev_loop_lock;
00143    period_t period_ms;
00147    bool enabled;
00151    std::list<s3_semaphore*> notification_list;
00155    s3_mutex state_lock;
00156   public:
00163    s3_periodic_notifier(period_t n_period_ms = 1);
00168    ~s3_periodic_notifier();
00172    void enable();
00176    void disable();
00180    void set_period(period_t n_period_ms);
00185    void subscribe(s3_semaphore& sem);
00190    void unsubscribe(s3_semaphore& sem);
00191   protected:
00197    bool is_subscribed(s3_semaphore& sem);
00206    void* main_loop(void*);
00212    void notify();
00213 };
00214 
00215 
00216 #endif

Send comments to: s3fc@stonethree.com SourceForge Logo