I use a list implementation that I once found in a book.
The node is defined as:
Code:
struct llnode
{
struct llnode *prev; // previous element
struct llnode *next; // next element
void *info; // pointer to the struct that keeps the actual info (let's say struct xx)
int len; // size of struct xx
};
The function remove_list (in my case ll_remove) will remove the node from the list but not free the info pointer (I guess that my
info is your
context).
The function ll_remove will return the info pointer. You can use that pointer for the thread_switch and free it afterwards (either after the call to thread_switch or maybe in thread_switch).
Code:
void * ll_remove(struct llhead *head, struct llnode *node)
{
void *info;
if(node==NULL)
return NULL;
info=node->info;
// move some pointers around
// so the node is removed
.....
// free the memory occupied by the node
free(node);
return info;
}
Your modified code will look like:
Code:
void thread_exit() {
void *context;
thread_t running_thread = list->element;
thread_t next_thread = list->next->element;
context=(void*)remove_list(&list, running_thread);
thread_switch(context, next_thread->context);
free(context);
}