11 pthread_mutex_t simulation_lock;
12 pthread_mutex_t application_lock;
24 pthread_mutex_lock(&application_lock);
30 pthread_mutex_unlock(&application_lock);
36 printf("Sim: thread started, now trying lock...\n");
37 pthread_mutex_lock(&simulation_lock);
38 printf("Sim: got lock, now run\n");
39 struct sim_args *args= (struct sim_args *)arg;
41 long steps2go= args->steps2go, steps= steps2go;
50 printf("Sim: %s steps done\n", steps2go);
52 else if (steps2go == 0)
55 /*else if (steps2go < 0)
57 if (done= !sys.simulating)
58 printf("Sim: requested to stop\n");
62 printf("Sim: finished, releasing lock...\n");
63 pthread_mutex_unlock(&simulation_lock);
64 printf("Sim: done\n");
72 pthread_mutex_lock(&application_lock);
78 pthread_mutex_unlock(&application_lock);
83 g_cmd(FILE *fin, FILE *fout)
86 fprintf(fout, "PC=%ld, simulating=%d\n", sys.PC, sys.simulating);
91 s_cmd(FILE *fin, FILE *fout)
94 fscanf(fin, " %ld", &sys.PC);
95 fprintf(fout, "PC=%ld\n", sys.PC);
100 start_simulation(int steps)
102 struct sim_args sargs;
103 sargs.steps2go= steps;
106 pthread_attr_init(&ta);
107 pthread_attr_setdetachstate(&ta, PTHREAD_CREATE_DETACHED);
108 pthread_create(&sim_th, &ta, sim_thread, &sargs);
109 pthread_attr_destroy(&ta);
113 r_cmd(FILE *fin, FILE *fout)
116 if (pthread_mutex_trylock(&simulation_lock) != 0)
118 fprintf(fout, "Simulation already runing\n");
122 fprintf(fout, "Run from PC=%ld\n", sys.PC);
123 start_simulation(-1);
124 pthread_mutex_unlock(&simulation_lock);
130 S_cmd(FILE *fin, FILE *fout)
132 fprintf(fout, "Trying to get app lock...\n");
134 fprintf(fout, "OK, lock is ours, set stopper flag...\n");
136 fprintf(fout, "Release lock, to give chance of stop\n");
138 fprintf(fout, "Wait for stop...\n");
139 pthread_mutex_lock(&simulation_lock);
140 fprintf(fout, "We got sim lock, so it stopped\n");
141 pthread_mutex_unlock(&simulation_lock);
142 fprintf(fout, "Stopped at PC=%ld\n", sys.PC);
153 input_thread(void *arg)
155 struct input_args *args= (struct input_args *)arg;
158 fin= fopen(args->fin_name, "r");
162 fout= fopen(args->fout_name, "w");
169 fprintf(fout, "%d> ", args->nr); fflush(fout);
170 fscanf(fin, " %99s", &cmd[0]);
171 fprintf(fout, "%d Got command: %c\n", args->nr, cmd[0]);
190 fprintf(fout, "%d Unknown command\n", args->nr);
194 fprintf(fout, "%d Console finished\n", args->nr);
202 main(int argc, char *argv[])
204 pthread_t input_th[3];
207 pthread_mutex_init(&simulation_lock, NULL);
208 pthread_mutex_init(&application_lock, NULL);
210 struct input_args iargs= { 0, NULL, NULL };
211 pthread_create(&input_th[0], NULL, input_thread, &iargs);
215 struct input_args iargs= { 1, argv[1], argv[1] };
216 pthread_create(&input_th[1], NULL, input_thread, &iargs);
221 struct input_args iargs= { 2, argv[2], argv[2] };
222 pthread_create(&input_th[2], NULL, input_thread, &iargs);
226 for (i= 0; i < threads; i++)
229 pthread_join(input_th[i], &ret);