00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <pthread.h>
00004 #include <unistd.h>
00005
00006 #include <hevent.h>
00007
00008 #include "hdplacer.h"
00009 #include "hdmsg.h"
00010 #include "hdtimer.h"
00011
00012
00013 static void cleanup_(int exit_code);
00014
00020 static int handle_event_();
00021
00023 static struct hevent* event_handler = NULL;
00024
00026 static int notify_server_quit = 1;
00027
00028
00029 static int handle_event_()
00030 {
00031 enum hdtevent incoming_event = (enum hdtevent)hevent_wait(event_handler);
00032
00033
00034 switch (incoming_event) {
00035 case HDTE_QUIT:
00036
00037 notify_server_quit = 0;
00038
00039 cleanup_(EXIT_SUCCESS);
00040 break;
00041 }
00042
00043 return 0;
00044 }
00045
00046
00047 static void cleanup_(int exit_code)
00048 {
00049
00050 if (notify_server_quit) {
00051 notify_server_quit = 0;
00052 hdmsg_notify(HDME_QUIT);
00053 }
00054
00055 hdmsg_synchronize();
00056 pthread_exit(NULL);
00057 }
00058
00059
00060 int hdtimer_notify(enum hdtevent event)
00061 {
00062 hevent_notify(event_handler, (int)event);
00063
00064 return 0;
00065 }
00066
00067 const int hdtimer_get_timeslice()
00068 {
00069 return 1000000;
00070 }
00071
00072
00073 void* hdtimer_main()
00074 {
00075
00076 event_handler = hevent_create(HEVENT_NONBLOCKING);
00077 if (event_handler == NULL) {
00078 notify_server_quit = 0;
00079 cleanup_(EXIT_FAILURE);
00080 }
00081
00082
00083 sleep(1);
00084
00085 while (1) {
00086
00087 usleep(hdtimer_get_timeslice());
00088
00089 hdplacer_notify(HDPE_TIMER);
00090
00091 handle_event_();
00092 }
00093
00094 hevent_remove(event_handler);
00095 cleanup_(EXIT_SUCCESS);
00096 }
00097
00098
00099
00100
00101
00102