core: introduce KERNEL_PID_FIRST and KERNEL_PID_LAST

This commit is contained in:
René Kijewski 2014-08-13 09:08:50 +02:00
parent 6999e6fb21
commit b31e5a8675
4 changed files with 32 additions and 19 deletions

View File

@ -5,19 +5,35 @@
#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
*/
#ifndef MAXTHREADS
#define MAXTHREADS 32
#endif
/**
* Canonical identifier for an invalid PID.
*/ */
#define KERNEL_PID_UNDEF -1 #define KERNEL_PID_UNDEF -1
/**
* 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
*/ */
#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;

View File

@ -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

View File

@ -39,7 +39,7 @@ inline kernel_pid_t thread_getpid(void)
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;
@ -158,19 +158,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()) {
@ -180,6 +175,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;

View File

@ -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[] = {
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)
"\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) {