summaryrefslogtreecommitdiffstats
path: root/Master/Masterarbeit/src/XorayaPluginExecutor/main.cpp
blob: c2e131ce7469dd80866bb5694957c9cbaee18432 (plain)
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
/*!
 * \file main.cpp
 * \author S. Eisenhauer
 * \date 31.08.2011
 * \brief Application startup
 */
 
/*!
 *	\mainpage XORAYA Plugin executor
 *	This is the main application running on the XORAYA connect to integrate with CanEasy.
*/
 
#include <errno.h>
#include <sys/mman.h>
#include <string.h>
#include <boost/thread.hpp>
#include <boost/bind.hpp>

#include "CPluginExecutor.h"
#include "global.h"

/*!
* \brief Allocates a safe stack
*/
void prefault_stack()
{
    unsigned char aucDummy[nMAX_SAFE_STACK];

    memset(aucDummy, 0, nMAX_SAFE_STACK);
    return;
}

/*!
* \brief Prevents memory of this process to be swapped to disc
* \warning process needs root privileges for this to work correctly
*/
void lockmem()
{
	int res = 0;
	if( (res = mlockall(MCL_CURRENT|MCL_FUTURE)) != 0)
	{
		ERROR_PRINT("mlockall failed: %d errno: %d",res,errno);
		//exit(-2);
	}
}

/*!
* \brief initialization code
*/
void init()
{
	lockmem();
	prefault_stack();
}

/*!
* \brief application starts here
* \param[in] argc number of arguments passed to process
* \param[in] argv the arguments
* \return exit code, 0 on success
*/
int main(int argc, char* argv[])
{
	int res = 0;
	int schedulerPolicy;
	sched_param sp;

	init();

	static CPluginExecutor xPluginExec;
	boost::thread xExecThread(
			boost::function<void()>(
					boost::bind(&CPluginExecutor::vRun,boost::ref(xPluginExec))
			)
	);

	if( argc == 2)
	{
		if( strcmp(argv[1],"RT") == 0 )
		{
			schedulerPolicy = SCHED_FIFO;
			sp.sched_priority = tenPrio::nenRT ; // rt priority
			if(pthread_setschedparam(xExecThread.native_handle(), schedulerPolicy, &sp))
			{
				ERROR_PRINT("pthread_setschedparam failed. Errno: %d",errno);
			}
		}
	}

	if( (res = pthread_getschedparam(xExecThread.native_handle(),&schedulerPolicy,&sp)) != 0 )
	{
		ERROR_PRINT("pthread_getschedparam failed: %d errno: %d",res,errno);
	}
	else
	{
		DEBUG_PRINT("executor thread prio: %d scheduler: %s"
				,sp.sched_priority
				,(schedulerPolicy==SCHED_OTHER)?"SCHED_OTHER":
						(schedulerPolicy==SCHED_FIFO)?"SCHED_FIFO":
								(schedulerPolicy==SCHED_RR)?"SCHED_RR":"other");
	}

	xExecThread.join();
	DEBUG_PRINT("exiting");
	return 0;
}