00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <errno.h>
00004 #include <unistd.h>
00005 #include <pthread.h>
00006
00007 #include <hlog.h>
00008 #include <hstructures.h>
00009 #include <hplacer.h>
00010 #include <hprocess.h>
00011 #include <hmsg.h>
00012 #include <hmqueue.h>
00013 #include <hevent.h>
00014
00015 #include "hdplacer.h"
00016 #include "hdmsg.h"
00017 #include "hdsched.h"
00018 #include "../platform_defines.h"
00019
00020
00021 static void cleanup_(int exit_code);
00022
00028 static int wait_event_();
00029 static struct hprocess* get_current_process_();
00030 static int set_current_process_(struct hprocess* process);
00031
00032 static struct hprocess* current_process = NULL;
00034 static struct hevent* event_handler = NULL;
00036 static int notify_server_quit = 1;
00037 static const char* bitfilespath = "../bitfiles";
00038 static const char* icapdevice = "/var/icap";
00039
00040 static int wait_event_()
00041 {
00042 enum hdpevent incoming_event = (enum hdmevent)hevent_wait(event_handler);
00043
00044 struct hprocess* next_process = hdsched_get_next_process();
00045 struct hprocess* current_process = get_current_process_();
00046 int replace_current = 0;
00047
00048
00049 switch (incoming_event) {
00050 case HDPE_QUIT:
00051
00052 notify_server_quit = 0;
00053
00054 cleanup_(EXIT_SUCCESS);
00055 break;
00056 case HDPE_TIMER:
00057
00058 if (current_process != next_process)
00059 replace_current = 1;
00060 break;
00061 }
00062
00063
00064 if (replace_current) {
00065 printf("hdplacer_main: Replacing process. old-pid=%d. new-pid=%d\n", hprocess_get_pid(current_process), hprocess_get_pid(next_process));
00066
00067
00068
00069 set_current_process_(next_process);
00070 hdsched_reschedule(current_process, HDSE_RESCHED_TIMER);
00071 } else
00072 printf("hdplacer_main: Keeping current process on FPGA. pid=%d\n", hprocess_get_pid(next_process));
00073
00074 return 0;
00075 }
00076
00077
00078 static struct hprocess* get_current_process_()
00079 {
00080 return current_process;
00081 }
00082
00083
00084 static int set_current_process_(struct hprocess* process)
00085 {
00086 current_process = process;
00087
00088 return 0;
00089 }
00090
00091
00092 static void cleanup_(int exit_code)
00093 {
00094
00095 if (notify_server_quit) {
00096 notify_server_quit = 0;
00097 hdmsg_notify(HDME_QUIT);
00098 }
00099
00100 hdmsg_synchronize();
00101 pthread_exit(NULL);
00102 }
00103
00104
00105 int hdplacer_notify(enum hdpevent event)
00106 {
00107 hevent_notify(event_handler, (int)event);
00108
00109 return 0;
00110 }
00111
00112
00113 void* hdplacer_main()
00114 {
00115
00116 event_handler = hevent_create(HEVENT_BLOCKING);
00117 if (event_handler == NULL) {
00118 notify_server_quit = 0;
00119 cleanup_(EXIT_FAILURE);
00120 }
00121
00122 hplacer_set_bitfilespath(bitfilespath);
00123 hplacer_set_icapdevice(icapdevice);
00124
00125
00126 sleep(1);
00127
00128 while (1) {
00129
00130 hdsched_notify(HDSE_SHORT);
00131 wait_event_();
00132 }
00133
00134 hevent_remove(event_handler);
00135 cleanup_(EXIT_SUCCESS);
00136 }
00137
00138
00139
00140
00141
00142