OpendTect-6_4  6.4
_execbatch.h
Go to the documentation of this file.
1 #ifndef _execbatch_h
2 #define _execbatch_h
3 
4 /*
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: A.H. Lammertink
9  Date: 30-10-2003
10  RCS: $Id$
11 ________________________________________________________________________
12 
13  The implementation of Execute_batch should be in the executable on
14  windows, but can be in a .so on *nix.
15  In order not to pollute batchprog.h, I've placed the implementation
16  into a separate file, which is included trough batchprog.h on win32
17  and included in batchprog.cc on *nix.
18 
19 */
20 
21 #include "commandlineparser.h"
22 #include "envvars.h"
23 #include "od_ostream.h"
24 #include "oddirs.h"
25 #include "oscommand.h"
26 #include "strmprov.h"
27 #include "hostdata.h"
28 
29 #ifdef __win__
30 # include <tchar.h>
31 # include <tlhelp32.h>
32 # include <Windows.h>
33 #else
34 # include "sys/resource.h"
35 #endif
36 
37 
38 #ifdef __win__
39 static void setBatchPriority( int argc, char** argv )
40 #else
41 static void setBatchPriority( int argc, char** argv, int pid )
42 #endif
43 {
44  const CommandLineParser clp( argc, argv );
45  float priority = mUdf(float);
46  clp.getVal( "priority", priority );
47 #ifdef __unix__
48  if ( mIsUdf(priority) )
49  {
50  int nicelvl = mUdf(int);
51  if ( !clp.getVal("nice",nicelvl) )
52  return;
53 
54  setpriority( PRIO_PROCESS, pid, nicelvl );
55  }
56  else
57  {
58  const int machprio =
59  OS::CommandExecPars::getMachinePriority( priority, false );
60  setpriority( PRIO_PROCESS, pid, machprio );
61  }
62 #else
63  if ( mIsUdf(priority) )
64  return;
65 
66  const int machprio =
68  const DWORD threadpriority =
69  machprio == 8 ? THREAD_PRIORITY_NORMAL
70  : ( machprio == 7 ? THREAD_PRIORITY_BELOW_NORMAL
71  : THREAD_PRIORITY_LOWEST );
72  if ( threadpriority != THREAD_PRIORITY_NORMAL )
73  SetPriorityClass( GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS );
74 
75  HANDLE curthread = INVALID_HANDLE_VALUE;
76  THREADENTRY32 threadlist;
77 
78  const DWORD dwOwnerPID( GetCurrentProcessId() );
79  curthread = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwOwnerPID );
80  if ( curthread == INVALID_HANDLE_VALUE )
81  return;
82 
83  threadlist.dwSize = sizeof(THREADENTRY32);
84  if ( !Thread32First(curthread,&threadlist) )
85  { CloseHandle(curthread); return; }
86 
87  do
88  {
89  if ( threadlist.th32OwnerProcessID != dwOwnerPID )
90  continue;
91 
92  SetThreadPriority(curthread,threadpriority );
93  } while ( Thread32Next(curthread,&threadlist) );
94 
95  CloseHandle( curthread );
96 #endif
97 }
98 
99 int Execute_batch( int* pargc, char** argv )
100 {
101  PIM().loadAuto( false );
102 
103  BP().init();
104  if ( !BP().stillok_ )
105  return 1;
106  if ( BP().inbg_ )
107  ForkProcess();
108 
109  BatchProgram& bp = BP();
110  bool allok = bp.initOutput();
111  if ( allok )
112  {
113  od_ostream logstrm( *bp.sdout_.ostrm );
114  const int pid = GetPID();
115 #ifdef __win__
116  setBatchPriority( *pargc, argv );
117 #else
118  setBatchPriority( *pargc, argv, pid );
119 #endif
120  logstrm << "Starting program: " << argv[0] << " " << bp.name()
121  << od_endl;
122  logstrm << "Processing on: " << HostData::localHostName() << od_endl;
123  logstrm << "Process ID: " << pid << od_endl;
124  allok = bp.go( logstrm );
125  }
126 
127  bp.stillok_ = allok;
128  BatchProgram::deleteInstance();
129 
130  return allok ? 0 : 1; // never reached.
131 }
132 
133 #endif
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:287
OD class for stream write common access to the user log file, or std::cout in batch progs...
Definition: od_ostream.h:26
void ForkProcess(void)
bool go(od_ostream &log_stream)
This method must be defined by user.
StreamData & sdout_
Definition: batchprog.h:98
static int getMachinePriority(float priolevel, bool iswin)
void loadAuto(bool late)
see class comments
od_ostream & od_endl(od_ostream &strm)
Definition: od_ostream.h:111
BatchProgram & BP()
Definition: commandlineparser.h:51
static void setBatchPriority(int argc, char **argv, int pid)
Definition: _execbatch.h:41
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
Main object for &#39;standard&#39; batch programs.
Definition: batchprog.h:49
int Execute_batch(int *pargc, char **argv)
Definition: _execbatch.h:99
bool getVal(const char *key, BufferString &, bool acceptnone=false, int valnr=1) const
int GetPID(void)
std::ostream * ostrm
Definition: strmdata.h:44
bool stillok_
Definition: batchprog.h:96
PluginManager & PIM()
virtual const OD::String & name() const
Definition: namedobj.h:47
static const char * localHostName()
shortcut to GetLocalHostName()

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019