Disk ARchive  2.5.2
Full featured and portable backup and archiving tool
thread_cancellation.hpp
Go to the documentation of this file.
00001 /*********************************************************************/
00002 // dar - disk archive - a backup/restoration program
00003 // Copyright (C) 2002-2052 Denis Corbin
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 // to contact the author : http://dar.linux.free.fr/email.html
00020 /*********************************************************************/
00021 
00034 
00035 #ifndef THREAD_CANCELLATION_HPP
00036 #define THREAD_CANCELLATION_HPP
00037 
00038 #include "../my_config.h"
00039 
00040 extern "C"
00041 {
00042 #if MUTEX_WORKS
00043 #if HAVE_PTHREAD_H
00044 #include <pthread.h>
00045 #endif
00046 #endif
00047 }
00048 #include <list>
00049 #include <map>
00050 #include "integers.hpp"
00051 
00052 namespace libdar
00053 {
00054 
00056 
00063 
00064     class thread_cancellation
00065     {
00066     public:
00067 
00069     thread_cancellation();
00070 
00072     virtual ~thread_cancellation() throw(Ebug);
00073 
00075 
00078     void check_self_cancellation() const;
00079 
00082 
00086     void block_delayed_cancellation(bool mode);
00087 
00088 
00089 #if MUTEX_WORKS
00090 
00091 
00096     static void cancel(pthread_t tid, bool x_immediate, U_64 x_flag);
00097 
00099 
00102     static bool cancel_status(pthread_t tid);
00103 
00105 
00108     static bool clear_pending_request(pthread_t tid);
00109 
00118     static void associate_tid_to_tid(pthread_t src, pthread_t dst);
00119 
00124     static void remove_association_for_tid(pthread_t src);
00125 
00129     static void remove_association_targeted_at(pthread_t dst);
00130 
00132     static void dead_thread(pthread_t tid);
00133 #endif
00134 
00136     static U_I count()
00137     {
00138 #if MUTEX_WORKS
00139         return info.size();
00140 #else
00141         return 0;
00142 #endif
00143     };
00144 
00145 #if MUTEX_WORKS
00146     private:
00147 
00148         // class types
00149 
00150     struct fields
00151     {
00152         pthread_t tid;             
00153         bool block_delayed;        
00154         bool immediate;            
00155         bool cancellation;         
00156         U_64 flag;                 
00157     };
00158 
00159         // object information
00160 
00161     fields status;
00162 
00163         // class's static variables and types
00164 
00165     static pthread_mutex_t access;                 //< mutex for the access to "info"
00166     static std::list<thread_cancellation *> info;  //< list of all object
00167     static std::list<fields> preborn;              //< canceled thread that still not have a thread_cancellation object to deal with cancellation
00168     static std::multimap<pthread_t, pthread_t> thread_asso; //< which other thread to propagate cancellation request to, given a initial tid
00169 
00170         // helper class routing
00171     static void set_cancellation_in_info_for(pthread_t tid,
00172                          bool cancel_status,
00173                          bool x_immediate,
00174                          U_64 x_flag,
00175                          bool & found,
00176                          bool & previous_val,
00177                          bool & bug);
00178     static void add_to_preborn(pthread_t tid, bool x_immediate, U_64 x_flag);
00179     static void remove_from_preborn(pthread_t tid, bool & found, bool & prev);
00180     static void find_asso_tid_with(pthread_t tid,
00181                        std::multimap<pthread_t, pthread_t>::iterator & begin,
00182                        std::multimap<pthread_t, pthread_t>::iterator & end);
00183 
00184 #endif
00185     };
00186 
00187 } // end of namespace
00188 
00189 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines