changes
[IRC.git] / Robust / src / Runtime / thread.c
1 #include "runtime.h"
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <stdlib.h>
6 #include "thread.h"
7 #include "option.h"
8 #include <signal.h>
9
10 #include <stdio.h>
11 int threadcount;
12 pthread_mutex_t gclock;
13 pthread_mutex_t gclistlock;
14 pthread_cond_t gccond;
15 pthread_mutex_t objlock;
16 pthread_cond_t objcond;
17 pthread_key_t threadlocks;
18
19 void threadexit() {
20 #ifdef THREADS
21   struct ___Object___ *ll=pthread_getspecific(threadlocks);
22   while(ll!=NULL) {
23     struct ___Object___ *llnext=ll->___nextlockobject___;    
24     ll->___nextlockobject___=NULL;
25     ll->___prevlockobject___=NULL;
26     ll->lockcount=0;
27     ll->tid=0; //unlock it
28     ll=llnext;
29   }
30   pthread_mutex_lock(&objlock);//wake everyone up
31   pthread_cond_broadcast(&objcond);
32   pthread_mutex_unlock(&objlock);
33 #endif
34   pthread_mutex_lock(&gclistlock);
35   threadcount--;
36   pthread_cond_signal(&gccond);
37   pthread_mutex_unlock(&gclistlock);
38   pthread_exit(NULL);
39 }
40
41 void threadhandler(int sig, siginfo_t *info, void *uap) {
42 #ifdef DEBUG
43   printf("sig=%d\n",sig);
44   printf("signal\n");
45 #endif
46   threadexit();
47 }
48
49 void initializethreads() {
50   struct sigaction sig;
51   threadcount=1;
52   pthread_mutex_init(&gclock, NULL);
53   pthread_mutex_init(&gclistlock, NULL);
54   pthread_cond_init(&gccond, NULL);
55   pthread_mutex_init(&objlock,NULL);
56   pthread_cond_init(&objcond,NULL);
57   pthread_key_create(&threadlocks, NULL);
58   processOptions();
59   initializeexithandler();
60
61   sig.sa_sigaction=&threadhandler;
62   sig.sa_flags=SA_SIGINFO;
63   sigemptyset(&sig.sa_mask);
64
65   /* Catch bus errors, segmentation faults, and floating point exceptions*/
66   sigaction(SIGBUS,&sig,0);
67   sigaction(SIGSEGV,&sig,0);
68   sigaction(SIGFPE,&sig,0);
69 }
70
71 #ifdef THREADS
72 void initthread(struct ___Thread___ * ___this___) {
73 #ifdef PRECISE_GC
74   struct ___Thread______staticStart____L___Thread____params p={1, NULL, ___this___};
75   ___Thread______staticStart____L___Thread___(&p);
76 #else
77   ___Thread______staticStart____L___Thread___(___this___);
78 #endif
79   pthread_mutex_lock(&gclistlock);
80   threadcount--;
81   pthread_cond_signal(&gccond);
82   pthread_mutex_unlock(&gclistlock);
83 }
84 #endif
85
86 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
87 #ifdef THREADS
88 #ifdef PRECISE_GC
89   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
90 #endif
91 #endif
92   usleep(___millis___);  
93 #ifdef THREADS
94 #ifdef PRECISE_GC
95   restartaftergc(tmp);
96 #endif
97 #endif
98 }
99
100 #ifdef THREADS
101 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
102   pthread_t thread;
103   int retval;
104   pthread_attr_t nattr;
105
106   pthread_mutex_lock(&gclistlock);
107   threadcount++;
108   pthread_mutex_unlock(&gclistlock);
109   pthread_attr_init(&nattr);
110   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
111   
112   do {
113     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
114     if (retval!=0)
115       usleep(1);
116   } while(retval!=0);
117
118   pthread_attr_destroy(&nattr);
119 }
120 #endif