-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.c
92 lines (91 loc) · 1.86 KB
/
thread.c
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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "thread.h"
struct threadLib *head,*tail;
void initlock(struct ticketLock *lk)
{
lk->ticketNumber = 0;
}
static inline int fetch_and_add(int* variable, int value)
{
__asm__ volatile("lock; xaddl %0, %1"
: "+r" (value), "+m" (*variable) // input + output
: // No input-only
: "memory"
);
return value;
}
void ticketLock_acquire(struct ticketLock *lk)
{
while (fetch_and_add(&lk->ticketNumber, 1) != 0)
;
__sync_synchronize();
}
void ticketLock_release(struct ticketLock *lk)
{
__sync_synchronize();
lk->ticketNumber = 0;
}
void thread_Add(threadLib *th){
if(!head){
head=th;
tail=th;
}
else{
tail->next=th;
th->prev=tail;
tail=th;
}
}
void thread_delete(int tid){
threadLib *p=head,*q=0;
while(p->tid!=tid && p->next){
q=p;
p=p->next;
}
if(p==head){
head=p->next;
free(p->stack-4096);
free(p);
}
else{
q->next=p->next;
p->next->prev=q;
if(p==tail){
tail=q;
}
free(p->stack-4096);
free(p);
}
}
int thread_create(int (*func)(void *),void *args){
threadLib* th=(threadLib*)malloc(sizeof(threadLib));
th->next=0;
th->prev=0;
char *stack=(char *)malloc(STACK_SIZE);
th->arg=(char *)args;
if(!stack){
printf(1,"Stack not allocated \n");
return -1;
}
th->stack=stack+4096;
th->tid=clone(func,th->stack,CLONE_VM |CLONE_THREAD,args);
sleep(5);
if(th->tid==-1){
printf(1,"Clone Failed \n");
return -1;
}
thread_Add(th);
return th->tid;
}
int thread_join(int tid){
int retID=join(tid);
thread_delete(tid);
return retID;
}
int thread_exit(int tid){
int x=tkill(tid);
thread_delete(tid);
return x;
}