Renamed thread/team syscalls to new scheme.

Minor cleanups.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6887 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2004-03-03 01:15:48 +00:00
parent 96246c6b3b
commit 025019e571
6 changed files with 162 additions and 129 deletions
+14 -7
View File
@@ -1,10 +1,17 @@
/*
** Copyright 2003-2004, The OpenBeOS Team. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <syscalls.h> #include <syscalls.h>
int main(int argc, char **argv) int
main(int argc, char **argv)
{ {
int i, rc; int i, rc;
team_id pid; team_id pid;
@@ -21,18 +28,18 @@ int main(int argc, char **argv)
printf("setenv() error\n"); printf("setenv() error\n");
return -1; return -1;
} }
printf("Getting VAR_1: "); printf("Getting VAR_1: ");
var = getenv("VAR_1"); var = getenv("VAR_1");
if (var) if (var)
printf("found set to: \"%s\"\n", var); printf("found set to: \"%s\"\n", var);
else else
printf("not found\n"); printf("not found\n");
printf("List of env variables set:\n"); printf("List of env variables set:\n");
for (i=0; environ[i]; i++) for (i = 0; environ[i]; i++)
printf("%s\n", environ[i]); printf("%s\n", environ[i]);
if (argc > 1) { if (argc > 1) {
char buffer[16]; char buffer[16];
char *_argv[] = { argv[0], buffer, NULL }; char *_argv[] = { argv[0], buffer, NULL };
@@ -47,8 +54,8 @@ int main(int argc, char **argv)
if (val > 0) { if (val > 0) {
printf("Spawning test (%d left)\n", val); printf("Spawning test (%d left)\n", val);
sprintf(buffer, "%d", val); sprintf(buffer, "%d", val);
pid = sys_create_team(_argv[0], _argv[0], _argv, 2, NULL, 0, 5); pid = _kern_create_team(_argv[0], _argv[0], _argv, 2, NULL, 0, 5);
sys_wait_on_team(pid, &rc); _kern_wait_for_team(pid, &rc);
} }
} }
return 0; return 0;
+23 -20
View File
@@ -3,34 +3,37 @@
** Distributed under the terms of the NewOS License. ** Distributed under the terms of the NewOS License.
*/ */
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <syscalls.h> #include <syscalls.h>
static static
void void
usage(char const *app) usage(char const *app)
{ {
printf("usage: %s [-s] ###\n", app); printf("usage: %s [-s] ###\n", app);
sys_exit(-1); _kern_exit(-1);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int num= 0; int num = 0;
int silent= 0; int silent = 0;
int result; int result;
switch(argc) { switch (argc) {
case 2: case 2:
num= atoi(argv[1]); num = atoi(argv[1]);
break; break;
case 3: case 3:
if(strcmp(argv[1], "-s")== 0) { if (strcmp(argv[1], "-s") == 0) {
num= atoi(argv[2]); num = atoi(argv[2]);
silent= 1; silent = 1;
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
@@ -40,27 +43,27 @@ main(int argc, char *argv[])
break; break;
} }
if(num < 2) { if (num < 2) {
result= 1; result = 1;
} else { } else {
team_id pid; team_id pid;
int retcode; int retcode;
char buffer[64]; char buffer[64];
char *aaargv[]= { "/boot/bin/fibo", "-s", buffer, NULL }; char *aaargv[] = { "/boot/bin/fibo", "-s", buffer, NULL };
int aaargc= 3; int aaargc = 3;
sprintf(buffer, "%d", num-1); sprintf(buffer, "%d", num-1);
pid= sys_create_team(aaargv[0], aaargv[0], aaargv, aaargc, NULL, 0, 5); pid = _kern_create_team(aaargv[0], aaargv[0], aaargv, aaargc, NULL, 0, 5);
sys_wait_on_team(pid, &retcode); _kern_wait_for_team(pid, &retcode);
result= retcode; result = retcode;
sprintf(buffer, "%d", num-2); sprintf(buffer, "%d", num-2);
pid= sys_create_team(aaargv[0], aaargv[0], aaargv, aaargc, NULL, 0, 5); pid = _kern_create_team(aaargv[0], aaargv[0], aaargv, aaargc, NULL, 0, 5);
sys_wait_on_team(pid, &retcode); _kern_wait_for_team(pid, &retcode);
result+= retcode; result += retcode;
} }
if(silent) { if (silent) {
return result; return result;
} else { } else {
printf("%d\n", result); printf("%d\n", result);
+18 -20
View File
@@ -26,40 +26,38 @@ main(void)
} }
rc = fstat(fd, &stat); rc = fstat(fd, &stat);
if(rc < 0) { if (rc < 0) {
printf("Cookie monster was here!!!\n"); printf("Cookie monster was here!!!\n");
sys_exit(1); exit(1);
} }
buf= malloc(stat.st_size + 1); buf = malloc(stat.st_size + 1);
read(fd, buf, stat.st_size); read(fd, buf, stat.st_size);
buf[stat.st_size]= 0; buf[stat.st_size] = 0;
close(fd); close(fd);
found= 0; found = 0;
for(i= 0; i< stat.st_size; i++) { for (i = 0; i < stat.st_size; i++) {
if(strncmp(buf+i, "#@#", 3)== 0) { if (!strncmp(buf + i, "#@#", 3))
found+= 1; found += 1;
}
} }
found = 1 + (sys_system_time() % found); found = 1 + (system_time() % found);
for(i = 0; i < stat.st_size; i++) { for (i = 0; i < stat.st_size; i++) {
if(strncmp(buf+i, "#@#", 3)== 0) { if (!strncmp(buf + i, "#@#", 3))
found-= 1; found -= 1;
}
if(found== 0) { if (found == 0) {
unsigned j; unsigned j;
for(j= i+1; j< stat.st_size; j++) { for (j = i + 1; j < stat.st_size; j++) {
if(strncmp(buf+j, "#@#", 3)== 0) { if (!strncmp(buf + j, "#@#", 3))
buf[j]= 0; buf[j] = 0;
}
} }
printf("%s\n", buf+i+3); printf("%s\n", buf + i + 3);
break; break;
} }
} }
+18 -19
View File
@@ -2,59 +2,58 @@
** Copyright 2001, Travis Geiselbrecht. All rights reserved. ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License. ** Distributed under the terms of the NewOS License.
*/ */
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <syscalls.h> #include <syscalls.h>
#include <fcntl.h> #include <fcntl.h>
static void setup_io()
static void
setup_io(void)
{ {
int i; int i;
for(i= 0; i< 256; i++) { for (i = 0; i< 256; i++) {
close(i); close(i);
} }
/* XXX - open currently ignores these flags, but they
* should be checked once it doesn't :)
* Is STDERR really O_WRONLY?
*/
open("/dev/console", O_RDONLY, 0); /* stdin */ open("/dev/console", O_RDONLY, 0); /* stdin */
open("/dev/console", O_WRONLY, 0); /* stdout */ open("/dev/console", O_WRONLY, 0); /* stdout */
open("/dev/console", O_WRONLY, 0); /* stderr */ open("/dev/console", O_WRONLY, 0); /* stderr */
} }
int main()
int
main()
{ {
setup_io(); setup_io();
printf("Welcome to OpenBeOS!\n"); printf("Welcome to OpenBeOS!\n");
if(1) { {
team_id pid; team_id pid;
pid = sys_create_team("/boot/bin/fortune", "/boot/bin/fortune", NULL, 0, NULL, 0, 5); pid = _kern_create_team("/boot/bin/fortune", "/boot/bin/fortune", NULL, 0, NULL, 0, 5);
if (pid >= 0) { if (pid >= 0) {
int retcode; int retcode;
sys_wait_on_team(pid, &retcode); _kern_wait_for_team(pid, &retcode);
} else { } else
printf("Failed to create a team for fortune.\n"); printf("Failed to create a team for fortune.\n");
}
} }
while (1) {
while(1) {
team_id pid; team_id pid;
pid = sys_create_team("/boot/bin/shell", "/boot/bin/shell", NULL, 0, NULL, 0, 5); pid = _kern_create_team("/boot/bin/shell", "/boot/bin/shell", NULL, 0, NULL, 0, 5);
if(pid >= 0) { if (pid >= 0) {
int retcode; int retcode;
printf("init: spawned shell, pid 0x%lx\n", pid); printf("init: spawned shell, pid 0x%lx\n", pid);
sys_wait_on_team(pid, &retcode); _kern_wait_for_team(pid, &retcode);
printf("init: shell exited with return code %d\n", retcode); printf("init: shell exited with return code %d\n", retcode);
} else { } else
printf("Failed to start a shell :(\n"); printf("Failed to start a shell :(\n");
}
} }
printf("init exiting\n"); printf("init exiting\n");
+62 -47
View File
@@ -14,7 +14,10 @@
static void port_test(void); static void port_test(void);
static int32 port_test_thread_func(void* arg); static int32 port_test_thread_func(void* arg);
static int32 test_thread(void *args)
#if 0
static int32
test_thread(void *args)
{ {
int i = (int)args; int i = (int)args;
@@ -24,19 +27,28 @@ static int32 test_thread(void *args)
} }
return 0; return 0;
} }
#endif
static int32 dummy_thread(void *args)
static int32
dummy_thread(void *args)
{ {
return 1; return 1;
} }
static int32 cpu_eater_thread(void *args)
#if 0
static int32
cpu_eater_thread(void *args)
{ {
for(;;) for(;;)
; ;
} }
#endif
static int32 fpu_cruncher_thread(void *args)
static int32
fpu_cruncher_thread(void *args)
{ {
double y = *(double *)args; double y = *(double *)args;
double z; double z;
@@ -337,16 +349,16 @@ int main(int argc, char **argv)
printf("%d...", i); printf("%d...", i);
t = sys_system_time(); t = system_time();
id = sys_create_team("/boot/bin/true", "true", NULL, 0, NULL, 0, 20); id = _kern_create_team("/boot/bin/true", "true", NULL, 0, NULL, 0, 20);
if(id <= 0x2) { if(id <= 0x2) {
printf("new team returned 0x%lx!\n", id); printf("new team returned 0x%lx!\n", id);
return -1; return -1;
} }
sys_wait_on_team(id, NULL); _kern_wait_for_team(id, NULL);
printf("done (%Ld usecs)\n", sys_system_time() - t); printf("done (%Ld usecs)\n", system_time() - t);
} }
} }
#endif #endif
@@ -361,21 +373,21 @@ int main(int argc, char **argv)
printf("spawning %d threads\n", 100); printf("spawning %d threads\n", 100);
for(i=0; i<100; i++) { for (i = 0; i < 100; i++) {
thread_id id; thread_id id;
bigtime_t t; bigtime_t t;
printf("%d...", i); printf("%d...", i);
t = sys_system_time(); t = system_time();
id = spawn_thread(&dummy_thread, "testthread", B_NORMAL_PRIORITY, NULL); id = spawn_thread(&dummy_thread, "testthread", B_NORMAL_PRIORITY, NULL);
if (id > 0) if (id > 0)
resume_thread(id); resume_thread(id);
sys_wait_on_thread(id, NULL); wait_for_thread(id, NULL);
printf("done (%Ld usecs)\n", sys_system_time() - t); printf("done (%Ld usecs)\n", system_time() - t);
} }
} }
#endif #endif
@@ -416,7 +428,8 @@ int main(int argc, char **argv)
port_id test_p1, test_p2, test_p3, test_p4; port_id test_p1, test_p2, test_p3, test_p4;
static void port_test(void) static void
port_test(void)
{ {
char testdata[5]; char testdata[5];
thread_id t; thread_id t;
@@ -426,29 +439,29 @@ static void port_test(void)
strcpy(testdata, "abcd"); strcpy(testdata, "abcd");
printf("porttest: port_create()\n"); printf("porttest: create_port()\n");
test_p1 = sys_port_create(1, "test port #1"); test_p1 = create_port(1, "test port #1");
test_p2 = sys_port_create(10, "test port #2"); test_p2 = create_port(10, "test port #2");
test_p3 = sys_port_create(1024, "test port #3"); test_p3 = create_port(1024, "test port #3");
test_p4 = sys_port_create(1024, "test port #4"); test_p4 = create_port(1024, "test port #4");
printf("porttest: port_find()\n"); printf("porttest: find_port()\n");
printf("'test port #1' has id %ld (should be %ld)\n", sys_port_find("test port #1"), test_p1); printf("'test port #1' has id %ld (should be %ld)\n", find_port("test port #1"), test_p1);
printf("porttest: port_write() on 1, 2 and 3\n"); printf("porttest: write_port() on 1, 2 and 3\n");
sys_port_write(test_p1, 1, &testdata, sizeof(testdata)); write_port(test_p1, 1, &testdata, sizeof(testdata));
sys_port_write(test_p2, 666, &testdata, sizeof(testdata)); write_port(test_p2, 666, &testdata, sizeof(testdata));
sys_port_write(test_p3, 999, &testdata, sizeof(testdata)); write_port(test_p3, 999, &testdata, sizeof(testdata));
printf("porttest: port_count(test_p1) = %ld\n", sys_port_count(test_p1)); printf("porttest: port_count(test_p1) = %ld\n", port_count(test_p1));
printf("porttest: port_write() on 1 with timeout of 1 sec (blocks 1 sec)\n"); printf("porttest: write_port() on 1 with timeout of 1 sec (blocks 1 sec)\n");
sys_port_write_etc(test_p1, 1, &testdata, sizeof(testdata), B_TIMEOUT, 1000000); write_port_etc(test_p1, 1, &testdata, sizeof(testdata), B_TIMEOUT, 1000000);
printf("porttest: port_write() on 2 with timeout of 1 sec (wont block)\n"); printf("porttest: write_port() on 2 with timeout of 1 sec (wont block)\n");
res = sys_port_write_etc(test_p2, 777, &testdata, sizeof(testdata), B_TIMEOUT, 1000000); res = write_port_etc(test_p2, 777, &testdata, sizeof(testdata), B_TIMEOUT, 1000000);
printf("porttest: res=%d, %s\n", res, res == 0 ? "ok" : "BAD"); printf("porttest: res=%d, %s\n", res, res == 0 ? "ok" : "BAD");
printf("porttest: port_read() on empty port 4 with timeout of 1 sec (blocks 1 sec)\n"); printf("porttest: read_port() on empty port 4 with timeout of 1 sec (blocks 1 sec)\n");
res = sys_port_read_etc(test_p4, &dummy, &dummy2, sizeof(dummy2), B_TIMEOUT, 1000000); res = read_port_etc(test_p4, &dummy, &dummy2, sizeof(dummy2), B_TIMEOUT, 1000000);
printf("porttest: res=%d, %s\n", res, res == ETIMEDOUT ? "ok" : "BAD"); printf("porttest: res=%d, %s\n", res, res == ETIMEDOUT ? "ok" : "BAD");
printf("porttest: spawning thread for port 1\n"); printf("porttest: spawning thread for port 1\n");
@@ -457,30 +470,32 @@ static void port_test(void)
resume_thread(t); resume_thread(t);
printf("porttest: write\n"); printf("porttest: write\n");
sys_port_write(test_p1, 1, &testdata, sizeof(testdata)); write_port(test_p1, 1, &testdata, sizeof(testdata));
// now we can write more (no blocking) // now we can write more (no blocking)
printf("porttest: write #2\n"); printf("porttest: write #2\n");
sys_port_write(test_p1, 2, &testdata, sizeof(testdata)); write_port(test_p1, 2, &testdata, sizeof(testdata));
printf("porttest: write #3\n"); printf("porttest: write #3\n");
sys_port_write(test_p1, 3, &testdata, sizeof(testdata)); write_port(test_p1, 3, &testdata, sizeof(testdata));
printf("porttest: waiting on spawned thread\n"); printf("porttest: waiting on spawned thread\n");
sys_wait_on_thread(t, NULL); wait_for_thread(t, NULL);
printf("porttest: close p1\n"); printf("porttest: close p1\n");
sys_port_close(test_p2); close_port(test_p2);
printf("porttest: attempt write p1 after close\n"); printf("porttest: attempt write p1 after close\n");
res = sys_port_write(test_p2, 4, &testdata, sizeof(testdata)); res = write_port(test_p2, 4, &testdata, sizeof(testdata));
printf("porttest: port_write ret %s (should give Bad port id)\n", strerror(res)); printf("porttest: write_port ret %s (should give Bad port id)\n", strerror(res));
printf("porttest: testing delete p2\n"); printf("porttest: testing delete p2\n");
sys_port_delete(test_p2); delete_port(test_p2);
printf("porttest: end test main thread\n"); printf("porttest: end test main thread\n");
} }
static int32 port_test_thread_func(void* arg)
static int32
port_test_thread_func(void* arg)
{ {
int32 msg_code; int32 msg_code;
int n; int n;
@@ -488,16 +503,16 @@ static int32 port_test_thread_func(void* arg)
printf("porttest: port_test_thread_func()\n"); printf("porttest: port_test_thread_func()\n");
n = sys_port_read(test_p1, &msg_code, &buf, 3); n = read_port(test_p1, &msg_code, &buf, 3);
printf("port_read #1 code %ld len %d buf %3s\n", msg_code, n, buf); printf("read_port #1 code %ld len %d buf %3s\n", msg_code, n, buf);
n = sys_port_read(test_p1, &msg_code, &buf, 4); n = read_port(test_p1, &msg_code, &buf, 4);
printf("port_read #1 code %ld len %d buf %4s\n", msg_code, n, buf); printf("read_port #1 code %ld len %d buf %4s\n", msg_code, n, buf);
buf[4] = 'X'; buf[4] = 'X';
n = sys_port_read(test_p1, &msg_code, &buf, 5); n = read_port(test_p1, &msg_code, &buf, 5);
printf("port_read #1 code %ld len %d buf %5s\n", msg_code, n, buf); printf("read_port #1 code %ld len %d buf %5s\n", msg_code, n, buf);
printf("porttest: testing delete p1 from other thread\n"); printf("porttest: testing delete p1 from other thread\n");
sys_port_delete(test_p1); delete_port(test_p1);
printf("porttest: end port_test_thread_func()\n"); printf("porttest: end port_test_thread_func()\n");
return 0; return 0;
+27 -16
View File
@@ -1,3 +1,9 @@
/*
** Copyright 2002-2004, The OpenBeOS Team. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@@ -17,7 +23,9 @@ struct the_test {
sem_id the_lock; sem_id the_lock;
}; };
static int32 counter_thread(void *data)
static int32
counter_thread(void *data)
{ {
struct the_test *tt = (struct the_test*)data; struct the_test *tt = (struct the_test*)data;
@@ -32,11 +40,12 @@ static int32 counter_thread(void *data)
} }
static int32 priority_test(void *data) static int32
priority_test(void *data)
{ {
int i; int i;
for (i=0; i<6; i++) { for (i = 0; i < 6; i++) {
printf("%s: %d\n", (char *)data, i); printf("%s: %d\n", (char *)data, i);
// sys_snooze(1000); // sys_snooze(1000);
} }
@@ -44,14 +53,15 @@ static int32 priority_test(void *data)
} }
static int32 communication_test(void *data) static int32
communication_test(void *data)
{ {
int times = (int)data; int times = (int)data;
int i, code; int i, code;
char buffer[1024]; char buffer[1024];
thread_id sender; thread_id sender;
for (i=0; i<times; i++) { for (i = 0; i < times; i++) {
code = receive_data(&sender, (void *)buffer, sizeof(buffer)); code = receive_data(&sender, (void *)buffer, sizeof(buffer));
printf(" -> received (%d): \"%s\"\n", code, buffer); printf(" -> received (%d): \"%s\"\n", code, buffer);
} }
@@ -59,7 +69,8 @@ static int32 communication_test(void *data)
} }
int main(int argc, char **argv) int
main(int argc, char **argv)
{ {
thread_id t[THREADS]; thread_id t[THREADS];
sem_id lock; sem_id lock;
@@ -75,8 +86,8 @@ int main(int argc, char **argv)
"=============================\n"); "=============================\n");
lock = create_sem(1, "test_lock"); lock = create_sem(1, "test_lock");
for (i=0;i<THREADS;i++) { for (i = 0; i < THREADS; i++) {
struct the_test *my_test = (struct the_test*)malloc(sizeof(struct the_test)); struct the_test *my_test = (struct the_test*)malloc(sizeof(struct the_test));
my_test->the_lock = lock; my_test->the_lock = lock;
my_test->current_val = &current_val; my_test->current_val = &current_val;
@@ -89,7 +100,7 @@ int main(int argc, char **argv)
} }
} }
sys_wait_on_thread(t[THREADS - 1], NULL); wait_for_thread(t[THREADS - 1], NULL);
printf("Test value = %d vs expected of %d\n", current_val, expected); printf("Test value = %d vs expected of %d\n", current_val, expected);
@@ -105,24 +116,24 @@ int main(int argc, char **argv)
printf("Snoozing...\n"); printf("Snoozing...\n");
snooze(100000); snooze(100000);
printf("Waiting for threads..."); printf("Waiting for threads...");
sys_wait_on_thread(t[0], NULL); wait_for_thread(t[0], NULL);
printf("1, "); printf("1, ");
sys_wait_on_thread(t[1], NULL); wait_for_thread(t[1], NULL);
printf("2, "); printf("2, ");
sys_wait_on_thread(t[2], NULL); wait_for_thread(t[2], NULL);
printf("3.\nDone. Spawning commthread...\n"); printf("3.\nDone. Spawning commthread...\n");
t[0] = spawn_thread(communication_test, "commthread", B_NORMAL_PRIORITY, (void *)5); t[0] = spawn_thread(communication_test, "commthread", B_NORMAL_PRIORITY, (void *)5);
printf("Spawned. Starting communication...\n"); printf("Spawned. Starting communication...\n");
resume_thread(t[0]); resume_thread(t[0]);
for (i=0; i<5; i++) { for (i = 0; i < 5; i++) {
printf("sending"); printf("sending");
send_data(t[0], i, comm_test[i], strlen(comm_test[i]) + 1); send_data(t[0], i, comm_test[i], strlen(comm_test[i]) + 1);
// Give time to the commthread to display info // Give time to the commthread to display info
snooze(10000); snooze(10000);
} }
sys_wait_on_thread(t[0], NULL); wait_for_thread(t[0], NULL);
return 0; return 0;
} }