This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
thread destructors
- From: Makarov <sancho at lemon>
- To: ecos-discuss at sources dot redhat dot com
- Date: Mon, 21 Oct 2002 18:28:13 +0400
- Subject: [ECOS] thread destructors
/*
Hello!
I use BDI2000, at91eb40, gcc 3.0.4, linux
I tried to use thread destructors, but with code below
I've got infinite loop calling thread_destructor().
What is wrong with my code?
How to use destructors properly?
In which context are destructors called?
Aren't they called in context of exited thread??? :(
A. Makarov
------------------------------------------------
*/
#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_arch.h>
#include <malloc.h>
#include <stdio.h>
#define NAME_LENGTH 20
struct thread_resources {
cyg_handle_t handle;
cyg_thread object;
char stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
char name[NAME_LENGTH];
};
static void thread_destructor(cyg_addrword_t data) {
struct thread_resources * resources = (struct thread_resources *)data;
printf( "destructor\n" );
cyg_thread_delete( resources->handle );
free( resources );
}
static void thread(cyg_addrword_t data) {
struct thread_resources * resources = (struct thread_resources *) data;
if( !cyg_thread_add_destructor( thread_destructor, data ) ) {
printf( "inetd_init_thread_destructor() error\n" );
}
printf( "Hello World! I am %s!\n", resources->name );
}
void cyg_user_start(void) {
struct thread_resources * resources;
resources = (struct thread_resources * )
malloc( sizeof(struct thread_resources) );
if( !resources ) {
printf( "malloc() error\n" );
return;
}
snprintf( resources->name, NAME_LENGTH, "A Thread" );
resources->name[NAME_LENGTH - 1] = 0;
cyg_thread_create(
4, // sched_info
thread, // entry
(cyg_addrword_t)(resources), // entry_data
resources->name, // name
resources->stack, // stack_base
CYGNUM_HAL_STACK_SIZE_TYPICAL, // stack_size
&(resources->handle), // handle
&(resources->object) // thread
);
cyg_thread_resume( resources->handle );
}
--
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss