dca_interface  6.3.4
customdb_samples/customdbsample_extended/linux/schedule_thread.cpp
1 /* IBM Source Code */
2 /* (C) Copyright IBM Corp. 2009, 2012 */
3 /* Licensed Materials - Property of IBM */
4 /* US Government Users Restricted Rights - Use duplication or disclosure restricted by GSA Schedule Contract with IBM Corp. */
5 
10 #include "dca/dca_base.h"
11 
12 #include "../mythreads.h"
13 
14 using namespace dca;
15 
16 struct ScheduleThreadData
17 {
18  const DcaInstance* pDcaInstance;
19  volatile bool shutdown;
20  volatile bool *errorSignal;
21 
22  ScheduleThreadData()
23  : pDcaInstance(NULL)
24  , shutdown(false)
25  , errorSignal(NULL)
26  {
27  }
28 };
29 
30 static ScheduleThreadData S_scheduleThreadData;
31 
32 static void* ScheduleThreadWorkerFunction( void* pThreadData )
33 {
34  const ScheduleThreadData* pScheduleThreadData = (ScheduleThreadData*)pThreadData;
35 
36  try {
37  bool error = false;
38  while( !error && !pScheduleThreadData->shutdown ) {
39  FunctionResult fr = pScheduleThreadData->pDcaInstance->schedule();
40  if( !fr ) {
41  error = true;
42  break;
43  }
44 
45  // sleep for 1 minute - avoid busy loop
46  for (size_t k=0; k<60 && !pScheduleThreadData->shutdown; ++k)
47  mySleep( 1000 );
48 
49  }
50  }
51  catch(...) {}
52 
53  pthread_exit(0);
54 }
55 
56 static pthread_t S_hScheduleThread = 0;
57 
58 void createScheduleThread( const DcaInstance& aDcaInstance, volatile bool *errorSignal )
59 {
60  S_scheduleThreadData.pDcaInstance = &aDcaInstance;
61  S_scheduleThreadData.errorSignal = errorSignal;
62 
63  pthread_create(&S_hScheduleThread, NULL, ScheduleThreadWorkerFunction, (void*)&S_scheduleThreadData);
64 }
65 
67 {
68  S_scheduleThreadData.shutdown = true;
69  pthread_join(S_hScheduleThread, NULL);
70 }
void mySleep(unsigned int ms)
sleeps the given interval (in milliseconds)
void createScheduleThread(const dca::DcaInstance &aDcaInstance, volatile bool *errorSignal)
Starts up the schedule thread and supplies the given DcaInstance.
This header includes all header files of the DCA Base Package.
Encapsulates the init and deinit of the DCA API.
Definition: base_classes.h:315
void shutdownScheduleThread()
Shuts down the previously started schedule thread.
Standard function result.
Definition: base_classes.h:148