Skip to content

Commit e504187

Browse files
committed
temp code sap
1 parent 8631dff commit e504187

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

kernel/param.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
#define MAXPATH 128 // maximum file path name
1414
#define NDISK 2
1515
#define NNETIF 2
16+
#define NTHREAD 128 // 2*NPROC

kernel/proc.c

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
struct cpu cpus[NCPU];
1313

1414
struct proc proc[NPROC];
15+
struct thread thread[NTHREAD];
1516

1617
struct proc *initproc;
1718

1819
int nextpid = 1;
1920
struct spinlock pid_lock;
2021

22+
int nexttid = 1;
23+
struct spinlock tid_lock;
24+
2125
extern void forkret(void);
2226
static void wakeup1(struct proc *chan);
2327

@@ -74,6 +78,25 @@ myproc(void) {
7478
return p;
7579
}
7680

81+
struct thread*
82+
nextthread(struct proc *p)
83+
{
84+
85+
}
86+
87+
88+
int
89+
alloctid() {
90+
int tid;
91+
92+
acquire(&tid_lock);
93+
tid = nexttid;
94+
nexttid = nexttid + 1;
95+
release(&tid_lock);
96+
97+
return tid;
98+
}
99+
77100
int
78101
allocpid() {
79102
int pid;
@@ -86,6 +109,40 @@ allocpid() {
86109
return pid;
87110
}
88111

112+
static struct thread*
113+
allocthread(uint64 stack, uint64 fnaddr)
114+
{
115+
struct thread *t;
116+
117+
for (t = thread; t < &thread[NTHREAD]; t++)
118+
{
119+
acquire(&t->lock);
120+
if(t->state == UNUSED)
121+
{
122+
goto threadfound;
123+
} else {
124+
release(&t->lock);
125+
}
126+
}
127+
128+
return 0;
129+
130+
threadfound:
131+
t->tid = alloctid();
132+
133+
//TODOS:
134+
// Trapframe
135+
136+
137+
// Set up new context to start executing at forkret,
138+
// which returns to user space.
139+
memset(&t->context, 0, sizeof t->context);
140+
t->context.ra = fnaddr;
141+
t->context.sp = stack;
142+
return t;
143+
144+
}
145+
89146
// Look in the process table for an UNUSED proc.
90147
// If found, initialize state required to run in the kernel,
91148
// and return with p->lock held.
@@ -117,11 +174,20 @@ allocproc(void)
117174
// An empty user page table.
118175
p->pagetable = proc_pagetable(p);
119176

120-
// Set up new context to start executing at forkret,
121-
// which returns to user space.
122-
memset(&p->context, 0, sizeof p->context);
123-
p->context.ra = (uint64)forkret;
124-
p->context.sp = p->kstack + PGSIZE;
177+
// allocate main execution thread
178+
uint64 fnaddr = (uint64)forkret;
179+
uint64 stack = p->kstack + PGSIZE;
180+
181+
struct thread *t = allocthread(p->kstack + PGSIZE, fnaddr);
182+
183+
if(t==0)
184+
{
185+
return 0;
186+
}
187+
188+
// add to process threadpool
189+
p->threads.head = t;
190+
p->threads.next = 0;
125191

126192
return p;
127193
}

kernel/proc.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ struct trapframe {
8282

8383
enum procstate { UNUSED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
8484

85+
struct thread
86+
{
87+
struct spinlock lock;
88+
89+
// t->lock must be held when using these:
90+
int tid;
91+
enum procstate state;
92+
93+
// these are private to thread, lock need not be held
94+
struct context context;
95+
};
96+
97+
struct threadlist
98+
{
99+
struct thread *head;
100+
struct threadlist *next;
101+
};
102+
103+
85104
// Per-process state
86105
struct proc {
87106
struct spinlock lock;
@@ -103,4 +122,11 @@ struct proc {
103122
struct file *ofile[NOFILE]; // Open files
104123
struct inode *cwd; // Current directory
105124
char name[16]; // Process name (debugging)
125+
126+
struct threadlist threads; // Data structure to handle dynamic allocation of threads
106127
};
128+
129+
// refactor tcb pcb
130+
// tcb - 1
131+
// stack - ??
132+
// context - 1

kernel/thread.c

Whitespace-only changes.

0 commit comments

Comments
 (0)