File: dintrthread.cpp

package info (click to toggle)
tango 9.2.5a%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 21,624 kB
  • ctags: 11,597
  • sloc: cpp: 135,480; sh: 21,772; makefile: 1,103; ansic: 1,083; java: 215; python: 55
file content (265 lines) | stat: -rw-r--r-- 6,786 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
static const char *RcsId = "$Id: dintrthread.cpp 28219 2015-07-06 06:09:46Z taurel $";

//+=================================================================================================================
//
// file :               dintrthread.cpp
//
// description :        C++ source code for the DevIntrThread class. This class is used for the device interface
//						change thread. The rule of this thread is to delay the event sending in order to minimize
//						the number of events thrown when dynamic command(s) and/or attribute(s) are added/removed
//						in a loop.
//
// project :            TANGO
//
// author(s) :          E.Taurel
//
// Copyright (C) :      2014
//						European Synchrotron Radiation Facility
//                      BP 220, Grenoble 38043
//                      FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tango is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Tango.
// If not, see <http://www.gnu.org/licenses/>.
//
// $Revision: 28219 $
//
//-================================================================================================================

#if HAVE_CONFIG_H
#include <ac_config.h>
#endif

#include <tango.h>
#include <dintrthread.h>
#include <eventsupplier.h>

namespace Tango
{

//+------------------------------------------------------------------------------------------------------------------
//
// method :
//		DevIntrThread::DevIntrThread
//
// description :
//		The device interface change thread constructor.
//
//-------------------------------------------------------------------------------------------------------------------

DevIntrThread::DevIntrThread(ShDevIntrTh &cmd,TangoMonitor &m,DeviceImpl *_d):
shared_data(cmd),p_mon(m),dev(_d)
{
	shared_data.cmd_pending = false;
}

//+-----------------------------------------------------------------------------------------------------------------
//
// method :
//		DevIntrThread::run_undetached
//
// description :
//		The device interface change thread main code
//
//------------------------------------------------------------------------------------------------------------------

void DevIntrThread::run(TANGO_UNUSED(void *ptr))
{

//
// The main loop
//

	bool exit = false;

	try
	{
		while (exit == false)
		{
			DevIntrCmdType received = get_command(DEV_INTR_THREAD_SLEEP_TIME);

			switch (received)
			{
			case DEV_INTR_COMMAND:
				execute_cmd();
				break;

			case DEV_INTR_TIME_OUT:
			    try
			    {
                    push_event();
			    }
			    catch (Tango::DevFailed &e)
			    {
			        cerr << "Received a DevFailed exception while trying to push the device interface change event!!!" << endl;
			        cerr << e.errors[0].desc.in() << endl;
			    }
				exit = true;
				break;
			}
		}

		{
			omni_mutex_lock sync(p_mon);
			shared_data.th_running = false;
		}
		omni_thread::exit();
	}
	catch (omni_thread_fatal &)
	{
		cerr << "OUPS !! A omni thread fatal exception in a device interface change event thread!!!!!!!!" << endl;
	}

}

//+-----------------------------------------------------------------------------------------------------------------
//
// method :
//		DevIntrThread::get_command
//
// description :
//		This method wait on the shared monitor for a new command to be sent to the device interface change thread.
//		The thread waits with a timeout.
//
// argument :
//		in :
//			- tout : The timeout in mS
//
// return :
//		One enum with two possible value: Thread awaken due to timeout or thread awaken due to a received command
//
//------------------------------------------------------------------------------------------------------------------

DevIntrCmdType DevIntrThread::get_command(DevLong tout)
{
	omni_mutex_lock sync(p_mon);
	DevIntrCmdType ret;

//
// Wait on monitor
//

	if (shared_data.cmd_pending == false)
	{
		cout4 << "DevIntrThread:: Going to wait on monitor" << endl;
		p_mon.wait(tout);
	}

//
// Test if it is a new command. If yes, copy its data locally
//

	if (shared_data.cmd_pending == true)
	{
		local_cmd = shared_data;
		ret = DEV_INTR_COMMAND;
	}
	else
		ret = DEV_INTR_TIME_OUT;

	return ret;
}

//+------------------------------------------------------------------------------------------------------------------
//
// method :
//		DevIntrThread::execute_cmd
//
// description :
//		This method is called when a command has been received. It execute the command!
//
//------------------------------------------------------------------------------------------------------------------

void DevIntrThread::execute_cmd()
{
	vector<string>::iterator pos;
	bool need_exit = false;

	switch (local_cmd.cmd_code)
	{

//
// A new rest
//

		case Tango::DEV_INTR_SLEEP :
		{
			break;
		}

//
// Ask locking thread to exit
//

		case Tango::DEV_INTR_EXIT :
			need_exit = true;
			break;
	}

//
// Inform requesting thread that the work is done
//

	{
		omni_mutex_lock sync(p_mon);
		shared_data.cmd_pending = false;
		p_mon.signal();
	}

//
// If the command was an exit one, do it now
//

	if (need_exit == true)
	{
		{
			omni_mutex_lock sync(p_mon);
			shared_data.th_running = false;
		}
		omni_thread::exit();
	}
}

//+------------------------------------------------------------------------------------------------------------------
//
// method :
//		DevIntrThread::push_event
//
// description :
//		This method pushes the device interface change event if there is a interface change
//
//------------------------------------------------------------------------------------------------------------------

void DevIntrThread::push_event()
{
	cout4 << "Device interface change event thread pushing event!" << endl;

	AutoTangoMonitor sync(dev,true);

	if (shared_data.interface.has_changed(dev) == true)
	{
		cout4 << "Device interface has changed" << endl;

		Device_5Impl *dev_5 = static_cast<Device_5Impl *>(dev);
		DevCmdInfoList_2 *cmds_list = dev_5->command_list_query_2();

		DevVarStringArray dvsa(1);
		dvsa.length(1);
		dvsa[0] = Tango::string_dup(AllAttr_3);
		AttributeConfigList_5 *atts_list = dev_5->get_attribute_config_5(dvsa);

		ZmqEventSupplier *event_supplier_zmq = Util::instance()->get_zmq_event_supplier();
		event_supplier_zmq->push_dev_intr_change_event(dev,false,cmds_list,atts_list);
	}
}

} // End of Tango namespace