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