S3FC project page S3FC home page

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

s3_thread_base.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 
00034 #ifndef S3_THREAD_BASE_H
00035 #define S3_THREAD_BASE_H
00036 
00037 // This needs to be defined to ensure thread safety.
00038 #define _REENTRANT 1
00039 
00040 
00041 #ifdef _WIN32
00042 // Define _MT as suggested in Win32 System Programming 2ed, p 214
00043 #define _MT
00044 #endif
00045 
00046 #include <s3fc/s3_exception.h>
00047 #include <s3fc/s3_mutex.h>
00048 #include <s3fc/s3_semaphore.h>
00049 
00050 #define THREAD_BASE_NEW_INTERFACE 1
00051 
00122 class s3_thread_base
00123 {
00124   public:
00125    typedef pthread_t s3_thread_id;
00126    // invalid thread id (our choice)
00127    static const pthread_t invalid_thread_id;
00128   protected:
00129    // true if thread has been started
00130    bool started;
00131    // true if thread is to be started as a detached process
00132    bool detached;
00133    // pthread ID and attributes
00134    pthread_t thread_id;
00135    pthread_attr_t thread_attr;
00136 
00140    s3_mutex state_lock;
00144    s3_semaphore state_changed;
00145    // terminate request flag and lock
00146    bool term_request;
00147    s3_mutex term_request_lock;
00148    
00149   public:
00154    s3_thread_base();
00165    virtual ~s3_thread_base();
00173    virtual void main_loop() = 0;
00179    s3_thread_id get_thread_id() const;
00186    void set_detached(bool n_detach);
00193    bool is_detached() const;
00201    void set_sched_round_robin(unsigned int priority);
00210    void set_sched_fifo(unsigned int priority);
00211 
00217    void set_sched_normal(unsigned int priority);
00218 
00224    bool is_running() const;
00233    void start();
00244    void stop();
00248    static void test_cancel();
00254    void wait_on_exit();
00261    void lock_state() const;
00268    void unlock_state() const;
00274    bool test_state() const;
00284    void signal_state_changed() const;
00296    void request_terminate();
00297    
00298 protected:
00307    void wait_on_state_changed() const;
00314    virtual void cleanup();
00323    static void* pthread_create_ccpp_wrapper(void* obj);
00330    static void pthread_cleanup_ccpp_wrapper(void* obj);
00342    bool test_terminate();
00343 };
00344 
00345 #endif

Send comments to: s3fc@stonethree.com SourceForge Logo