Simplified threads implementation somewhat

This commit is contained in:
Devine Lu Linvega 2023-05-03 11:32:38 -07:00
parent 29e130ace0
commit 7d1c62b2cb
1 changed files with 18 additions and 27 deletions

View File

@ -75,14 +75,11 @@ system_inspect(Uxn *u)
system_print(&u->rst, "rst"); system_print(&u->rst, "rst");
} }
/* Friends */ static Uint8 *global_ram, friends_count = 0;
static Uint16 friends_tasks[MAX_FRIENDS];
static pthread_t friends[MAX_FRIENDS];
Uint8 *global_ram; static void *
Uint16 friends_tasks[MAX_FRIENDS];
pthread_t friends[MAX_FRIENDS];
int friends_count = 0;
void *
friend_task(void *vector) friend_task(void *vector)
{ {
Uxn friend; Uxn friend;
@ -91,26 +88,20 @@ friend_task(void *vector)
return NULL; return NULL;
} }
int static void
wait_friends(void) system_friend(Uint8 *ram, Uint16 task)
{ {
int i; if(!task) {
for(i = 0; i < friends_count; ++i) while(friends_count)
pthread_join(friends[i], NULL); pthread_join(friends[--friends_count], NULL);
return friends_count = 0; } else if(friends_count >= MAX_FRIENDS) {
} system_error("friends", "Too many threads");
} else {
int
spawn_friend(Uint8 *ram, Uint16 task)
{
if(!task)
return wait_friends();
if(friends_count >= MAX_FRIENDS)
return system_error("friends", "Too many threads");
global_ram = ram; global_ram = ram;
friends_tasks[friends_count] = task; friends_tasks[friends_count] = task;
pthread_create(&friends[friends_count], NULL, friend_task, (void *)&friends_tasks[friends_count]); pthread_create(&friends[friends_count], NULL, friend_task, (void *)&friends_tasks[friends_count]);
return friends_count++; friends_count++;
}
} }
/* IO */ /* IO */
@ -123,7 +114,7 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port)
system_cmd(u->ram, PEEK2(d + 2)); system_cmd(u->ram, PEEK2(d + 2));
break; break;
case 0x5: case 0x5:
spawn_friend(u->ram, PEEK2(d + 4)); system_friend(u->ram, PEEK2(d + 4));
break; break;
case 0xe: case 0xe:
system_inspect(u); system_inspect(u);