Skip to content
Snippets Groups Projects
Commit 003dd196 authored by Kaspar Schleiser's avatar Kaspar Schleiser
Browse files

Merge pull request #1566 from Kijewski/undef_is_null

core: let PIDs begin with 1
parents a48ab812 427a5fba
No related branches found
No related tags found
No related merge requests found
...@@ -5,10 +5,27 @@ ...@@ -5,10 +5,27 @@
#include <inttypes.h> #include <inttypes.h>
/** /**
* @def KERNEL_PID_UNDEF * @def MAXTHREADS
* @brief Identifier to detect an invalid PID * @brief The maximum number of threads to be scheduled
*/ */
#define KERNEL_PID_UNDEF -1 #ifndef MAXTHREADS
#define MAXTHREADS 32
#endif
/**
* Canonical identifier for an invalid PID.
*/
#define KERNEL_PID_UNDEF 0
/**
* The first valid PID (inclusive).
*/
#define KERNEL_PID_FIRST (KERNEL_PID_UNDEF + 1)
/**
* The last valid PID (inclusive).
*/
#define KERNEL_PID_LAST (KERNEL_PID_FIRST + MAXTHREADS - 1)
/** /**
* Macro for printing formatter * Macro for printing formatter
...@@ -16,8 +33,7 @@ ...@@ -16,8 +33,7 @@
#define PRIkernel_pid PRIi16 #define PRIkernel_pid PRIi16
/** /**
* @brief Unique process identifier * Unique process identifier
*
*/ */
typedef int16_t kernel_pid_t; typedef int16_t kernel_pid_t;
......
...@@ -84,8 +84,7 @@ ...@@ -84,8 +84,7 @@
#include "bitarithm.h" #include "bitarithm.h"
#include "tcb.h" #include "tcb.h"
#include "attributes.h" #include "attributes.h"
#include "kernel_types.h"
#define MAXTHREADS 32 /**< the maximum number of threads to be scheduled */
/** /**
* @def SCHED_PRIO_LEVELS * @def SCHED_PRIO_LEVELS
...@@ -132,7 +131,7 @@ extern volatile unsigned int sched_context_switch_request; ...@@ -132,7 +131,7 @@ extern volatile unsigned int sched_context_switch_request;
/** /**
* Thread table * Thread table
*/ */
extern volatile tcb_t *sched_threads[MAXTHREADS]; extern volatile tcb_t *sched_threads[KERNEL_PID_LAST + 1];
/** /**
* Currently active thread * Currently active thread
...@@ -168,7 +167,7 @@ typedef struct { ...@@ -168,7 +167,7 @@ typedef struct {
/** /**
* Thread statistics table * Thread statistics table
*/ */
extern schedstat sched_pidlist[MAXTHREADS]; extern schedstat sched_pidlist[KERNEL_PID_LAST + 1];
/** /**
* @brief Register a callback that will be called on every scheduler run * @brief Register a callback that will be called on every scheduler run
......
...@@ -40,7 +40,7 @@ volatile int sched_num_threads = 0; ...@@ -40,7 +40,7 @@ volatile int sched_num_threads = 0;
volatile unsigned int sched_context_switch_request; volatile unsigned int sched_context_switch_request;
volatile tcb_t *sched_threads[MAXTHREADS]; volatile tcb_t *sched_threads[KERNEL_PID_LAST + 1];
volatile tcb_t *sched_active_thread; volatile tcb_t *sched_active_thread;
volatile kernel_pid_t sched_active_pid = KERNEL_PID_UNDEF; volatile kernel_pid_t sched_active_pid = KERNEL_PID_UNDEF;
...@@ -50,7 +50,7 @@ static uint32_t runqueue_bitcache = 0; ...@@ -50,7 +50,7 @@ static uint32_t runqueue_bitcache = 0;
#if SCHEDSTATISTICS #if SCHEDSTATISTICS
static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL; static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL;
schedstat sched_pidlist[MAXTHREADS]; schedstat sched_pidlist[KERNEL_PID_LAST + 1];
#endif #endif
void sched_run(void) void sched_run(void)
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
volatile tcb_t *thread_get(kernel_pid_t pid) volatile tcb_t *thread_get(kernel_pid_t pid)
{ {
if ((pid != KERNEL_PID_UNDEF) && (0 <= pid) && (pid < MAXTHREADS)) { if ((pid != KERNEL_PID_UNDEF) && (KERNEL_PID_FIRST <= pid) && (pid <= KERNEL_PID_LAST)) {
return sched_threads[pid]; return sched_threads[pid];
} }
return NULL; return NULL;
...@@ -153,19 +153,14 @@ kernel_pid_t thread_create(char *stack, int stacksize, char priority, int flags, ...@@ -153,19 +153,14 @@ kernel_pid_t thread_create(char *stack, int stacksize, char priority, int flags,
dINT(); dINT();
} }
kernel_pid_t pid = 0; kernel_pid_t pid = KERNEL_PID_UNDEF;
for (kernel_pid_t i = KERNEL_PID_FIRST; i <= KERNEL_PID_LAST; ++i) {
while (pid < MAXTHREADS) { if (sched_threads[i] == NULL) {
if (sched_threads[pid] == NULL) { pid = i;
sched_threads[pid] = cb;
cb->pid = pid;
break; break;
} }
pid++;
} }
if (pid == KERNEL_PID_UNDEF) {
if (pid == MAXTHREADS) {
DEBUG("thread_create(): too many threads!\n"); DEBUG("thread_create(): too many threads!\n");
if (!inISR()) { if (!inISR()) {
...@@ -175,6 +170,9 @@ kernel_pid_t thread_create(char *stack, int stacksize, char priority, int flags, ...@@ -175,6 +170,9 @@ kernel_pid_t thread_create(char *stack, int stacksize, char priority, int flags,
return -EOVERFLOW; return -EOVERFLOW;
} }
sched_threads[pid] = cb;
cb->pid = pid;
cb->sp = thread_stack_init(function, arg, stack, stacksize); cb->sp = thread_stack_init(function, arg, stack, stacksize);
cb->stack_start = stack; cb->stack_start = stack;
......
...@@ -91,7 +91,7 @@ static void flip_periodic_interrupt_called(uint8_t reg_c) ...@@ -91,7 +91,7 @@ static void flip_periodic_interrupt_called(uint8_t reg_c)
static void measure_nop_nop_nops_per_tick(void) static void measure_nop_nop_nops_per_tick(void)
{ {
x86_rtc_set_periodic_callback(flip_periodic_interrupt_called); x86_rtc_set_periodic_callback(flip_periodic_interrupt_called);
x86_rtc_set_periodic(TICK_HZ_REG_A, 0, 0, true); x86_rtc_set_periodic(TICK_HZ_REG_A, 0, KERNEL_PID_FIRST, true);
for (unsigned i = 0; i < NNN_TICK_ITERATIONS; ++i) { for (unsigned i = 0; i < NNN_TICK_ITERATIONS; ++i) {
periodic_interrupt_called = false; periodic_interrupt_called = false;
...@@ -132,7 +132,7 @@ static void update_cb(uint8_t reg_c) ...@@ -132,7 +132,7 @@ static void update_cb(uint8_t reg_c)
static void init_bases(void) static void init_bases(void)
{ {
x86_rtc_set_periodic_callback(update_cb); x86_rtc_set_periodic_callback(update_cb);
x86_rtc_set_periodic(RTC_REG_A_HZ_2, 0, 0, true); x86_rtc_set_periodic(RTC_REG_A_HZ_2, 0, KERNEL_PID_FIRST, true);
eINT(); eINT();
periodic_interrupt_called = false; periodic_interrupt_called = false;
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "hwtimer.h" #include "hwtimer.h"
#include "sched.h" #include "sched.h"
#include "tcb.h" #include "tcb.h"
#include "kernel_types.h"
/* list of states copied from tcb.h */ /* list of states copied from tcb.h */
const char *state_names[] = { const char *state_names[] = {
...@@ -40,7 +41,6 @@ const char *state_names[] = { ...@@ -40,7 +41,6 @@ const char *state_names[] = {
void thread_print_all(void) void thread_print_all(void)
{ {
const char queued_name[] = {'_', 'Q'}; const char queued_name[] = {'_', 'Q'};
int i;
#ifdef DEVELHELP #ifdef DEVELHELP
int overall_stacksz = 0, overall_used = 0; int overall_stacksz = 0, overall_used = 0;
#endif #endif
...@@ -56,7 +56,7 @@ void thread_print_all(void) ...@@ -56,7 +56,7 @@ void thread_print_all(void)
"\n" "\n"
, "name", "state"); , "name", "state");
for (i = 0; i < MAXTHREADS; i++) { for (kernel_pid_t i = KERNEL_PID_FIRST; i <= KERNEL_PID_LAST; i++) {
tcb_t *p = (tcb_t *)sched_threads[i]; tcb_t *p = (tcb_t *)sched_threads[i];
if (p != NULL) { if (p != NULL) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment