forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulib.c
137 lines (116 loc) · 1.75 KB
/
ulib.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
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
125
126
127
128
129
130
131
132
133
134
135
136
#include "types.h"
#include "stat.h"
#include "fcntl.h"
#include "user.h"
#include "x86.h"
void Lock_Init(Lock* mutex)
{
mutex->Is_Locked = 0;
}
void Lock_Acquire(Lock* mutex)
{
__sync_synchronize();
while(xchg(&(mutex->Is_Locked), 1) != 0);
return;
}
void Lock_Release(Lock* mutex)
{
asm volatile("movl $0, %0" : "+m" (mutex->Is_Locked) : );
}
int
thread_create(void (*worker)(void*,void*),void* arg1,void* arg2)
{
void* Child_Stack=malloc(4096);
int Thread_id=clone(worker,arg1,arg2,Child_Stack);
return Thread_id;
}
int thread_join(int thread_id)
{
return join(thread_id);
}
char*
strcpy(char *s, const char *t)
{
char *os;
os = s;
while((*s++ = *t++) != 0)
;
return os;
}
int
strcmp(const char *p, const char *q)
{
while(*p && *p == *q)
p++, q++;
return (uchar)*p - (uchar)*q;
}
uint
strlen(const char *s)
{
int n;
for(n = 0; s[n]; n++)
;
return n;
}
void*
memset(void *dst, int c, uint n)
{
stosb(dst, c, n);
return dst;
}
char*
strchr(const char *s, char c)
{
for(; *s; s++)
if(*s == c)
return (char*)s;
return 0;
}
char*
gets(char *buf, int max)
{
int i, cc;
char c;
for(i=0; i+1 < max; ){
cc = read(0, &c, 1);
if(cc < 1)
break;
buf[i++] = c;
if(c == '\n' || c == '\r')
break;
}
buf[i] = '\0';
return buf;
}
int
stat(const char *n, struct stat *st)
{
int fd;
int r;
fd = open(n, O_RDONLY);
if(fd < 0)
return -1;
r = fstat(fd, st);
close(fd);
return r;
}
int
atoi(const char *s)
{
int n;
n = 0;
while('0' <= *s && *s <= '9')
n = n*10 + *s++ - '0';
return n;
}
void*
memmove(void *vdst, const void *vsrc, int n)
{
char *dst;
const char *src;
dst = vdst;
src = vsrc;
while(n-- > 0)
*dst++ = *src++;
return vdst;
}