1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
22     /// \file generic_thread.hpp
23     /// \brief class generic_thread provides a way to interact with a generic_file ran in an other thread
24     /// \ingroup Private
25     ///
26 
27 
28 #ifndef GENERIC_THREAD_HPP
29 #define GENERIC_THREAD_HPP
30 
31 #include "../my_config.h"
32 
33 #include "generic_file.hpp"
34 #include "messaging.hpp"
35 #include "slave_thread.hpp"
36 #include "erreurs.hpp"
37 
38 namespace libdar
39 {
40 
41 
42     class generic_thread : public generic_file
43     {
44     public:
45 	    // default values for constuctor
46 	static const unsigned int tampon_block_size = 102401;
47 	static const unsigned int tampon_num_block = 1000;
48 	static const unsigned int tampon_block_size_ctrl = 1024;
49 	static const unsigned int tampon_num_block_ctrl = 10;
50 
51 	    /// constructor
52 	    ///
53 	    /// \param[in] ptr is the generic_file that will be read from/written to by a separated thread
54 	    /// \param[in] block_size is the size of block used to pass information to and from the remote thread
55 	    /// \param[in] num_block maximum number of blocks that can be sent without being retrieved by the other threads
56 	    /// \note that the pointed to generic_file must exist during the whole life of the generic_thread. Its memory
57 	    /// management after the generic_thread has died is under the responsibility of the caller of the generic_thread
58 	generic_thread(generic_file *ptr,
59 		       U_I data_block_size = tampon_block_size,
60 		       U_I data_num_block = tampon_num_block,
61 		       U_I ctrl_block_size = tampon_block_size_ctrl,
62 		       U_I ctrl_num_block = tampon_num_block_ctrl);
63 	generic_thread(const generic_thread & ref); //< copy constructor is disabled (throws exception)
operator =(const generic_thread & ref)64 	const generic_thread & operator = (const generic_thread & ref) { throw SRC_BUG; };
65 	virtual ~generic_thread();
66 
67 	    // inherited methods from generic_file
68 
69 	virtual bool skippable(skippability direction, const infinint & amount);
70 	virtual bool skip(const infinint & pos);
71 	virtual bool skip_to_eof();
72 	virtual bool skip_relative(S_I x);
73 	virtual infinint get_position() const;
74 
75     protected:
76 	    // inherited from generic_file
77 	virtual void inherited_read_ahead(const infinint & amount);
78 	virtual U_I inherited_read(char *a, U_I size);
79 	virtual void inherited_write(const char *a, U_I size);
80 
81 	    /// generic_file inherited method to sync all pending writes
82 	    ///
83 	    /// \note no data in transit in this object so we should not do anything,
84 	    /// however for performance propagating the order to slave_thread
85 	virtual void inherited_sync_write();
86 	virtual void inherited_flush_read();
87 	virtual void inherited_terminate();
88 
89 
90     private:
91 	libthreadar::fast_tampon<char> toslave_data;
92 	libthreadar::fast_tampon<char> tomaster_data;
93 	libthreadar::fast_tampon<char> toslave_ctrl;
94 	libthreadar::fast_tampon<char> tomaster_ctrl;
95 	slave_thread *remote;
96 	bool reached_eof;     //< whether we reached end of file
97 	char data_header;     //< contains 1 byte header for data
98 	char data_header_eof; //< contains 1 byte header for data + eof
99 	bool running;         //< whether a remote is expected to run, tid is then set
100 	pthread_t tid;        //< thread id of remote
101 
102 	    // the following variables are locally used in quite all methods
103 	    // they do not contain valuable information outside each method call
104 	messaging_encode order;
105 	messaging_decode answer;
106 	unsigned int num;
107 	char *ptr;
108 	label dataname;
109 
110 	void send_order();
111 	void read_answer();   //< \note ptr must be released/recycled after this call
112 	void check_answer(msg_type expected);
113 	void wake_up_slave_if_asked(); //< check whether an order to wakeup the slave has been received, and send wake up the slave
release_block_answer()114 	void release_block_answer() { tomaster_ctrl.fetch_recycle(ptr); ptr = nullptr; };
115 	void release_data_ptr();
116 	void purge_data_pipe(); //< drops all data in the toslave_data pipe
117 	void my_run();
118 	void my_join();
119     };
120 
121 } // end of namespace
122 
123 #endif
124