Use a linked list for threads in each CPU

Allows for unlimited threads in each CPU.
This commit is contained in:
Rodrigo Arias 2021-11-18 17:05:37 +01:00
parent 2e659f6dd3
commit 8153742c1f
3 changed files with 26 additions and 26 deletions

6
emu.h
View File

@ -288,6 +288,10 @@ struct ovni_ethread {
/* Burst times */ /* Burst times */
int nbursts; int nbursts;
int64_t burst_time[MAX_BURSTS]; int64_t burst_time[MAX_BURSTS];
/* These pointers keep a linked list of threads in each CPU */
struct ovni_ethread *prev;
struct ovni_ethread *next;
}; };
/* State of each emulated process */ /* State of each emulated process */
@ -352,7 +356,7 @@ struct ovni_cpu {
/* The threads assigned to this CPU */ /* The threads assigned to this CPU */
size_t nthreads; size_t nthreads;
struct ovni_ethread *thread[OVNI_MAX_THR]; struct ovni_ethread *thread;
/* Running threads */ /* Running threads */
size_t nrunning_threads; size_t nrunning_threads;

View File

@ -19,6 +19,7 @@
#include "emu.h" #include "emu.h"
#include "prv.h" #include "prv.h"
#include "chan.h" #include "chan.h"
#include "utlist.h"
#include <assert.h> #include <assert.h>
@ -137,12 +138,10 @@ static void
cpu_update_th_stats(struct ovni_cpu *cpu) cpu_update_th_stats(struct ovni_cpu *cpu)
{ {
struct ovni_ethread *th, *th_running = NULL, *th_active = NULL; struct ovni_ethread *th, *th_running = NULL, *th_active = NULL;
size_t i;
int active = 0, running = 0; int active = 0, running = 0;
for(i=0; i<cpu->nthreads; i++) DL_FOREACH(cpu->thread, th)
{ {
th = cpu->thread[i];
if(th->state == TH_ST_RUNNING) if(th->state == TH_ST_RUNNING)
{ {
th_running = th; th_running = th;
@ -198,20 +197,18 @@ emu_get_cpu(struct ovni_loom *loom, int cpuid)
return &loom->cpu[cpuid]; return &loom->cpu[cpuid];
} }
static int static struct ovni_ethread *
emu_cpu_find_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread) emu_cpu_find_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread)
{ {
size_t i; struct ovni_ethread *p = NULL;
for(i=0; i<cpu->nthreads; i++) DL_FOREACH(cpu->thread, p)
if(cpu->thread[i] == thread) {
break; if(p == thread)
return p;
}
/* Not found */ return NULL;
if(i >= cpu->nthreads)
return -1;
return i;
} }
/* Add the given thread to the list of threads assigned to the CPU */ /* Add the given thread to the list of threads assigned to the CPU */
@ -219,17 +216,15 @@ static void
cpu_add_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread) cpu_add_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread)
{ {
/* Found, abort */ /* Found, abort */
if(emu_cpu_find_thread(cpu, thread) >= 0) if(emu_cpu_find_thread(cpu, thread) != NULL)
{ {
err("The thread %d is already assigned to %s\n", err("The thread %d is already assigned to %s\n",
thread->tid, cpu->name); thread->tid, cpu->name);
abort(); abort();
} }
/* Ensure that we have room for another thread */ DL_APPEND(cpu->thread, thread);
assert(cpu->nthreads < OVNI_MAX_THR); cpu->nthreads++;
cpu->thread[cpu->nthreads++] = thread;
update_cpu(cpu); update_cpu(cpu);
} }
@ -237,17 +232,19 @@ cpu_add_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread)
static void static void
cpu_remove_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread) cpu_remove_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread)
{ {
int i, j; struct ovni_ethread *p;
i = emu_cpu_find_thread(cpu, thread); p = emu_cpu_find_thread(cpu, thread);
/* Not found, abort */ /* Not found, abort */
if(i < 0) if(p == NULL)
{
err("cannot remove missing thread %d from cpu %s\n",
thread->tid, cpu->name);
abort(); abort();
}
for(j=i; j+1 < (int)cpu->nthreads; j++) DL_DELETE(cpu->thread, thread);
cpu->thread[i] = cpu->thread[j+1];
cpu->nthreads--; cpu->nthreads--;
update_cpu(cpu); update_cpu(cpu);

1
ovni.h
View File

@ -41,7 +41,6 @@ extern "C" {
typedef struct json_value_t JSON_Value; typedef struct json_value_t JSON_Value;
#define OVNI_MAX_CPU 256 #define OVNI_MAX_CPU 256
#define OVNI_MAX_THR 256
#define OVNI_TRACEDIR "ovni" #define OVNI_TRACEDIR "ovni"
#define OVNI_MAX_HOSTNAME 512 #define OVNI_MAX_HOSTNAME 512