dca_interface  6.3.4
url_samples/urldbsample_extended/linux/schedule_thread.cpp
Go to the documentation of this file.
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 
12 #include "dca/dca_base.h"
13 
14 #include "../mythreads.h"
15 
16 #include <sys/time.h>
17 #include <fcntl.h>
18 #include <cerrno>
19 
20 using namespace dca;
21 
26 struct ScheduleThreadData
27 {
28  const DcaInstance* pDcaInstance;
29  volatile bool shutdown;
30  ScheduleEventSubscriberIntf* myScheduleEventSubscriber;
31 
32  ScheduleThreadData()
33  : pDcaInstance(NULL)
34  , shutdown(false)
35  , myScheduleEventSubscriber (NULL)
36  {
37  }
38 };
39 
40 static ScheduleThreadData S_scheduleThreadData;
41 
42 static void* ScheduleThreadWorkerFunction( void* pThreadData )
43 {
44  const ScheduleThreadData* pScheduleThreadData = (ScheduleThreadData*)pThreadData;
45 
46  try {
47  bool error = false;
48  while( !error && !pScheduleThreadData->shutdown ) {
49  FunctionResult fr = pScheduleThreadData->pDcaInstance->schedule( pScheduleThreadData->myScheduleEventSubscriber );
50  if( !fr ) {
51  error = true;
52  break;
53  }
54 
55  // sleep for 1 minute - avoid busy loop
56  for (size_t k=0; k<60 && !pScheduleThreadData->shutdown; ++k)
57  mySleep( 1000 );
58  }
59  }
60  catch(...) {}
61 
62  pthread_exit(0);
63 }
64 
65 static pthread_t S_hScheduleThread = 0;
66 
67 void createScheduleThread( const DcaInstance& aDcaInstance, ScheduleEventSubscriberIntf* pSubscriber )
68 {
69  S_scheduleThreadData.pDcaInstance = &aDcaInstance;
70  S_scheduleThreadData.myScheduleEventSubscriber = pSubscriber;
71 
72  pthread_create(&S_hScheduleThread, NULL, ScheduleThreadWorkerFunction, (void*)&S_scheduleThreadData);
73 }
74 
76 {
77  S_scheduleThreadData.shutdown = true;
78  pthread_join(S_hScheduleThread, NULL);
79 }
void mySleep(unsigned int ms)
sleeps the given interval (in milliseconds)
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 createScheduleThread(const DcaInstance &aDcaInstance, ScheduleEventSubscriberIntf *pSubscriber)
Starts up the schedule thread and supplies the given DcaInstance.
void shutdownScheduleThread()
Shuts down the previously started schedule thread.
Standard function result.
Definition: base_classes.h:148
An interface for schedule event notifications. Derive a class from this interface and implement onEve...
Definition: base_classes.h:526