dca_interface  6.3.4
url_samples/urldbsample_extended/linux/perform_update_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 UpdateThreadData
27 {
28  const UpdateModule* pUpdateModule;
29  volatile bool shutdown;
30 
31  UpdateThreadData()
32  : pUpdateModule(NULL)
33  , shutdown(false)
34  {
35  }
36 };
37 
38 static UpdateThreadData S_updateThreadData;
39 
40 static void* PerformUpdateThreadWorkerFunction( void* pThreadData )
41 {
42  const UpdateThreadData* pUpdateThreadData = (UpdateThreadData*)pThreadData;
43 
44  try {
45  bool error = false;
46  while( !error && !pUpdateThreadData->shutdown ) {
47  bool force = false;
48  UpdateResults results;
49  FunctionResult fr = pUpdateThreadData->pUpdateModule->performUpdate(force, results);
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 && !pUpdateThreadData->shutdown; ++k)
57  mySleep( 1000 );
58  }
59  }
60  catch(...) {}
61 
62  pthread_exit(0);
63 }
64 
65 static pthread_t S_hUpdateThread = 0;
66 
67 void createPerformUpdateThread( const UpdateModule& aUpdateModule )
68 {
69  S_updateThreadData.pUpdateModule = &aUpdateModule;
70 
71  pthread_create(&S_hUpdateThread , NULL, PerformUpdateThreadWorkerFunction, (void*)&S_updateThreadData);
72 }
73 
75 {
76  S_updateThreadData.shutdown = true;
77  pthread_join(S_hUpdateThread, NULL);
78 }
79 
80 void mySleep( unsigned nTimeMs )
81 {
82  timespec sleeptime;
83  sleeptime.tv_sec = nTimeMs / 1000;
84  sleeptime.tv_nsec = 1000*1000*(nTimeMs % 1000);
85 
86  // nanosleep will fail, if we receive any signal, so we have to loop until it is successful
87  timespec remaintime;
88  while( nanosleep( &sleeptime, &remaintime ) == -1 )
89  {
90  int tmp_errno = errno;
91  if( tmp_errno != EINTR ) {
92  // nanosleep failed / tmp_errno
93  break;
94  }
95  // call again with remaining time
96  sleeptime.tv_sec = remaintime.tv_sec;
97  sleeptime.tv_nsec = remaintime.tv_nsec;
98  }
99 }
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 UpdateModule &aUpdateModule)
Starts up the update thread and supplies 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