Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread-local memory #506

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/include/tcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ typedef struct tcb_t {
cib_t msg_queue;
msg_t *msg_array;

char *lcoal_mem;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?


const char *name;
char *stack_start;
int stack_size;
Expand Down
32 changes: 32 additions & 0 deletions core/include/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*
* @author Freie Universität Berlin, Computer Systems & Telematics
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/

#ifndef __THREAD_H
Expand Down Expand Up @@ -49,6 +50,37 @@
*/
int thread_create(char *stack, int stacksize, char priority, int flags, void (*function) (void), const char *name);


/**
* @brief Reserve a given amount of memory on the top of the stack as thread local memory
*
* Always reserve the full number size of bytes or none, do not try to fit as many bytes as possible into the stack
*
* @param stack Lowest address of preallocated stack space
* @param stacksize
* @param localmemsize number of bytes to reserve for the thread-local memory (taken from given stack memory)
* @param flags Options:
* YIELD: force context switch.
* CREATE_SLEEPING: set new thread to sleeping state, thread must be woken up manually.
* CREATE_STACKTEST: initialize stack with values needed for stack overflow testing.
*
* @param priority Priority of newly created thread. Lower number means higher
* priority. 0 means highest possible priority. Lowest priority is
* PRIORITY_IDLE-1, usually 30.
*
* @return returns <0 on error, pid of newly created task else.
*/
int thread_create_with_local_mem(char *stack, int stacksize, int localmemsize, char priority,
int flags, void (*function) (void), const char *name);

/**
* @brief Acces the threads local memory
*
* @param[in] pid pid of the thread to access
* @return pointer to the threads local memory, 0 on error
*/
char *thread_get_local_mem(int pid);

/**
* @brief returns the status of a process.
* @return STATUS_NOT_FOUND if pid is unknown
Expand Down
25 changes: 25 additions & 0 deletions core/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @brief Threading implementation
*
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
Expand Down Expand Up @@ -199,6 +200,8 @@ int thread_create(char *stack, int stacksize, char priority, int flags, void (*f
cb->rq_entry.next = NULL;
cb->rq_entry.prev = NULL;

cb->local_mem = NULL;

cb->name = name;

cb->wait_data = NULL;
Expand Down Expand Up @@ -237,3 +240,25 @@ int thread_create(char *stack, int stacksize, char priority, int flags, void (*f

return pid;
}

int thread_create_with_local_mem(char *stack, int stacksize, int localmemsize, char priority,
int flags, void (*function) (void), const char *name)
{
// the thread-local memory is put in the beginning
char *localmam = stack;
stack += localmemsize;
stacksize -= localmemsize;

// create the thread
int pid = thread_create(stack, stacksize, priority, flags, function, name);

// set pointer to local mem
sched_threads[pid]->local_mem = localmem;

return pid;
}

char *thread_get_local_mem(int pid)
{
return sched_threads[pid]->local_mem;
}