00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "jsoc_main.h"
00012 #include "tdsignals.h"
00013
00014 char *module_name = "threadalrm";
00015
00016 #define kIn "in"
00017 #define kNada "nada"
00018
00019 ModuleArgs_t module_args[] =
00020 {
00021 {ARG_STRING, kIn, kNada, "Just for looks"},
00022 {ARG_END}
00023 };
00024
00025 typedef enum
00026 {
00027 kThreadSigErr_Success
00028 } ThreadSigErr_t;
00029
00030 static int gLoop = 1;
00031 static int gRearm = 0;
00032 static pthread_mutex_t mutex;
00033 static struct timeval tv0;
00034
00035
00036
00037 void shandler(int sig, pthread_mutex_t *mtx)
00038 {
00039 float elapsed;
00040 struct timeval tv1;
00041 gettimeofday(&tv1, NULL);
00042
00043 elapsed = (float)((tv1.tv_sec * 1000000.0 + tv1.tv_usec -
00044 (tv0.tv_sec * 1000000.0 + tv0.tv_usec)) / 1000000.0);
00045
00046 fprintf(stdout, "Thread '%lld' received alarm signal '%d'.\n", (long long )pthread_self(), sig);
00047 fprintf(stdout, "Elapsed time is %f seconds.\n", elapsed);
00048
00049
00050 pthread_mutex_lock(mtx);
00051 gLoop = 0;
00052 pthread_mutex_unlock(mtx);
00053 }
00054
00055 void shandler2(int sig, pthread_mutex_t *mtx)
00056 {
00057 float elapsed;
00058 struct timeval tv1;
00059 gettimeofday(&tv1, NULL);
00060
00061 elapsed = (float)((tv1.tv_sec * 1000000.0 + tv1.tv_usec -
00062 (tv0.tv_sec * 1000000.0 + tv0.tv_usec)) / 1000000.0);
00063
00064 fprintf(stdout, "Thread '%lld' received alarm signal '%d'.\n", (long long )pthread_self(), sig);
00065 fprintf(stdout, "Elapsed time is %f seconds.\n", elapsed);
00066
00067 pthread_mutex_lock(mtx);
00068 gRearm = 1;
00069 pthread_mutex_unlock(mtx);
00070 }
00071
00072
00073
00074
00075
00076 int DoIt(void)
00077 {
00078 int err = 0;
00079 ThreadSigErr_t error = kThreadSigErr_Success;
00080 td_alarm_t alarm = 0;
00081 td_alarm_t alarm2 = 0;
00082
00083 pthread_mutex_init(&mutex, NULL);
00084
00085
00086 int seconds = 4;
00087 int seconds2 = 2;
00088 int ntimes = 3;
00089 gettimeofday(&tv0, NULL);
00090
00091
00092
00093
00094
00095 err = td_createalarm(seconds2, shandler2, &mutex, &alarm2) ||
00096 td_createalarm(seconds, shandler, &mutex, &alarm);
00097
00098 if (!err)
00099 {
00100
00101 fprintf(stdout, "Entering loop.\n");
00102
00103 while (1)
00104 {
00105
00106 pthread_mutex_lock(&mutex);
00107 if (gLoop != 0 && gLoop % 1000000 == 0)
00108 {
00109 fprintf(stdout, "Iteration # %d\n", gLoop);
00110 }
00111 pthread_mutex_unlock(&mutex);
00112
00113
00114 pthread_mutex_lock(&mutex);
00115 if (gRearm)
00116 {
00117 if (ntimes-- > 0)
00118 {
00119 pthread_mutex_unlock(&mutex);
00120
00121 td_destroyalarm(&alarm2, &mutex);
00122
00123 if (td_createalarm(seconds2, shandler2, &mutex, &alarm2))
00124 {
00125 pthread_mutex_unlock(&mutex);
00126 break;
00127 }
00128
00129 pthread_mutex_lock(&mutex);
00130 gRearm = 0;
00131 pthread_mutex_unlock(&mutex);
00132 }
00133 else
00134 {
00135
00136 break;
00137 }
00138 }
00139 else
00140 {
00141 pthread_mutex_unlock(&mutex);
00142 }
00143
00144 pthread_mutex_lock(&mutex);
00145 if (gLoop)
00146 {
00147 ++gLoop;
00148 pthread_mutex_unlock(&mutex);
00149 }
00150 else
00151 {
00152 gLoop = 1;
00153 pthread_mutex_unlock(&mutex);
00154 td_destroyalarm(&alarm, &mutex);
00155 }
00156 }
00157
00158 fprintf(stdout, "Got out of loop.\n");
00159 }
00160 else
00161 {
00162 fprintf(stderr, "Failed to create one or more alarms.\n");
00163 }
00164
00165
00166 pthread_mutex_destroy(&mutex);
00167
00168 return error;
00169 }