This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
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   struct ___Object___ *ll=pthread_getspecific(threadlocks);
21   while(ll!=NULL) {
22     struct ___Object___ *llnext=ll->___nextlockobject___;    
23     ll->___nextlockobject___=NULL;
24     ll->___prevlockobject___=NULL;
25     ll->lockcount=0;
26     ll->tid=0; //unlock it
27     ll=llnext;
28   }
29   pthread_mutex_lock(&objlock);//wake everyone up
30   pthread_cond_broadcast(&objcond);
31   pthread_mutex_unlock(&objlock);
32   pthread_mutex_lock(&gclistlock);
33   threadcount--;
34   pthread_cond_signal(&gccond);
35   pthread_mutex_unlock(&gclistlock);
36   pthread_exit(NULL);
37 }
38
39 void threadhandler(int sig, siginfo_t *info, void *uap) {
40 #ifdef DEBUG
41   printf("sig=%d\n",sig);
42   printf("signal\n");
43 #endif
44   threadexit();
45 }
46
47 void initializethreads() {
48   struct sigaction sig;
49   threadcount=1;
50   pthread_mutex_init(&gclock, NULL);
51   pthread_mutex_init(&gclistlock, NULL);
52   pthread_cond_init(&gccond, NULL);
53   pthread_mutex_init(&objlock,NULL);
54   pthread_cond_init(&objcond,NULL);
55   pthread_key_create(&threadlocks, NULL);
56   processOptions();
57   initializeexithandler();
58
59   sig.sa_sigaction=&threadhandler;
60   sig.sa_flags=SA_SIGINFO;
61   sigemptyset(&sig.sa_mask);
62
63   /* Catch bus errors, segmentation faults, and floating point exceptions*/
64   sigaction(SIGBUS,&sig,0);
65   sigaction(SIGSEGV,&sig,0);
66   sigaction(SIGFPE,&sig,0);
67 }
68
69 void initthread(struct ___Thread___ * ___this___) {
70 #ifdef PRECISE_GC
71   struct ___Thread______staticStart____L___Thread____params p={1, NULL, ___this___};
72   ___Thread______staticStart____L___Thread___(&p);
73 #else
74   ___Thread______staticStart____L___Thread___(___this___);
75 #endif
76   pthread_mutex_lock(&gclistlock);
77   threadcount--;
78   pthread_cond_signal(&gccond);
79   pthread_mutex_unlock(&gclistlock);
80 }
81
82 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
83 #ifdef THREADS
84 #ifdef PRECISE_GC
85   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
86 #endif
87 #endif
88   usleep(___millis___);  
89 #ifdef THREADS
90 #ifdef PRECISE_GC
91   restartaftergc(tmp);
92 #endif
93 #endif
94 }
95
96 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
97   pthread_t thread;
98   int retval;
99   pthread_attr_t nattr;
100
101   pthread_mutex_lock(&gclistlock);
102   threadcount++;
103   pthread_mutex_unlock(&gclistlock);
104   pthread_attr_init(&nattr);
105   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
106   
107   do {
108     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
109     if (retval!=0)
110       usleep(1);
111   } while(retval!=0);
112
113   pthread_attr_destroy(&nattr);
114 }