UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-11-17 02:45 PM

aaronc
Member
Registered: 2007-04-25
Posts: 8

Re: ThreadCreate

Hi Every1,

I have been given the following spec to write a ThreadCreate, I am totally lost, I only have a 2-3 days to do this. Please help.

Thanks

// ----------------------------------------
// OS_ThreadCreate
//
// Creates a thread so it can be managed by the RTOS.
// Threads can be created either prior to the start of
// multithreading or by a running thread. A thread cannot
// be created by an ISR. A thread must be written as an
// infinite loop and must not return.
//
// Input:
//   thread is a pointer to the thread's code.
//   pData is a pointer to an optional data area used to
//     pass parameters to the thread when it is created.
//   pStack is a pointer to the thread's top-of-stack.
//     The stack is used to store local variables,
//     function parameters, return addresses, and CPU
//     registers during an interrupt.
//   priority is the thread priority. A unique priority
//     number must be assigned to each thread and the
//     lower the number, the higher the priority.
// Output:
//   Returns one of the following error codes:
//   OS_NO_ERROR if the function was successful. 
//   OS_PRIORITY_EXISTS if the requested priority already exists. 
//   OS_PRIORITY_INVALID if priority is higher than OS_LOWEST_PRIORITY. 
//   OS_NO_MORE_TCBS if the RTOS doesn't have any more TCBs to assign.
// Conditions:
//   A thread cannot be created by an ISR.
//   You should not use thread priority OS_LOWEST_PRIORITY
//   because it is reserved for use by the RTOS for the idle thread.


OS_ERROR OS_ThreadCreate(void (*thread)(void *pd), void *pData, void *pStack, UINT8 priority);

Offline

#2 2007-11-17 03:32 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: ThreadCreate

It totally depends on your OS/kernel you're using. And as you're using some sort of real-time OS, we really don't know more than you do. It seems to be targeted for some embedded system. What you must do depends on how much the OS already can do. It seems it supports multitasking between different threads/processes, because of those priority stuff. In that case you just need to write a small wrapper that does the setup of those things, using the existing functionality.

Offline

#3 2007-11-17 09:58 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: ThreadCreate

As i3839 stated - its hard to know what you are asking for without knowing the target RTOS...

Also - what is an ISR... Interrupt Service Register is one that comes to mind... maybe 'internal sub / system routine'... or um... ?

Anyway, if you are using e.g. one of the real time implementations of Linux ( or another OS that supplies a full set of pthreads interfaces ) AND you can use system functions instead of completely rolling your own threading then all you would need to do is:

* retrieves the min / max priority levels for the current scheduler via calls to sched_get_priority_min and sched_get_priority_max

* keeps a global table of existing threads and priorities

* validates the user provided priority is within the min / max range

* validates the user provided priority doesn't have a conflict with the rules prior to startup

* sets up the desired thread stack address and size via an attribute created with a call to pthread_attr_setstack

* call pthread_setschedprio with the desired priority once the new thread is created...


If your OS doesn't provide these functions ( or not being able to use the mysterious ISRs(?) means you have to write your provide all of the POSIX / thread style functionality 'by hand' then... a rough and very incomplete outline of what you need to do is:

* create a thread structure to hold thread states e.g. having variables to contain the thread's function ptr, stack location pointer, stack size, current stack pointer, current code pointer, priority and so forth - these are your control blocks

* create an interrupt style function that has a very precise (* we are talking realtime here *) calling period that is at least several cycles longer than the time required to switch from one thread structure to another i.e. sleeping one thread and starting another

* create the time slicing etc. functions i.e. to keep track of total number of slices required according to number of threads and their priorities so you can calculate when a interrupt ( or the number of interrupts if called in fixed length time segments ) indicates a thread context switch should occur

* a bit of assembly code to pull / modify the return address from the processor register(s) when an interrupt occurs - this is so you can save / change it on the fly and effectively 'switch' threads by ( very carefully ) changing the address...

* a bit of assembly to handle the quirks ( platform dependent ) of doing things like modifying an interrupt return address if the code happens to be in system / kernel code at the time of the interrupt...

* etc. etc. etc. ...

I guess in the end... analysis is if you only have a few days and can use the first outline then no issues... if you have to write everything by hand... you are going to need a lot of caffeine, skill and luck... because its not going to be a simple task...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

Board footer

Powered by FluxBB