OpendTect  6.6
_execbatch.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: A.H. Lammertink
8  Date: 30-10-2003
9  RCS: $Id$
10 ________________________________________________________________________
11 
12  The implementation of Execute_batch should be in the executable on
13  windows, but can be in a .so on *nix.
14  In order not to pollute batchprog.h, I've placed the implementation
15  into a separate file, which is included trough batchprog.h on win32
16  and included in batchprog.cc on *nix.
17 
18 */
19 
20 #include "commandlineparser.h"
21 #include "envvars.h"
22 #include "od_ostream.h"
23 #include "oddirs.h"
24 #include "oscommand.h"
25 
26 #ifdef __win__
27 # include <tchar.h>
28 # include <tlhelp32.h>
29 # include <Windows.h>
30 #else
31 # include "sys/resource.h"
32 #endif
33 
34 
35 #ifdef __win__
36 static void setBatchPriority( int argc, char** argv, float priority )
37 #else
38 static void setBatchPriority( int argc, char** argv, float priority, int pid )
39 #endif
40 {
41  const CommandLineParser clp( argc, argv );
42  //TODO: replace by reading the IOPar
43  clp.getVal( OS::CommandExecPars::sKeyPriority(), priority );
44 #ifdef __unix__
45  if ( mIsUdf(priority) )
46  {
47  int nicelvl = mUdf(int);
48  if ( !clp.getVal("nice",nicelvl) )
49  return;
50 
51  setpriority( PRIO_PROCESS, pid, nicelvl );
52  }
53  else
54  {
55  const int machprio =
56  OS::CommandExecPars::getMachinePriority( priority, false );
57  setpriority( PRIO_PROCESS, pid, machprio );
58  }
59 #else
60  if ( mIsUdf(priority) )
61  return;
62 
63  const int machprio =
65  const DWORD threadpriority =
66  machprio == 8 ? THREAD_PRIORITY_NORMAL
67  : ( machprio == 7 ? THREAD_PRIORITY_BELOW_NORMAL
68  : THREAD_PRIORITY_LOWEST );
69  if ( threadpriority != THREAD_PRIORITY_NORMAL )
70  SetPriorityClass( GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS );
71 
72  HANDLE curthread = INVALID_HANDLE_VALUE;
73  THREADENTRY32 threadlist;
74 
75  const DWORD dwOwnerPID( GetCurrentProcessId() );
76  curthread = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwOwnerPID );
77  if ( curthread == INVALID_HANDLE_VALUE )
78  return;
79 
80  threadlist.dwSize = sizeof(THREADENTRY32);
81  if ( !Thread32First(curthread,&threadlist) )
82  { CloseHandle(curthread); return; }
83 
84  do
85  {
86  if ( threadlist.th32OwnerProcessID != dwOwnerPID )
87  continue;
88 
89  SetThreadPriority(curthread,threadpriority );
90  } while ( Thread32Next(curthread,&threadlist) );
91 
92  CloseHandle( curthread );
93 #endif
94 }
95 
96 void Execute_batch( int* pargc, char** argv )
97 {
98  PIM().loadAuto( false );
99  if ( !BP().init() )
100  return;
101 
102  PIM().loadAuto( true );
103 #ifdef __win__
104  setBatchPriority( *pargc, argv, BP().getPriority() );
105 #else
106  setBatchPriority( *pargc, argv, BP().getPriority(), GetPID() );
107 #endif
108 }
109 
111 {
112  BP().loadModules();
113  BP().modulesLoaded();
114 }
115 
116 
118 {
119  BatchProgram& bp = BP();
120  bp.initWork();
121  const bool res = bp.doWork( *bp.strm_ );
122  bp.postWork( res );
123 }
124 
125 
127 {
128  BatchProgram& bp = BP();
129  if ( bp.canReceiveRequests() )
130  {
131  bp.startTimer();
134  "Batch program executor");
135  }
136  else
137  {
138  doWorkCB( cb );
139  bp.endWorkCB( cb );
140  }
141 }
doWorkCB
void doWorkCB(CallBacker *)
Definition: _execbatch.h:117
Strat::init
void init()
envvars.h
setBatchPriority
static void setBatchPriority(int argc, char **argv, float priority, int pid)
Definition: _execbatch.h:38
Threads::Locker
Locks the lock, shutting out access from other threads if needed.
Definition: threadlock.h:85
PIM
PluginManager & PIM()
mSCB
#define mSCB(fn)
Definition: callback.h:45
Execute_batch
void Execute_batch(int *pargc, char **argv)
Definition: _execbatch.h:96
od_ostream.h
mIsUdf
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:289
BatchProgram
Main object for 'standard' batch programs.
Definition: batchprog.h:64
BatchProgram::batchprogthreadlock_
Threads::Lock batchprogthreadlock_
Definition: batchprog.h:134
commandlineparser.h
CallBacker
Inherit from this class to be able to send and/or receive CallBacks.
Definition: callback.h:185
launchDoWorkCB
void launchDoWorkCB(CallBacker *cb)
Definition: _execbatch.h:126
loadModulesCB
void loadModulesCB(CallBacker *)
Definition: _execbatch.h:110
CommandLineParser
Definition: commandlineparser.h:53
GetPID
int GetPID(void)
OS::CommandExecPars::sKeyPriority
static const char * sKeyPriority()
Definition: oscommand.h:72
BatchProgram::strm_
od_ostream * strm_
Definition: batchprog.h:116
CommandLineParser::getVal
bool getVal(const char *key, BufferString &, bool acceptnone=false, int valnr=1) const
PluginManager::loadAuto
void loadAuto(bool late)
see class comments
BatchProgram::thread_
Threads::Thread * thread_
Definition: batchprog.h:133
oddirs.h
Threads::Thread
Is the base class for all threads. Start it by creating it and give it the function or CallBack to ex...
Definition: thread.h:282
mUdf
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:274
OS::CommandExecPars::getMachinePriority
static int getMachinePriority(float priolevel, bool iswin)
BP
BatchProgram & BP()
oscommand.h

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