1 //======================================================================
2 //	global_amp.cxx
3 //
4 //  Author(s):
5 //	Robert Stiles, KK5VD, Copyright (C) 2014
6 //
7 // This file is part of FLAMP.
8 //
9 // This is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This software is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 //
22 // =====================================================================
23 
24 #ifndef __flamp_global_amp__
25 #define __flamp_global_amp__
26 
27 //! Use to sync class cAmp global variables via a threaded mutex lock access.
28 class cAmpGlobal {
29 
30 protected:
31 	cAmp *amp;                 //!< @brief Current selected receive cAmp pointer
32 	std::vector<cAmp *> amp_array;
33 	pthread_mutex_t mutex_amp; //!< @brief Mutex locks for pointer cAmp pointer access
34 	int locked;
35 
36 public:
37 	cAmpGlobal();
38 	~cAmpGlobal();
39 
is_locked(void)40 	bool is_locked(void) { if(locked) return true; return false; }
lock_count(void)41 	int lock_count(void) { return locked; }
42 
43 	//! Access method for transmit cAmp pointer
44 	//! @param none (void)
45 	//! @return cAmp class pointer for current selected tx queue item.
46 	cAmp *get_amp(void);
47 	int get_index(void);
48 
49 	int amp2index(cAmp *amp);
50 	cAmp *index2amp(int pos);
51 
52 	//! Access method for transmit cAmp pointer.
53 	//! @param Set current selected TX cAmp ponter for global access.
54 	//! @return bool false if cAmp pointer NULL.
55 	bool set(cAmp *amp);
56 	bool set(int pos);
57 
58 	//! Free allocaled cAmp memory.
59 	//! @param The cAmp pointer to be freed.
60 	//! @return none (void)
61 	void free(cAmp *amp);
62 	void free_all(void);
63 
64 	bool add(cAmp *amp);
65 	bool remove(cAmp *amp);
66 	bool remove(int pos);
67 	size_t size(void);
count(void)68 	size_t count(void) { return size(); }
69 
lock()70 	void lock() {
71 		locked++;
72 		pthread_mutex_lock(&mutex_amp);
73 	}
74 
unlock()75 	void unlock() {
76 		locked--;
77 		pthread_mutex_unlock(&mutex_amp);
78 	}
79 };
80 
81 
82 #endif /* defined(__flamp_global_amp__) */
83