source: branches/fscctrl_safety_limits/dim/src/dim_thr_old.c

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