aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/main.c
blob: 1acd8348bb1099998262866f7e35d4c6369230ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "threadimpl.h"
#include <signal.h>

typedef struct Mainarg Mainarg;
struct Mainarg
{
	int	argc;
	char	**argv;
};

int	mainstacksize;
int	_threadnotefd;
int	_threadpasserpid;
static void mainlauncher(void*);
extern void (*_sysfatal)(char*, va_list);

void
_threaddie(int x)
{
	extern char *_threadexitsallstatus;
	USED(x);

	if(_threadexitsallstatus)
		exit(_threadexitsallstatus[0] ? 1 : 0);
}

int
main(int argc, char **argv)
{
	Mainarg *a;
	Proc *p;

	signal(SIGTERM, _threaddie);
//	rfork(RFREND);

//_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0;
	_systhreadinit();
	_qlockinit(_threadrendezvous);
	_sysfatal = _threadsysfatal;
//	notify(_threadnote);
	if(mainstacksize == 0)
		mainstacksize = 32*1024;

	a = _threadmalloc(sizeof *a, 1);
	a->argc = argc;
	a->argv = argv;

	p = _newproc(mainlauncher, a, mainstacksize, "threadmain", 0, 0);
	_schedinit(p);
	abort();	/* not reached */
	return 0;
}

static void
mainlauncher(void *arg)
{
	Mainarg *a;

	a = arg;
	threadmain(a->argc, a->argv);
	threadexits("threadmain");
}

void
_threadsignal(void)
{
}

void
_threadsignalpasser(void)
{
}

int
_schedfork(Proc *p)
{
	return ffork(RFMEM|RFNOWAIT, _schedinit, p);
}

void
_schedexit(Proc *p)
{
	char ex[ERRMAX];
	Proc **l;

	lock(&_threadpq.lock);
	for(l=&_threadpq.head; *l; l=&(*l)->next){
		if(*l == p){
			*l = p->next;
			if(*l == nil)
				_threadpq.tail = l;
			break;
		}
	}
	unlock(&_threadpq.lock);

	strncpy(ex, p->exitstr, sizeof ex);
	ex[sizeof ex-1] = '\0';
	free(p);
	_exit(ex[0]);
}

int
nrand(int n)
{
	return random()%n;
}

void
_systhreadinit(void)
{
}

void
threadstats(void)
{
	extern int _threadnrendez, _threadhighnrendez,
		_threadnalt, _threadhighnentry;
	fprint(2, "*** THREAD LIBRARY STATS ***\n");
	fprint(2, "nrendez %d high simultaneous %d\n", 
		_threadnrendez, _threadhighnrendez);
	fprint(2, "nalt %d high simultaneous entry %d\n",
		_threadnalt, _threadhighnentry);
}