diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-06-26 17:46:51 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-06-26 17:46:51 +0000 |
commit | ae74e84615e37f8c23fd8cadcd9e24476b011ce3 (patch) | |
tree | 1db29dba41e05b11ee03fb66f7a9b34460d10009 /nuttx/examples | |
parent | 499004501fe2610086d76f04b204590aa9f220c6 (diff) | |
download | px4-nuttx-ae74e84615e37f8c23fd8cadcd9e24476b011ce3.tar.gz px4-nuttx-ae74e84615e37f8c23fd8cadcd9e24476b011ce3.tar.bz2 px4-nuttx-ae74e84615e37f8c23fd8cadcd9e24476b011ce3.zip |
integrating NXFLAT
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1962 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/examples')
-rw-r--r-- | nuttx/examples/nxflat/tests/mutex/mutex.c | 46 | ||||
-rw-r--r-- | nuttx/examples/nxflat/tests/pthread/pthread.c | 2 | ||||
-rw-r--r-- | nuttx/examples/nxflat/tests/task/task.c | 28 |
3 files changed, 54 insertions, 22 deletions
diff --git a/nuttx/examples/nxflat/tests/mutex/mutex.c b/nuttx/examples/nxflat/tests/mutex/mutex.c index 8e528da00..f5a84d14b 100644 --- a/nuttx/examples/nxflat/tests/mutex/mutex.c +++ b/nuttx/examples/nxflat/tests/mutex/mutex.c @@ -39,6 +39,7 @@ #include <stdio.h> #include <stdlib.h> +#include <unistd.h> #include <pthread.h> /**************************************************************************** @@ -49,23 +50,29 @@ static pthread_mutex_t mut; static volatile int my_mutex = 0; static unsigned long nloops[2] = {0, 0}; static unsigned long nerrors[2] = {0, 0}; +static volatile boolean bendoftest; /**************************************************************************** * Private Functions ****************************************************************************/ /* NOTE: it is necessary for functions that are referred to by function pointers - * pointer to be declared with global scope (at least for ARM). Otherwise, + * pointer to be declared with global scope (at least for ARM). Otherwise, * a relocation type that is not supported by NXFLAT is generated by GCC. */ void thread_func(void *parameter) { - int my_id = (int)parameter; + int my_id = (int)parameter; int my_ndx = my_id - 1; - volatile int i; - - for (;;) + int i; + + /* Loop 20 times. There is a 100 MS delay in the loop so this should + * take about 2 seconds. The main thread will stop this thread after + * 2 seconds by setting bendoftest in any event. + */ + + for (i = 0; i < 20 && !bendoftest; i++); { if ((pthread_mutex_lock(&mut)) != 0) { @@ -80,8 +87,8 @@ void thread_func(void *parameter) nerrors[my_ndx]++; } - my_mutex = 1; - for (i = 0; i < 1000; i++); + my_mutex = 1; + usleep(100000); my_mutex = 0; if ((pthread_mutex_unlock(&mut)) != 0) @@ -99,9 +106,8 @@ void thread_func(void *parameter) int main(int argc, char **argv) { - pthread_t thread1, thread2; - - printf("Starting threads\n"); + pthread_t thread1; + pthread_t thread2; /* Initialize the mutex */ @@ -109,20 +115,34 @@ int main(int argc, char **argv) /* Start two thread instances */ - if ((pthread_create(&thread1, NULL, (void*)&thread_func, (void*)1)) != 0) + printf("Starting thread 1\n"); + bendoftest = FALSE; + if ((pthread_create(&thread1, NULL, (void*)thread_func, (void*)1)) != 0) { fprintf(stderr, "Error in thread#1 creation\n"); } - if ((pthread_create(&thread2, NULL, (void*)&thread_func, (void*)2)) != 0) + printf("Starting thread 2\n"); + if ((pthread_create(&thread2, NULL, (void*)thread_func, (void*)2)) != 0) { fprintf(stderr, "Error in thread#2 creation\n"); } - printf("Press control-C to terminate the example\n"); + /* Wait a bit for the threads to do their thing. */ + + sleep(2); + /* Then ask them politely to stop running */ + + printf("Stopping threads\n"); + bendoftest = TRUE; pthread_join(thread1, NULL); pthread_join(thread2, NULL); + + printf("\tThread1\tThread2\n"); + printf("Loops\t%ld\t%ld\n", nloops[0], nloops[1]); + printf("Errors\t%ld\t%ld\n", nerrors[0], nerrors[1]); + return 0; } diff --git a/nuttx/examples/nxflat/tests/pthread/pthread.c b/nuttx/examples/nxflat/tests/pthread/pthread.c index 6de82a46d..019ec453e 100644 --- a/nuttx/examples/nxflat/tests/pthread/pthread.c +++ b/nuttx/examples/nxflat/tests/pthread/pthread.c @@ -76,7 +76,7 @@ enum exit_values_e ****************************************************************************/ /* NOTE: it is necessary for functions that are referred to by function pointers - * pointer to be declared with global scope (at least for ARM). Otherwise, + * pointer to be declared with global scope (at least for ARM). Otherwise, * a relocation type that is not supported by NXFLAT is generated by GCC. */ diff --git a/nuttx/examples/nxflat/tests/task/task.c b/nuttx/examples/nxflat/tests/task/task.c index 6083d68bd..63e8188ed 100644 --- a/nuttx/examples/nxflat/tests/task/task.c +++ b/nuttx/examples/nxflat/tests/task/task.c @@ -54,12 +54,16 @@ static char child_name[] = "child"; static char child_arg[] = "Hello from your parent!"; static sem_t g_sem; +#if CONFIG_TASK_NAME_SIZE == 0 +static char no_name[] = "<noname>"; +#endif + /**************************************************************************** * Privite Functions ****************************************************************************/ /* NOTE: it is necessary for functions that are referred to by function pointers - * pointer to be declared with global scope (at least for ARM). Otherwise, + * pointer to be declared with global scope (at least for ARM). Otherwise, * a relocation type that is not supported by NXFLAT is generated by GCC. */ @@ -74,15 +78,23 @@ static sem_t g_sem; printf("Child: Exit-ting with status=2\n"); exit(2); } - printf("Child: argv[0]=\"%s\"\n", argv[0]); - if (strcmp(argv[0], child_name) != 0) +#if CONFIG_TASK_NAME_SIZE == 0 + if (strcmp(argv[0], no_name) != 0) + { + printf("Child: expected argv[0] to be \"%s\"\n", no_name); + printf("Child: Exit-ting with status=3\n"); + exit(3); + } +#else + if (strncmp(argv[0], child_name, CONFIG_TASK_NAME_SIZE) != 0) { printf("Child: expected argv[0] to be \"%s\"\n", child_name); printf("Child: Exit-ting with status=3\n"); exit(3); } +#endif printf("Child: argv[1]=\"%s\"\n", argv[1]); @@ -106,7 +118,7 @@ int main(int argc, char **argv) { pid_t parent_pid = getpid(); char *child_argv[2]; - int ret; + pid_t child_pid; printf("Parent: Started, pid=%d\n", parent_pid); @@ -116,15 +128,15 @@ int main(int argc, char **argv) child_argv[0] = child_arg; child_argv[1] = 0; - ret = task_create(child_name, 50, 512, child_task, (const char**)child_argv); - if (ret != 0) + child_pid = task_create(child_name, 50, 512, child_task, (const char**)child_argv); + if (child_pid < 0) { printf("Parent: task_create failed: %d\n", errno); } - printf("Parent: Waiting for child\n"); + printf("Parent: Waiting for child (pid=%d)\n", child_pid); sem_wait(&g_sem); - printf("Parent: Exiting\n"); + printf("Parent: Exit-ing\n"); sem_destroy(&g_sem); return 0; } |