Orocos Real-Time Toolkit  2.9.0
startstop.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon May 10 19:10:29 CEST 2004 startstop.cxx
3 
4  startstop.cxx - description
5  -------------------
6  begin : Mon May 10 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
45 #include "../rtt-config.h"
46 #include <os/startstop.h>
47 #include "os/MainThread.hpp"
48 #include "os/StartStopManager.hpp"
49 #include "../internal/GlobalEngine.hpp"
50 #include "../types/GlobalsRepository.hpp"
51 #include "../types/TypekitRepository.hpp"
52 
53 #ifdef OROPKG_OS_THREAD_SCOPE
54 # include <boost/scoped_ptr.hpp>
55 # include "../extras/dev/DigitalOutInterface.hpp"
56  using namespace RTT;
57 #endif
58 
59 #ifdef OS_HAVE_IOSTREAM
60 #include <iostream>
61 using namespace std;
62 #else
63 #include <cstdio>
64 #endif
65 
66 #include "../Logger.hpp"
67 #include "TimeService.hpp"
68 
69 using namespace RTT;
70 using namespace RTT::os;
71 
72 static int os_argc_arg;
73 static char** os_argv_arg;
74 
75 int __os_init(int argc, char** argv )
76 {
77 #ifdef OS_HAVE_MANUAL_CRT
78  DO_GLOBAL_CTORS();
79 #endif
80 
81  os_argc_arg = argc;
82  os_argv_arg = argv;
83 
85  Logger::log() << Logger::Debug << "MainThread started." << Logger::endl;
86 
87  Logger::log() << Logger::Debug << "Starting StartStopManager." << Logger::endl;
88  int ret = os::StartStopManager::Instance()->start() ? 0 : 1;
89 
90 #ifdef OROPKG_OS_THREAD_SCOPE
91  unsigned int bit = 0;
92 
93  boost::scoped_ptr<DigitalOutInterface> pp;
94  DigitalOutInterface* d = 0;
95  // this is the device users can use across all threads to control the
96  // scope's output.
97  if ( DigitalOutInterface::nameserver.getObject("ThreadScope") )
98  d = DigitalOutInterface::nameserver.getObject("ThreadScope");
99  else
100  Logger::log() << Logger::Error<< "Failed to find 'ThreadScope' object in DigitalOutInterface::nameserver." << Logger::endl;
101  if ( d ) {
102  Logger::log() << Logger::Info << "ThreadScope : main thread toggles bit "<< bit << Logger::endl;
103  d->switchOn( bit );
104  }
105 #endif
106  return ret;
107 }
108 
109 int __os_main_argc(void) {
110  return os_argc_arg;
111 }
112 char** __os_main_argv(void) {
113  return os_argv_arg;
114 }
115 
116 void __os_printFailure(const char* prog)
117 {
118 #ifdef OS_HAVE_IOSTREAM
119  cerr <<endl<< " Orocos has detected an uncaught C++ exception"<<endl;
120  cerr << " in the ORO_main() function."<<endl;
121  cerr << " You might have called a function which throws"<<endl;
122  cerr << " without a try {} catch {} block."<< endl << endl;
123  cerr << "To Debug this situation, issue the following command:"<<endl<<endl;
124  cerr << " valgrind -v --num-callers=16 "<< prog <<" [options...] --nocatch" << endl;
125  cerr << "Which will show where the exception occured."<<endl;
126  cerr << " ( Change num-callers for more/less detail."<<endl;
127  cerr << " Also, compiling orocos and your program with"<<endl;
128  cerr << " -g adds more usefull information. )"<<endl<<endl;
129 #else
130  printf("Orocos intercepted an uncaught C++ exception\n");
131 #endif
132 
133 }
134 
135 void __os_printException(const char* prog, std::exception& arg)
136 {
137 #ifdef OS_HAVE_IOSTREAM
138  cerr << endl<<" Caught std::exception." << endl << " what(): " << arg.what() <<endl;
139 #endif
140  __os_printFailure(prog);
141 }
142 
143 const char* oro_catchflag = "--nocatch";
144 const char* oro_catchflag2 = "--no-catch";
145 
146 int __os_checkException(int& argc, char** argv)
147 {
148  bool dotry = true;
149  // look for --nocatch/--no-catch flag :
150  for( int i=1; i < argc; ++i)
151  if ( strncmp(oro_catchflag, argv[i], strlen(oro_catchflag) ) == 0
152  || strncmp(oro_catchflag2, argv[i], strlen(oro_catchflag2) ) == 0 ) {
153  // if --no-catch was given last, remove it from the argc.
154  if ( i == argc-1)
155  --argc;
156  dotry = false;
157  }
158 
159  return dotry;
160 }
161 
162 void __os_exit(void)
163 {
164 #ifdef OROPKG_OS_THREAD_SCOPE
165  if (d)
166  d->switchOff(bit);
167 #endif
168 
170 
172 
174 
176 
177  Logger::log() << Logger::Debug << "Stopping StartStopManager." << Logger::endl;
180 
181  // This should be the (one but) last message to be logged :
182  Logger::log() << Logger::Debug << "Stopping MainThread." << Logger::endl;
183 
184  // Stop logging
185  Logger::Release();
186 
187  // Stop TimeService if present.
189 
190  // Stop Main Thread
192 
193 #ifdef OS_HAVE_MANUAL_CRT
194  DO_GLOBAL_DTORS();
195 #endif
196 }
static NameServer< DigitalOutInterface * > nameserver
The NameServer of this interface.
static ThreadInterface * Instance()
Return an object which represents the main thread.
Definition: MainThread.cpp:58
Prototype definitions of common os init/exit calls.
void __os_printException(const char *prog, std::exception &arg)
Internal: Inform the user that a failure occured.
Definition: startstop.cpp:135
int __os_checkException(int &argc, char **argv)
Internal: Check if the user requests disabling exception catching at run-time.
Definition: startstop.cpp:146
int __os_init(int argc, char **argv)
Call this function from within main() before using the RTT services.
Definition: startstop.cpp:75
static void Release()
Removes all Typekits and transports from memory.
STL namespace.
bool start()
Call all registered start functions.
static bool Release()
Releases the TimeService Reference counting might aid in making this call safe.
Definition: TimeService.cpp:52
int __os_main_argc(void)
Call this function to find out how many arguments were passed to the process&#39; main() function...
Definition: startstop.cpp:109
static StartStopManager * Instance()
virtual void switchOn(unsigned int n)=0
Sets the n&#39;th output on.
basic_ostreams & endl(basic_ostreams &s)
Flush and newline.
Definition: rtstreams.cpp:110
void __os_exit(void)
Call this function from within main() after using the RTT services, just before returning.
Definition: startstop.cpp:162
char ** __os_main_argv(void)
Call this function to get the list of arguments of this process&#39; main() function. ...
Definition: startstop.cpp:112
OS Abstractions such as Mutexes, Semaphores and Threads.
Definition: Atomic.hpp:45
static void Release()
This is called to cleanup the main thread.
Definition: MainThread.cpp:66
static void Release()
Frees all globals.
const char * oro_catchflag2
Definition: startstop.cpp:144
void __os_printFailure(const char *prog)
Internal: Inform the user that a failure occured.
Definition: startstop.cpp:116
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
ValueType getObject(const NameType &s) const
Get the object registered for a name.
Definition: NameServer.hpp:136
static Logger & log()
As Instance(), but more userfriendly.
Definition: Logger.cpp:117
static void Release()
Delete the singleton logger.
Definition: Logger.cpp:82
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
void stop()
Call all registered stop functions.
static RTT_API void Release()
A class representing a Digital Output device which can read or write a maximum of 32 bits at once...
const char * oro_catchflag
Definition: startstop.cpp:143