dca_interface  6.3.4
customdb_samples/customdbsample_extended/linux/perform_update_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 #include <sys/time.h>
15 #include <fcntl.h>
16 #include <cerrno>
17 
18 using namespace dca;
19 
20 struct UpdateThreadData
21 {
22  const UpdateModule* pUpdateModule;
23  volatile bool shutdown;
24  volatile bool *error;
25 
26  UpdateThreadData()
27  : pUpdateModule(NULL)
28  , shutdown(false)
29  , error(NULL)
30  {
31  }
32 };
33 
34 static UpdateThreadData S_updateThreadData;
35 
36 static void* PerformUpdateThreadWorkerFunction( void* pThreadData )
37 {
38  const UpdateThreadData* pUpdateThreadData = (UpdateThreadData*)pThreadData;
39 
40  try {
41  bool error = false;
42  while( !error && !pUpdateThreadData->shutdown ) {
43  bool force = false;
44  UpdateResults results;
45  FunctionResult fr = pUpdateThreadData->pUpdateModule->performUpdate(force, results);
46  if( !fr ) {
47  error = true;
48  break;
49  }
50 
51  // sleep for 1 minute - avoid busy loop
52  for (size_t k=0; k<60 && !pUpdateThreadData->shutdown; ++k)
53  mySleep( 1000 );
54  }
55  }
56  catch(...) {}
57 
58  pthread_exit(0);
59 }
60 
61 static pthread_t S_hUpdateThread = 0;
62 
63 void createPerformUpdateThread( const UpdateModule& aUpdateModule, volatile bool *errorSignal )
64 {
65  S_updateThreadData.pUpdateModule = &aUpdateModule;
66  S_updateThreadData.error = errorSignal;
67 
68  pthread_create(&S_hUpdateThread , NULL, PerformUpdateThreadWorkerFunction, (void*)&S_updateThreadData);
69 }
70 
72 {
73  S_updateThreadData.shutdown = true;
74  pthread_join(S_hUpdateThread, NULL);
75 }
76 
77 void mySleep( unsigned nTimeMs )
78 {
79  timespec sleeptime;
80  sleeptime.tv_sec = nTimeMs / 1000;
81  sleeptime.tv_nsec = 1000*1000*(nTimeMs % 1000);
82 
83  // nanosleep will fail, if we receive any signal, so we have to loop until it is successful
84  timespec remaintime;
85  while( nanosleep( &sleeptime, &remaintime ) == -1 )
86  {
87  int tmp_errno = errno;
88  if( tmp_errno != EINTR ) {
89  // nanosleep failed / tmp_errno
90  break;
91  }
92  // call again with remaining time
93  sleeptime.tv_sec = remaintime.tv_sec;
94  sleeptime.tv_nsec = remaintime.tv_nsec;
95  }
96 }
97 
void mySleep(unsigned int ms)
sleeps the given interval (in milliseconds)
The update module is used to download and install DCA content and engine updates.
Definition: base_classes.h:917
Encapsulates the results of an update process.
Definition: base_classes.h:998
void createPerformUpdateThread(const dca::UpdateModule &aUpdateModule, volatile bool *errorSignal)
Starts up the update thread and supply the given UpdateModule.
void shutdownPerformUpdateThread()
Shuts down the previously started update thread.
This header includes all header files of the DCA Base Package.
Standard function result.
Definition: base_classes.h:148