00001
00002
00003
00004
00005
00006
00007 #include "jsoc_main.h"
00008
00009 char *module_name = "threadsigs";
00010
00011 #define kIn "in"
00012 #define kNada "nada"
00013
00014 ModuleArgs_t module_args[] =
00015 {
00016 {ARG_STRING, kIn, kNada, "Just for looks"},
00017 {ARG_END}
00018 };
00019
00020 typedef enum
00021 {
00022 kThreadSigErr_Success
00023 } ThreadSigErr_t;
00024
00025
00026 static int gLoop = 1;
00027 static pthread_mutex_t mutex;
00028
00029 static void *sigthreadfxn(void *arg)
00030 {
00031 sigset_t bsigs;
00032 sigset_t rsigs;
00033 int siggot;
00034 int err = 0;
00035
00036
00037 sigfillset(&bsigs);
00038 pthread_sigmask(SIG_BLOCK, &bsigs, NULL);
00039
00040
00041 sigemptyset(&rsigs);
00042 sigaddset(&rsigs, SIGALRM);
00043
00044
00045 err = sigwait(&rsigs, &siggot);
00046
00047 if (!err)
00048 {
00049 fprintf(stdout, "Got ALRM signal.\n");
00050
00051
00052 pthread_mutex_lock(&mutex);
00053 gLoop = 0;
00054 pthread_mutex_unlock(&mutex);
00055 }
00056
00057 return arg;
00058 }
00059
00060
00061
00062
00063
00064 int DoIt(void)
00065 {
00066 int status = 0;
00067 ThreadSigErr_t error = kThreadSigErr_Success;
00068 pthread_t sigthread;
00069 pthread_t mainthread;
00070
00071 mainthread = pthread_self();
00072
00073 pthread_mutex_init(&mutex, NULL);
00074
00075
00076 sigset_t rsigs;
00077 sigemptyset(&rsigs);
00078 sigaddset(&rsigs, SIGALRM);
00079 pthread_sigmask(SIG_BLOCK, &rsigs, NULL);
00080
00081
00082
00083
00084 if((status = pthread_create(&sigthread, NULL, &sigthreadfxn, (void *)&mainthread)))
00085 {
00086 fprintf(stderr,"Thread creation failed: %d\n", status);
00087 exit(1);
00088 }
00089
00090
00091 while (1)
00092 {
00093
00094 pthread_mutex_lock(&mutex);
00095 if (gLoop != 0 && gLoop % 10000 == 0)
00096 {
00097 fprintf(stdout, "Iteration # %d\n", gLoop);
00098 }
00099 pthread_mutex_unlock(&mutex);
00100
00101 pthread_mutex_lock(&mutex);
00102 if (gLoop == 100000)
00103 {
00104
00105 pthread_kill(sigthread, SIGALRM);
00106 }
00107 pthread_mutex_unlock(&mutex);
00108
00109 pthread_mutex_lock(&mutex);
00110 if (gLoop)
00111 {
00112 ++gLoop;
00113 }
00114 else
00115 {
00116 pthread_mutex_unlock(&mutex);
00117 break;
00118 }
00119
00120 pthread_mutex_unlock(&mutex);
00121 }
00122
00123 fprintf(stdout, "Got out of loop - exiting.\n");
00124
00125
00126 pthread_join(sigthread, NULL);
00127
00128 pthread_mutex_destroy(&mutex);
00129
00130 return error;
00131 }