1 | #include <signal.h>
|
---|
2 | #define DIMLIB
|
---|
3 | #include "dim.h"
|
---|
4 |
|
---|
5 | #ifndef WIN32
|
---|
6 |
|
---|
7 | #ifndef NOTHREADS
|
---|
8 | #include <pthread.h>
|
---|
9 | #include <semaphore.h>
|
---|
10 | #ifdef solaris
|
---|
11 | #include <synch.h>
|
---|
12 | #endif
|
---|
13 | #ifdef darwin
|
---|
14 | #include <sys/types.h>
|
---|
15 | #include <sys/stat.h>
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | pthread_t IO_thread = 0;
|
---|
19 | pthread_t ALRM_thread = 0;
|
---|
20 | pthread_t INIT_thread = 0;
|
---|
21 | pthread_t MAIN_thread = 0;
|
---|
22 | #ifndef darwin
|
---|
23 | sem_t DIM_INIT_Sema;
|
---|
24 | /*
|
---|
25 | sem_t DIM_WAIT_Sema;
|
---|
26 | */
|
---|
27 | #else
|
---|
28 | sem_t *DIM_INIT_Semap;
|
---|
29 | /*
|
---|
30 | sem_t *DIM_WAIT_Semap;
|
---|
31 | */
|
---|
32 | #endif
|
---|
33 | int INIT_count = 0;
|
---|
34 | /*
|
---|
35 | int WAIT_count = 0;
|
---|
36 | */
|
---|
37 | int DIM_THR_init_done = 0;
|
---|
38 |
|
---|
39 | void *dim_tcpip_thread(void *tag)
|
---|
40 | {
|
---|
41 | extern int dim_tcpip_init();
|
---|
42 | extern void tcpip_task();
|
---|
43 | /*
|
---|
44 | int prio;
|
---|
45 |
|
---|
46 | thr_getprio(thr_self(),&prio);
|
---|
47 | thr_setprio(thr_self(),prio+10);
|
---|
48 | */
|
---|
49 | if(tag){}
|
---|
50 | IO_thread = pthread_self();
|
---|
51 |
|
---|
52 | dim_tcpip_init(1);
|
---|
53 | if(INIT_thread)
|
---|
54 | {
|
---|
55 | #ifndef darwin
|
---|
56 | sem_post(&DIM_INIT_Sema);
|
---|
57 | #else
|
---|
58 | sem_post(DIM_INIT_Semap);
|
---|
59 | #endif
|
---|
60 | }
|
---|
61 | while(1)
|
---|
62 | {
|
---|
63 | tcpip_task();
|
---|
64 | /*
|
---|
65 | #ifndef darwin
|
---|
66 | sem_post(&DIM_WAIT_Sema);
|
---|
67 | #else
|
---|
68 | sem_post(DIM_WAIT_Semap);
|
---|
69 | #endif
|
---|
70 | */
|
---|
71 | dim_signal_cond();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | void *dim_dtq_thread(void *tag)
|
---|
76 | {
|
---|
77 | extern int dim_dtq_init();
|
---|
78 | extern int dtq_task();
|
---|
79 | /*
|
---|
80 | int prio;
|
---|
81 |
|
---|
82 | thr_getprio(thr_self(),&prio);
|
---|
83 | thr_setprio(thr_self(),prio+5);
|
---|
84 | */
|
---|
85 | if(tag){}
|
---|
86 | ALRM_thread = pthread_self();
|
---|
87 |
|
---|
88 | dim_dtq_init(1);
|
---|
89 | if(INIT_thread)
|
---|
90 | {
|
---|
91 | #ifndef darwin
|
---|
92 | sem_post(&DIM_INIT_Sema);
|
---|
93 | #else
|
---|
94 | sem_post(DIM_INIT_Semap);
|
---|
95 | #endif
|
---|
96 | }
|
---|
97 | while(1)
|
---|
98 | {
|
---|
99 | dtq_task();
|
---|
100 | /*
|
---|
101 | #ifndef darwin
|
---|
102 | sem_post(&DIM_WAIT_Sema);
|
---|
103 | #else
|
---|
104 | sem_post(DIM_WAIT_Semap);
|
---|
105 | #endif
|
---|
106 | */
|
---|
107 | dim_signal_cond();
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | void dim_init()
|
---|
112 | {
|
---|
113 | pthread_t t_id;
|
---|
114 | void ignore_sigpipe();
|
---|
115 | extern int dna_init();
|
---|
116 | /*
|
---|
117 | #ifdef LYNXOS
|
---|
118 | */
|
---|
119 | pthread_attr_t attr;
|
---|
120 | /*
|
---|
121 | #endif
|
---|
122 | */
|
---|
123 | if(!DIM_THR_init_done)
|
---|
124 | {
|
---|
125 | /*
|
---|
126 | int prio;
|
---|
127 | */
|
---|
128 | DIM_THR_init_done = 1;
|
---|
129 | dna_init();
|
---|
130 | /*
|
---|
131 | thr_getprio(thr_self(),&prio);
|
---|
132 | thr_setprio(thr_self(),prio+3);
|
---|
133 | */
|
---|
134 | INIT_thread = pthread_self();
|
---|
135 | MAIN_thread = INIT_thread;
|
---|
136 |
|
---|
137 | #ifndef darwin
|
---|
138 | sem_init(&DIM_INIT_Sema, 0, INIT_count);
|
---|
139 | /*
|
---|
140 | sem_init(&DIM_WAIT_Sema, 0, WAIT_count);
|
---|
141 | */
|
---|
142 | #else
|
---|
143 | DIM_INIT_Semap = sem_open("/Dim_INIT_Sem", O_CREAT, S_IRUSR | S_IWUSR, INIT_count);
|
---|
144 | /*
|
---|
145 | DIM_WAIT_Semap = sem_open("/Dim_WAIT_Sem", O_CREAT, S_IRUSR | S_IWUSR, WAIT_count);
|
---|
146 | */
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | ignore_sigpipe();
|
---|
150 |
|
---|
151 | #if defined (LYNXOS) && !defined (__Lynx__)
|
---|
152 | pthread_attr_create(&attr);
|
---|
153 | pthread_create(&t_id, attr, dim_dtq_thread, 0);
|
---|
154 | #else
|
---|
155 | /*
|
---|
156 | pthread_create(&t_id, NULL, dim_dtq_thread, 0);
|
---|
157 | */
|
---|
158 | pthread_attr_init(&attr);
|
---|
159 | pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
|
---|
160 | pthread_create(&t_id, &attr, dim_dtq_thread, 0);
|
---|
161 | #endif
|
---|
162 | #ifndef darwin
|
---|
163 | sem_wait(&DIM_INIT_Sema);
|
---|
164 | #else
|
---|
165 | sem_wait(DIM_INIT_Semap);
|
---|
166 | #endif
|
---|
167 | #if defined (LYNXOS) && !defined (__Lynx__)
|
---|
168 | pthread_create(&t_id, attr, dim_tcpip_thread, 0);
|
---|
169 | #else
|
---|
170 | pthread_create(&t_id, &attr, dim_tcpip_thread, 0);
|
---|
171 | #endif
|
---|
172 | #ifndef darwin
|
---|
173 | sem_wait(&DIM_INIT_Sema);
|
---|
174 | #else
|
---|
175 | sem_wait(DIM_INIT_Semap);
|
---|
176 | #endif
|
---|
177 | INIT_thread = 0;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | void dim_stop()
|
---|
182 | {
|
---|
183 | void dim_tcpip_stop(), dim_dtq_stop();
|
---|
184 | /*
|
---|
185 | int i;
|
---|
186 | int n = 0;
|
---|
187 |
|
---|
188 | for( i = 0; i< Curr_N_Conns; i++ )
|
---|
189 | {
|
---|
190 | if(Net_conns[i].channel != 0)
|
---|
191 | n++;
|
---|
192 | }
|
---|
193 | if(n)
|
---|
194 | return;
|
---|
195 | */
|
---|
196 | if(IO_thread)
|
---|
197 | pthread_cancel(IO_thread);
|
---|
198 | if(ALRM_thread)
|
---|
199 | pthread_cancel(ALRM_thread);
|
---|
200 | if(IO_thread)
|
---|
201 | pthread_join(IO_thread,0);
|
---|
202 | if(ALRM_thread)
|
---|
203 | pthread_join(ALRM_thread,0);
|
---|
204 | #ifndef darwin
|
---|
205 | sem_destroy(&DIM_INIT_Sema);
|
---|
206 | /*
|
---|
207 | sem_destroy(&DIM_WAIT_Sema);
|
---|
208 | */
|
---|
209 | #else
|
---|
210 | sem_unlink("/Dim_INIT_Sem");
|
---|
211 | /*
|
---|
212 | sem_unlink("/Dim_WAIT_Sem");
|
---|
213 | */
|
---|
214 | sem_close(DIM_INIT_Semap);
|
---|
215 | /*
|
---|
216 | sem_close(DIM_WAIT_Semap);
|
---|
217 | */
|
---|
218 | #endif
|
---|
219 | dim_tcpip_stop();
|
---|
220 | dim_dtq_stop();
|
---|
221 | IO_thread = 0;
|
---|
222 | ALRM_thread = 0;
|
---|
223 | DIM_THR_init_done = 0;
|
---|
224 | }
|
---|
225 |
|
---|
226 | dim_long dim_start_thread(void *(*thread_ast)(void *), dim_long tag)
|
---|
227 | {
|
---|
228 | pthread_t t_id;
|
---|
229 | pthread_attr_t attr;
|
---|
230 |
|
---|
231 | #if defined (LYNXOS) && !defined (__Lynx__)
|
---|
232 | pthread_attr_create(&attr);
|
---|
233 | pthread_create(&t_id, attr, (void *)thread_ast, (void *)tag);
|
---|
234 | #else
|
---|
235 | pthread_attr_init(&attr);
|
---|
236 | pthread_create(&t_id, &attr, thread_ast, (void *)tag);
|
---|
237 | #endif
|
---|
238 | return((dim_long)t_id);
|
---|
239 | }
|
---|
240 |
|
---|
241 | int dim_stop_thread(dim_long t_id)
|
---|
242 | {
|
---|
243 | int ret;
|
---|
244 | ret = pthread_cancel((pthread_t)t_id);
|
---|
245 | dim_print_date_time();
|
---|
246 | printf("dim_stop_thread: this function is obsolete, it creates memory leaks\n");
|
---|
247 | return ret;
|
---|
248 | }
|
---|
249 |
|
---|
250 | int dim_set_scheduler_class(int pclass)
|
---|
251 | {
|
---|
252 | #ifdef __linux__
|
---|
253 | int ret, prio, p;
|
---|
254 | struct sched_param param;
|
---|
255 |
|
---|
256 | if(pclass == 0)
|
---|
257 | {
|
---|
258 | pclass = SCHED_OTHER;
|
---|
259 | }
|
---|
260 | else if(pclass == 1)
|
---|
261 | {
|
---|
262 | pclass = SCHED_FIFO;
|
---|
263 | }
|
---|
264 | else if(pclass == 2)
|
---|
265 | {
|
---|
266 | pclass = SCHED_RR;
|
---|
267 | }
|
---|
268 | prio = sched_get_priority_min(pclass);
|
---|
269 | ret = pthread_getschedparam(MAIN_thread, &p, ¶m);
|
---|
270 | if( (p == SCHED_OTHER) || (pclass == SCHED_OTHER) )
|
---|
271 | param.sched_priority = prio;
|
---|
272 | ret = pthread_setschedparam(MAIN_thread, pclass, ¶m);
|
---|
273 | if(ret)
|
---|
274 | return 0;
|
---|
275 | ret = pthread_getschedparam(IO_thread, &p, ¶m);
|
---|
276 | if( (p == SCHED_OTHER) || (pclass == SCHED_OTHER) )
|
---|
277 | param.sched_priority = prio;
|
---|
278 | ret = pthread_setschedparam(IO_thread, pclass, ¶m);
|
---|
279 | if(ret)
|
---|
280 | return 0;
|
---|
281 | ret = pthread_getschedparam(ALRM_thread, &p, ¶m);
|
---|
282 | if( (p == SCHED_OTHER) || (pclass == SCHED_OTHER) )
|
---|
283 | param.sched_priority = prio;
|
---|
284 | ret = pthread_setschedparam(ALRM_thread, pclass, ¶m);
|
---|
285 | if(!ret)
|
---|
286 | return 1;
|
---|
287 | #endif
|
---|
288 | return 0;
|
---|
289 | }
|
---|
290 |
|
---|
291 | int dim_get_scheduler_class(int *pclass)
|
---|
292 | {
|
---|
293 | #ifdef __linux__
|
---|
294 | int ret;
|
---|
295 | struct sched_param param;
|
---|
296 |
|
---|
297 | ret = pthread_getschedparam(MAIN_thread, pclass, ¶m);
|
---|
298 | if(!ret)
|
---|
299 | return 1;
|
---|
300 | #endif
|
---|
301 | return 0;
|
---|
302 | }
|
---|
303 |
|
---|
304 | int dim_set_priority(int threadId, int prio)
|
---|
305 | {
|
---|
306 | #ifdef __linux__
|
---|
307 | pthread_t id = MAIN_thread;
|
---|
308 | int ret;
|
---|
309 | int pclass;
|
---|
310 | struct sched_param param;
|
---|
311 |
|
---|
312 | if(threadId == 1)
|
---|
313 | id = MAIN_thread;
|
---|
314 | else if(threadId == 2)
|
---|
315 | id = IO_thread;
|
---|
316 | else if(threadId == 3)
|
---|
317 | id = ALRM_thread;
|
---|
318 |
|
---|
319 | ret = pthread_getschedparam(id, &pclass, ¶m);
|
---|
320 | param.sched_priority = prio;
|
---|
321 | ret = pthread_setschedparam(id, pclass, ¶m);
|
---|
322 | if(!ret)
|
---|
323 | return 1;
|
---|
324 | #endif
|
---|
325 | return 0;
|
---|
326 | }
|
---|
327 |
|
---|
328 | int dim_get_priority(int threadId, int *prio)
|
---|
329 | {
|
---|
330 | #ifdef __linux__
|
---|
331 | pthread_t id=MAIN_thread;
|
---|
332 | int ret;
|
---|
333 | int pclass;
|
---|
334 | struct sched_param param;
|
---|
335 |
|
---|
336 | if(threadId == 1)
|
---|
337 | id = MAIN_thread;
|
---|
338 | else if(threadId == 2)
|
---|
339 | id = IO_thread;
|
---|
340 | else if(threadId == 3)
|
---|
341 | id = ALRM_thread;
|
---|
342 |
|
---|
343 | ret = pthread_getschedparam(id, &pclass, ¶m);
|
---|
344 | *prio = param.sched_priority;
|
---|
345 | if(!ret)
|
---|
346 | return 1;
|
---|
347 | #endif
|
---|
348 | return 0;
|
---|
349 | }
|
---|
350 |
|
---|
351 | void ignore_sigpipe()
|
---|
352 | {
|
---|
353 |
|
---|
354 | struct sigaction sig_info;
|
---|
355 | sigset_t set;
|
---|
356 | void pipe_sig_handler();
|
---|
357 |
|
---|
358 | if( sigaction(SIGPIPE, 0, &sig_info) < 0 )
|
---|
359 | {
|
---|
360 | perror( "sigaction(SIGPIPE)" );
|
---|
361 | exit(1);
|
---|
362 | }
|
---|
363 | if(sig_info.sa_handler)
|
---|
364 | {
|
---|
365 | /*
|
---|
366 | printf("DIM ignore_sigpipe() - Handler already defined %08X\n", sig_info.sa_handler);
|
---|
367 | */
|
---|
368 | return;
|
---|
369 | }
|
---|
370 | sigemptyset(&set);
|
---|
371 | sig_info.sa_handler = pipe_sig_handler;
|
---|
372 | sig_info.sa_mask = set;
|
---|
373 | #ifndef LYNXOS
|
---|
374 | sig_info.sa_flags = SA_RESTART;
|
---|
375 | #else
|
---|
376 | sig_info.sa_flags = 0;
|
---|
377 | #endif
|
---|
378 |
|
---|
379 | if( sigaction(SIGPIPE, &sig_info, 0) < 0 )
|
---|
380 | {
|
---|
381 | perror( "sigaction(SIGPIPE)" );
|
---|
382 | exit(1);
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | void pipe_sig_handler( int num )
|
---|
387 | {
|
---|
388 | if(num){}
|
---|
389 | /*
|
---|
390 | printf( "*** pipe_sig_handler called ***\n" );
|
---|
391 | */
|
---|
392 | }
|
---|
393 |
|
---|
394 | void dim_init_threads()
|
---|
395 | {
|
---|
396 | dim_init();
|
---|
397 | }
|
---|
398 |
|
---|
399 | void dim_stop_threads()
|
---|
400 | {
|
---|
401 | dim_stop();
|
---|
402 | }
|
---|
403 |
|
---|
404 | int dim_wait(void)
|
---|
405 | {
|
---|
406 | pthread_t id;
|
---|
407 |
|
---|
408 | id = pthread_self();
|
---|
409 |
|
---|
410 | if((id == ALRM_thread) || (id == IO_thread))
|
---|
411 | {
|
---|
412 | return(-1);
|
---|
413 | }
|
---|
414 | /*
|
---|
415 | #ifndef darwin
|
---|
416 | sem_wait(&DIM_WAIT_Sema);
|
---|
417 | #else
|
---|
418 | sem_wait(DIM_WAIT_Semap);
|
---|
419 | #endif
|
---|
420 | */
|
---|
421 | dim_wait_cond();
|
---|
422 | return(-1);
|
---|
423 | }
|
---|
424 |
|
---|
425 | /*
|
---|
426 | static void show_ast()
|
---|
427 | {
|
---|
428 | sigset_t oset;
|
---|
429 |
|
---|
430 | sigprocmask(SIG_SETMASK,0,&oset);
|
---|
431 | printf("---THREAD id = %d, mask = %x %x\n",
|
---|
432 | pthread_self(), oset.__sigbits[1], oset.__sigbits[0]);
|
---|
433 | }
|
---|
434 | */
|
---|
435 |
|
---|
436 | pthread_t Dim_thr_locker = 0;
|
---|
437 | int Dim_thr_counter = 0;
|
---|
438 | #ifdef LYNXOS
|
---|
439 | pthread_mutex_t Global_DIM_mutex;
|
---|
440 | pthread_mutex_t Global_cond_mutex;
|
---|
441 | pthread_cond_t Global_cond;
|
---|
442 | #else
|
---|
443 | pthread_mutex_t Global_DIM_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
444 | pthread_mutex_t Global_cond_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
445 | pthread_cond_t Global_cond = PTHREAD_COND_INITIALIZER;
|
---|
446 | #endif
|
---|
447 | int Global_cond_counter = 0;
|
---|
448 | int Global_cond_waiters = 0;
|
---|
449 |
|
---|
450 | void dim_lock()
|
---|
451 | {
|
---|
452 | /*printf("Locking %d ", pthread_self());*/
|
---|
453 | if(Dim_thr_locker != pthread_self())
|
---|
454 | {
|
---|
455 | /*
|
---|
456 | #ifdef __linux__
|
---|
457 | pthread_testcancel();
|
---|
458 | #endif
|
---|
459 | */
|
---|
460 | pthread_mutex_lock(&Global_DIM_mutex);
|
---|
461 | Dim_thr_locker=pthread_self();
|
---|
462 | /*printf(": Locked ");*/
|
---|
463 | }
|
---|
464 | /*printf("Counter = %d\n",Dim_thr_counter);*/
|
---|
465 | Dim_thr_counter++;
|
---|
466 | }
|
---|
467 | void dim_unlock()
|
---|
468 | {
|
---|
469 | /*printf("Un-Locking %d ", pthread_self());*/
|
---|
470 | Dim_thr_counter--;
|
---|
471 | /*printf("Counter = %d ",Dim_thr_counter);*/
|
---|
472 | if(!Dim_thr_counter)
|
---|
473 | {
|
---|
474 | Dim_thr_locker=0;
|
---|
475 | pthread_mutex_unlock(&Global_DIM_mutex);
|
---|
476 | /*printf(": Un-Locked ");*/
|
---|
477 | }
|
---|
478 | /* printf("\n");*/
|
---|
479 | }
|
---|
480 |
|
---|
481 | void dim_wait_cond()
|
---|
482 | {
|
---|
483 | pthread_mutex_lock(&Global_cond_mutex);
|
---|
484 | Global_cond_waiters++;
|
---|
485 | if(!Global_cond_counter)
|
---|
486 | {
|
---|
487 | pthread_cond_wait(&Global_cond, &Global_cond_mutex);
|
---|
488 | }
|
---|
489 | Global_cond_waiters--;
|
---|
490 | if(!Global_cond_waiters)
|
---|
491 | Global_cond_counter--;
|
---|
492 | pthread_mutex_unlock(&Global_cond_mutex);
|
---|
493 | }
|
---|
494 |
|
---|
495 | void dim_signal_cond()
|
---|
496 | {
|
---|
497 | pthread_mutex_lock(&Global_cond_mutex);
|
---|
498 | if(!Global_cond_waiters)
|
---|
499 | {
|
---|
500 | Global_cond_counter = 1;
|
---|
501 | }
|
---|
502 | else
|
---|
503 | {
|
---|
504 | Global_cond_counter++;
|
---|
505 | pthread_cond_broadcast(&Global_cond);
|
---|
506 | }
|
---|
507 | pthread_mutex_unlock(&Global_cond_mutex);
|
---|
508 | }
|
---|
509 |
|
---|
510 | #else
|
---|
511 |
|
---|
512 | void dim_init()
|
---|
513 | {
|
---|
514 | }
|
---|
515 |
|
---|
516 | void dim_init_threads()
|
---|
517 | {
|
---|
518 | }
|
---|
519 |
|
---|
520 | void dim_stop_threads()
|
---|
521 | {
|
---|
522 | }
|
---|
523 |
|
---|
524 | void dim_stop()
|
---|
525 | {
|
---|
526 | }
|
---|
527 |
|
---|
528 | int dim_wait()
|
---|
529 | {
|
---|
530 | pause();
|
---|
531 | return(-1);
|
---|
532 | }
|
---|
533 |
|
---|
534 | dim_long dim_start_thread(void (*thread_ast)(), dim_long tag)
|
---|
535 |
|
---|
536 | {
|
---|
537 | printf("dim_start_thread: not available\n");
|
---|
538 | return (dim_long)0;
|
---|
539 | }
|
---|
540 |
|
---|
541 | int dim_stop_thread(dim_long t_id)
|
---|
542 | {
|
---|
543 | printf("dim_stop_thread: not available\n");
|
---|
544 | return 0;
|
---|
545 | }
|
---|
546 | #endif
|
---|
547 |
|
---|
548 | #else
|
---|
549 | #include <windows.h>
|
---|
550 |
|
---|
551 | DWORD IO_thread = 0;
|
---|
552 | DWORD ALRM_thread = 0;
|
---|
553 | DWORD MAIN_thread = 0;
|
---|
554 | HANDLE hIO_thread;
|
---|
555 | HANDLE hALRM_thread;
|
---|
556 | HANDLE hMAIN_thread;
|
---|
557 | DllExp HANDLE Global_DIM_event_auto = 0;
|
---|
558 | DllExp HANDLE Global_DIM_mutex = 0;
|
---|
559 | DllExp HANDLE Global_DIM_event_manual = 0;
|
---|
560 | void dim_tcpip_stop(), dim_dtq_stop();
|
---|
561 |
|
---|
562 | typedef struct{
|
---|
563 | void (*thread_ast)();
|
---|
564 | dim_long tag;
|
---|
565 |
|
---|
566 | }THREAD_PARAMS;
|
---|
567 |
|
---|
568 | #ifndef STDCALL
|
---|
569 | dim_long dim_start_thread(void (*thread_ast)(), dim_long tag)
|
---|
570 | #else
|
---|
571 | dim_long dim_start_thread(dim_long (*thread_ast)(void *), void *tag)
|
---|
572 | #endif
|
---|
573 | {
|
---|
574 | DWORD threadid = 0;
|
---|
575 | HANDLE hthread;
|
---|
576 |
|
---|
577 | #ifndef STDCALL
|
---|
578 | hthread = CreateThread(
|
---|
579 | NULL, /* no security attributes */
|
---|
580 | 0, /* use default stack size */
|
---|
581 | (void *)thread_ast, /* thread function */
|
---|
582 | (void *)tag, /* argument to thread function */
|
---|
583 | 0, /* use default creation flags */
|
---|
584 | &threadid); /* returns the thread identifier */
|
---|
585 | #else
|
---|
586 | hthread = CreateThread(
|
---|
587 | NULL, /* no security attributes */
|
---|
588 | 0, /* use default stack size */
|
---|
589 | thread_ast, /* thread function */
|
---|
590 | tag, /* argument to thread function */
|
---|
591 | 0, /* use default creation flags */
|
---|
592 | &threadid); /* returns the thread identifier */
|
---|
593 | #endif
|
---|
594 | return (dim_long)hthread;
|
---|
595 | }
|
---|
596 |
|
---|
597 |
|
---|
598 | int dim_stop_thread(dim_long thread_id)
|
---|
599 | {
|
---|
600 | int ret;
|
---|
601 |
|
---|
602 | ret = TerminateThread((HANDLE)thread_id, 0);
|
---|
603 | CloseHandle((HANDLE)thread_id);
|
---|
604 | printf("dim_stop_thread: this function is obsolete, it creates memory leaks\n");
|
---|
605 | return ret;
|
---|
606 | }
|
---|
607 |
|
---|
608 |
|
---|
609 | void create_io_thread()
|
---|
610 | {
|
---|
611 | int tcpip_task(void *);
|
---|
612 |
|
---|
613 | #ifndef STDCALL
|
---|
614 | hIO_thread = CreateThread(
|
---|
615 | NULL, /* no security attributes */
|
---|
616 | 0, /* use default stack size */
|
---|
617 | (void *)tcpip_task, /* thread function */
|
---|
618 | 0, /* argument to thread function */
|
---|
619 | 0, /* use default creation flags */
|
---|
620 | &IO_thread); /* returns the thread identifier */
|
---|
621 | #else
|
---|
622 | hIO_thread = CreateThread(
|
---|
623 | NULL, /* no security attributes */
|
---|
624 | 0, /* use default stack size */
|
---|
625 | tcpip_task, /* thread function */
|
---|
626 | 0, /* argument to thread function */
|
---|
627 | 0, /* use default creation flags */
|
---|
628 | &IO_thread); /* returns the thread identifier */
|
---|
629 | #endif
|
---|
630 | }
|
---|
631 |
|
---|
632 | void create_alrm_thread()
|
---|
633 | {
|
---|
634 |
|
---|
635 | int dtq_task(void *);
|
---|
636 |
|
---|
637 | #ifndef STDCALL
|
---|
638 | hALRM_thread = CreateThread(
|
---|
639 | NULL,
|
---|
640 | 0,
|
---|
641 | (void *)dtq_task,
|
---|
642 | 0,
|
---|
643 | 0,
|
---|
644 | &ALRM_thread);
|
---|
645 | #else
|
---|
646 | hALRM_thread = CreateThread(
|
---|
647 | NULL,
|
---|
648 | 0,
|
---|
649 | dtq_task,
|
---|
650 | 0,
|
---|
651 | 0,
|
---|
652 | &ALRM_thread);
|
---|
653 | #endif
|
---|
654 | }
|
---|
655 |
|
---|
656 | void dim_init_threads()
|
---|
657 | {
|
---|
658 | static int done = 0;
|
---|
659 |
|
---|
660 | if(!done)
|
---|
661 | {
|
---|
662 | hMAIN_thread = GetCurrentThread();
|
---|
663 | done = 1;
|
---|
664 | }
|
---|
665 | }
|
---|
666 |
|
---|
667 | void dim_stop_threads()
|
---|
668 | {
|
---|
669 | /*
|
---|
670 | int i;
|
---|
671 | int n = 0;
|
---|
672 |
|
---|
673 | for( i = 0; i< Curr_N_Conns; i++ )
|
---|
674 | {
|
---|
675 | if(Net_conns[i].channel != 0)
|
---|
676 | n++;
|
---|
677 | }
|
---|
678 | if(n)
|
---|
679 | return;
|
---|
680 | */
|
---|
681 | if(hIO_thread)
|
---|
682 | TerminateThread(hIO_thread, 0);
|
---|
683 | if(hALRM_thread)
|
---|
684 | TerminateThread(hALRM_thread, 0);
|
---|
685 | if(Global_DIM_mutex)
|
---|
686 | CloseHandle(Global_DIM_mutex);
|
---|
687 | if(Global_DIM_event_auto)
|
---|
688 | CloseHandle(Global_DIM_event_auto);
|
---|
689 | if(Global_DIM_event_manual)
|
---|
690 | CloseHandle(Global_DIM_event_manual);
|
---|
691 | hIO_thread = 0;
|
---|
692 | hALRM_thread = 0;
|
---|
693 | Global_DIM_mutex = 0;
|
---|
694 | Global_DIM_event_auto = 0;
|
---|
695 | Global_DIM_event_manual = 0;
|
---|
696 | dim_tcpip_stop();
|
---|
697 | dim_dtq_stop();
|
---|
698 | }
|
---|
699 |
|
---|
700 | void dim_stop()
|
---|
701 | {
|
---|
702 | dim_stop_threads();
|
---|
703 | }
|
---|
704 |
|
---|
705 | int dim_set_scheduler_class(int pclass)
|
---|
706 | {
|
---|
707 | HANDLE hProc;
|
---|
708 | int ret;
|
---|
709 | DWORD p;
|
---|
710 |
|
---|
711 | #ifndef PXI
|
---|
712 | hProc = GetCurrentProcess();
|
---|
713 |
|
---|
714 | if(pclass == -1)
|
---|
715 | p = IDLE_PRIORITY_CLASS;
|
---|
716 | /*
|
---|
717 | else if(pclass == -1)
|
---|
718 | p = BELOW_NORMAL_PRIORITY_CLASS;
|
---|
719 | */
|
---|
720 | else if(pclass == 0)
|
---|
721 | p = NORMAL_PRIORITY_CLASS;
|
---|
722 | /*
|
---|
723 | else if(pclass == 1)
|
---|
724 | p == ABOVE_NORMAL_PRIORITY_CLASS;
|
---|
725 | */
|
---|
726 | else if(pclass == 1)
|
---|
727 | p = HIGH_PRIORITY_CLASS;
|
---|
728 | else if(pclass == 2)
|
---|
729 | p = REALTIME_PRIORITY_CLASS;
|
---|
730 | ret = SetPriorityClass(hProc, p);
|
---|
731 | if(ret)
|
---|
732 | return 1;
|
---|
733 | ret = GetLastError();
|
---|
734 | printf("ret = %x %d\n",ret, ret);
|
---|
735 | return 0;
|
---|
736 | #else
|
---|
737 | return 0;
|
---|
738 | #endif
|
---|
739 | }
|
---|
740 |
|
---|
741 | int dim_get_scheduler_class(int *pclass)
|
---|
742 | {
|
---|
743 | HANDLE hProc;
|
---|
744 | DWORD ret;
|
---|
745 |
|
---|
746 | #ifndef PXI
|
---|
747 | hProc = GetCurrentProcess();
|
---|
748 |
|
---|
749 | ret = GetPriorityClass(hProc);
|
---|
750 | if(ret == 0)
|
---|
751 | return 0;
|
---|
752 | if(ret == IDLE_PRIORITY_CLASS)
|
---|
753 | *pclass = -1;
|
---|
754 | /*
|
---|
755 | else if(ret == BELOW_NORMAL_PRIORITY_CLASS)
|
---|
756 | *pclass = -1;
|
---|
757 | */
|
---|
758 | else if(ret == NORMAL_PRIORITY_CLASS)
|
---|
759 | *pclass = 0;
|
---|
760 | /*
|
---|
761 | else if(ret == ABOVE_NORMAL_PRIORITY_CLASS)
|
---|
762 | *pclass = 1;
|
---|
763 | */
|
---|
764 | else if(ret == HIGH_PRIORITY_CLASS)
|
---|
765 | *pclass = 1;
|
---|
766 | else if(ret == REALTIME_PRIORITY_CLASS)
|
---|
767 | *pclass = 2;
|
---|
768 | return 1;
|
---|
769 | #else
|
---|
770 | *pclass = 0;
|
---|
771 | return 0;
|
---|
772 | #endif
|
---|
773 | }
|
---|
774 |
|
---|
775 | int dim_set_priority(int threadId, int prio)
|
---|
776 | {
|
---|
777 | HANDLE id;
|
---|
778 | int ret, p;
|
---|
779 |
|
---|
780 | #ifndef PXI
|
---|
781 | if(threadId == 1)
|
---|
782 | id = hMAIN_thread;
|
---|
783 | else if(threadId == 2)
|
---|
784 | id = hIO_thread;
|
---|
785 | else if(threadId == 3)
|
---|
786 | id = hALRM_thread;
|
---|
787 |
|
---|
788 | if(prio == -3)
|
---|
789 | p = THREAD_PRIORITY_IDLE;
|
---|
790 | if(prio == -2)
|
---|
791 | p = THREAD_PRIORITY_LOWEST;
|
---|
792 | if(prio == -1)
|
---|
793 | p = THREAD_PRIORITY_BELOW_NORMAL;
|
---|
794 | if(prio == 0)
|
---|
795 | p = THREAD_PRIORITY_NORMAL;
|
---|
796 | if(prio == 1)
|
---|
797 | p = THREAD_PRIORITY_ABOVE_NORMAL;
|
---|
798 | if(prio == 2)
|
---|
799 | p = THREAD_PRIORITY_HIGHEST;
|
---|
800 | if(prio == 3)
|
---|
801 | p = THREAD_PRIORITY_TIME_CRITICAL;
|
---|
802 |
|
---|
803 | ret = SetThreadPriority(id, p);
|
---|
804 | if(ret)
|
---|
805 | return 1;
|
---|
806 | return 0;
|
---|
807 | #else
|
---|
808 | return 0;
|
---|
809 | #endif
|
---|
810 | }
|
---|
811 |
|
---|
812 | int dim_get_priority(int threadId, int *prio)
|
---|
813 | {
|
---|
814 | HANDLE id;
|
---|
815 | int ret, p;
|
---|
816 |
|
---|
817 | #ifndef PXI
|
---|
818 | if(threadId == 1)
|
---|
819 | id = hMAIN_thread;
|
---|
820 | else if(threadId == 2)
|
---|
821 | id = hIO_thread;
|
---|
822 | else if(threadId == 3)
|
---|
823 | id = hALRM_thread;
|
---|
824 |
|
---|
825 | ret = GetThreadPriority(id);
|
---|
826 | if(ret == THREAD_PRIORITY_ERROR_RETURN)
|
---|
827 | return 0;
|
---|
828 | if(ret == THREAD_PRIORITY_IDLE)
|
---|
829 | p = -3;
|
---|
830 | if(ret == THREAD_PRIORITY_LOWEST)
|
---|
831 | p = -2;
|
---|
832 | if(ret == THREAD_PRIORITY_BELOW_NORMAL)
|
---|
833 | p = -1;
|
---|
834 | if(ret == THREAD_PRIORITY_NORMAL)
|
---|
835 | p = 0;
|
---|
836 | if(ret == THREAD_PRIORITY_ABOVE_NORMAL)
|
---|
837 | p = 1;
|
---|
838 | if(ret == THREAD_PRIORITY_HIGHEST)
|
---|
839 | p = 2;
|
---|
840 | if(ret == THREAD_PRIORITY_TIME_CRITICAL)
|
---|
841 | p = 3;
|
---|
842 | *prio = p;
|
---|
843 | return 1;
|
---|
844 | #else
|
---|
845 | *prio = 0;
|
---|
846 | return 0;
|
---|
847 | #endif
|
---|
848 | }
|
---|
849 |
|
---|
850 | void dim_init()
|
---|
851 | {
|
---|
852 | }
|
---|
853 |
|
---|
854 | void dim_no_threads()
|
---|
855 | {
|
---|
856 | }
|
---|
857 |
|
---|
858 | int dim_wait()
|
---|
859 | {
|
---|
860 | pause();
|
---|
861 | return(1);
|
---|
862 | }
|
---|
863 |
|
---|
864 | void dim_lock()
|
---|
865 | {
|
---|
866 | if(!Global_DIM_mutex)
|
---|
867 | {
|
---|
868 | Global_DIM_mutex = CreateMutex(NULL,FALSE,NULL);
|
---|
869 | }
|
---|
870 | WaitForSingleObject(Global_DIM_mutex, INFINITE);
|
---|
871 | }
|
---|
872 |
|
---|
873 | void dim_unlock()
|
---|
874 | {
|
---|
875 | ReleaseMutex(Global_DIM_mutex);
|
---|
876 | }
|
---|
877 |
|
---|
878 | void dim_pause()
|
---|
879 | {
|
---|
880 | HANDLE handles[2];
|
---|
881 |
|
---|
882 | if(!Global_DIM_event_auto)
|
---|
883 | {
|
---|
884 | Global_DIM_event_auto = CreateEvent(NULL,FALSE,FALSE,NULL);
|
---|
885 | Global_DIM_event_manual = CreateEvent(NULL,TRUE,FALSE,NULL);
|
---|
886 | }
|
---|
887 | else
|
---|
888 | {
|
---|
889 | /*
|
---|
890 | WaitForSingleObject(Global_DIM_event, INFINITE);
|
---|
891 | */
|
---|
892 | handles[0] = Global_DIM_event_auto;
|
---|
893 | handles[1] = Global_DIM_event_manual;
|
---|
894 | WaitForMultipleObjects(2, handles, FALSE, INFINITE);
|
---|
895 | }
|
---|
896 | }
|
---|
897 |
|
---|
898 | void dim_wake_up()
|
---|
899 | {
|
---|
900 | if(Global_DIM_event_auto)
|
---|
901 | {
|
---|
902 | SetEvent(Global_DIM_event_auto);
|
---|
903 | }
|
---|
904 | if(Global_DIM_event_manual)
|
---|
905 | {
|
---|
906 | SetEvent(Global_DIM_event_manual);
|
---|
907 | ResetEvent(Global_DIM_event_manual);
|
---|
908 | }
|
---|
909 | }
|
---|
910 |
|
---|
911 | void dim_sleep(unsigned int t)
|
---|
912 | {
|
---|
913 | Sleep(t*1000);
|
---|
914 | }
|
---|
915 |
|
---|
916 | void dim_win_usleep(unsigned int t)
|
---|
917 | {
|
---|
918 | Sleep(t/1000);
|
---|
919 | }
|
---|
920 |
|
---|
921 | #endif
|
---|