summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpatacongo <patacongo@7fd9a85b-ad96-42d3-883c-3090e2eb8679>2007-02-17 23:21:28 +0000
committerpatacongo <patacongo@7fd9a85b-ad96-42d3-883c-3090e2eb8679>2007-02-17 23:21:28 +0000
commit2ec7825e0da8f1af15869d94b0272215acc29ec8 (patch)
tree1c390958fae49e34dce698b175487e6d4681e540
parent8a23a89176bb272afaecf2075ee246289ded08db (diff)
NuttX RTOS
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@3 7fd9a85b-ad96-42d3-883c-3090e2eb8679
-rw-r--r--nuttx/Documentation/NuttxPortingGuide.html121
-rw-r--r--nuttx/Documentation/NuttxUserGuide.html4105
-rw-r--r--nuttx/Makefile139
-rw-r--r--nuttx/arch/README.txt189
-rw-r--r--nuttx/arch/c5471/Make.defs70
-rw-r--r--nuttx/arch/c5471/defconfig157
-rw-r--r--nuttx/arch/c5471/include/arch.h80
-rw-r--r--nuttx/arch/c5471/include/irq.h252
-rw-r--r--nuttx/arch/c5471/include/types.h70
-rw-r--r--nuttx/arch/c5471/ld.script107
-rwxr-xr-xnuttx/arch/c5471/setenv.sh46
-rw-r--r--nuttx/arch/c5471/src/Makefile79
-rw-r--r--nuttx/arch/c5471/src/c5471.h376
-rw-r--r--nuttx/arch/c5471/src/up_assert.c82
-rw-r--r--nuttx/arch/c5471/src/up_blocktask.c168
-rw-r--r--nuttx/arch/c5471/src/up_copystate.c75
-rw-r--r--nuttx/arch/c5471/src/up_createstack.c126
-rw-r--r--nuttx/arch/c5471/src/up_dataabort.c68
-rw-r--r--nuttx/arch/c5471/src/up_exit.c111
-rw-r--r--nuttx/arch/c5471/src/up_fullcontextrestore.S115
-rw-r--r--nuttx/arch/c5471/src/up_head.S125
-rw-r--r--nuttx/arch/c5471/src/up_idle.c79
-rw-r--r--nuttx/arch/c5471/src/up_initialize.c93
-rw-r--r--nuttx/arch/c5471/src/up_initialstate.c85
-rw-r--r--nuttx/arch/c5471/src/up_internal.h99
-rw-r--r--nuttx/arch/c5471/src/up_interruptcontext.c68
-rw-r--r--nuttx/arch/c5471/src/up_irq.c243
-rw-r--r--nuttx/arch/c5471/src/up_prefetchabort.c69
-rw-r--r--nuttx/arch/c5471/src/up_releasepending.c131
-rw-r--r--nuttx/arch/c5471/src/up_releasestack.c78
-rw-r--r--nuttx/arch/c5471/src/up_reprioritizertr.c178
-rw-r--r--nuttx/arch/c5471/src/up_saveusercontext.S121
-rw-r--r--nuttx/arch/c5471/src/up_schedulesigaction.c186
-rw-r--r--nuttx/arch/c5471/src/up_sigdeliver.c117
-rw-r--r--nuttx/arch/c5471/src/up_syscall.c82
-rw-r--r--nuttx/arch/c5471/src/up_timerisr.c93
-rw-r--r--nuttx/arch/c5471/src/up_unblocktask.c160
-rw-r--r--nuttx/arch/c5471/src/up_undefinedinsn.c68
-rw-r--r--nuttx/arch/c5471/src/up_usestack.c118
-rw-r--r--nuttx/arch/c5471/src/up_vectors.S449
-rw-r--r--nuttx/arch/sim/Make.defs69
-rw-r--r--nuttx/arch/sim/defconfig154
-rw-r--r--nuttx/arch/sim/include/arch.h80
-rw-r--r--nuttx/arch/sim/include/irq.h106
-rw-r--r--nuttx/arch/sim/include/types.h70
-rwxr-xr-xnuttx/arch/sim/setenv.sh45
-rw-r--r--nuttx/arch/sim/src/Makefile76
-rw-r--r--nuttx/arch/sim/src/up_allocateheap.c81
-rw-r--r--nuttx/arch/sim/src/up_blocktask.c157
-rw-r--r--nuttx/arch/sim/src/up_createstack.c109
-rw-r--r--nuttx/arch/sim/src/up_devconsole.c136
-rw-r--r--nuttx/arch/sim/src/up_exit.c123
-rw-r--r--nuttx/arch/sim/src/up_head.c77
-rw-r--r--nuttx/arch/sim/src/up_idle.c84
-rw-r--r--nuttx/arch/sim/src/up_initialize.c88
-rw-r--r--nuttx/arch/sim/src/up_initialstate.c81
-rw-r--r--nuttx/arch/sim/src/up_internal.h91
-rw-r--r--nuttx/arch/sim/src/up_interruptcontext.c74
-rw-r--r--nuttx/arch/sim/src/up_releasepending.c118
-rw-r--r--nuttx/arch/sim/src/up_releasestack.c81
-rw-r--r--nuttx/arch/sim/src/up_reprioritizertr.c169
-rw-r--r--nuttx/arch/sim/src/up_schedulesigaction.c109
-rw-r--r--nuttx/arch/sim/src/up_setjmp.S129
-rw-r--r--nuttx/arch/sim/src/up_unblocktask.c146
-rw-r--r--nuttx/arch/sim/src/up_usestack.c100
-rw-r--r--nuttx/drivers/Makefile74
-rw-r--r--nuttx/drivers/dev_null.c89
-rw-r--r--nuttx/examples/ostest/Makefile75
-rw-r--r--nuttx/examples/ostest/cancel.c308
-rw-r--r--nuttx/examples/ostest/cond.c284
-rw-r--r--nuttx/examples/ostest/dev_null.c91
-rw-r--r--nuttx/examples/ostest/main.c178
-rw-r--r--nuttx/examples/ostest/mqueue.c333
-rw-r--r--nuttx/examples/ostest/mutex.c120
-rw-r--r--nuttx/examples/ostest/ostest.h91
-rw-r--r--nuttx/examples/ostest/sem.c238
-rw-r--r--nuttx/examples/ostest/sighand.c254
-rw-r--r--nuttx/examples/ostest/timedwait.c161
-rw-r--r--nuttx/fs/Makefile75
-rw-r--r--nuttx/fs/fs_close.c88
-rw-r--r--nuttx/fs/fs_dup.c158
-rw-r--r--nuttx/fs/fs_files.c273
-rw-r--r--nuttx/fs/fs_inode.c568
-rw-r--r--nuttx/fs/fs_internal.h103
-rw-r--r--nuttx/fs/fs_ioctl.c75
-rw-r--r--nuttx/fs/fs_open.c167
-rw-r--r--nuttx/fs/fs_read.c97
-rw-r--r--nuttx/fs/fs_write.c98
-rw-r--r--nuttx/include/assert.h96
-rw-r--r--nuttx/include/ctype.h187
-rw-r--r--nuttx/include/debug.h103
-rw-r--r--nuttx/include/errno.h198
-rw-r--r--nuttx/include/mqueue.h120
-rw-r--r--nuttx/include/nuttx/arch.h433
-rw-r--r--nuttx/include/nuttx/compiler.h73
-rw-r--r--nuttx/include/nuttx/fs.h215
-rw-r--r--nuttx/include/nuttx/irq.h107
-rw-r--r--nuttx/include/nuttx/kmalloc.h94
-rw-r--r--nuttx/include/nuttx/lib.h79
-rw-r--r--nuttx/include/nuttx/os_external.h85
-rw-r--r--nuttx/include/pthread.h326
-rw-r--r--nuttx/include/queue.h118
-rw-r--r--nuttx/include/sched.h308
-rw-r--r--nuttx/include/semaphore.h103
-rw-r--r--nuttx/include/signal.h176
-rw-r--r--nuttx/include/stddef.h49
-rw-r--r--nuttx/include/stdio.h285
-rw-r--r--nuttx/include/stdlib.h116
-rw-r--r--nuttx/include/string.h94
-rw-r--r--nuttx/include/sys/types.h143
-rw-r--r--nuttx/include/time.h126
-rw-r--r--nuttx/include/unistd.h84
-rw-r--r--nuttx/include/wdog.h95
-rw-r--r--nuttx/lib/Makefile96
-rw-r--r--nuttx/lib/dq_addafter.c74
-rw-r--r--nuttx/lib/dq_addbefore.c73
-rw-r--r--nuttx/lib/dq_addfirst.c73
-rw-r--r--nuttx/lib/dq_addlast.c73
-rw-r--r--nuttx/lib/dq_rem.c83
-rw-r--r--nuttx/lib/dq_remfirst.c81
-rw-r--r--nuttx/lib/dq_remlast.c81
-rw-r--r--nuttx/lib/lib_fclose.c98
-rw-r--r--nuttx/lib/lib_fflush.c171
-rw-r--r--nuttx/lib/lib_fgetc.c98
-rw-r--r--nuttx/lib/lib_filesem.c143
-rw-r--r--nuttx/lib/lib_fopen.c232
-rw-r--r--nuttx/lib/lib_fprintf.c92
-rw-r--r--nuttx/lib/lib_fputc.c98
-rw-r--r--nuttx/lib/lib_fputs.c126
-rw-r--r--nuttx/lib/lib_fread.c98
-rw-r--r--nuttx/lib/lib_fwrite.c98
-rw-r--r--nuttx/lib/lib_getenv.c163
-rw-r--r--nuttx/lib/lib_init.c203
-rw-r--r--nuttx/lib/lib_internal.h166
-rw-r--r--nuttx/lib/lib_libfread.c257
-rw-r--r--nuttx/lib/lib_libfwrite.c162
-rw-r--r--nuttx/lib/lib_libsprintf.c90
-rw-r--r--nuttx/lib/lib_libvsprintf.c854
-rw-r--r--nuttx/lib/lib_lowprintf.c106
-rw-r--r--nuttx/lib/lib_lowstream.c72
-rw-r--r--nuttx/lib/lib_memcmp.c74
-rw-r--r--nuttx/lib/lib_memcpy.c60
-rw-r--r--nuttx/lib/lib_memmove.c72
-rw-r--r--nuttx/lib/lib_memset.c59
-rw-r--r--nuttx/lib/lib_memstream.c70
-rw-r--r--nuttx/lib/lib_printf.c103
-rw-r--r--nuttx/lib/lib_puts.c111
-rw-r--r--nuttx/lib/lib_rand.c212
-rw-r--r--nuttx/lib/lib_rawprintf.c100
-rw-r--r--nuttx/lib/lib_rawstream.c78
-rw-r--r--nuttx/lib/lib_rint.c123
-rw-r--r--nuttx/lib/lib_sem.c143
-rw-r--r--nuttx/lib/lib_sprintf.c95
-rw-r--r--nuttx/lib/lib_sscanf.c328
-rw-r--r--nuttx/lib/lib_stdstream.c70
-rw-r--r--nuttx/lib/lib_strchr.c71
-rw-r--r--nuttx/lib/lib_strcmp.c63
-rw-r--r--nuttx/lib/lib_strcpy.c59
-rw-r--r--nuttx/lib/lib_strdup.c61
-rw-r--r--nuttx/lib/lib_streamsem.c99
-rw-r--r--nuttx/lib/lib_strlen.c59
-rw-r--r--nuttx/lib/lib_strncpy.c57
-rw-r--r--nuttx/lib/lib_strtol.c176
-rw-r--r--nuttx/lib/lib_ungetc.c124
-rw-r--r--nuttx/lib/lib_vfprintf.c93
-rw-r--r--nuttx/lib/lib_vprintf.c91
-rw-r--r--nuttx/lib/lib_vsprintf.c95
-rw-r--r--nuttx/lib/sq_addafter.c71
-rw-r--r--nuttx/lib/sq_addfirst.c67
-rw-r--r--nuttx/lib/sq_addlast.c72
-rw-r--r--nuttx/lib/sq_rem.c83
-rw-r--r--nuttx/lib/sq_remafter.c78
-rw-r--r--nuttx/lib/sq_remfirst.c75
-rw-r--r--nuttx/lib/sq_remlast.c87
-rw-r--r--nuttx/mm/Makefile76
-rw-r--r--nuttx/mm/Makefile.test64
-rw-r--r--nuttx/mm/mm_addfreechunk.c91
-rw-r--r--nuttx/mm/mm_calloc.c68
-rw-r--r--nuttx/mm/mm_environment.h120
-rw-r--r--nuttx/mm/mm_free.c151
-rw-r--r--nuttx/mm/mm_initialize.c145
-rw-r--r--nuttx/mm/mm_internal.h185
-rw-r--r--nuttx/mm/mm_mallinfo.c108
-rw-r--r--nuttx/mm/mm_malloc.c203
-rw-r--r--nuttx/mm/mm_memalign.c212
-rw-r--r--nuttx/mm/mm_realloc.c336
-rw-r--r--nuttx/mm/mm_sem.c177
-rw-r--r--nuttx/mm/mm_shrinkchunk.c140
-rw-r--r--nuttx/mm/mm_size2ndx.c70
-rw-r--r--nuttx/mm/mm_test.c439
-rw-r--r--nuttx/mm/mm_zalloc.c68
-rw-r--r--nuttx/sched/Makefile130
-rw-r--r--nuttx/sched/abort.c81
-rw-r--r--nuttx/sched/atexit.c103
-rw-r--r--nuttx/sched/clock_getres.c119
-rw-r--r--nuttx/sched/clock_gettime.c148
-rw-r--r--nuttx/sched/clock_initialize.c128
-rw-r--r--nuttx/sched/clock_internal.h101
-rw-r--r--nuttx/sched/clock_settime.c119
-rw-r--r--nuttx/sched/exit.c112
-rw-r--r--nuttx/sched/get_errno_ptr.c70
-rw-r--r--nuttx/sched/getpid.c84
-rw-r--r--nuttx/sched/gmtime_r.c215
-rw-r--r--nuttx/sched/irq_attach.c107
-rw-r--r--nuttx/sched/irq_dispatch.c98
-rw-r--r--nuttx/sched/irq_initialize.c90
-rw-r--r--nuttx/sched/irq_internal.h82
-rw-r--r--nuttx/sched/irq_unexpectedisr.c83
-rw-r--r--nuttx/sched/mktime.c211
-rw-r--r--nuttx/sched/mq_close.c189
-rw-r--r--nuttx/sched/mq_descreate.c156
-rw-r--r--nuttx/sched/mq_findnamed.c103
-rw-r--r--nuttx/sched/mq_getattr.c114
-rw-r--r--nuttx/sched/mq_initialize.c246
-rw-r--r--nuttx/sched/mq_internal.h200
-rw-r--r--nuttx/sched/mq_msgfree.c135
-rw-r--r--nuttx/sched/mq_msgqfree.c107
-rw-r--r--nuttx/sched/mq_notify.c170
-rw-r--r--nuttx/sched/mq_open.c226
-rw-r--r--nuttx/sched/mq_receive.c236
-rw-r--r--nuttx/sched/mq_send.c398
-rw-r--r--nuttx/sched/mq_setattr.c115
-rw-r--r--nuttx/sched/mq_unlink.c142
-rw-r--r--nuttx/sched/os_internal.h267
-rw-r--r--nuttx/sched/os_start.c382
-rw-r--r--nuttx/sched/pthread_attrdestroy.c108
-rw-r--r--nuttx/sched/pthread_attrgetinheritsched.c112
-rw-r--r--nuttx/sched/pthread_attrgetschedparam.c109
-rw-r--r--nuttx/sched/pthread_attrgetschedpolicy.c106
-rw-r--r--nuttx/sched/pthread_attrgetstacksize.c107
-rw-r--r--nuttx/sched/pthread_attrinit.c111
-rw-r--r--nuttx/sched/pthread_attrsetinheritsched.c112
-rw-r--r--nuttx/sched/pthread_attrsetschedparam.c107
-rw-r--r--nuttx/sched/pthread_attrsetschedpolicy.c110
-rw-r--r--nuttx/sched/pthread_attrsetstacksize.c107
-rw-r--r--nuttx/sched/pthread_cancel.c143
-rw-r--r--nuttx/sched/pthread_completejoin.c211
-rw-r--r--nuttx/sched/pthread_condattrdestroy.c82
-rw-r--r--nuttx/sched/pthread_condattrinit.c85
-rw-r--r--nuttx/sched/pthread_condbroadcast.c142
-rw-r--r--nuttx/sched/pthread_conddestroy.c87
-rw-r--r--nuttx/sched/pthread_condinit.c91
-rw-r--r--nuttx/sched/pthread_condsignal.c126
-rw-r--r--nuttx/sched/pthread_condtimedwait.c306
-rw-r--r--nuttx/sched/pthread_condwait.c137
-rw-r--r--nuttx/sched/pthread_create.c342
-rw-r--r--nuttx/sched/pthread_detach.c134
-rw-r--r--nuttx/sched/pthread_exit.c119
-rw-r--r--nuttx/sched/pthread_findjoininfo.c98
-rw-r--r--nuttx/sched/pthread_getschedparam.c123
-rw-r--r--nuttx/sched/pthread_getspecific.c119
-rw-r--r--nuttx/sched/pthread_initialize.c193
-rw-r--r--nuttx/sched/pthread_internal.h129
-rw-r--r--nuttx/sched/pthread_join.c213
-rw-r--r--nuttx/sched/pthread_keycreate.c135
-rw-r--r--nuttx/sched/pthread_keydelete.c93
-rw-r--r--nuttx/sched/pthread_mutexattrdestroy.c104
-rw-r--r--nuttx/sched/pthread_mutexattrgetpshared.c104
-rw-r--r--nuttx/sched/pthread_mutexattrinit.c103
-rw-r--r--nuttx/sched/pthread_mutexattrsetpshared.c104
-rw-r--r--nuttx/sched/pthread_mutexdestroy.c128
-rw-r--r--nuttx/sched/pthread_mutexinit.c122
-rw-r--r--nuttx/sched/pthread_mutexlock.c138
-rw-r--r--nuttx/sched/pthread_mutextrylock.c137
-rw-r--r--nuttx/sched/pthread_mutexunlock.c127
-rw-r--r--nuttx/sched/pthread_removejoininfo.c136
-rw-r--r--nuttx/sched/pthread_self.c91
-rw-r--r--nuttx/sched/pthread_setcancelstate.c127
-rw-r--r--nuttx/sched/pthread_setschedparam.c103
-rw-r--r--nuttx/sched/pthread_setspecific.c131
-rw-r--r--nuttx/sched/pthread_yield.c87
-rw-r--r--nuttx/sched/sched_addblocked.c117
-rw-r--r--nuttx/sched/sched_addprioritized.c170
-rw-r--r--nuttx/sched/sched_addreadytorun.c144
-rw-r--r--nuttx/sched/sched_free.c103
-rw-r--r--nuttx/sched/sched_getfiles.c77
-rw-r--r--nuttx/sched/sched_getparam.c142
-rw-r--r--nuttx/sched/sched_getprioritymax.c102
-rw-r--r--nuttx/sched/sched_getprioritymin.c102
-rw-r--r--nuttx/sched/sched_getscheduler.c130
-rw-r--r--nuttx/sched/sched_getstreams.c77
-rw-r--r--nuttx/sched/sched_gettcb.c104
-rw-r--r--nuttx/sched/sched_lock.c109
-rw-r--r--nuttx/sched/sched_lockcount.c93
-rw-r--r--nuttx/sched/sched_mergepending.c168
-rw-r--r--nuttx/sched/sched_processtimer.c181
-rw-r--r--nuttx/sched/sched_releasefiles.c90
-rw-r--r--nuttx/sched/sched_releasetcb.c142
-rw-r--r--nuttx/sched/sched_removeblocked.c113
-rw-r--r--nuttx/sched/sched_removereadytorun.c117
-rw-r--r--nuttx/sched/sched_rrgetinterval.c156
-rw-r--r--nuttx/sched/sched_setparam.c266
-rw-r--r--nuttx/sched/sched_setscheduler.c188
-rw-r--r--nuttx/sched/sched_setupidlefiles.c113
-rw-r--r--nuttx/sched/sched_setuppthreadfiles.c94
-rw-r--r--nuttx/sched/sched_setupstreams.c80
-rw-r--r--nuttx/sched/sched_setuptaskfiles.c112
-rw-r--r--nuttx/sched/sched_unlock.c122
-rw-r--r--nuttx/sched/sched_yield.c103
-rw-r--r--nuttx/sched/sem_close.c144
-rw-r--r--nuttx/sched/sem_destroy.c122
-rw-r--r--nuttx/sched/sem_findnamed.c103
-rw-r--r--nuttx/sched/sem_getvalue.c112
-rw-r--r--nuttx/sched/sem_init.c110
-rw-r--r--nuttx/sched/sem_initialize.c99
-rw-r--r--nuttx/sched/sem_internal.h99
-rw-r--r--nuttx/sched/sem_open.c206
-rw-r--r--nuttx/sched/sem_post.c165
-rw-r--r--nuttx/sched/sem_trywait.c141
-rw-r--r--nuttx/sched/sem_unlink.c141
-rw-r--r--nuttx/sched/sem_wait.c174
-rw-r--r--nuttx/sched/sem_waitirq.c128
-rw-r--r--nuttx/sched/sig_action.c273
-rw-r--r--nuttx/sched/sig_addset.c99
-rw-r--r--nuttx/sched/sig_allocatependingsigaction.c134
-rw-r--r--nuttx/sched/sig_cleanup.c117
-rw-r--r--nuttx/sched/sig_deliver.c164
-rw-r--r--nuttx/sched/sig_delset.c99
-rw-r--r--nuttx/sched/sig_emptyset.c88
-rw-r--r--nuttx/sched/sig_fillset.c88
-rw-r--r--nuttx/sched/sig_findaction.c100
-rw-r--r--nuttx/sched/sig_initialize.c262
-rw-r--r--nuttx/sched/sig_internal.h190
-rw-r--r--nuttx/sched/sig_ismember.c99
-rw-r--r--nuttx/sched/sig_lowest.c88
-rw-r--r--nuttx/sched/sig_mqnotempty.c113
-rw-r--r--nuttx/sched/sig_pending.c128
-rw-r--r--nuttx/sched/sig_procmask.c172
-rw-r--r--nuttx/sched/sig_queue.c131
-rw-r--r--nuttx/sched/sig_received.c384
-rw-r--r--nuttx/sched/sig_releasependingsigaction.c118
-rw-r--r--nuttx/sched/sig_releasependingsignal.c126
-rw-r--r--nuttx/sched/sig_removependingsignal.c108
-rw-r--r--nuttx/sched/sig_suspend.c180
-rw-r--r--nuttx/sched/sig_timedwait.c267
-rw-r--r--nuttx/sched/sig_unmaskpendingsignal.c135
-rw-r--r--nuttx/sched/sig_waitinfo.c89
-rw-r--r--nuttx/sched/sleep.c101
-rw-r--r--nuttx/sched/task_create.c498
-rw-r--r--nuttx/sched/task_delete.c165
-rw-r--r--nuttx/sched/task_restart.c178
-rw-r--r--nuttx/sched/usleep.c98
-rw-r--r--nuttx/sched/wd_cancel.c154
-rw-r--r--nuttx/sched/wd_create.c105
-rw-r--r--nuttx/sched/wd_delete.c108
-rw-r--r--nuttx/sched/wd_initialize.c136
-rw-r--r--nuttx/sched/wd_internal.h121
-rw-r--r--nuttx/sched/wd_start.c312
-rw-r--r--nuttx/tools/Makefile.mkconfig43
-rwxr-xr-xnuttx/tools/configure.sh78
-rw-r--r--nuttx/tools/mkconfig.c239
-rwxr-xr-xnuttx/tools/mkdeps.sh77
-rwxr-xr-xnuttx/tools/zipme.sh105
353 files changed, 52170 insertions, 0 deletions
diff --git a/nuttx/Documentation/NuttxPortingGuide.html b/nuttx/Documentation/NuttxPortingGuide.html
new file mode 100644
index 0000000000..576e2c86d7
--- /dev/null
+++ b/nuttx/Documentation/NuttxPortingGuide.html
@@ -0,0 +1,121 @@
+<html>
+
+<head>
+<title>Nuttx Porting Manual</title>
+<meta name="author" content="Gregory Nutt">
+</head>
+
+<body>
+<center>
+ <big><b>
+ <p>Nuttx Operating System</p>
+ <p>Porting Guide</p>
+ </b></big>
+ <p><small>by</small></p>
+ <p>Gregory Nutt</p>
+ <p><small>Last Update: February 8, 2007</small></p>
+</center>
+
+<center><h1>Table of Contents</h1></center>
+<li>1.0 <a href="#Introduction">1.0 Introduction</a></li>
+<li>2.0 <a href="#DirectoryStructure">Directory Structure</a></li>
+<ul>
+ <li>2.1 <a href="#DirStructDocumentation">Documentation</a></li>
+ <l1>2.2 <a href="#DirStructArch">arch</a></li>
+ <li>2.3 <a href="#DirStructDrivers">drivers</a></li>
+ <li>2.4 <a href="#DirStructExamples">examples</a></li>
+ <li>2.5 <a href="#DirStructFs">fs</a></li>
+ <li>2.6 <a href="#DirStructInclude">include</a></li>
+ <li>2.7 <a href="#DirStructLib">lib</a></li>
+ <li>2.8 <a href="#DirStructMm">mm</a></li>
+ <li>2.9 <a href="#DirStructSched">sched</a></li>
+ <li>2.10 <a href="#DirStructDrivers">tools</a></li>
+</ul>
+<li>3.0 <a href="#DirectoryConfiAndBuild">Configuring and Building</a></li>
+
+<hr>
+<h1>1.0 <a name="Introduction">Introduction</a></h1>
+
+<p><b>Overview</b>
+ This document provides and overview of the Nuttx build and configuration
+ logic and provides hints for the incorporation of new processor/board archectures
+ into the build.
+</p>
+<p>
+ See also arch/README.txt.
+</p>
+
+<p><b>General Philosophy</b>.
+
+<hr>
+<h1>2.0 <a name="DirectoryStructure">Directory Structure</a></h1>
+
+<p>The general directly layout for Nuttx is very similar to the directory structure
+of the Linux kernel -- at least at the most superficial layers.
+At the top level is the main makefile and a series of sub-directories identified
+below and discussed in the following paragraphs:</p>
+
+<ul><pre>
+.
+|-- Makefile
+|-- <a href="#DirStructDocumentation">Documentation</a>
+| `-- <i>(documentation files)</i>
+|-- <a href="#DirStructArch">arch</a>
+| |-- <i>(architecture)</i>
+| | |-- Make.defs
+| | |-- defconfig
+| | |-- include
+| | |-- setenv.sh
+| | `-- src
+| `-- <i>(other architectures)</i>
+|-- <a href="#DirStructDrivers">drivers</a>
+| |-- Makefile
+| `-- <i>(driver source files)</i>
+|-- <a href="#DirStructExamples">examples</a>
+| `-- <i>(example)</i>
+| |-- Makefile
+| `-- <i>(example source files)</i>
+|-- <a href="#DirStructFs">fs</a>
+| |-- Makefile
+| `-- <i>(fs source files)</i>
+|-- <a href="#DirStructInclude">include</a>
+| |-- <i>(standard header files)</i>
+| |-- nuttx
+| | `-- <i>(nuttx specific header files)</i>
+| `- sys
+| | `-- <i>(more standard header files)</i>
+|-- <a href="#DirStructLib">lib</a>
+| |-- Makefile
+| `-- <i>(lib source files)</i>
+|-- <a href="#DirStructMm">mm</a>
+| |-- Makefile
+| `-- <i>(mm source files)</i>
+|-- <a href="#DirStructSched">sched</a>
+| |-- Makefile
+| `-- <i>(sched source files)</i>
+`-- <a href="#DirStructDrivers">tools</a>
+ |-- Makefile.mkconfig
+ |-- configure.sh
+ |-- mkconfig.c
+ |-- mkdeps.sh
+ `-- zipme
+</pre></ul>
+
+<h2>2.1 <a name="DirStructDocumentation">Documentation</a></h2>
+<h2>2.2 <a name="DirStructArch">arch</a></h2>
+<h2>2.3 <a name="DirStructDrivers">drivers</a></h2>
+<h2>2.4 <a name="DirStructExamples">examples</a></h2>
+<h2>2.5 <a name="DirStructFs">fs</a></h2>
+<h2>2.6 <a name="DirStructInclude">include</a></h2>
+<h2>2.7 <a name="DirStructLib">lib</a></h2>
+<h2>2.8 <a name="DirStructMm">mm</a></h2>
+<h2>2.9 <a name="DirStructSched">sched</a></h2>
+<h2>2.10 <a name="DirStructDrivers">tools</a></h2>
+
+<hr>
+<h1>3.0 <a name="DirectoryConfiAndBuild">Configuring and Building</a></h1>
+
+</body>
+</html>
+
+
diff --git a/nuttx/Documentation/NuttxUserGuide.html b/nuttx/Documentation/NuttxUserGuide.html
new file mode 100644
index 0000000000..c8e26278d3
--- /dev/null
+++ b/nuttx/Documentation/NuttxUserGuide.html
@@ -0,0 +1,4105 @@
+<HTML>
+
+<HEAD>
+<TITLE>Nuttx Users Manual</TITLE>
+<META NAME="AUTHOR" CONTENT="Gregory Nutt">
+</HEAD>
+
+<BODY>
+<CENTER><BIG><B>
+Nuttx Operating System
+<P>
+User's Manual
+</B></BIG>
+<P>
+<SMALL>by</SMALL>
+<P>
+Gregory Nutt
+<P>
+<SMALL>Last Update: January 28, 2007</SMALL>
+</CENTER>
+
+<H1>1.0 <A NAME="Introduction">Introduction</A></H1>
+
+<P>
+This user's manual is divided into five sections:
+<UL>
+<LI><B>Section 1.0, <A HREF="#Introduction">Introduction</A></B>:
+This section provides an overview of the Nuttx user's manual.
+<LI><B>Section 2.0, <A HREF="#OS_Interfaces">OS Interfaces</A></B>:
+This section details the interfaces provided by Nuttx from the
+perspective of the firmware developer. This section is divided
+into several paragraphs that describe different groups of OS interfaces:
+<UL>
+<LI>Paragraph 2.1 <A HREF="#Task_Control">Task Control Interfaces</A>
+<LI>Paragraph 2.2 <A HREF="#Task_Schedule">Task Scheduling Interfaces</A>
+<LI>Paragraph 2.3 <A HREF="#Task_Switch">Task Switching Interfaces</A>
+<LI>Paragraph 2.4 <A HREF="#Message_Queue">Named Message Queue Interfaces</A>
+<LI>Paragraph 2.5 <A HREF="#Semaphores">Counting Semaphore Interfaces</A>
+<LI>Paragraph 2.6 <A HREF="#Watchdogs">Watchdog Timer Interfaces</A>
+<LI>Paragraph 2.7 <A HREF="#Signals">Signal Interfaces</A>
+<LI>Paragraph 2.8 <A HREF="#Pthread">Pthread Interfaces</A>
+</UL>
+<LI><B>Section 3.0, <A HREF="#Data_Structures">OS Data Structures</A></B>:
+This section documents the data structures that are used at the Nuttx
+interface.
+<LI><B>Section 4.0, <A HREF="#Problems">Known Problems</A></B>. This section
+lists known problems in the latest release of Nuttx.
+</UL>
+<HR>
+
+<H1>2.0 <A NAME="OS_Interfaces">OS Interfaces</A></H1>
+
+<P>
+This section describes each C-callable interface to the Nuttx
+Operating System. The description of each interface is presented
+in the following format:
+<P>
+<B>Function Prototype:</B> The C prototype of the interface function
+is provided.
+<P>
+<B>Description:</B> The operation performed by the interface function
+is discussed.
+<P>
+<B>Input Parameters:</B> All input parameters are listed along
+with brief descriptions of each input parameter.
+<P>
+<B>Returned Values:</B> All possible values returned by the interface
+function are listed. Values returned as side-effects (through
+pointer input parameters or through global variables) will be
+addressed in the description of the interface function.
+<P>
+<B>Assumptions/Limitations:</B> Any unusual assumptions made by
+the interface function or any non-obvious limitations to the use
+of the interface function will be indicated here.
+<P>
+<B>POSIX Compatibility:</B> Any significant differences between the
+Nuttx interface and its corresponding POSIX interface will be noted
+here.
+<P>
+NOTE: In order to achieve an independent name space for the Nuttx
+interface functions, differences in function names and types are
+to be expected and will not be identified as differences in these
+paragraphs.
+<HR>
+
+<H2>2.1 <A NAME="Task_Control">Task Control Interfaces</A></H2>
+
+<p>
+<b>Tasks</b>.
+Nuttx is a flat address OS. As such it does not support &quot;processes&quot;
+in the way that, say, Linux does.
+Nuttx only supports simple threads running within the same address space.
+However, the programming model makes a distinction between &quot;tasks&quot;
+and pthreads:
+<ul>
+<li><i>tasks</i> are threads which have a degree of independence
+<li><a href="#Pthread"><i>pthreads</i></a> share some resources.
+</ul>
+<p>
+<b>File Descriptors and Streams</b>.
+This applies, in particular, in the area of opened file descriptors and streams.
+When a task is started using the interfaces in this section, it will be created
+with at most three open files.
+
+If CONFIG_DEV_CONSOLE is defined, the first three file descriptors (corresponding
+to stdin, stdout, stderr) will be duplicated for the the new task.
+Since these file descriptors are duplicated, the child task can free close
+them or manipulate them in any way without effecting the parent task.
+File-related operations (open, close, etc.) within a task will have no effect
+on other tasks.
+Since the three file descriptors are duplicated, it is also possible to perform
+some level of redirection.
+<p>
+pthreads, on the other hand, will always share file descriptors with the parent
+thread. In this case, file operations will have effect only all pthreads the
+were started from the same parent thread.
+
+<H3>2.1.1 task_create</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ int task_create(
+ char *name,
+ int priority,
+ int stack_size,
+ main_t entry,
+ char *arg1, char *arg2, char *arg3, char *arg4);
+</PRE>
+
+<P>
+<B>Description:</B>
+ This function creates and activates a new task with a
+ specified priority and returns its system-assigned ID.
+</p>
+
+<P>The entry address entry is the address of the &quot;main&quot;
+ function of the task.
+ This function will be called once the C environment has been set up.
+ The specified function will be called with four arguments.
+ Should the specified routine return, a call to exit() will automatically be made.
+</P>
+<P>
+ Note that four (and only four) arguments must be passed for the
+ spawned functions.
+</P>
+<p>
+ The newly created task does not inherity characteristics
+ from the parent task: The new task is started at the
+ default system priority and with the SCHED_FIFO scheduling
+ policy. These characteristcs may be modified after the new
+ task has been started.
+</p>
+<P>
+<B>Input Parameters:</B>
+ <UL>
+ <LI><I>name</I>. Name of the new task</LI>
+ <LI><I>priority</I>. Priority of the new task</LI>
+ <LI><I>stack_size</I>. size (in bytes) of the stack needed</LI>
+ <LI><I>entry</I>. Entry point of a new task</LI>
+ <LI><I>arg1, arg2, arg3, and arg4</I>. Four required task arguments to pass to the task</LI>
+ </UL>
+<P>
+ <B>Returned Values:</B>
+</P>
+<UL>
+<LI>
+ Returns the non-zero task ID of the new task or
+ ERROR if memory is insufficient or the task cannot be
+ created (errno is not set).
+</LI>
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the following similar interface:
+<PRE>
+ int taskSpawn(
+ char *name,
+ int priority,
+ int options,
+ int stackSize,
+ FUNCPTR entryPt,
+ int arg1, int arg2, int arg3, int arg4, int arg5,
+ int arg6, int arg7, int arg8, int arg9, int arg10);
+</PRE>
+
+<P>
+The Nuttx task_create() differs from VxWorks' taskSpawn() in the
+following ways:
+<UL>
+<LI>Function name
+<LI>Various differences in types or arguments
+<LI>There is no options arguement.
+<LI>One four parameters can be passed to a task (VxWorks allows
+ten).
+</UL>
+
+<H3>2.1.2 task_init</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ STATUS task_init(
+ _TCB *tcb,
+ char *name,
+ int priority,
+ uint32 *stack,
+ uint32 stack_size,
+ maint_t entry,
+ int arg1, int arg2, int arg3, int arg4);
+</PRE>
+
+<P>
+<B>Description:</B>
+<P>
+ This function initializes a Task Control Block (TCB)
+ in preparation for starting a new thread.
+</P>
+<P>
+ Unlike task_create(), task_init() does not activate the task.
+ This must be done by calling task_activate().
+</P>
+<P>
+<B>Input Parameters:</B>
+ <UL>
+ <LI><I>tcb</I>. Address of the new task's TCB
+ <LI><I>name</I>. Name of the new task (not used)
+ <LI><I>priority</I>. Priority of the new task
+ <LI><I>stack</I>. Start of the pre-allocated stack
+ <LI><I>stack_size</I>. size (in bytes) of the pre-allocated stack
+ <LI><I>entry</I>. Entry point of a new task
+ <LI><I>arg1, arg2, arg3, and arg4</I>. Four required task arguments to pass to the task
+ </UL>
+</p>
+<P>
+<B>Returned Values:</B>
+</p>
+<UL>
+ <LI><P>OK, or ERROR if the task cannot be initialized.</P>
+ <P>This function can only failure is it is unable to assign
+ a new, unique task ID to the TCB (errno is not set).</P>
+</UL>
+<P>
+<B>Assumptions/Limitations:</B>
+<UL>
+<LI>task_init() is provided to support internal OS functionality. It is
+<B>not recommended</B> for normal usage. task_create() is the preferred
+mechanism to initialize and start a new task.
+</UL>
+<P>
+<B>POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the following similar interface:
+<PRE>
+ STATUS taskInit(
+ WIND_TCB *pTcb,
+ char *name,
+ int priority,
+ int options,
+ uint32 *pStackBase,
+ int stackSize,
+ FUNCPTR entryPt,
+ int arg1, int arg2, int arg3, int arg4, int arg5,
+ int arg6, int arg7, int arg8, int arg9, int arg10);
+</PRE>
+
+<P>
+The Nuttx task_init() differs from VxWorks' taskInit() in the
+following ways:
+<UL>
+<LI>Function name
+<LI>Various differences in types or arguments
+<LI>There is no options argument.
+<LI>One four parameters can be passed to a task (VxWorks allows ten).
+</UL>
+
+<H3>2.1.3 task_activate</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ STATUS task_activate( _TCB *tcb );
+</PRE>
+
+<P>
+<B>Description:</B> This function activates tasks created by task_init().
+Without activation, a task is ineligible for execution by the
+scheduler.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>tcb</I>. The TCB for the task for the task (same as the
+task_init argument).
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>OK, or ERROR if the task cannot be activated (errno is not set).
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<UL>
+<LI>task_activate() is provided to support internal OS functionality. It is
+<B>not recommended</B> for normal usage. task_create() is the preferred
+mechanism to initialize and start a new task.
+</UL>
+<P>
+<B>POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the following similar interface:
+<PRE>
+ STATUS taskActivate( int tid );
+</PRE>
+
+<P>
+The Nuttx task_activate() differs from VxWorks' taskActivate() in the
+following ways:
+<UL>
+<LI>Function name
+<LI>With VxWork's taskActivate, the pid argument is supposed to be
+the pointer to the WIND_TCB cast to an integer.
+</UL>
+
+<H3>2.1.4 task_delete</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ STATUS task_delete( pid_t pid );
+</PRE>
+
+<P>
+<B>Description:</B> This function causes a specified task to cease
+to exist -- its stack and TCB will be deallocated. This function
+is the companion to task_create().
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>pid</I>. The task ID of the task to delete. An ID of
+zero signifies the calling task.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>OK, or ERROR if the task cannot be deleted.
+This function can fail if the provided pid does not correspond to a task (errno is not set)
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+task_delete() must be used with caution: If the task holds resources
+(for example, allocated memory or semaphores needed by other tasks), then
+task_delete() can strand those resources.
+<P>
+<B>POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the following similar interface:
+<PRE>
+ STATUS taskDelete( int tid );
+</PRE>
+
+<P>
+The Nuttx task_delete() differs from VxWorks' taskDelete() in
+the following ways:
+<UL>
+<LI>No support is provided for calling the tasks deletion routines
+(because taskDeleteHookAdd() is not supported).
+<LI>Deletion of self is not supported. Use _exit();
+</UL>
+
+<H3>2.1.5 exit</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ void exit( int code );
+
+ #include &lt;nuttx/unistd.h&gt;
+ void _exit( int code );
+</PRE>
+
+<P>
+<B>Description:</B> This function causes the calling task to cease
+to exist -- its stack and TCB will be deallocated. exit differs from
+_exit in that it flushs streams, closes file descriptors and will
+execute any function registered with atexit().
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>code</I>. (ignored)
+</UL>
+
+<P>
+<B>Returned Values:</B> None.
+
+<P>
+<B>Assumptions/Limitations:</B>
+
+<P>
+<B>POSIX Compatibility:</B> This is equivalent to the ANSI interface:
+<PRE>
+ void exit( int code );
+</PRE>
+And the unix interface:
+<PRE>
+ void _exit( int code );
+</PRE>
+
+<P>
+The Nuttx exit() differs from ANSI exit() in
+the following ways:
+<UL>
+<LI>The <I>code</I> parameter is ignored.
+</UL>
+
+<H3>2.1.6 task_restart</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ STATUS task_restart( pid_t pid );
+</PRE>
+
+<P>
+<B>Description:</B> This function &quot;restarts&quot; a task.
+The task is first terminated and then reinitialized with same
+ID, priority, original entry point, stack size, and parameters
+it had when it was first started.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>pid</I>. The task ID of the task to delete. An ID of
+zero signifies the calling task.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>
+ OK, or ERROR if the task ID is invalid or the task could
+ not be restarted.
+ This function can fail if:
+ (1) A pid of zero or the pid of the calling task is provided (functionality not implemented)
+ (2) The pid is not associated with any task known to the system.
+</LI>
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the following similar interface:
+<PRE>
+ STATUS taskRestart (int tid);
+</PRE>
+
+<P>
+The Nuttx task_restart() differs from VxWorks' taskRestart() in
+the following ways:
+<UL>
+<LI>Restart of the currently running task is not supported.
+<LI>The VxWorks description says that the ID, priority, etc. take
+the value that they had when the task was <I>terminated</I>.
+</UL>
+
+<H3>2.1.7 getpid</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;unistd.h&gt;
+ pid_t getpid( void );
+</PRE>
+
+<P>
+<B>Description:</B> This function returns the task ID of the
+calling task. The task ID will be invalid if called at the interrupt
+level.
+<P>
+<B>Input Parameters:</B> None.
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>The task ID of the calling task.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B>
+Compatible with the POSIX interface of the same name.
+
+<H2>2.2 <A NAME="Task_Schedule">Task Scheduling Interfaces</A></H2>
+
+<P>
+Nuttx performs strict priority scheduling: Tasks of higher
+priority have exclusive access to the CPU until they become blocked.
+At that time, the CPU is available to tasks of lower priority.
+Tasks of equal priority are scheduled FIFO.
+<P>
+The OS interfaces described in the following paragraphs provide
+a POSIX- compliant interface to the Nuttx scheduler. However, these
+POSIX interfaces assume a greater range of scheduling options
+than those provided by the Nuttx scheduler. As a result, many of
+these POSIX interfaces are little more than stubs.
+
+<H3>2.2.1 sched_setparam</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ int sched_setparam( pid_t pid, const struct sched_param *param );
+</PRE>
+
+<P>
+<B>Description:</B> This function sets the priority of the task
+specified by pid input parameter.
+<P>
+NOTE: Setting a task's priority to the same value has the similar
+effect to sched_yield() -- The task will be moved to after all
+other tasks with the same priority.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>pid</I>. The task ID of the task. If pid is zero, the
+priority of the calling task is set.
+<LI><I>param</I>. A structure whose member sched_priority is the
+integer priority. The range of valid priority numbers is from
+SCHED_PRIORITY_MIN through SCHED_PRIORITY_MAX.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>
+ 0 (OK) if successful, otherwise -1 (ERROR).
+ This function can fail for the following reasons:
+ (1) parm is NULL or parm->sched_priority is out of range.
+ (2) pid does not correspond to any task.
+</LI>
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the full POSIX implementation include:
+<UL>
+<LI>The range of priority values for the POSIX call is 0 to 255
+</UL>
+
+<H3>2.2.2 sched_getparam</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ int sched_getparam (pid_t pid, struct sched_param *param);
+</PRE>
+
+<P>
+<B>Description:</B> This function gets the scheduling priority
+of the task specified by pid.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>pid</I>. The task ID of the task. If pid is zero, the
+priority of the calling task is returned.
+<LI><I>param</I>. A structure whose member sched_priority is the
+integer priority. The task's priority is copied to the sched_priority
+element of this structure.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK) if successful, otherwise -1 (ERROR).
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.2.3 sched_setscheduler</H3>
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ int sched_setscheduler (pid_t pid, int policy, const struct sched_param *param);
+</PRE>
+<P>
+ <B>Description:</B>
+ <i>sched_setscheduler()</i> sets both the scheduling policy
+ and the priority for the task identified by pid.
+ If pid equals zero, the scheduler of the calling
+ thread will be set.
+ The parameter 'param' holds the priority of the thread under the new policy.
+</p>
+<P>
+<B>Input Parameters:</B>
+<UL>
+ <LI><I>pid</I>. The task ID of the task. If pid is zero, the
+ priority of the calling task is set.
+ <LI><I>policy</I>. Scheduling policy requested (either SCHED_FIFO
+ or SCHED_RR).
+ <LI><I>param</I>. A structure whose member sched_priority is the
+ integer priority. The range of valid priority numbers is from
+ SCHED_PRIORITY_MIN through SCHED_PRIORITY_MAX.
+</UL>
+<P>
+ <B>Returned Values:</B>
+ On success, <i>sched_setscheduler()</i> returns OK (zero). On
+ error, ERROR (-1) is returned, and errno is set appropriately:
+</p>
+<ul>
+ <li>EINVAL The scheduling policy is not one of the
+ recognized policies.</li>
+ <li>ESRCH The task whose ID is pid could not be found.</li>
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.2.4 sched_getscheduler</H3>
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ int sched_getscheduler (pid_t pid);
+</PRE>
+<P>
+ <B>Description:</B>
+ <i>sched_getscheduler()</i> returns the scheduling policy
+ currently applied to the process identified by pid. If
+ pid equals zero, the policy of the calling process will
+ be retrieved.
+ *
+ * Inputs:
+ *
+ * Return Value:
+
+ This function returns the current scheduling
+policy.
+<P>
+<B>Input Parameters:</B>
+<UL>
+ <LI><I>pid</I>.
+ The task ID of the task to query.
+ If pid is zero, the calling task is queried.
+ </LI>
+</UL>
+<P>
+<B>Returned Values:</B>
+<UL>
+ <LI>
+ On success, <i>sched_getscheduler()</i> returns the policy for
+ the task (either SCHED_FIFO or SCHED_RR).
+ On error, ERROR (-1) is returned, and errno is set appropriately:
+ <ul>
+ <li>ESRCH The task whose ID is pid could not be found.</li>
+ </ul>
+ </li>
+</UL>
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the full POSIX implementation include:
+<UL>
+<LI>Does not report errors via <I>errno</I>.
+</UL>
+
+<H3>2.2.5 sched_yield</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ int sched_yield( void );
+</PRE>
+
+<P>
+<B>Description:</B> This function forces the calling task to give
+up the CPU (only to other tasks at the same priority).
+<P>
+<B>Input Parameters:</B> None.
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK) or -1 (ERROR)
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.2.6 sched_get_priority_max</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ int sched_get_priority_max (int policy)
+</PRE>
+
+<P>
+<B>Description:</B> This function returns the value of the highest
+possible task priority for a specified scheduling policy.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>policy</I>. Scheduling policy requested.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>The maximum priority value or -1 (ERROR).
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.2.7 sched_get_priority_min</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ int sched_get_priority_min (int policy);
+</PRE>
+
+<P>
+<B>Description:</B> This function returns the value of the lowest
+possible task priority for a specified scheduling policy.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>policy</I>. Scheduling policy requested.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>The minimum priority value or -1 (ERROR)
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.2.8 sched_get_rr_interval</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ int sched_get_rr_interval (pid_t pid, struct timespec *interval);
+</PRE>
+
+<P>
+ <B>Description:</B>
+ <i>sched_rr_get_interval()</i> writes the timeslice interval
+ for task identified by <i>pid</i> into the timespec structure
+ pointed to by <i>interval</i>. If pid is zero, the timeslice
+ for the calling process is written into 'interval. The
+ identified process should be running under the SCHED_RR
+ scheduling policy.'
+</p>
+<P>
+ <B>Input Parameters:</B>
+</p>
+<UL>
+<LI><I>pid</I>. The task ID of the task. If pid is zero, the
+priority of the calling task is returned.
+<LI><I>interval</I>. A structure used to return the time slice.
+</UL>
+
+<P>
+ <B>Returned Values:</B>
+ On success, sched_rr_get_interval() returns OK (0). On
+ error, ERROR (-1) is returned, and errno is set to:
+</p>
+<UL>
+ <LI>EFAULT Cannot copy to interval</LI>
+ <LI>EINVAL Invalid pid.</LI>
+ <LI>ENOSYS The system call is not yet implemented.</LI>
+ <LI>ESRCH The process whose ID is pid could not be found.</LI>
+</UL>
+
+<P>
+ <B>Assumptions/Limitations:</B>
+<P>
+ <B>POSIX Compatibility:</B> Comparable to the POSIX
+ interface of the same name.
+</P>
+
+<H2>2.3 <A NAME="Task_Switch">Task Switching Interfaces</A></H2>
+
+<H3>2.3.1 sched_lock</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ STATUS sched_lock( void );
+</PRE>
+
+<P>
+<B>Description:</B> This function disables context switching by
+Disabling addition of new tasks to the ready-to-run task list.
+The task that calls this function will be the only task that is
+allowed to run until it either calls sched_unlock (the appropriate
+number of times) or until it blocks itself.
+<P>
+<B>Input Parameters:</B> None.
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>OK or ERROR.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the comparable interface:
+<PRE>
+ STATUS taskLock( void );
+</PRE>
+
+<H3>2.3.2 sched_unlock</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ STATUS sched_unlock( void );
+</PRE>
+
+<P>
+<B>Description:</B> This function decrements the preemption lock
+count. Typically this is paired with sched_lock() and concludes
+a critical section of code. Preemption will not be unlocked until
+sched_unlock() has been called as many times as sched_lock().
+When the lockCount is decremented to zero, any tasks that were
+eligible to preempt the current task will execute.
+<P>
+<B>Input Parameters:</B> None.
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>OK or ERROR.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the comparable interface:
+<PRE>
+ STATUS taskUnlock( void );
+</PRE>
+
+<H3>2.3.3 sched_lockcount</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;sched.h&gt;
+ sint32 sched_lockcount( void )
+</PRE>
+
+<P>
+<B>Description:</B> This function returns the current value of
+the lockCount. If zero, preemption is enabled; if non-zero, this
+value indicates the number of times that sched_lock() has been called
+on this thread of execution.
+<P>
+<B>Input Parameters:</B> None.
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>The current value of the lockCount.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> None.
+<HR>
+
+<H2>2.4 <A NAME="Message_Queue">Named Message Queue Interfaces</A></H2>
+
+<P>
+The Nuttx supports POSIX named message queues for intertask communication.
+Any task may send or receive messages on named message queues.
+Interrupt handlers may send messages via named message queues.
+
+<H3>2.4.1 mq_open</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;mqueue.h&gt;
+ mqd_t mq_open( const char *mqName, int oflags, ... );
+</PRE>
+
+<P>
+<B>Description:</B> This function establish a connection between
+a named message queue and the calling task. After a successful
+call of mq_open(), the task can reference the message queue using
+the address returned by the call. The message queue remains usable
+until it is closed by a successful call to mq_close().
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>mqName</I>. Name of the queue to open
+<LI><I>oflags</I>. Open flags. These may be any combination of:
+<UL>
+<LI><I>O_RDONLY</I>. Open for read access.
+<LI><I>O_WRONLY</I>. Open for write access.
+<LI><I>O_RDWR</I>. Open for both read &amp; write access.
+<LI><I>O_CREAT</I>. Create message queue if it does not already
+exist.
+<LI><I>O_EXCL</I>. Name must not exist when opened.
+<LI><I>O_NONBLOCK</I>. Don't wait for data.
+</UL>
+
+<LI><I>... Optional parameters</I>.
+When the O_CREAT flag is specified, POSIX requires that a third
+and fourth parameter be supplied:
+<UL>
+<LI><I>mode</I>. The mode parameter is of type mode_t. In the POSIX
+specification, this mode value provides file permission bits for the
+message queue. This parameter is required but not used in the present
+implementation.
+<LI><I>attr</I>. A pointer to an mq_attr that is provided to initialize.
+the message queue. If attr is NULL, then the messages queue is created
+with implementation-defined default message queue attributes. If attr is
+non-NULL, then the message queue mq_maxmsg attribute is set to the
+corresponding value when the queue is created. The mq_maxmsg attribute
+determines the maximum number of messages that can be queued before
+addition attempts to send messages on the message queue fail or cause the
+sender to block; the mq_msgsize attribute determines the maximum size of a
+message that can be sent or received. Other elements of attr are ignored
+(i.e, set to default message queue attributes).
+</UL>
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>A message queue descriptor or -1 (ERROR)
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX interface
+of the same name.
+Differences from the full POSIX implementation include:
+<UL>
+<LI>The mq_msgsize attributes determines the maximum size of a message that
+may be sent or received. In the present implementation, this maximum
+message size is limited at 22 bytes.
+</UL>
+
+<H3>2.4.2 mq_close</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;mqueue.h&gt;
+ int mq_close( mqd_t mqdes );
+</PRE>
+
+<P>
+<B>Description:</B> This function is used to indicate that the
+calling task is finished with the specified message queued mqdes.
+The mq_close() deallocates any system resources allocated by the
+system for use by this task for its message queue.
+<P>
+If the calling task has attached a notification request to the message
+queue via this <I>mqdes</I> (see mq_notify()), this attachment will be
+removed and the message queue is available for another task to attach
+for notification.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>mqdes</I>. Message queue descriptor.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK) if the message queue is closed successfully, otherwise,
+-1 (ERROR).
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<UL>
+<LI>The behavior of a task that is blocked on either a mq_send() or
+mq_receive() is undefined when mq_close() is called.
+<LI>The result of using this message queue descriptor after successful
+return from mq_close() is undefined.
+</UL>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX interface
+of the same name.
+
+<H3>2.4.3 mq_unlink</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;mqueue.h&gt;
+ int mq_unlink( const char *mqName );
+</PRE>
+
+<P>
+<B>Description:</B> This function removes the message queue named
+by &quot;mqName.&quot; If one or more tasks have the message queue
+open when mq_unlink() is called, removal of the message queue
+is postponed until all references to the message queue have been
+closed.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>mqName</I>. Name of the message queue
+</UL>
+
+<P>
+<B>Returned Values:</B> None.
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.4.4 mq_send</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;mqueue.h&gt;
+ int mq_send( mqd_t mqdes, const void *msg, size_t msgLen, int msgPrio );
+</PRE>
+
+<P>
+<B>Description:</B> This function adds the specified message (msg)
+to the message queue (mqdes). The &quot;msgLen&quot; parameter
+specifies the length of the message in bytes pointed to by &quot;msg.&quot;
+This length must not exceed the maximum message length from the
+mq_getattr().
+<P>
+If the message queue is not full, mq_send() will in the message
+in the message queue at the position indicated by the &quot;msgPrio&quot;
+argument. Messages with higher priority will be inserted before
+lower priority messages. The value of &quot;msgPrio&quot; must
+not exceed MQ_PRIO_MAX.
+<P>
+If the specified message queue is full and O_NONBLOCK is not
+set in the message queue, then mq_send() will block until space
+becomes available to the queue the message.
+<P>
+If the message queue is full and osNON_BLOCK is set, the message
+is not queued and ERROR is returned.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>mqdes</I>. Message queue descriptor
+<LI><I>msg</I>. Message to send
+<LI><I>msgLen</I>. The length of the message in bytes
+<LI><I>msgPrio</I>. The priority of the message
+</UL>
+
+<P>
+<B>Returned Values:</B> None.
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the full POSIX implementation include:
+<UL>
+<LI>Control is not returned if a signal is received.
+</UL>
+
+<H3>2.4.5 mq_receive</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;mqueue.h&gt;
+ int mq_receive( mqd_t mqdes, void *msg, size_t msgLen, int *msgPrio );
+</PRE>
+
+<P>
+<B>Description:</B> This function receives the oldest of the highest
+priority messages from the message queue specified by &quot;mqdes.&quot;
+If the size of the buffer in bytes (msgLen) is less than the &quot;mq_msgsize&quot;
+attribute of the message queue, mq_receive will return an error.
+Otherwise, the select message is removed from the queue and copied
+to &quot;msg.&quot;
+<P>
+If the message queue is empty and O_NONBLOCK was not set, mq_receive()
+will block until a message is added to the message queue. If more
+than one task is waiting to receive a message, only the task with
+the highest priority that has waited the longest will be unblocked.
+<P>
+If the queue is empty and O_NONBLOCK is set, ERROR will be
+returned.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>mqdes</I>. Message Queue Descriptor
+<LI><I>msg</I>. Buffer to receive the message
+<LI><I>msgLen</I>. Size of the buffer in bytes
+<LI><I>msgPrio</I>. If not NULL, the location to store message
+priority.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>Length of the selected message in bytes, otherwise -1 (ERROR).
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the full POSIX implementation include:
+<UL>
+<LI>Control is not returned if a signal is received.
+</UL>
+
+<H3>2.4.6 mq_notify</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;mqueue.h&gt;
+ int mq_notify( mqd_t mqdes, const struct sigevent *notification );
+</PRE>
+
+<P>
+<B>Description:</B> If the &quot;notification&quot; input parameter
+is not NULL, this function connects the task with the message queue such
+that the specified signal will be sent to the task whenever the message
+changes from empty to non-empty. One notification can be attached
+to a message queue.
+<P>
+If &quot;notification&quot; is NULL, the attached notification
+is detached (if it was held by the calling task) and the queue
+is available to attach another notification.
+<P>
+When the notification is sent to the registered task, its registration
+will be removed. The message queue will then be available for
+registration.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>mqdes</I>. Message queue descriptor
+<LI><I>notification</I>. Real-time signal structure containing:
+<UL>
+<LI><I>sigev_notify</I>. Should be osSIGEV_SIGNAL (but actually
+ignored)
+<LI><I>sigev_signo</I>. The signo to use for the notification
+<LI><I>sigev_value</I>. Value associated with the signal
+</UL>
+
+</UL>
+
+<P>
+<B>Returned Values:</B> None.
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX interface
+of the same name.
+Differences from the full POSIX implementation include:
+<UL>
+<LI>The notification signal will be sent to the registered task even if
+another task is waiting for the message queue to become non-empty. This is
+inconsistent with the POSIX specification which states, &quot;If a process
+has registered for notification of message arrival at a message queue and
+some process is blocked in <I>mq_receive</I> waiting to receive a message
+when a message arrives at the queue, the arriving message shall satisfy the
+appropriate <I>mq_receive()</I> ... The resulting behavior is as if the
+message queue remains empty, and no notification shall be sent.&quot;
+</UL>
+
+<H3>2.4.7 mq_setattr</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;mqueue.h&gt;
+ int mq_setattr( mqd_t mqdes, const struct mq_attr *mqStat,
+ struct mq_attr *oldMqStat);
+</PRE>
+
+<P>
+<B>Description:</B> This function sets the attributes associated
+with the specified message queue &quot;mqdes.&quot; Only the &quot;O_NONBLOCK&quot;
+bit of the &quot;mq_flags&quot; can be changed.
+<P>
+If &quot;oldMqStat&quot; is non-null, mq_setattr() will store
+the previous message queue attributes at that location (just as
+would have been returned by mq_getattr()).
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>mqdes</I>. Message queue descriptor
+<LI><I>mqStat</I>. New attributes
+<LI><I>oldMqState</I>. Old attributes
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK) if attributes are set successfully, otherwise -1
+(ERROR).
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.4.8 mq_getattr</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;mqueue.h&gt;
+ int mq_getattr( mqd_t mqdes, struct mq_attr *mqStat);
+</PRE>
+
+<P>
+<B>Description:</B> This functions gets status information and
+attributes associated with the specified message queue.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>mqdes</I>. Message queue descriptor
+<LI><I>mqStat</I>. Buffer in which to return attributes. The returned
+attributes include:
+<UL>
+<LI><I>mq_maxmsg</I>. Max number of messages in queue.
+<LI><I>mq_msgsize</I>. Max message size.
+<LI><I>mq_flags</I>. Queue flags.
+<LI><I>mq_curmsgs</I>. Number of messages currently in queue.
+</UL>
+
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK) if attributes provided, -1 (ERROR) otherwise.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H2>2.5 <A NAME="Semaphores">Counting Semaphore Interfaces</A></H2>
+
+<P>
+<B>Semaphores</B>. Semaphores are the basis for
+synchronization and mutual exclusion in Nuttx. Nuttx supports
+POSIX semaphores.
+<P>
+Semaphores are the preferred mechanism for gaining exclusive access to a
+resource. sched_lock() and sched_unlock() can also be used for this purpose.
+However, sched_lock() and sched_unlock() have other undesirable side-affects
+in the operation of the system: sched_lock() also prevents higher-priority
+tasks from running that do not depend upon the semaphore-managed resource
+and, as a result, can adversely affect system response times.
+<P>
+<B>Priority Inversion</B>. Proper use of semaphores avoids the issues of
+sched_lock(). However, consider the following example:
+<OL>
+<LI>Some low-priority task, <I>Task C</I>, acquires a semphore in order to
+get exclusive access to a protected resource.
+<LI><I>Task C</I> is suspended to allow some high-priority task,
+<I>Task A</I>, to execute.
+<LI><I>Task A</I> attempts to acquire the semaphore held by <I>Task C</I> and
+gets blocked until <I>Task C</I> relinquishes the semaphore.
+<LI><I>Task C</I> is allowed to execute again, but gets suspended by some
+medium-priority <I>Task B</I>.
+</OL>
+At this point, the high-priority <I>Task A</I> cannot execute until
+<I>Task B</I> (and possibly other medium-priority tasks) completes and until
+<I>Task C</I> relinquishes the semaphore. In effect, the high-priority task,
+<I>Task A</I> behaves as though it were lower in priority than the
+low-priority task, <I>Task C</I>! This phenomenon is called <I>priority
+inversion</I>.
+<P>
+Some operating systems avoid priority inversion by <I>automatically</I>
+increasing the priority of the low-priority <I>Task C</I> (the operable
+buzz-word for this behavior is <I>mutex</I> semaphores). The Nuttx does not
+support this behavior. As a consequence, it is left to the designer to
+provide implementations that will not suffer from priority inversion.
+The designer may, as examples:
+<UL>
+<LI>Implement all tasks that need the semphore-managed resources at the
+same priority level,
+<LI>Boost the priority of the low-priority task before the semaphore is
+acquired, or
+<LI>Use sched_lock() in the low-priority task.
+</UL>
+<P>
+
+<H3>2.5.1 sem_init</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;semaphore.h&gt;
+ int sem_init ( sem_t *sem, int pshared, unsigned int value );
+</PRE>
+
+<P>
+<B>Description:</B> This function initializes the UN-NAMED semaphore
+sem. Following a successful call to sem_init(), the semaphore
+may be used in subsequent calls to sem_wait(), sem_post(), and
+sem_trywait(). The semaphore remains usable until it is destroyed.
+<P>
+Only <I>sem</I> itself may be used for performing synchronization. The
+result of referring to copies of <I>sem</I> in calls to <I>sem_wait()</I>,
+<I>sem_trywait()</I>, <I>sem_post()</I>, and <I>sem_destroy()</I>, is
+not defined.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>sem</I>. Semaphore to be initialized
+<LI><I>pshared</I>. Process sharing (not used)
+<LI><I>value</I>. Semaphore initialization value
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if unsuccessful.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the full POSIX implementation include:
+<UL>
+<LI>pshared is not used.
+</UL>
+
+<H3>2.5.2 sem_destroy</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;semaphore.h&gt;
+ int sem_destroy ( sem_t *sem );
+</PRE>
+
+<P>
+<B>Description:</B> This function is used to destroy the un-named semaphore
+indicated by <I>sem</I>. Only a semaphore that was created using
+<I>sem_init()</I> may be destroyed using <I>sem_destroy()</I>. The effect
+of calling <I>sem_destroy()</I> with a named semaphore is undefined. The
+effect of subsequent use of the semaphore <I>sem</I> is undefined until
+<I>sem</I> is re-initialized by another call to <I>sem_init()</I>.
+<P>
+The effect of destroying a semaphore upon which other tasks are currently
+blocked is undefined.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>sem</I>. Semaphore to be destroyed.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if unsuccessful.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.5.3 sem_open</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;semaphore.h&gt;
+ sem_t *sem_open ( const char *name, int oflag, ...);
+</PRE>
+
+<P>
+<B>Description:</B> This function establishes a connection between
+named semaphores and a task. Following a call to sem_open() with
+the semaphore name, the task may reference the semaphore associated
+with name using the address returned by this call. The semaphore
+may be used in subsequent calls to sem_wait(), sem_trywait(),
+and sem_post(). The semaphore remains usable until the semaphore
+is closed by a successful call to sem_close().
+<P>
+If a task makes multiple calls to sem_open() with the same name,
+then the same semaphore address is returned (provided there have
+been no calls to sem_unlink()).
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>name</I>. Semaphore name
+<LI><I>oflag</I>. Semaphore creation options. This may one of
+the following bit settings:
+<UL>
+<LI><I>oflag</I> = 0: Connect to the semaphore only if it already
+exists.
+<LI><I>oflag</I> = O_CREAT: Connect to the semaphore if it exists,
+otherwise create the semaphore.
+<LI><I>oflag</I> = O_CREAT with O_EXCL (O_CREAT|O_EXCL): Create
+a new semaphore unless one of this name already exists.
+</UL>
+<LI>... Optional parameters.
+NOTE: When the O_CREAT flag is specified, POSIX requires that a third
+and fourth parameter be supplied:
+<UL>
+<LI><I>mode</I>. The mode parameter is of type mode_t.
+This parameter is required but not used in the present
+implementation.
+<LI><I>value</I>. The value parameter is type unsigned int. The semaphore
+is created with an initial value of <I>value</I>. Valid initial values for
+semaphores must be less than or equal to <I>SEM_MAX_VALUE</I>.
+</UL>
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>A pointer to sem_t or -1 (ERROR) if unsuccessful.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the full POSIX implementation include:
+<UL>
+<LI>Treatment of links/connections is highly simplified. It is
+just a counting semaphore.
+</UL>
+
+<H3>2.5.4 sem_close</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;semaphore.h&gt;
+ int sem_close ( sem_t *sem );
+</PRE>
+
+<P>
+<B>Description:</B> This function is called to indicate that the
+calling task is finished with the specified named semaphore, sem.
+The sem_close() deallocates any system resources allocated by
+the system for this named semaphore.
+<P>
+If the semaphore has not been removed with a call to sem_unlink(),
+then sem_close() has no effect on the named semaphore. However,
+when the named semaphore has been fully unlinked, the semaphore
+will vanish when the last task closes it.
+<P>
+Care must be taken to avoid risking the deletion of a semaphore
+that another calling task has already locked.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>sem</I>. Semaphore descriptor
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if unsuccessful.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<UL>
+<LI>Care must be taken to avoid deletion of a semaphore that another task
+has already locked.
+<LI>sem_close() must not be called with an un-named semaphore.
+</UL>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.5.5 sem_unlink</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;semaphore.h&gt;
+ int sem_unlink ( const char *name );
+</PRE>
+
+<P>
+<B>Description:</B> This function will remove the semaphore named by the
+input name parameter. If one or more tasks have the semaphore named by
+name oepn when sem_unlink() is called, destruction of the semaphore will
+be postponed until all references have been destroyed by calls to
+sem_close().
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>name</I>. Semaphore name
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if unsuccessful.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<UL>
+<LI>Care must be taken to avoid deletion of a semaphore that another task
+has already locked.
+<LI>sem_unlink() must not be called with an un-named semaphore.
+</UL>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the full POSIX implementation include:
+<UL>
+<LI>Treatment of links/connections is highly simplified. It is
+just a counting semaphore.
+<LI>Calls to sem_open() to re-create or re-connect to the semaphore may
+refer to the same semaphore; POSIX specifies that a new semaphore with the
+same name should be created after sem_unlink() is called.
+</UL>
+
+<H3>2.5.6 sem_wait</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;semaphore.h&gt;
+ int sem_wait ( sem_t *sem );
+</PRE>
+
+<P>
+<B>Description:</B> This function attempts to lock the semaphore
+referenced by sem. If the semaphore as already locked by another
+task, the calling task will not return until it either successfully acquires
+the lock or the call is interrupted by a signal.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>sem</I>. Semaphore descriptor.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) is unsuccessful
+</UL>
+<P>
+If <I>sem_wait</I> returns -1 (ERROR) then the cause of the failure
+will be indicated by the thread-specific <I>errno</I> value (a pointer
+to this value can be obtained using <I>get_errno_ptr()</I>). The following
+lists the possible values for <I>errno</I>:
+<P>
+<UL>
+<LI><I>EINVAL</I>: Indicates that the <I>sem</I> input parameter is
+not valid.
+<LI><I>EINTR</I>: Indicates that the wait was interrupt by a signal
+received by this task. In this case, the semaphore has not be acquired.
+</UL>
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.5.7 sem_trywait</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;semaphore.h&gt;
+ int sem_trywait ( sem_t *sem );
+</PRE>
+
+<P>
+<B>Description:</B> This function locks the specified semaphore
+only if the semaphore is currently not locked. In any event, the call
+returns without blocking.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>sem</I>. The semaphore descriptor
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK) or -1 (ERROR) if unsuccessful
+</UL>
+If <I>sem_wait</I> returns -1 (ERROR) then the cause of the failure
+will be indicated by the thread-specific <I>errno</I> value (a pointer
+to this value can be obtained using <I>get_errno_ptr()</I>). The following
+lists the possible values for <I>errno</I>:
+<P>
+<UL>
+<LI><I>EINVAL</I>: Indicates that the <I>sem</I> input parameter is
+not valid.
+<LI><I>EAGAIN</I>: Indicates that the semaphore was not acquired.
+</UL>
+<P>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.5.8 sem_post</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;semaphore.h&gt;
+ int sem_post ( sem_t *sem );
+</PRE>
+
+<P>
+<B>Description:</B> When a task has finished with a semaphore,
+it will call sem_post(). This function unlocks the semaphore referenced
+by <I>sem</I> by performing the semaphore unlock operation.
+<P>
+If the semaphore value resulting from this operation is positive, then
+no tasks were blocked waiting for the semaphore to become unlocked;
+The semaphore value is simply incremented.
+<P>
+If the value of the semaphore resulting from this operation is zero, then
+on of the tasks blocked waiting for the semaphore will be allowed to
+return successfully from its call to <I>sem_wait()</I>.
+<P>
+<B>NOTE</B>: <I>sem_post()</I> may be called from an interrupt handler.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>sem</I>. Semaphore descriptor
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK) or -1 (ERROR) if unsuccessful.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B> This function cannot be called
+from an interrupt handler. It assumes the currently executing
+task is the one that is performing the unlock.
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.5.9 sem_getvalue</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;semaphore.h&gt;
+ int sem_getvalue ( sem_t *sem, int *sval );
+</PRE>
+
+<P>
+<B>Description:</B> This function updates the location referenced
+by sval argument to have the value of the semaphore referenced
+by sem without effecting the state of the semaphore. The updated
+value represents the actual semaphore value that occurred at some
+unspecified time during the call, but may not reflect the actual
+value of the semaphore when it is returned to the calling task.
+<P>
+If sem is locked, the value return by sem_getvalue() will either
+be zero or a negative number whose absolute value represents the
+number of tasks waiting for the semaphore.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>sem</I>. Semaphore descriptor
+<LI><I>sval</I>. Buffer by which the value is returned
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK) or -1 (ERROR) if unsuccessful.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<HR>
+
+<H2>2.6 <A NAME="Watchdogs">Watchdog Timer Interfaces</A></H2>
+
+<P>
+The Nuttx provides a general watchdog timer facility. This
+facility allows the Nuttx user to specify a watchdog timer function
+that will run after a specified delay. The watchdog timer function
+will run in the context of the timer interrupt handler. Because
+of this, a limited number of Nuttx interfaces are available to
+the watchdog timer function. However, the watchdog timer function
+may use mq_send(), and sigqueue() to communicate with Nuttx tasks.
+
+<H3>2.6.1 wd_create</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;wdog.h&gt;
+ WDOG_ID wd_create (void);
+</PRE>
+
+<P>
+<B>Description:</B> The wd_create function will create a watchdog
+by allocating the appropriate resources for the watchdog.
+<P>
+<B>Input Parameters:</B> None.
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>Pointer to watchdog that may be used as a handle in subsequent
+Nuttx calls (i.e., the watchdog ID), or NULL if insufficient resources
+are available to create the watchdogs.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the following comparable interface:
+<PRE>
+ WDOG_ID wdCreate (void);
+</PRE>
+
+<P>
+Differences from the VxWorks interface include:
+<UL>
+<LI>The number of available watchdogs is fixed (configured at
+initialization time).
+</UL>
+
+<H3>2.6.2 wd_delete</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;wdog.h&gt;
+ STATUS wd_delete (WDOG_ID wdId);
+</PRE>
+
+<P>
+<B>Description:</B> The wd_delete function will deallocate a
+watchdog. The watchdog will be removed from the timer queue if
+has been started.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>wdId</I>. The watchdog ID to delete. This is actually a
+pointer to a watchdog structure.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>OK or ERROR
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B> It is the responsibility of the
+caller to assure that the watchdog is inactive before deleting
+it.
+<P>
+<B> POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the following comparable interface:
+<PRE>
+ STATUS wdDelete (WDOG_ID wdId);
+</PRE>
+
+<P>
+Differences from the VxWorks interface include:
+<UL>
+<LI>Does not make any checks to see if the watchdog is being used
+before de-allocating it (i.e., never returns ERROR).
+</UL>
+
+<H3>2.6.3 wd_start</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;wdog.h&gt;
+ STATUS wd_start( WDOG_ID wdId, int delay, wdentry_t wdentry,
+ int parm1, int parm2, int parm3, int parm4 );
+</PRE>
+
+<P>
+<B>Description:</B> This function adds a watchdog to the timer
+queue. The specified watchdog function will be called from the
+interrupt level after the specified number of ticks has elapsed.
+Watchdog timers may be started from the interrupt level.
+<P>
+Watchdog times execute in the context of the timer interrupt handler, but
+with the PIC/PID address environment that was in place when wd_start()
+was called.
+<P>
+Watchdog timers execute only once.
+<P>
+To replace either the timeout delay or the function to be executed,
+call wd_start again with the same wdId; only the most recent
+wd_start() on a given watchdog ID has any effect.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>wdId</I>. Watchdog ID
+<LI><I>delay</I>. Delay count in clock ticks
+<LI><I>wdentry</I>. Function to call on timeout
+<LI><I>parm1..4</I>. Parameters to pass to wdentry
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>OK or ERROR
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B> The watchdog routine runs in the
+context of the timer interrupt handler and is subject to all ISR
+restrictions.
+<P>
+<B> POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the following comparable interface:
+<PRE>
+ STATUS wdStart (WDOG_ID wdId, int delay, FUNCPTR wdentry, int parameter);
+</PRE>
+
+<P>
+Differences from the VxWorks interface include:
+<UL>
+<LI>The present implementation supports four parameters passed
+to wdentry; VxWorks supports only a single parameter.
+</UL>
+
+<H3>2.6.4 wd_cancel</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;wdog.h&gt;
+ STATUS wd_cancel (WDOG_ID wdId);
+</PRE>
+
+<P>
+<B>Description:</B> This function cancels a currently running
+watchdog timer. Watchdog timers may be canceled from the interrupt
+level.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>wdId</I>. ID of the watchdog to cancel.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>OK or ERROR
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> This is a NON-POSIX interface.
+VxWorks provides the following comparable interface:
+<PRE>
+ STATUS wdCancel (WDOG_ID wdId);
+</PRE>
+
+<HR>
+
+<H2>2.7 <A NAME="Signals">Signal Interfaces</A></H2>
+
+<P>
+The Nuttx provides signal interfaces for tasks. Signals are
+used to alter the flow control of tasks by communicating asynchronous
+events within or between task contexts. Any task or interrupt
+handler can post (or send) a signal to a particular task. The
+task being signaled will execute task-specified signal handler
+function the next time that the task has priority.
+The signal handler is a user-supplied function that is bound to
+a specific signal and performs whatever actions are necessary
+whenever the signal is received.
+<P>
+Signal handlers execute in the context of the task that registered
+the signal handler.
+<P>
+There are no predefined actions for any signal.
+The default action for all signals (i.e., when no signal handler has
+been supplied by the user) is to ignore the signal.
+<P>
+Tasks may also suspend themselves and wait until a signal is received.
+
+<H3>2.7.1 sigemptyset</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigemptyset(sigset_t *set);
+</PRE>
+
+<P>
+<B>Description:</B> This function initializes the signal set specified
+by set such that all signals are excluded.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>set</I>. Signal set to initialize.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if the signal set cannot be initialized.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.7.2 sigfillset</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigfillset(sigset_t *set);
+</PRE>
+
+<P>
+<B>Description:</B> This function initializes the signal set specified
+by set such that all signals are included.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>set</I>. Signal set to initialize
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if the signal set cannot be initialized.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.7.3 sigaddset</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigaddset(sigset_t *set, int signo);
+</PRE>
+
+<P>
+<B>Description:</B> This function adds the signal specified by
+signo to the signal set specified by set.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>set</I>. Signal set to add signal to
+<LI><I>signo</I>. Signal to add
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if the signal number is invalid.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.7.4 sigdelset</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigdelset(sigset_t *set, int signo);
+</PRE>
+
+<P>
+<B>Description:</B> This function deletes the signal specified
+by signo from the signal set specified by set.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>set</I>. Signal set to delete the signal from
+<LI><I>signo</I>. Signal to delete
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if the signal number is invalid.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.7.5 sigismember</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigismember(const sigset_t *set, int signo);
+</PRE>
+
+<P>
+<B>Description:</B> This function tests whether the signal specified
+by signo is a member of the set specified by set.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>set</I>. Signal set to test
+<LI><I>signo</I>. Signal to test for
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>1 (TRUE), if the specified signal is a member of the set,
+<LI>0 (OK or FALSE), if it is not, or
+<LI>-1 (ERROR) if the signal number is invalid.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.7.6 sigaction</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigaction( int signo, const struct sigaction *act,
+ struct sigaction *oact );
+</PRE>
+
+<P>
+<B>Description:</B> This function allows the calling task to
+examine and/or specify the action to be associated with a specific
+signal.
+<P>
+The structure sigaction, used to describe an action to be taken, is defined
+to include the following members:
+<UL>
+<LI><I>sa_u.sa_handler</I>. A pointer to a signal-catching function.
+<LI><I>sa_u.sa_sigaction</I>. An alternative form for the signal catching
+function.
+<LI><I>sa_mask</I>. Additional set of signals to be blocked during
+execution of the signal-catching function.
+<LI><I>sa_flags</I>: Special flags to affect behavior of a signal.
+</UL>
+<P>
+If the argument act is not NULL, it points to a structure specifying the
+action to be associated with the specified signal. If the argument oact
+is not NULL, the action previously associated with the signal is stored
+in the location pointed to by the argument oact. If the argument act is
+NULL, signal handling is unchanged by this function call; thus, the call
+can be used to enquire about the current handling of a given signal.
+<P>
+When a signal is caught by a signal-catching function installed by the
+sigaction() function, a new signal mask is calculated and installed for
+the duration of the signal-catching function. This mask is formed by taking
+the union of the current signal mask and the value of the sa_mask for the
+signal being delivered, and then including the signal being delivered. If
+and when the signal handler returns, the original signal mask is restored.
+<P>
+Signal catching functions execute in the same address environment as the
+task that called sigaction() to install the signal-catching function.
+<P>
+Once an action is installed for a specific signal, it remains installed
+until another action is explicitly requested by another call to
+sigaction().
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>sig</I>. Signal of interest
+<LI><I>act</I>. Location of new handler
+<LI><I>oact</I>. Location to store old handler
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if the signal number is invalid.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the POSIX implementation include:
+<UL>
+<LI>Special values of sa_handler in the struct sigaction act input
+not handled (SIG_DFL, SIG_IGN).
+<LI>All sa_flags in struct sigaction of act input are ignored
+(all treated like SA_SIGINFO).
+</UL>
+
+<H3>2.7.7 sigprocmask</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigprocmask(int how, const sigset_t *set, sigset_t *oset);
+</PRE>
+
+<P>
+<B>Description:</B> This function allows the calling task to
+examine and/or change its signal mask. If the set is not NULL,
+then it points to a set of signals to be used to change the currently
+blocked set. The value of how indicates the manner in which the
+set is changed.
+<P>
+If there are any pending unblocked signals after the call to sigprocmask(),
+those signals will be delivered before sigprocmask() returns.
+<P>
+If sigprocmask() fails, the signal mask of the task is not changed.
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>how</I>. How the signal mast will be changed:
+<UL>
+<LI><I>osSIG_BLOCK</I>. The resulting set is the union of the
+current set and the signal set pointed to by the <I>set</I> input parameter.
+<LI><I>osSIG_UNBLOCK</I>. The resulting set is the intersection
+of the current set and the complement of the signal set pointed
+to by the <I>set</I> input parameter.
+<LI><I>osSIG_SETMASK</I>. The resulting set is the signal set
+pointed to by the <I>set</I> input parameter.
+</UL>
+
+<LI><I>set</I>. Location of the new signal mask
+<LI><I>oset</I>. Location to store the old signal mask
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK), or -1 (ERROR) if how is invalid.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.7.8 sigpending</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigpending( sigset_t *set );
+</PRE>
+
+<P>
+<B>Description:</B> This function stores the returns the set of
+signals that are blocked for delivery and that are pending for
+the calling task in the space pointed to by set.
+<P>
+If the task receiving a signal has the signal blocked via its
+sigprocmask, the signal will pend until it is unmasked. Only one pending
+signal (for a given signo) is retained by the system. This is consistent
+with POSIX which states: &quot;If a subsequent occurrence of a pending
+signal is generated, it is implementation defined as to whether the signal
+is delivered more than once.&quot;
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>set</I>. The location to return the pending signal set.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>0 (OK) or -1 (ERROR)
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.7.9 sigsuspend</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigsuspend( const sigset_t *set );
+</PRE>
+
+<P>
+<B>Description:</B> The sigsuspend() function replaces the signal mask
+with the set of signals pointed to by the argument set and then suspends
+the task until delivery of a signal to the task.
+<P>
+If the effect of the set argument is to unblock a pending signal, then
+no wait is performed.
+<P>
+The original signal mask is restored when sigsuspend() returns.
+<P>
+Waiting for an empty signal set stops a task without freeing any
+resources (a very bad idea).
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>set</I>. The value of the signal <B>mask</B> to use while
+suspended.
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>-1 (ERROR) always
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the POSIX specification include:
+<UL>
+<LI>POSIX does not indicate that the original signal mask is restored.
+<LI>POSIX states that sigsuspend() &quot;suspends the task until
+delivery of a signal whose action is either to execute a signal-catching
+function or to terminate the task.&quot; Only delivery of the signal
+is required in the present implementation (even if the signal is ignored).
+</UL>
+
+<H3>2.7.10 sigwaitinfo</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigwaitinfo(const sigset_t *set, struct siginfo *info);
+</PRE>
+
+<P>
+<B>Description:</B> This function is equivalent to sigtimedwait()
+with a NULL timeout parameter. (see below).
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>set</I>. The set of pending signals to wait for.
+<LI><I>info</I>. The returned signal values
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>Signal number that cause the wait to be terminated, otherwise
+-1 (ERROR) is returned.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.7.11 sigtimedwait</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigtimedwait( const sigset_t *set, struct siginfo *info,
+ const struct timespec *timeout );
+</PRE>
+
+<P>
+<B>Description:</B> This function selects the pending signal set
+specified by the argument set. If multiple signals are pending in set,
+it will remove and return the lowest numbered one. If no signals in set
+are pending at the time of the call, the calling task will be suspended
+until one of the signals in set becomes pending OR until the task
+interrupted by an unblocked signal OR until the time interval specified by
+timeout (if any), has expired. If timeout is NULL, then the timeout interval
+is forever.
+<P>
+If the info argument is non-NULL, the selected signal number is
+stored in the si_signo member and the cause of the signal is store
+in the si_code member. The content of si_value is only meaningful
+if the signal was generated by sigqueue(). The following values
+for si_code are defined in signal.h:
+<UL>
+<LI>Standard:
+<UL>
+<LI><I>SI_QUEUE</I>. Signal sent from sigqueue
+<LI><I>SI_MESGQ</I>. Signal generated by arrival of a message
+on an empty message queue
+</UL>
+
+<LI>Unique to this implementation:
+<UL>
+<LI><I>SI_TIMEOUT</I>. No Signal, restarted by timeout
+</UL>
+
+</UL>
+
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>set</I>. The set of pending signals to wait for.
+<LI><I>info</I>. The returned signal values
+<LI><I>timeout</I>. The amount of time to wait
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>Signal number that cause the wait to be terminated, otherwise
+-1 (ERROR) is returned.
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the POSIX interface include:
+<UL>
+<LI>Values for si_codes differ
+<LI>No mechanism to return cause of ERROR. (It can be inferred
+from si_code in a non-standard way).
+<LI>POSIX states that &quot;If no signal is pending at the time of the
+call, the calling task shall be suspended until one or more signals
+in set become pending or until it is interrupted by an unblocked,
+<I>caught</I> signal.&quot; The present implementation does not require
+that the unblocked signal be caught; the task will be resumed even if
+the unblocked signal is ignored.
+</UL>
+
+<H3>2.7.12 sigqueue</H3>
+
+<P>
+<B>Function Prototype:</B>
+<PRE>
+ #include &lt;signal.h&gt;
+ int sigqueue (int tid, int signo, const union sigval value);
+</PRE>
+
+<P>
+<B>Description:</B> This function sends the signal specified by
+signo with the signal parameter value to the task specified
+by tid.
+<P>
+If the receiving task has the signal blocked via its sigprocmask,
+the signal will pend until it is unmasked. Only one pending signal
+(for a given signo) is retained by the system. This is consistent with
+POSIX which states: &quot;If a subsequent occurrence of a pending signal
+is generated, it is implementation defined as to whether the signal
+is delivered more than once.&quot;
+<P>
+<B>Input Parameters:</B>
+<UL>
+<LI><I>tid</I>. Process ID of task to receive signal
+<LI><I>signo</I>. Signal number
+<LI><I>value</I>. Value to pass to task with signal
+</UL>
+
+<P>
+<B>Returned Values:</B>
+<UL>
+<LI>OK or ERROR
+</UL>
+
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B> POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+Differences from the POSIX interface include:
+<UL>
+<LI>Default action is to ignore signals.
+<LI>Signals are processed one at a time in order
+<LI>Signals will (most likely) be processed on the caller's thread
+of execution.
+<LI>POSIX states that, &quot;If signo is zero (the null signal), error
+checking will be performed but no signal is actually sent.&quot;
+There is no null signal in the present implementation; a zero signal will
+be sent.
+</UL>
+
+<H2>2.8 <A NAME="Pthread">Pthread Interfaces</A></H2>
+<P>
+<H3>2.8.1 pthread_attr_init</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_init(pthread_attr_t *attr);
+</PRE>
+<P>
+<B>Description:</B>
+Initializes a thread attributes object (attr) with default values
+for all of the individual attributes used by the implementation.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+<P>
+<H3>2.8.2 pthread_attr_destroy</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_destroy(pthread_attr_t *attr);
+</PRE>
+<P>
+<B>Description:</B>
+An attributes object can be deleted when it is no longer needed.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+<P>
+<H3>2.8.3 pthread_attr_setschedpolicy</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.4 pthread_attr_getschedpolicy</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.5 pthread_attr_setschedparam</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_setschedparam(pthread_attr_t *attr,
+ const struct sched_param *param);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.6 pthread_attr_getschedparam</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_getschedparam(pthread_attr_t *attr,
+ struct sched_param *param);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.7 pthread_attr_setinheritsched</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_setinheritsched(pthread_attr_t *attr,
+ int inheritsched);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+<P>
+<H3>2.8.8 pthread_attr_getinheritsched</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_getinheritsched(const pthread_attr_t *attr,
+ int *inheritsched);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.9 pthread_attr_setstacksize</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_setstacksize(pthread_attr_t *attr, long stacksize);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.10 pthread_attr_getstacksize</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_attr_getstacksize(pthread_attr_t *attr, long *stackaddr);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.11 pthread_create</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_create(pthread_t *thread, pthread_attr_t *attr,
+ pthread_startroutine_t startRoutine,
+ pthread_addr_t arg);
+</PRE>
+<P>
+<B>Description:</B>
+To create a thread object and runnable thread, a routine
+must be specified as the new thread's start routine. An
+argument may be passed to this routine, as an untyped
+address; an untyped address may also be returned as the
+routine's value. An attributes object may be used to
+specify details about the kind of thread being created.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.12 pthread_detach</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_detach(pthread_t thread);
+</PRE>
+<P>
+<B>Description:</B>
+A thread object may be "detached" to specify that the
+return value and completion status will not be requested.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.13 pthread_exit</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ void pthread_exit(pthread_addr_t pvValue);
+</PRE>
+<P>
+<B>Description:</B>
+A thread may terminate it's own execution.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.14 pthread_cancel</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_cancel(pthread_t thread);
+</PRE>
+<P>
+<B>Description:</B>
+
+<p>The pthread_cancel() function shall request that thread
+be canceled. The target thread's cancelability state determines
+when the cancellation takes effect. When the
+cancellation is acted on, thread shall be terminated.</p>
+
+<p>When cancelability is disabled, all cancels are held pending
+in the target thread until the thread changes the cancelability.
+When cancelability is deferred, all cancels are held pending in
+the target thread until the thread changes the cancelability or
+calls pthread_testcancel().</p>
+
+<p>Cancelability is asynchronous; all cancels are acted upon
+immediately (when enable), interrupting the thread with its processing.</p>
+
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>thread</I>.
+Identifies the thread to be canceled.</li>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>ptnread_cancel()</I> function will return zero (<I>OK</I>).
+Otherwise, an error number will be returned to indicate the error:
+<P>
+<UL>
+<LI><I>ESRCH</I>.
+No thread could be found corresponding to that specified by the given thread ID.</li>
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name. Except:</p>
+<UL>
+<LI>The thread-specific data destructor functions shall be called for thread.
+However, these destructors are not currently supported.</li>
+<li>Cancellation types are not supported. The thread will be canceled
+at the time that pthread_cancel() is called or, if cancelation is disabled, at
+the time when cancelation is re-enabled.</li>
+<li><tt>pthread_testcancel()</tt> is not supported.</li>
+<li>Thread cancellation at <i>cancellation points</i> is not supported.</li>
+</UL>
+
+<H3>2.8.15 pthread_setcancelstate</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_setcancelstate(int state, int *oldstate);
+</PRE>
+<P>
+<B>Description:</B>
+<P>The <i>pthread_setcancelstate()</i> function atomically
+sets both the calling thread's cancelability state to the indicated
+state and returns the previous cancelability state at the location
+referenced by oldstate.
+Legal values for state are PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_DISABLE.<.li>
+
+<p>Any pending thread cancelation may occur at the time that the
+cancelation state is set to PTHREAD_CANCEL_ENABLE.</p>
+
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>state</I>
+New cancelation state. One of PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE.<.li>
+<LI><I>oldstate</I>.
+Location to return the previous cancelation state.
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>pthread_setcancelstate()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be returned to indicate the error:
+<P>
+<UL>
+<LI><I>ESRCH</I>.
+No thread could be found corresponding to that specified by the given thread ID.</li>
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.16 pthread_setcancelstate</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_setcancelstate(int state, int *oldstate);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.17 pthread_join</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_join(pthread_t thread, pthread_addr_t *ppvValue);
+</PRE>
+<P>
+<B>Description:</B>
+A thread can await termination of another thread and retrieve
+the return value of the thread.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.18 pthread_yield</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ void pthread_yield(void);
+</PRE>
+<P>
+<B>Description:</B>
+A thread may tell the scheduler that its processor can be
+made available.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.19 pthread_self</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ pthread_t pthread_self(void);
+</PRE>
+<P>
+<B>Description:</B>
+A thread may obtain a copy of its own thread handle.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.20 pthread_getschedparam</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_getschedparam(pthread_t thread, int *policy,
+ struct sched_param *param);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.21 pthread_setschedparam</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_setschedparam(pthread_t thread, int policy,
+ const struct sched_param *param);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.22 pthread_key_create</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_key_create( pthread_key_t *key, void (*destructor)(void*) )
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+This function creates a thread-specific data key visible
+to all threads in the system. Although the same key value
+may be used by different threads, the values bound to
+the key by <I>pthread_setspecific()</I> are maintained on a
+per-thread basis and persist for the life of the calling
+thread.
+<P>
+Upon key creation, the value <I>NULL</I> will be associated with
+the the new key in all active threads. Upon thread
+creation, the value <I>NULL</I> will be associated with all
+defined keys in the new thread.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>key</I> is a pointer to the key to create.
+<LI><I>destructor</I> is an optional destructor() function that may
+be associated with each key that is invoked when a
+thread exits. However, this argument is ignored in
+the current implementation.
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>pthread_key_create()</I> function will
+store the newly created key value at *<I>key</I> and return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>EAGAIN</I>. The system lacked sufficient resources
+to create another thread-specific data key, or the
+system-imposed limit on the total number of keys
+per task {<I>PTHREAD_KEYS_MAX</I>} has been exceeded
+<LI><I>ENONMEM</I> Insufficient memory exists to create the key.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+<UL>
+<LI>The present implementation ignores the destructor argument.
+</UL>
+
+<H3>2.8.23 pthread_setspecific</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_setspecific( pthread_key_t key, void *value )
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+The <I>pthread_setspecific()</I> function associates a thread-
+specific value with a key obtained via a previous call
+to <I>pthread_key_create()</I>. Different threads may bind
+different values to the same key. These values are
+typically pointers to blocks of dynamically allocated
+memory that have been reserved for use by the calling
+thread.
+<P>
+The effect of calling <I>pthread_setspecific()</I> with a key value
+not obtained from <I>pthread_key_create()</I> or after a key has been
+deleted with <I>pthread_key_delete()</I> is undefined.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>key</I>. The data key to set the binding for.
+<LI><I>value</I>. The value to bind to the key.
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, <I>pthread_setspecific()</I> will return zero (<I>OK</I>).
+Otherwise, an error number will be returned:
+<P>
+<UL>
+<LI><I>ENOMEM</I>. Insufficient memory exists to associate the value
+with the key.
+<LI><I>EINVAL</I>. The key value is invalid.
+</UL>
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+<UL>
+<LI>pthread_setspecific() may be called from a thread-specific data
+destructor function.
+</UL>
+
+<H3>2.8.24 pthread_getspecific</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ void *pthread_getspecific( pthread_key_t key )
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+The <I>pthread_getspecific()</I> function returns the value
+currently bound to the specified key on behalf of the
+calling thread.
+<P>
+The effect of calling <I>pthread_getspecific()</I> with a key value
+not obtained from <I>pthread_key_create()</I> or after a key has been
+deleted with <I>pthread_key_delete()</I> is undefined.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>key</I>. The data key to get the binding for.
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+The function <I>pthread_getspecific()</I> returns the thread-
+specific data associated with the given key. If no
+thread specific data is associated with the key, then
+the value <I>NULL</I> is returned.
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+<UL>
+<LI>pthread_getspecific() may be called from a thread-specific data
+destructor function.
+</UL>
+
+<H3>2.8.25 pthread_key_delete</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_key_delete( pthread_key_t key )
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+This POSIX function should delete a thread-specific data
+key previously returned by <I>pthread_key_create()</I>. However,
+this function does nothing in the present implementation.
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>key</I>. The key to delete
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+<UL>
+<LI>Always returns <I>EINVAL</I>.
+</UL>
+<P>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.26 pthread_mutexattr_init</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_mutexattr_init(pthread_mutexattr_t *attr);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.27 pthread_mutexattr_destroy</A></H3>
+<P>
+<B>Function Protoype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.28 pthread_mutexattr_getpshared</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr,
+ int *pshared);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.29 pthread_mutexattr_setpshared</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr,
+ int pshared);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.30 pthread_mutex_init</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_mutex_init(pthread_mutex_t *mutex,
+ pthread_mutexattr_t *attr);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.31 pthread_mutex_destroy</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_mutex_destroy(pthread_mutex_t *mutex);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.32 pthread_mutex_lock</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_mutex_lock(pthread_mutex_t *mutex);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.33 pthread_mutex_trylock</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_mutex_trylock(pthread_mutex_t *mutex);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.34 pthread_mutex_unlock</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_mutex_unlock(pthread_mutex_t *mutex);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.35 pthread_condattr_init</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_condattr_init(pthread_condattr_t *attr);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.36 pthread_condattr_destroy</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_condattr_destroy(pthread_condattr_t *attr);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.37 pthread_cond_init</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.38 pthread_cond_destroy</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_cond_destroy(pthread_cond_t *cond);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.39 pthread_cond_broadcast</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_cond_broadcast(pthread_cond_t *cond);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.40 pthread_cond_signal</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_cond_signal(pthread_cond_t *dond);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.41 pthread_cond_wait</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<H3>2.8.42 pthread_cond_timedwait</A></H3>
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE>
+ #include &lt;pthread.h&gt;
+ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
+ const struct timespec *abstime);
+</PRE>
+<P>
+<B>Description:</B>
+<P>
+<B>Input Parameters:</B>
+<P>
+<UL>
+<LI><I>parm</I>
+</UL>
+<P>
+<B>Returned Values:</B>
+<P>
+If successful, the <I>xxx()</I> function will return
+zero (<I>OK</I>). Otherwise, an error number will be
+returned to indicate the error:
+<P>
+<UL>
+<LI><I>Exxx</I>.
+</UL>
+<B>Assumptions/Limitations:</B>
+<P>
+<B>POSIX Compatibility:</B> Comparable to the POSIX
+interface of the same name.
+
+<P>
+<HR>
+<H1>3.0 <A NAME="Data_Structures">OS Data Structures</A></H1>
+<H2>3.1 Scalar types</H2>
+<P>
+Many of the types used to communicate with Nuttx are simple
+scalar types. These types are used to provide architecture independence
+of the OS from the application. The scalar types used at the Nuttx
+interface include:
+<UL>
+<LI>pid_t
+<LI>size_t
+<LI>sigset_t
+<LI>STATUS
+<LI>time_t
+</UL>
+
+<H2>3.2 Hidden Interface Structures</H2>
+<P>
+Several of the types used to interface with Nuttx are
+structures that are intended to be hidden from the application.
+From the standpoint of the application, these structures (and
+structure pointers) should be treated as simple handles to reference
+OS resources. These hidden structures include:
+<UL>
+<LI>_TCB
+<LI>mqd_t
+<LI>sem_t
+<LI>WDOG_ID
+<LI>pthread_key_t
+</UL>
+<P>
+In order to maintain portability, applications should not reference
+specific elements within these hidden structures. These hidden
+structures will not be described further in this user's manual.
+<P>
+
+<H2>3.3. Access to the <I>errno</I> Variable</H2>
+<P>
+A pointer to the thread-specific <I>errno</I>. value is available through a
+function call:
+<P>
+<B>Function Prototype:</B>
+<P>
+<PRE> int *get_errno_ptr( void )</PRE>
+<P>
+<B>Description</B>: <I>osGetErrnorPtr()</I> returns a pointer to
+the thread-specific <I>errno</I> value.
+<P>
+This differs somewhat from the use for errno in a multi-threaded process environment:
+Each pthread will have its own private copy of errno and the errno will not be shared
+between pthreads.
+<P>
+<B>Input Parameters</B>: None
+<P>
+<B>Returned Values</B>:
+<P>
+<UL>
+<LI>A pointer to the thread-specific <I>errno</I> value.
+</UL>
+<P>
+
+<H2>3.4 User Interface Structures</H2>
+<P>
+<H3>3.4.1 main_t</H3>
+<P>
+main_t defines the type of a task entry point. main_t is declared
+in sys/types.h as:
+<PRE>
+ typedef int (*main_t)(int argc, char *argv[]);
+</PRE>
+
+<H3>3.4.2 struct sched_param</H3>
+
+<P>
+This structure is used to pass scheduling priorities to and from
+Nuttx;
+<PRE>
+ struct sched_param
+ {
+ int sched_priority;
+ };
+</PRE>
+
+<H3>3.4.3 struct timespec</H3>
+
+<P>
+This structure is used to pass timing information between the
+Nuttx and a user application:
+<PRE>
+ struct timespec
+ {
+ time_t tv_sec; /* Seconds */
+ long tv_nsec; /* Nanoseconds */
+ };
+</PRE>
+
+<H3>3.4.4 struct mq_attr</H3>
+
+<P>
+This structure is used to communicate message queue attributes
+between Nuttx and a MoBY application:
+<PRE>
+ struct mq_attr {
+ size_t mq_maxmsg; /* Max number of messages in queue */
+ size_t mq_msgsize; /* Max message size */
+ unsigned mq_flags; /* Queue flags */
+ size_t mq_curmsgs; /* Number of messages currently in queue */
+ };
+</PRE>
+
+<H3>3.4.5 struct sigaction</H3>
+
+<P>
+The following structure defines the action to take for given signal:
+<PRE>
+ struct sigaction {
+ union {
+ saHandType *_sa_handler;
+ saVxHandType *_sa_sigaction;
+ } sa_u;
+ sigset_t sa_mask;
+ int sa_flags;
+ };
+ #define sa_handler sa_u._sa_handler
+ #define sa_sigaction sa_u._sa_sigaction
+</PRE>
+
+<P>
+where:
+<PRE>
+ typedef void saHandType( int signo );
+ typedef void saVxHandType( int signo, siginfo_t *info, void *context );
+</PRE>
+
+<H3>3.4.6 struct siginfo/siginfo_t</H3>
+
+<P>
+The following types is used to pass parameters to/from signal
+handlers:
+<PRE>
+ typedef struct siginfo
+ {
+ int si_signo;
+ int si_code;
+ union sigval si_value;
+ } siginfo_t;
+</PRE>
+
+<H3>3.4.7 union sigval</H3>
+
+<P>
+This defines the type of the struct siginfo si_value field and
+is used to pass parameters with signals.
+<PRE>
+ union sigval
+ {
+ int sival_int;
+ void *sival_ptr;
+ };
+</PRE>
+
+<H3>3.4.8 struct sigevent</H3>
+
+<P>
+The following is used to attach a signal to a message queue to
+notify a task when a message is available on a queue.
+<PRE>
+ struct sigevent
+ {
+ int sigev_signo;
+ union sigval sigev_value;
+ int sigev_notify;
+ };
+</PRE>
+<HR>
+
+<H1>4.0 <A NAME="Problems">Known Problems</A></H1>
+
+<P>
+This section documents know problems with Nuttx at the time
+of this writing.
+<P>
+<HR>
+<B>Problem</B>:
+There is a problem with the unblock logic in os_signal.c when message queue
+becomes not-empty -- sig_mqnotempty() calls sig_received().
+sig_received() relies on task_state == TSTATE_WAIT_SIG and will ignore
+tasks that are waiting on a message queue to become non-empty.
+<P>
+<B>Priority</B>: <B>LOW</B>. If a task is blocked a waiting for a message
+queue to become non-empty, it will be re-started anyway.
+<HR>
+
+<B>Problem</B>: task_restart won't restart a running task
+<P>
+<B>Priority</B>: <B>LOW</B>.
+
+</BODY>
+</HTML>
diff --git a/nuttx/Makefile b/nuttx/Makefile
new file mode 100644
index 0000000000..34bb7ab895
--- /dev/null
+++ b/nuttx/Makefile
@@ -0,0 +1,139 @@
+############################################################
+# Makefile
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+TOPDIR = ${shell pwd}
+-include ${TOPDIR}/.config
+-include ${TOPDIR}/Make.defs
+
+ARCH_DIR = arch/$(CONFIG_ARCH)
+ARCH_SRC = $(ARCH_DIR)/src
+
+SUBDIRS = sched lib $(ARCH_SRC) mm fs drivers examples/$(CONFIG_EXAMPLE)
+
+OBJS = $(ARCH_SRC)/up_head.o
+LIBGCC = ${shell $(CC) -print-libgcc-file-name}
+LIBS = sched/libsched.a $(ARCH_SRC)/libarch.a mm/libmm.a \
+ fs/libfs.a drivers/libdrivers.a lib/liblib.a \
+ examples/$(CONFIG_EXAMPLE)/lib$(CONFIG_EXAMPLE).a
+LDLIBS = -lsched -larch -lmm -lfs -ldrivers -llib -l$(CONFIG_EXAMPLE) $(LIBGCC) $(EXTRA_LIBS)
+
+BIN = nuttx
+
+LDFLAGS += -Lsched -Llib -L$(ARCH_SRC) -Lmm -Lfs -Ldrivers -Lexamples/$(CONFIG_EXAMPLE)
+
+all: $(BIN)
+.PHONY: clean context clean_context distclean
+
+tools/mkconfig:
+ $(MAKE) -C tools -f Makefile.mkconfig TOPDIR=$(TOPDIR) mkconfig
+
+include/nuttx/config.h: $(ARCH_DIR)/defconfig tools/mkconfig
+ tools/mkconfig $(ARCH_DIR) > include/nuttx/config.h
+
+include/arch: include/nuttx/config.h
+ ln -sf $(TOPDIR)/$(ARCH_DIR)/include include/arch
+
+context: check_context include/nuttx/config.h include/arch
+
+clean_context:
+ rm -f include/nuttx/config.h
+ rm -f include/arch
+
+check_context:
+ @if [ ! -e ${TOPDIR}/.config -o ! -e ${TOPDIR}/Make.defs ]; then \
+ echo "" ; echo "Nuttx has not been configured:" ; \
+ echo " cd tools; ./configure.sh <target>\n" ; echo "" ;\
+ exit 1 ; \
+ fi
+
+sched/libsched.a: context
+ $(MAKE) -C sched TOPDIR=$(TOPDIR) libsched.a
+
+lib/liblib.a: context
+ $(MAKE) -C lib TOPDIR=$(TOPDIR) liblib.a
+
+$(ARCH_SRC)/libarch.a: context
+ $(MAKE) -C $(ARCH_SRC) TOPDIR=$(TOPDIR) libarch.a
+
+$(ARCH_SRC)/up_head.o: context
+ $(MAKE) -C $(ARCH_SRC) TOPDIR=$(TOPDIR) up_head.o
+
+mm/libmm.a: context
+ $(MAKE) -C mm TOPDIR=$(TOPDIR) libmm.a
+
+fs/libfs.a: context
+ $(MAKE) -C fs TOPDIR=$(TOPDIR) libfs.a
+
+drivers/libdrivers.a: context
+ $(MAKE) -C drivers TOPDIR=$(TOPDIR) libdrivers.a
+
+examples/$(CONFIG_EXAMPLE)/lib$(CONFIG_EXAMPLE).a: context
+ $(MAKE) -C examples/$(CONFIG_EXAMPLE) TOPDIR=$(TOPDIR) lib$(CONFIG_EXAMPLE).a
+
+$(BIN): context depend $(OBJS) $(LIBS)
+ifeq ($(CONFIG_ARCH),sim)
+ $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(LDLIBS)
+ $(NM) $(BIN) | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' | sort > System.map
+else
+ $(LD) --entry=__start $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(LDLIBS)
+ $(NM) $(BIN) | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' | sort > System.map
+ @export vflashstart=`$(OBJDUMP) --all-headers $(BIN) | grep _vflashstart | cut -d' ' -f1`; \
+ if [ ! -z "$$vflashstart" ]; then \
+ $(OBJCOPY) --adjust-section-vma=.vector=0x$$vflashstart $(BIN) $(BIN).flashimage; \
+ mv $(BIN).flashimage $(BIN); \
+ fi
+endif
+
+depend:
+ @for dir in $(SUBDIRS) ; do \
+ $(MAKE) -C $$dir TOPDIR=$(TOPDIR) depend ; \
+ done
+
+clean:
+ @for dir in $(SUBDIRS) ; do \
+ $(MAKE) -C $$dir TOPDIR=$(TOPDIR) clean ; \
+ done
+ $(MAKE) -C tools -f Makefile.mkconfig TOPDIR=$(TOPDIR) clean
+ $(MAKE) -C mm -f Makefile.test TOPDIR=$(TOPDIR) clean
+ rm -f $(BIN) context mm_test System.map *~ *.flashimage
+
+distclean: clean clean_context
+ @for dir in $(SUBDIRS) ; do \
+ $(MAKE) -C $$dir TOPDIR=$(TOPDIR) distclean ; \
+ done
+ $(MAKE) -C examples/$(CONFIG_EXAMPLE) TOPDIR=$(TOPDIR) distclean
+ rm -f Make.defs setenv.sh .config
+
+
diff --git a/nuttx/arch/README.txt b/nuttx/arch/README.txt
new file mode 100644
index 0000000000..85175c6a07
--- /dev/null
+++ b/nuttx/arch/README.txt
@@ -0,0 +1,189 @@
+Architecture-Specific Code
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+The file include/nuttx/arch.h identifies all of the APIs that must
+be provided by the architecture specific logic. (It also includes
+arch/<arch-name>/arch.h as described below).
+
+Directory Structure
+^^^^^^^^^^^^^^^^^^^
+
+Thie arch directory contains architecture specific logic. Each aructure
+provide a a subdirectory <arch-name>under arch/ with the folling characteristics:
+
+
+ <arch-name>
+ |-- Make.defs
+ |-- defconfig
+ |-- setenv.sh
+ |-- include
+ | |-- arch.h
+ | |-- irq.h
+ | `-- types.h
+ `-- src
+ |-- Makefile
+ `-- (architecture-specific source files)
+
+Summary of Files
+^^^^^^^^^^^^^^^^
+
+Make.defs -- This makefile fragment provides architecture and
+ tool-specific build options. It will be included by all other
+ makefiles in the build (once it is installed). This make fragment
+ should define:
+
+ Tools: CC, LD, AR, NM, OBJCOPY, OBJDUMP
+ Tool options: CFLAGS, LDFLAGS
+
+ When this makefile fragment runs, it will be passed TOPDIR which
+ is the path to the root directory of the build. This makefile
+ fragment may include ${TOPDIR}/.config to perform configuration
+ specific settings. For example, the CFLAGS will most likely be
+ different if CONFIG_DEBUG=y.
+
+defconfig -- This is a configuration file similar to the Linux
+ configuration file. In contains varialble/value pairs like:
+
+ CONFIG_VARIABLE=value
+
+ This configuration file will be used at build time:
+
+ (1) as a makefile fragment included in other makefiles, and
+ (2) to generate include/nuttx/config.h which is included by
+ most C files in the system.
+
+ The following variables are recognized by the build (you may
+ also include architecture-specific settings).
+
+ Architecture selection:
+
+ CONFIG_ARCH - identifies the arch subdirectory
+ CONFIG_ARCH_name - for use in C code
+
+ General OS setup
+
+ CONFIG_EXAMPLE - identifies the subdirectgory in examples
+ that will be used in the build
+ CONFIG_DEBUG - enables built-in debug options
+ CONFIG_DEBUG_VERBOSE - enables verbose debug output
+ CONFIG_HAVE_LOWPUTC - architecture supports low-level, boot
+ time console output
+ CONFIG_RR_INTERVAL - The round robin timeslice will be set
+ this number of milliseconds; Round robin scheduling can
+ be disabled by setting this value to zero.
+ CONFIG_SCHED_INSTRUMENTATION - enables instrumentation in
+ scheduler to monitor system performance
+ CONFIG_TASK_NAME_SIZE - Spcifies that maximum size of a
+ task name to save in the TCB. Useful if scheduler
+ instrumentation is selected. Set to zero to disable.
+ CONFIG_START_YEAR, CONFIG_START_MONTH, CONFIG_START_DAY -
+ Used to initialize the internal time logic.
+ CONFIG_JULIAN_TIME - Enables Julian time conversions
+ CONFIG_DEV_CONSOLE - Set if architecture-specific logic
+ provides /dev/console. Enables stdout, stderr, stdin.
+
+ Allow for artchitecture optimized implementations
+
+ The architecture can provide optimized versions of the
+ following to improve sysem performance
+
+ CONFIG_ARCH_MEMCPY, CONFIG_ARCH_MEMCMP, CONFIG_ARCH_MEMMOVE
+ CONFIG_ARCH_MEMSET, CONFIG_ARCH_STRCMP, CONFIG_ARCH_STRCPY
+ CONFIG_ARCH_STRNCPY, CONFIG_ARCH_STRLEN, CONFIG_ARCH_BZERO
+ CONFIG_ARCH_KMALLOC, CONFIG_ARCH_KZMALLOC, CONFIG_ARCH_KFREE
+
+ General Compile environment setup
+
+ CONFIG_HAVE_LONG_LONG - enable if your architecture supports
+ long long types and if you plan to use them
+
+ Sizes of configurable things (0 disables)
+
+ CONFIG_NPTHREAD_KEYS - The number of items of thread-
+ specific data that can be retained
+ CONFIG_NFILE_DESCRIPTORS - The maximum number of file
+ descriptors (one for each open)
+ CONFIG_NFILE_STREAMS - The maximum number of streams that
+ can be fopen'ed
+ CONFIG_STDIO_BUFFER_SIZE - Size of the buffer to allocate
+ on fopen. (Only if CONFIG_NFILE_STREAMS > 0)
+ CONFIG_NUNGET_CHARS - Number of characters that can be
+ buffered by ungetc() (Only if CONFIG_NFILE_STREAMS > 0)
+ CONFIG_PREALLOC_MQ_MSGS - The number of pre-allocated message
+ structures. The system manages a pool of preallocated
+ message structures to minimize dynamic allocations
+ CONFIG_MQ_MAXMSGSIZE - Message structures are allocated with
+ a fixed payload size given by this settin (does not include
+ other message structure overhead.
+ CONFIG_PREALLOC_WDOGS - The number of pre-allocated watchdog
+ structures. The system manages a pool of preallocated
+ watchdog structures to minimize dynamic allocations
+
+ Stack and heap information
+
+ CONFIG_BOOT_FROM_FLASH - Some configurations support XIP
+ operation from FLASH.
+ CONFIG_STACK_POINTER - The initial stack pointer
+ CONFIG_PROC_STACK_SIZE - The size of the initial stack
+ CONFIG_PTHREAD_STACK_MIN - Minimum pthread stack size
+ CONFIG_PTHREAD_STACK_DEFAULT - Default pthread stack size
+ CONFIG_HEAP_BASE - The beginning of the heap
+ CONFIG_HEAP_SIZE - The size of the heap
+
+setenv.sh -- This is a script that you can include that will be installed at
+ the toplevel of the directory structure and can be sourced to set any
+ necessary environment variables.
+
+include/arch.h
+ This is a hook for any architecture specific definitions that may
+ be needed by the system. It is included by include/nuttx/arch.h
+
+include/types.h
+ This provides architecture/toolchain-specific definitions for
+ standard types. This file should typedef:
+
+ sbyte, ubyte, uint8, boolean, sint16, uint16, sint32, uint32, sint64, uint64
+
+ This file will be included by include/sys/types.h and be made
+ available to all files.
+
+include/irq.h
+ This file needs to define some architecture specific functions (usually
+ inline) and structure. These include:
+
+ - struct xcptcontext. This structures represents the saved context
+ of a thread.
+
+ - static inline uint32 irqsave(void) -- Used to disable
+ all interrupts.
+
+ - static inline void irqrestore(uint32 flags) -- Used to
+ restore interrupts enables to the same state as before irqsave
+ was called.
+
+ This file must also define NR_IRQS, the total number of IRQs supported
+ by the board.
+
+src/Makefile
+ This makefile will be executed to build the targets src/libup.a and
+ src/up_head.o. The up_head.o file holds the entry point into the system
+ (power-on reset entry point, for example). It will be used in
+ the final link with libup.a and other system archives to generate the
+ final executable.
+
+Configuring NuttX
+^^^^^^^^^^^^^^^^^
+
+Configuring NuttX requires only copying
+
+ arch/<arch-name>/Make.def to ${TOPDIR}/Make.defs
+ arch/<arch-name>/setenv.sh to ${TOPDIR}/setenv.sh
+ arch/<arch-name>/defconfig to ${TOPDIR}/.config
+
+There is a script that automates these steps. The following steps will
+accomplish the same configuration:
+
+ cd tools
+ ./configure.sh <arch-name>
+
+
diff --git a/nuttx/arch/c5471/Make.defs b/nuttx/arch/c5471/Make.defs
new file mode 100644
index 0000000000..7e68d8e566
--- /dev/null
+++ b/nuttx/arch/c5471/Make.defs
@@ -0,0 +1,70 @@
+############################################################
+# Make.defs
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+include ${TOPDIR}/.config
+
+ifeq ("${CONFIG_DEBUG}","y")
+ ARCHOPTIMIZATION = -g
+else
+ ARCHOPTIMIZATION = -Os -fno-strict-aliasing -fno-strength-reduce \
+ -fomit-frame-pointer
+endif
+
+ARCHCPUFLAGS = -mapcs-32 -mcpu=arm7tdmi -msoft-float
+ARCHPICFLAGS = -fpic
+ARCHWARNINGS = -Wall -Wstrict-prototypes -Wshadow
+ARCHDEFINES =
+ARCHINCLUDES = -I. -isystem $(TOPDIR)/include
+ARCHSCRIPT = -T$(TOPDIR)/arch/$(CONFIG_ARCH)/ld.script
+
+CROSSDEV = arm-elf-
+CC = $(CROSSDEV)gcc
+LD = $(CROSSDEV)ld
+AR = $(CROSSDEV)ar
+NM = $(CROSSDEV)nm
+OBJCOPY = $(CROSSDEV)objcopy
+OBJDUMP = $(CROSSDEV)objdump
+
+CFLAGS = $(ARCHWARNINGS) $(ARCHOPTIMIZATION) \
+ $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) -pipe
+
+LDFLAGS = $(ARCHSCRIPT)
+EXTRA_LIBS =
+
+ifeq ("${CONFIG_DEBUG}","y")
+ LDFLAGS += -g
+endif
+
+
diff --git a/nuttx/arch/c5471/defconfig b/nuttx/arch/c5471/defconfig
new file mode 100644
index 0000000000..c48d0b8379
--- /dev/null
+++ b/nuttx/arch/c5471/defconfig
@@ -0,0 +1,157 @@
+############################################################
+# defconfig
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+#
+# architecture selection
+#
+# CONFIG_ARCH - identifies the arch subdirectory
+# CONFIG_ARCH_name - for use in C code
+# CONFIG_ROM_VECTORS - unique to arm7tdmi
+#
+CONFIG_ARCH=c5471
+CONFIG_ARCH_C5471=y
+CONFIG_ROM_VECTORS=n
+
+#
+# General OS setup
+#
+# CONFIG_EXAMPLE - identifies the subdirectgory in examples
+# that will be used in the build
+# CONFIG_DEBUG - enables built-in debug options
+# CONFIG_DEBUG_VERBOSE - enables verbose debug output
+# CONFIG_HAVE_LOWPUTC - architecture supports low-level, boot
+# time console output
+# CONFIG_RR_INTERVAL - The round robin timeslice will be set
+# this number of milliseconds; Round robin scheduling can
+# be disabled by setting this value to zero.
+# CONFIG_SCHED_INSTRUMENTATION - enables instrumentation in
+# scheduler to monitor system performance
+# CONFIG_TASK_NAME_SIZE - Spcifies that maximum size of a
+# task name to save in the TCB. Useful if scheduler
+# instrumentation is selected. Set to zero to disable.
+# CONFIG_START_YEAR, CONFIG_START_MONTH, CONFIG_START_DAY -
+# Used to initialize the internal time logic.
+# CONFIG_JULIAN_TIME - Enables Julian time conversions
+# CONFIG_DEV_CONSOLE - Set if architecture-specific logic
+# provides /dev/console. Enables stdout, stderr, stdin.
+#
+CONFIG_EXAMPLE=ostest
+CONFIG_DEBUG=y
+CONFIG_DEBUG_VERBOSE=n
+CONFIG_ARCH_LOWPUTC=n
+CONFIG_RR_INTERVAL=200
+CONFIG_SCHED_INSTRUMENTATION=n
+CONFIG_TASK_NAME_SIZE=0
+CONFIG_START_YEAR=2007
+CONFIG_START_MONTH=2
+CONFIG_START_DAY=13
+CONFIG_JULIAN_TIME=n
+CONFIG_DEV_CONSOLE=n
+
+#
+# Allow for artchitecture optimized implementations
+#
+# The architecture can provide optimized versions of the
+# following to improve sysem performance
+#
+CONFIG_ARCH_MEMCPY=n
+CONFIG_ARCH_MEMCMP=n
+CONFIG_ARCH_MEMMOVE=n
+CONFIG_ARCH_MEMSET=n
+CONFIG_ARCH_STRCMP=n
+CONFIG_ARCH_STRCPY=n
+CONFIG_ARCH_STRNCPY=n
+CONFIG_ARCH_STRLEN=n
+CONFIG_ARCH_BZERO=n
+CONFIG_ARCH_KMALLOC=n
+CONFIG_ARCH_KZMALLOC=n
+CONFIG_ARCH_KFREE=n
+
+# General Compile environment setup
+#
+# CONFIG_HAVE_LONG_LONG - enable if your architecture supports
+# long long types and if you plan to use them
+CONFIG_HAVE_LONG_LONG=n
+
+#
+# Sizes of configurable things (0 disables)
+#
+# CONFIG_NPTHREAD_KEYS - The number of items of thread-
+# specific data that can be retained
+# CONFIG_NFILE_DESCRIPTORS - The maximum number of file
+# descriptors (one for each open)
+# CONFIG_NFILE_STREAMS - The maximum number of streams that
+# can be fopen'ed
+# CONFIG_STDIO_BUFFER_SIZE - Size of the buffer to allocate
+# on fopen. (Only if CONFIG_NFILE_STREAMS > 0)
+# CONFIG_NUNGET_CHARS - Number of characters that can be
+# buffered by ungetc() (Only if CONFIG_NFILE_STREAMS > 0)
+# CONFIG_PREALLOC_MQ_MSGS - The number of pre-allocated message
+# structures. The system manages a pool of preallocated
+# message structures to minimize dynamic allocations
+# CONFIG_MQ_MAXMSGSIZE - Message structures are allocated with
+# a fixed payload size given by this settin (does not include
+# other message structure overhead.
+# CONFIG_PREALLOC_WDOGS - The number of pre-allocated watchdog
+# structures. The system manages a pool of preallocated
+# watchdog structures to minimize dynamic allocations
+#
+CONFIG_NPTHREAD_KEYS=4
+CONFIG_NFILE_DESCRIPTORS=32
+CONFIG_NFILE_STREAMS=16
+CONFIG_STDIO_BUFFER_SIZE=1024
+CONFIG_NUNGET_CHARS=2
+CONFIG_PREALLOC_MQ_MSGS=32
+CONFIG_MQ_MAXMSGSIZE=32
+CONFIG_PREALLOC_WDOGS=32
+
+#
+# Stack and heap information
+#
+# CONFIG_BOOT_FROM_FLASH - Some configurations support XIP
+# operation from FLASH.
+# CONFIG_STACK_POINTER - The initial stack pointer (arm7tdmi only)
+# CONFIG_PROC_STACK_SIZE - The size of the initial stack
+# CONFIG_PTHREAD_STACK_MIN - Minimum pthread stack size
+# CONFIG_PTHREAD_STACK_DEFAULT - Default pthread stack size
+# CONFIG_HEAP_BASE - The beginning of the heap
+# CONFIG_HEAP_SIZE - The size of the heap
+#
+CONFIG_BOOT_FROM_FLASH=n
+CONFIG_STACK_POINTER=0x02100000
+CONFIG_PROC_STACK_SIZE=0x00001000
+CONFIG_PTHREAD_STACK_MIN=256
+CONFIG_PTHREAD_STACK_DEFAULT=4096
+CONFIG_HEAP_BASE=0x02100000
+CONFIG_HEAP_SIZE=0x00100000
diff --git a/nuttx/arch/c5471/include/arch.h b/nuttx/arch/c5471/include/arch.h
new file mode 100644
index 0000000000..86d6652b62
--- /dev/null
+++ b/nuttx/arch/c5471/include/arch.h
@@ -0,0 +1,80 @@
+/************************************************************
+ * arch.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/* This file should never be included directed but, rather,
+ * only indirectly through nuttx/arch.h
+ */
+
+#ifndef __ARCH_C5471_ARCH_H
+#define __ARCH_C5471_ARCH_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Inline functions
+ ************************************************************/
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ARCH_C5471_ARCH_H */
+
diff --git a/nuttx/arch/c5471/include/irq.h b/nuttx/arch/c5471/include/irq.h
new file mode 100644
index 0000000000..c3bb57c293
--- /dev/null
+++ b/nuttx/arch/c5471/include/irq.h
@@ -0,0 +1,252 @@
+/************************************************************
+ * irq.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/* This file should never be included directed but, rather,
+ * only indirectly through nuttx/irq.h
+ */
+
+#ifndef __ARCH_C5471_IRQ_H
+#define __ARCH_C5471_IRQ_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* IRQ Stack Frame Format:
+ *
+ * Context is always saved/restored in the same way:
+ *
+ * (1) stmia rx, {r0-r3, r12}
+ * (2) stmia rx, (cpsr, r4-r11, r13-r14}
+ *
+ * This results in the following set of indices that
+ * can be used to access individual registers in the
+ * xcp.regs array:
+ */
+
+#define JB_R0 (0)
+#define JB_R1 (1)
+#define JB_R2 (2)
+#define JB_R3 (3)
+#define JB_R12 (4)
+
+#define XCPTCONTEXT_IRQ_REGS (5)
+#define XCPTCONTEXT_UOFFSET (4 * XCPTCONTEXT_IRQ_REGS)
+
+#define JB_CPSR (0 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R4 (1 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R5 (2 + XCPTCONTEXT_IRQ_REGS
+#define JB_R6 (3 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R7 (4 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R8 (5 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R9 (6 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R10 (7 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R11 (8 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R13 (9 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R14 (10 + XCPTCONTEXT_IRQ_REGS)
+#define JB_R15 /* Not saved */
+
+#define XCPTCONTEXT_USER_REG (11)
+#define XCPTCONTEST_REGS (XCPTCONTEXT_USER_REG+XCPTCONTEXT_IRQ_REGS)
+#define XCPTCONTEXT_SIZE (4 * XCPTCONTEST_REGS)
+
+#define JB_A1 JB_R0
+#define JB_A2 JB_R1
+#define JB_A3 JB_R2
+#define JB_A4 JB_R3
+#define JB_V1 JB_R4
+#define JB_V2 JB_R5
+#define JB_V3 JB_R6
+#define JB_V4 JB_R7
+#define JB_V5 JB_R8
+#define JB_V6 JB_R9
+#define JB_V7 JB_R10
+#define JB_SB JB_R9
+#define JB_SL JB_R10
+#define JB_FP JB_R11
+#define JB_IP JB_R12
+#define JB_SP JB_R13
+#define JB_LR JB_R14
+#define JB_PC JB_R15
+
+/* C5471 Interrupts */
+
+#define C5471_IRQ_TIMER0 0
+#define C5471_IRQ_TIMER1 1
+#define C5471_IRQ_TIMER2 2
+#define C5471_IRQ_GPIO0 3
+#define C5471_IRQ_ETHER 4
+#define C5471_IRQ_KBGPIO_0_7 5
+#define C5471_IRQ_UART 6
+#define C5471_IRQ_UART_IRDA 7
+#define C5471_IRQ_KBGPIO_8_15 8
+#define C5471_IRQ_GPIO3 9
+#define C5471_IRQ_GPIO2 10
+#define C5471_IRQ_I2C 11
+#define C5471_IRQ_GPIO1 12
+#define C5471_IRQ_SPI 13
+#define C5471_IRQ_GPIO_4_19 14
+#define C5471_IRQ_API 15
+
+#define C5471_IRQ_WATCHDOG C5471_IRQ_TIMER0
+#define C5471_IRQ_SYSTIMER C5471_IRQ_TIMER1
+#define NR_IRQS (C5471_IRQ_API+1)
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/* This struct defines the way the registers are stored. We
+ * need to save:
+ *
+ * 1 CPSR
+ * 7 Static registers, v1-v7 (aka r4-r10)
+ * 1 Frame pointer, fp (aka r11)
+ * 1 Stack pointer, sp (aka r13)
+ * 1 Return address, lr (aka r14)
+ * ---
+ * 11 (XCPTCONTEXT_USER_REG)
+ *
+ * On interrupts, we also need to save:
+ * 4 Volatile registers, a1-a4 (aka r0-r3)
+ * 1 Scratch Register, ip (aka r12)
+ *---
+ * 5 (XCPTCONTEXT_IRQ_REGS)
+ *
+ * For a total of 17 (XCPTCONTEST_REGS)
+ */
+
+#ifndef __ASSEMBLY__
+struct xcptcontext
+{
+ /* The following function pointer is non-zero if there
+ * are pending signals to be processed.
+ */
+
+ void *sigdeliver; /* Actual type is sig_deliver_t */
+
+ /* These are saved copies of LR and CPSR used during
+ * signal processing.
+ */
+
+ uint32 saved_lr;
+ uint32 saved_cpsr;
+
+ /* Register save area */
+
+ uint32 regs[XCPTCONTEST_REGS];
+};
+#endif
+
+/************************************************************
+ * Inline functions
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+
+/* Save the current interrupt enable state & disable IRQs */
+
+static inline uint32 irqsave(void)
+{
+ unsigned long flags;
+ unsigned long temp;
+ __asm__ __volatile__
+ (
+ "\tmrs %0, cpsr\n"
+ "\torr %1, %0, #128\n"
+ "\tmsr cpsr_c, %1"
+ : "=r" (flags), "=r" (temp)
+ :
+ : "memory");
+ return flags;
+}
+
+/* Restore saved IRQ & FIQ state */
+
+static inline void irqrestore(uint32 flags)
+{
+ __asm__ __volatile__
+ (
+ "msr cpsr_c, %0"
+ :
+ : "r" (flags)
+ : "memory");
+}
+
+static inline void system_call(swint_t func, uint32 parm1,
+ uint32 parm2, uint32 parm3)
+{
+ __asm__ __volatile__
+ (
+ "mov\tr0,%0\n\t"
+ "mov\tr1,%1\n\t"
+ "mov\tr2,%2\n\t"
+ "mov\tr2,%3\n\t"
+ "swi\t0x900001\n\t"
+ :
+ : "r" ((long)(func)), "r" ((long)(parm1)),
+ "r" ((long)(parm2)), "r" ((long)(parm3))
+ : "r0", "r1", "r2", "r3", "lr");
+}
+#endif
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+#endif
+
+#endif /* __ARCH_C5471_IRQ_H */
+
diff --git a/nuttx/arch/c5471/include/types.h b/nuttx/arch/c5471/include/types.h
new file mode 100644
index 0000000000..7b39302f0b
--- /dev/null
+++ b/nuttx/arch/c5471/include/types.h
@@ -0,0 +1,70 @@
+/************************************************************
+ * types.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/* This file should never be included directed but, rather,
+ * only indirectly through sys/types.h
+ */
+
+#ifndef __ARCH_C5471_TYPES_H
+#define __ARCH_C5471_TYPES_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Type Declarations
+ ************************************************************/
+
+typedef char sbyte;
+typedef unsigned char ubyte;
+typedef unsigned char uint8;
+typedef unsigned char boolean;
+typedef short sint16;
+typedef unsigned short uint16;
+typedef int sint32;
+typedef unsigned int uint32;
+typedef long long sint64;
+typedef unsigned long long uint64;
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#endif /* __ARCH_C5471_TYPES_H */
diff --git a/nuttx/arch/c5471/ld.script b/nuttx/arch/c5471/ld.script
new file mode 100644
index 0000000000..f8bdf630ed
--- /dev/null
+++ b/nuttx/arch/c5471/ld.script
@@ -0,0 +1,107 @@
+/************************************************************
+ * ld.script
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+OUTPUT_ARCH(arm)
+ENTRY(_stext)
+SECTIONS
+{
+ /* Interrupt vector trampoline and command line parameters
+ * are provided in IRAM by the rrload bootloader. Vectors will be
+ * copied into _svectors from _vflashstart.
+ */
+
+ . = 0xffc00000;
+ _svectors = ABSOLUTE(.);
+
+ /* These are locations in IRAM where the rrload bootloader passes
+ * information to the running program
+ */
+
+ . = 0xffc00020;
+ __KernCommandLineMagicStr = .; /* magic pattern string == "kcmdline-->" */
+ . = 0xffc0002C; /* advance to .+strlen("kcmdline-->")+1 */
+ __KernCommandLineOverride = .; /* location of kernel command line string */
+
+ . = 0xffc00100;
+ __EtherMACMagicStr = .; /* magic pattern string == "etherMAC-->" */
+ . = 0xffc0010C; /* advance to .+strlen("etherMAC-->")+1 */
+ __EtherMAC = .;
+
+
+ /* The OS entry point is here */
+
+ . = 0x01030000;
+ .text : {
+ _stext = ABSOLUTE(.);
+ *(.text)
+ *(.fixup)
+ *(.gnu.warning)
+ *(.rodata)
+ *(.glue_7)
+ *(.glue_7t)
+ *(.got) /* Global offset table */
+ _etext = ABSOLUTE(.);
+ }
+
+ _eronly = ABSOLUTE(.); /* See below */
+ . = ALIGN(4096);
+
+ .data : {
+ _sdata = ABSOLUTE(.);
+ *(.data)
+ CONSTRUCTORS
+ _edata = ABSOLUTE(.);
+ }
+
+ .bss : { /* BSS */
+ _sbss = ABSOLUTE(.);
+ *(.bss)
+ *(COMMON)
+ _ebss = ABSOLUTE(.);
+ }
+ /* Stabs debugging sections. */
+ .stab 0 : { *(.stab) }
+ .stabstr 0 : { *(.stabstr) }
+ .stab.excl 0 : { *(.stab.excl) }
+ .stab.exclstr 0 : { *(.stab.exclstr) }
+ .stab.index 0 : { *(.stab.index) }
+ .stab.indexstr 0 : { *(.stab.indexstr) }
+ .comment 0 : { *(.comment) }
+ .debug_abbrev 0 : { *(.debug_abbrev) }
+ .debug_info 0 : { *(.debug_info) }
+ .debug_line 0 : { *(.debug_line) }
+ .debug_pubnames 0 : { *(.debug_pubnames) }
+ .debug_aranges 0 : { *(.debug_aranges) }
+}
diff --git a/nuttx/arch/c5471/setenv.sh b/nuttx/arch/c5471/setenv.sh
new file mode 100755
index 0000000000..bb3bd49c5c
--- /dev/null
+++ b/nuttx/arch/c5471/setenv.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+# setenv.sh
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+if [ "$(basename $0)" = "setenv" ] ; then
+ echo "You must source this script, not run it!" 1>&2
+ exit 1
+fi
+
+if [ -z ${PATH_ORIG} ]; then export PATH_ORIG=${PATH}; fi
+
+WD=`pwd`
+export BUILDROOT_BIN=${WD}/../buildroot/build_arm_nofpu/staging_dir/bin
+export PATH=${BUILDROOT_BIN}:/sbin:/usr/sbin:${PATH_ORIG}
+
+echo "PATH : ${PATH}"
diff --git a/nuttx/arch/c5471/src/Makefile b/nuttx/arch/c5471/src/Makefile
new file mode 100644
index 0000000000..d5dc016e1e
--- /dev/null
+++ b/nuttx/arch/c5471/src/Makefile
@@ -0,0 +1,79 @@
+############################################################
+# Makefile
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+-include $(TOPDIR)/Make.defs
+
+MKDEP = $(TOPDIR)/tools/mkdeps.sh
+CFLAGS += -I$(TOPDIR)/sched
+
+ASRCS = up_vectors.S up_saveusercontext.S up_fullcontextrestore.S
+AOBJS = $(ASRCS:.S=.o)
+
+CSRCS = up_initialize.c up_initialstate.c up_idle.c \
+ up_irq.c up_syscall.c up_dataabort.c up_prefetchabort.c \
+ up_undefinedinsn.c up_interruptcontext.c up_timerisr.c \
+ up_createstack.c up_usestack.c up_releasestack.c \
+ up_exit.c up_assert.c up_blocktask.c up_unblocktask.c \
+ up_releasepending.c up_reprioritizertr.c up_copystate.c \
+ up_schedulesigaction.c up_sigdeliver.c
+COBJS = $(CSRCS:.c=.o)
+
+SRCS = $(ASRCS) $(CSRCS)
+OBJS = $(AOBJS) $(COBJS)
+
+all: up_head.o libarch.a
+
+$(AOBJS) up_head.o: %.o: %.S
+ $(CC) -c $(CFLAGS) -D__ASSEMBLY__ $< -o $@
+
+$(COBJS): %.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
+libarch.a: $(OBJS)
+ $(AR) rcs $@ $(OBJS)
+
+.depend: Makefile $(SRCS)
+ $(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
+ touch $@
+
+depend: .depend
+
+clean:
+ rm -f libarch.a *.o *~
+
+distclean: clean
+ rm -f Make.dep .depend
+
+-include Make.dep
diff --git a/nuttx/arch/c5471/src/c5471.h b/nuttx/arch/c5471/src/c5471.h
new file mode 100644
index 0000000000..c7104be621
--- /dev/null
+++ b/nuttx/arch/c5471/src/c5471.h
@@ -0,0 +1,376 @@
+/************************************************************
+ * c5471.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __C5471_H
+#define __C5471_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+# include <sys/types.h>
+#endif
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* Arm7Tdmi *************************************************/
+
+/* CPSR bits */
+
+#define USR26_MODE 0x00
+#define FIQ26_MODE 0x01
+#define IRQ26_MODE 0x02
+#define SVC26_MODE 0x03
+#define USR_MODE 0x10
+#define FIQ_MODE 0x11
+#define IRQ_MODE 0x12
+#define SVC_MODE 0x13
+#define ABT_MODE 0x17
+#define UND_MODE 0x1b
+#define SYSTEM_MODE 0x1f
+#define MODE_MASK 0x1f
+#define T_BIT 0x20
+#define F_BIT 0x40
+#define I_BIT 0x80
+#define CC_V_BIT (1 << 28)
+#define CC_C_BIT (1 << 29)
+#define CC_Z_BIT (1 << 30)
+#define CC_N_BIT (1 << 31)
+
+/* UARTs ****************************************************/
+
+#define UART_IRDA_BASE 0xffff0800
+#define UART_MODEM_BASE 0xffff1000
+#define UARTn_IO_RANGE 0x00000800
+
+/* Common UART Registers. Expressed as offsets from the BASE address */
+
+#define UART_RHR_OFFS 0x00000000 /* Rcv Holding Register */
+#define UART_THR_OFFS 0x00000004 /* Xmit Holding Register */
+#define UART_FCR_OFFS 0x00000008 /* FIFO Control Register */
+#define UART_RFCR_OFFS 0x00000008 /* Rcv FIFO Control Register */
+#define UART_TFCR_OFFS 0x00000008 /* Xmit FIFO Control Register */
+#define UART_SCR_OFFS 0x0000000c /* Status Control Register */
+#define UART_LCR_OFFS 0x00000010 /* Line Control Register */
+#define UART_LSR_OFFS 0x00000014 /* Line Status Register */
+#define UART_SSR_OFFS 0x00000018 /* Supplementary Status Register */
+#define UART_MCR_OFFS 0x0000001c /* Modem Control Register */
+#define UART_MSR_OFFS 0x00000020 /* Modem Status Register */
+#define UART_IER_OFFS 0x00000024 /* Interrupt Enable Register */
+#define UART_ISR_OFFS 0x00000028 /* Interrupt Status Register */
+#define UART_EFR_OFFS 0x0000002c /* Enhanced Feature Register */
+#define UART_XON1_OFFS 0x00000030 /* XON1 Character Register */
+#define UART_XON2_OFFS 0x00000034 /* XON2 Character Register */
+#define UART_XOFF1_OFFS 0x00000038 /* XOFF1 Character Register */
+#define UART_XOFF2_OFFS 0x0000003c /* XOFF2 Character Register */
+#define UART_SPR_OFFS 0x00000040 /* Scratch-pad Register */
+#define UART_DIV_115K_OFFS 0x00000044 /* Divisor for baud generation */
+#define UART_DIV_BIT_RATE_OFFS 0x00000048 /* For baud rate generation */
+#define UART_TCR_OFFS 0x0000004c /* Transmission Control Register */
+#define UART_TLR_OFFS 0x00000050 /* Trigger Level Register */
+#define UART_MDR_OFFS 0x00000054 /* Mode Definition Register */
+
+/* Registers available only for the IrDA UART (absolute address). */
+
+#define UART_IRDA_MDR1 0xffff0854 /* Mode Definition Register 1 */
+#define UART_IRDA_MDR2 0xffff0858 /* Mode Definition Register 2 */
+#define UART_IRDA_TXFLL 0xffff085c /* LS Xmit Frame Length Register */
+#define UART_IRDA_TXFLH 0xffff0860 /* MS Xmit Frame Length Register */
+#define UART_IRDA_RXFLL 0xffff0864 /* LS Rcvd Frame Length Register */
+#define UART_IRDA_RXFLH 0xffff0868 /* MS Rcvd Frame Length Register */
+#define UART_IRDA_SFLSR 0xffff086c /* Status FIFO Line Status Reg */
+#define UART_IRDA_SFREGL 0xffff0870 /* LS Status FIFO Register */
+#define UART_IRDA_SFREGH 0xffff0874 /* MS Status FIFO Register */
+#define UART_IRDA_BLR 0xffff0878 /* Begin of File Length Register */
+#define UART_IRDA_PULSE_WIDTH 0xffff087c /* Pulse Width Register */
+#define UART_IRDA_ACREG 0xffff0880 /* Auxiliary Control Register */
+#define UART_IRDA_PULSE_START 0xffff0884 /* Start time of pulse */
+#define UART_IRDA_RX_W_PTR 0xffff0888 /* RX FIFO write pointer */
+#define UART_IRDA_RX_R_PTR 0xffff088c /* RX FIFO read pointer */
+#define UART_IRDA_TX_W_PTR 0xffff0890 /* TX FIFO write pointer */
+#define UART_IRDA_TX_R_PTR 0xffff0894 /* TX FIFO read pointer */
+#define UART_IRDA_STATUS_W_PTR 0xffff0898 /* Write pointer of status FIFO */
+#define UART_IRDA_STATUS_R_PTR 0xffff089c /* Read pointer of status FIFO */
+#define UART_IRDA_RESUME 0xffff08a0 /* Resume register */
+#define UART_IRDA_MUX 0xffff08a4 /* Selects UART_IRDA output mux */
+
+/* Registers available for the Modem UART (absolute addresses) */
+
+#define UART_MODEM_MDR 0xffff1054 /* Mode Definition Register */
+#define UART_MODEM_UASR 0xffff1058 /* UART Auto-baud Status Register */
+#define UART_MODEM_RDPTR_URX 0xffff105c /* RX FIFO Read Pointer Register */
+#define UART_MODEM_WRPTR_URX 0xffff1060 /* RX FIFO Write Pointer Register */
+#define UART_MODEM_RDPTR_UTX 0xffff1064 /* TX FIFO Read Pointer Register */
+#define UART_MODEM_WRPTR_UTX 0xffff1068 /* TX FIFO Write Pointer Register */
+
+/* UART Settings ********************************************/
+
+/* Miscellaneous UART settings. */
+
+#define UART_RX_FIFO_NOEMPTY 0x00000001
+#define UART_SSR_TXFULL 0x00000001
+#define UART_LSR_TREF 0x00000020
+
+#define UART_XMIT_FIFO_SIZE 64
+#define UART_IRDA_XMIT_FIFO_SIZE 64
+
+/* UART_LCR Register */
+ /* Bits 31-7: Reserved */
+#define UART_LCR_BOC 0x00000040 /* Bit 6: Break Control */
+ /* Bit 5: Parity Type 2 */
+#define UART_LCR_ParEven 0x00000010 /* Bit 4: Parity Type 1 */
+#define UART_LCR_ParOdd 0x00000000
+#define UART_LCR_ParEn 0x00000008 /* Bit 3: Paity Enable */
+#define UART_LCR_ParDis 0x00000000
+#define UART_LCR_2stop 0x00000004 /* Bit 2: Number of stop bits */
+#define UART_LCR_1stop 0x00000000
+#define UART_LCR_5bits 0x00000000 /* Bits 0-1: Word-length */
+#define UART_LCR_6bits 0x00000001
+#define UART_LCR_7bits 0x00000002
+#define UART_LCR_8bits 0x00000003
+
+#define UART_FCR_FTL 0x00000000
+#define UART_FCR_FIFO_EN 0x00000001
+#define UART_FCR_TX_CLR 0x00000002
+#define UART_FCR_RX_CLR 0x00000004
+
+#define UART_IER_RecvInt 0x00000001
+#define UART_IER_XmitInt 0x00000002
+#define UART_IER_LineStsInt 0x00000004
+#define UART_IER_ModemStsInt 0x00000008 /* IrDA UART only */
+#define UART_IER_XoffInt 0x00000020
+#define UART_IER_RtsInt 0x00000040 /* IrDA UART only */
+#define UART_IER_CtsInt 0x00000080 /* IrDA UART only */
+#define UART_IER_AllInts 0x000000ff
+
+#define BAUD_115200 0x00000001
+#define BAUD_57600 0x00000002
+#define BAUD_38400 0x00000003
+#define BAUD_19200 0x00000006
+#define BAUD_9600 0x0000000C
+#define BAUD_4800 0x00000018
+#define BAUD_2400 0x00000030
+#define BAUD_1200 0x00000060
+
+#define MDR_UART_MODE 0x00000000 /* Both IrDA and Modem UARTs */
+#define MDR_SIR_MODE 0x00000001 /* IrDA UART only */
+#define MDR_AUTOBAUDING_MODE 0x00000002 /* Modem UART only */
+#define MDR_RESET_MODE 0x00000007 /* Both IrDA and Modem UARTs */
+
+/* SPI ******************************************************/
+
+#define MAX_SPI 3
+
+#define SPI_REGISTER_BASE 0xffff2000
+
+/* GIO ******************************************************/
+
+#define MAX_GIO (35)
+
+#define GIO_REGISTER_BASE 0xffff2800
+
+#define GPIO_IO 0xffff2800 /* Writeable when I/O is configured
+ * as an output; reads value on I/O
+ * pin when I/O is configured as an
+ * input */
+#define GPIO_CIO 0xffff2804 /* GPIO configuration register */
+#define GPIO_IRQA 0xffff2808 /* In conjunction with GPIO_IRQB
+ * determines the behavior when GPIO
+ * pins configured as input IRQ */
+#define GPIO_IRQB 0xffff280c /* Determines the behavior when GPIO
+ * pins configured as input IRQ */
+#define GPIO_DDIO 0xffff2810 /* Delta Detect Register
+ * (detects changes in the I/O pins) */
+#define GPIO_EN 0xffff2814 /* Selects register for muxed GPIOs */
+
+#define KGIO_REGISTER_BASE 0xffff2900
+
+#define KBGPIO_IO 0xffff2900 /* Keyboard I/O bits: Writeable
+ * when KBGPIO is configured as an
+ * output; reads value on I/O pin
+ * when KBGPIO is configured as an
+ * input */
+#define KBGPIO_CIO 0xffff2904 /* KBGPIO configuration register */
+#define KBGPIO_IRQA 0xffff2908 /* In conjunction with KBGPIO_IRQB
+ * determines the behavior when
+ * KBGPIO pins configured as input
+ * IRQ */
+#define KBGPIO_IRQB 0xffff290c /* In conjunction with KBGPIO_IRQA
+ * determines the behavior when
+ * KBGPIO pins configured as input
+ * IRQ */
+#define KBGPIO_DDIO 0xffff2910 /* Delta Detect Register (detects
+ * changes in the KBGPIO pins) */
+#define KBGPIO_EN 0xffff2914 /* Selects register for muxed
+ * KBGPIOs */
+
+/* Timers ***************************************************/
+
+#define C5471_TIMER0_CTRL 0xffff2a00
+#define C5471_TIMER0_CNT 0xffff2a04
+#define C5471_TIMER1_CTRL 0xffff2b00
+#define C5471_TIMER1_CNT 0xffff2b04
+#define C5471_TIMER2_CTRL 0xffff2c00
+
+#define C5471_TIMER2_CNT 0xffff2c04
+
+/* Interrupts */
+
+#define HAVE_SRC_IRQ_BIN_REG 0
+
+#define INT_FIRST_IO 0xffff2d00
+#define INT_IO_RANGE 0x5C
+
+#define IT_REG 0xffff2d00
+#define MASK_IT_REG 0xffff2d04
+#define SRC_IRQ_REG 0xffff2d08
+#define SRC_FIQ_REG 0xffff2d0c
+#define SRC_IRQ_BIN_REG 0xffff2d10
+#define INT_CTRL_REG 0xffff2d18
+
+#define ILR_IRQ0_REG 0xffff2d1C /* 0-Timer 0 */
+#define ILR_IRQ1_REG 0xffff2d20 /* 1-Timer 1 */
+#define ILR_IRQ2_REG 0xffff2d24 /* 2-Timer 2 */
+#define ILR_IRQ3_REG 0xffff2d28 /* 3-GPIO0 */
+#define ILR_IRQ4_REG 0xffff2d2c /* 4-Ethernet */
+#define ILR_IRQ5_REG 0xffff2d30 /* 5-KBGPIO[7:0] */
+#define ILR_IRQ6_REG 0xffff2d34 /* 6-Uart serial */
+#define ILR_IRQ7_REG 0xffff2d38 /* 7-Uart IRDA */
+#define ILR_IRQ8_REG 0xffff2d3c /* 8-KBGPIO[15:8] */
+#define ILR_IRQ9_REG 0xffff2d40 /* 9-GPIO3 */
+#define ILR_IRQ10_REG 0xffff2d44 /* 10-GPIO2 */
+#define ILR_IRQ11_REG 0xffff2d48 /* 11-I2C */
+#define ILR_IRQ12_REG 0xffff2d4c /* 12-GPIO1 */
+#define ILR_IRQ13_REG 0xffff2d50 /* 13-SPI */
+#define ILR_IRQ14_REG 0xffff2d54 /* 14-GPIO[19:4] */
+#define ILR_IRQ15_REG 0xffff2d58 /* 15-API */
+
+/* I2C ******************************************************/
+
+#define MAX_I2C 1
+
+/* API ******************************************************/
+
+#define DSPRAM_BASE 0xffe00000 /* DSPRAM base address */
+#define DSPRAM_END 0xffe03fff
+
+/* This is the API address range in the DSP address space. */
+
+#define DSPMEM_DSP_START 0x2000
+#define DSPMEM_DSP_END 0x3fff
+
+/* This is the API address range in the ARM address space. */
+
+#define DSPMEM_ARM_START DSPRAM_BASE /* Defined in hardware.h */
+#define DSPMEM_ARM_END DSPRAM_END
+
+/* DSPMEM_IN_RANGE is a generic macro to test is a value is within
+ * a range of values.
+ */
+
+#define DSPMEM_IN_RANGE(addr, start, end) \
+ ((((__u32)(addr)) >= (start)) && (((__u32)(addr)) <= (end)))
+
+/* DSPMEM_ADDR_ALIGNED verifies that a potential DSP address is
+ * properly word aligned.
+ */
+
+#define DSPMEM_ADDR_ALIGNED(addr, cpu) ((((__u32)(addr)) & 1) == 0)
+
+/* DSPMEM_DSP_ADDR checks if a DSP address lies in within the
+ * DSP's API address range.
+ */
+
+#define DSPMEM_DSP_ADDR(addr, cpu) \
+ DSPMEM_IN_RANGE(addr, DSPMEM_DSP_START, DSPMEM_DSP_END)
+
+/* DSPMEM_ARM_ADDR checks if a ARM address lies in within the
+ * ARM's API address range.
+ */
+
+#define DSPMEM_ARM_ADDR(addr) \
+ DSPMEM_IN_RANGE(addr, DSPMEM_ARM_START, DSPMEM_ARM_END)
+
+/* DSPMEM_DSP_TO_ARM maps a DSP API address into an ARM API address */
+
+#define DSPMEM_DSP_TO_ARM(addr, cpu) \
+ ((((__u32)(addr) - DSPMEM_DSP_START) << 1) + DSPMEM_ARM_START)
+
+/* DSPMEM_ARM_TO_DSP maps an ARM API address into a DSP API address */
+
+#define DSPMEM_ARM_TO_DSP(addr) \
+ ((((__u32)(addr) - DSPMEM_ARM_START) >> 1) + DSPMEM_DSP_START)
+
+/************************************************************
+ * Inline Functions
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+
+# define getreg8(a) (*(volatile ubyte *)(a))
+# define putreg8(v,a) (*(volatile ubyte *)(a) = (v))
+# define getreg32(a) (*(volatile uint32 *)(a))
+# define putreg32(v,a) (*(volatile uint32 *)(a) = (v))
+
+
+/* Some compiler options will convert short loads and stores into byte loads
+ * and stores. We don't want this to happen for IO reads and writes!
+ */
+
+/* # define getreg16(a) (*(volatile uint16 *)(a)) */
+static inline unsigned short getreg16(unsigned int addr)
+{
+ unsigned short retval;
+ __asm__ __volatile__("\tldrh %0, [%1]\n\t" : "=r"(retval) : "r"(addr));
+ return retval;
+}
+
+/* define putreg16(v,a) (*(volatile uint16 *)(a) = (v)) */
+static inline void putreg16(uint16 val, unsigned int addr)
+{
+ __asm__ __volatile__("\tstrh %0, [%1]\n\t": : "r"(val), "r"(addr));
+}
+
+/* Most C5471 registers are 16-bits wide */
+
+#define getreg(a) getreg16(1)
+#define putreg(v,a) putreg16(v,a)
+
+#endif
+
+#endif /* __C5471_H */
diff --git a/nuttx/arch/c5471/src/up_assert.c b/nuttx/arch/c5471/src/up_assert.c
new file mode 100644
index 0000000000..eaddc7c8e1
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_assert.c
@@ -0,0 +1,82 @@
+/************************************************************
+ * up_assert.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <debug.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_assert
+ ************************************************************/
+
+void up_assert(const ubyte *filename, uint32 lineno)
+{
+ dbg("Assertion failed at file:%s line: %d\n",
+ filename, lineno);
+ exit(EXIT_FAILURE);
+}
+
+/************************************************************
+ * Name: up_assert_code
+ ************************************************************/
+
+void up_assert_code(const ubyte *filename, uint32 lineno, uint16 errorcode)
+{
+ dbg("Assertion failed at file:%s line: %d error code: %d\n",
+ filename, lineno, errorcode);
+ exit(errorcode);
+}
diff --git a/nuttx/arch/c5471/src/up_blocktask.c b/nuttx/arch/c5471/src/up_blocktask.c
new file mode 100644
index 0000000000..6b42d42702
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_blocktask.c
@@ -0,0 +1,168 @@
+/************************************************************
+ * up_blocktask.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_block_task
+ *
+ * Description:
+ * The currently executing task at the head of
+ * the ready to run list must be stopped. Save its context
+ * and move it to the inactive list specified by task_state.
+ *
+ * Inputs:
+ * tcb: Refers to a task in the ready-to-run list (normally
+ * the task at the the head of the list). It most be
+ * stopped, its context saved and moved into one of the
+ * waiting task lists. It it was the task at the head
+ * of the ready-to-run list, then a context to the new
+ * ready to run task must be performed.
+ * task_state: Specifies which waiting task list should be
+ * hold the blocked task TCB.
+ *
+ ************************************************************/
+
+void up_block_task(_TCB *tcb, tstate_t task_state)
+{
+ /* Verify that the context switch can be performed */
+
+ if ((tcb->task_state < FIRST_READY_TO_RUN_STATE) ||
+ (tcb->task_state > LAST_READY_TO_RUN_STATE))
+ {
+ PANIC(OSERR_BADBLOCKSTATE);
+ }
+ else
+ {
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ boolean switch_needed;
+
+ dbg("Blocking TCB=%p\n", tcb);
+
+ /* Remove the tcb task from the ready-to-run list. If we
+ * are blocking the task at the head of the task list (the
+ * most likely case), then a context switch to the next
+ * ready-to-run task is needed. In this case, it should
+ * also be true that rtcb == tcb.
+ */
+
+ switch_needed = sched_removereadytorun(tcb);
+
+ /* Add the task to the specified blocked task list */
+
+ sched_addblocked(tcb, (tstate_t)task_state);
+
+ /* If there are any pending tasks, then add them to the g_readytorun
+ * task list now
+ */
+
+ if (g_pendingtasks.head)
+ {
+ switch_needed |= sched_mergepending();
+ }
+
+ /* Now, perform the context switch if one is needed */
+
+ if (switch_needed)
+ {
+ /* Are we in an interrupt handler? */
+
+ if (current_regs)
+ {
+ /* Yes, then we have to do things differently.
+ * Just copy the current_regs into the OLD rtcb.
+ */
+
+ up_copystate(rtcb->xcp.regs, current_regs);
+
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* Then switch contexts */
+
+ up_copystate(current_regs, rtcb->xcp.regs);
+ }
+
+ /* Copy the user C context into the TCB at the (old) head of the
+ * g_readytorun Task list. if up_saveusercontext returns a non-zero
+ * value, then this is really the previously running task restarting!
+ */
+
+ else if (!up_saveusercontext(rtcb->xcp.regs))
+ {
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* Then switch contexts */
+
+ up_fullcontextrestore(rtcb->xcp.regs);
+ }
+ }
+ }
+}
diff --git a/nuttx/arch/c5471/src/up_copystate.c b/nuttx/arch/c5471/src/up_copystate.c
new file mode 100644
index 0000000000..8e8586699b
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_copystate.c
@@ -0,0 +1,75 @@
+/************************************************************
+ * up_copystate.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_undefinedinsn
+ ************************************************************/
+
+/* A little faster than most memcpy's */
+
+void up_copystate(uint32 *dest, uint32 *src)
+{
+ int i;
+ for (i = 0; i < XCPTCONTEST_REGS; i++)
+ {
+ *dest++ = *src++;
+ }
+}
+
diff --git a/nuttx/arch/c5471/src/up_createstack.c b/nuttx/arch/c5471/src/up_createstack.c
new file mode 100644
index 0000000000..20276bff09
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_createstack.c
@@ -0,0 +1,126 @@
+/************************************************************
+ * up_createstack.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Types
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_create_stack
+ *
+ * Description:
+ * Allocate a stack for a new thread and setup
+ * up stack-related information in the TCB.
+ *
+ * The following TCB fields must be initialized:
+ * adj_stack_size: Stack size after adjustment for hardware,
+ * processor, etc. This value is retained only for debug
+ * purposes.
+ * stack_alloc_ptr: Pointer to allocated stack
+ * adj_stack_ptr: Adjusted StatckAllocPtr for HW. The
+ * initial value of the stack pointer.
+ *
+ * Inputs:
+ * tcb: The TCB of new task
+ * stack_size: The requested stack size. At least this much
+ * must be allocated.
+ ************************************************************/
+
+STATUS up_create_stack(_TCB *tcb, uint32 stack_size)
+{
+ if (tcb->stack_alloc_ptr &&
+ tcb->adj_stack_size != stack_size)
+ {
+ sched_free(tcb->stack_alloc_ptr);
+ tcb->stack_alloc_ptr = NULL;
+ }
+
+ if (!tcb->stack_alloc_ptr)
+ {
+ tcb->stack_alloc_ptr = (uint32 *)kzmalloc(stack_size);
+ }
+
+ if (tcb->stack_alloc_ptr)
+ {
+ uint32 top_of_stack;
+ uint32 size_of_stack;
+
+ /* The Arm7Tdmi uses a push-down stack: the stack grows
+ * toward loweraddresses in memory. The stack pointer
+ * register, points to the lowest, valid work address
+ * (the "top" of the stack). Items on the stack are
+ * referenced as positive word offsets from sp.
+ */
+
+ top_of_stack = (uint32)tcb->stack_alloc_ptr + stack_size - 4;
+
+ /* The Arm7Tdmi stack must be aligned at word (4 byte)
+ * boundaries. If necessary top_of_stack must be rounded
+ * down to the next boundary
+ */
+
+ top_of_stack &= ~3;
+ size_of_stack = top_of_stack - (uint32)tcb->stack_alloc_ptr + 4;
+
+ /* Save the adjusted stack values in the _TCB */
+
+ tcb->adj_stack_size = top_of_stack;
+ tcb->adj_stack_size = size_of_stack;
+
+ return OK;
+ }
+
+ return ERROR;
+}
diff --git a/nuttx/arch/c5471/src/up_dataabort.c b/nuttx/arch/c5471/src/up_dataabort.c
new file mode 100644
index 0000000000..ca1ade7238
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_dataabort.c
@@ -0,0 +1,68 @@
+/************************************************************
+ * up_dataabort.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_dataabort
+ ************************************************************/
+
+void up_dataabort(uint32 *regs)
+{
+ PANIC(OSERR_ERREXCEPTION);
+}
diff --git a/nuttx/arch/c5471/src/up_exit.c b/nuttx/arch/c5471/src/up_exit.c
new file mode 100644
index 0000000000..8e211e0c2e
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_exit.c
@@ -0,0 +1,111 @@
+/************************************************************
+ * up_exit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: _exit
+ *
+ * Description:
+ * This function causes the currently executing task to cease
+ * to exist. This is a special case of task_delete().
+ *
+ ************************************************************/
+
+void _exit(int status)
+{
+ _TCB* tcb = (_TCB*)g_readytorun.head;
+
+ dbg("TCB=%p exitting\n", tcb);
+
+ /* Remove the tcb task from the ready-to-run list. We can
+ * ignore the return value because we know that a context
+ * switch is needed.
+ */
+
+ (void)sched_removereadytorun(tcb);
+
+ /* Move the TCB to the specified blocked task list and delete it */
+
+ sched_addblocked(tcb, TSTATE_TASK_INACTIVE);
+ task_delete(tcb->pid);
+
+ /* If there are any pending tasks, then add them to the g_readytorun
+ * task list now
+ */
+
+ if (g_pendingtasks.head)
+ {
+ (void)sched_mergepending();
+ }
+
+ /* Now, perform the context switch to the new ready-to-run task at the
+ * head of the list.
+ */
+
+ tcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", tcb);
+
+ /* Then switch contexts */
+
+ up_fullcontextrestore(tcb->xcp.regs);
+}
+
diff --git a/nuttx/arch/c5471/src/up_fullcontextrestore.S b/nuttx/arch/c5471/src/up_fullcontextrestore.S
new file mode 100644
index 0000000000..c4b230316c
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_fullcontextrestore.S
@@ -0,0 +1,115 @@
+/**************************************************************************
+ * up_fullcontextrestore.S
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **************************************************************************/
+
+/**************************************************************************
+ * Included Files
+ **************************************************************************/
+
+#include <nuttx/irq.h>
+#include "up_internal.h"
+
+/**************************************************************************
+ * Private Definitions
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Types
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Function Prototypes
+ **************************************************************************/
+
+/**************************************************************************
+ * Global Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Functions
+ **************************************************************************/
+
+/**************************************************************************
+ * Public Functions
+ **************************************************************************/
+
+/**************************************************************************
+ * Name: up_fullcontextrestore
+ **************************************************************************/
+
+ .globl up_fullcontextrestore
+ .type up_fullcontextrestore, function
+up_fullcontextrestore:
+
+ /* On entry, a1 (r0) holds address of the register save area */
+
+ /* Restore the volatile registers. This is not necessary for
+ * normally task-to-task context switches (where the context
+ * was saved by up_saveusercontext()), but is necesary when
+ * the full context was saved through interrupt handling.
+ */
+
+ /* Recover the user context (we will then have a new stack pointer) */
+
+ add r1, r0, #XCPTCONTEXT_UOFFSET
+ ldmia r1, {r3-r11, r13-r14}
+
+ /* Save the CSPR value and one scratch register on the stack */
+
+ sub sp, sp, #2*4 /* Create a frame to hold two regs */
+ stmia sp, {r3, r4} /* Save the CPSR (r3) and scratch (r4) */
+
+ /* Then recover the remaining registers */
+
+ ldmia r0, {r0-r3, r12} /* Recover volatile regs */
+
+ /* Now we can restore the CPSR (probably re-enabling interrupts) */
+
+ ldr r4, [sp]
+ msr cpsr, r4
+
+ /* Then recover the correct r4 value */
+
+ ldr r4, [sp, #4]
+
+ /* Destroy the temporary stack frame and return */
+
+ add sp, sp, #2*4
+ movs pc, lr
+ .size up_fullcontextrestore, . - up_fullcontextrestore
+
diff --git a/nuttx/arch/c5471/src/up_head.S b/nuttx/arch/c5471/src/up_head.S
new file mode 100644
index 0000000000..f4540c54b6
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_head.S
@@ -0,0 +1,125 @@
+/************************************************************
+ * up_head.S
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include "c5471.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * OS Entry Point
+ ************************************************************/
+
+/* We assume the bootloader has already initialized most of the h/w for
+ * us and that only leaves us having to do some os specific things
+ * below.
+ */
+ .global __start
+ .type __start, #function
+__start:
+
+/* First, setup initial processor mode */
+
+ mov r0, #(SVC_MODE | I_BIT | F_BIT )
+ msr cpsr, r0
+
+/* Setup system stack (and get the BSS range) */
+
+ adr r0, LC0
+ ldmia r0, {r4, r5, sp}
+
+/* Clear system BSS section */
+
+ mov r0, #0
+1: cmp r4, r5
+ strcc r0, [r4], #4
+ bcc 1b
+
+/* Copy system .data sections to new home in RAM. */
+
+#ifdef CONFIG_BOOT_FROM_FLASH
+
+ adr r3, LC2
+ ldmia r3, {r0, r1, r2}
+
+1: ldmia r0!, {r3 - r10}
+ stmia r1!, {r3 - r10}
+ cmp r1, r2
+ blt 1b
+
+#endif
+
+/* Initialize Kernel Stack Contents */
+
+#if 0
+ mov r1, sp
+ sub r1, r1, #INITIAL_STACK_SIZE
+ ldr r0, L_STACK_MAGIC
+ str r0, [r1], #4
+ ldr r0, L_STACK_UNTOUCHED_MAGIC
+1: cmp r1, sp
+ strcc r0, [r1], #4
+ bcc 1b
+#endif
+
+/* Jump to OS entry */
+
+ mov fp, #0
+ b os_start
+
+/* Variables */
+
+LC0: .long _sbss
+ .long _ebss
+ .long CONFIG_STACK_POINTER+CONFIG_PROC_STACK_SIZE-4
+
+#ifdef CONFIG_BOOT_FROM_FLASH
+LC2: .long _eronly @ Where .data defaults are stored in Flash.
+ .long _sdata @ Where .data needs to reside in SDRAM.
+ .long _edata
+#endif
+
+#if 0
+L_STACK_UNTOUCHED_MAGIC: .long 0xfeef1ef0
+L_STACK_MAGIC: .long 0xdeadbeef
+#endif
+ .end
+
diff --git a/nuttx/arch/c5471/src/up_idle.c b/nuttx/arch/c5471/src/up_idle.c
new file mode 100644
index 0000000000..46177a3de8
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_idle.c
@@ -0,0 +1,79 @@
+/************************************************************
+ * up_idle.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_idle
+ *
+ * Description:
+ * up_idle() is the logic that will be executed when their
+ * is no other ready-to-run task. This is processor idle
+ * time and will continue until some interrupt occurs to
+ * cause a context switch from the idle task.
+ *
+ * Processing in this state may be processor-specific. e.g.,
+ * this is where power management operations might be
+ * performed.
+ *
+ ************************************************************/
+
+void up_idle(void)
+{
+}
+
diff --git a/nuttx/arch/c5471/src/up_initialize.c b/nuttx/arch/c5471/src/up_initialize.c
new file mode 100644
index 0000000000..660cf44d72
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_initialize.c
@@ -0,0 +1,93 @@
+/************************************************************
+ * up_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Types
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_initialize
+ *
+ * Description:
+ * up_initialize will be called once during OS
+ * initialization after the basic OS services have been
+ * initialized. The architecture specific details of
+ * initializing the OS will be handled here. Such things as
+ * setting up interrupt service routines, starting the
+ * clock, and registering device drivers are some of the
+ * things that are different for each processor and hardware
+ * platform.
+ *
+ * up_initialize is called after the OS initialized but
+ * before the init process has been started and before the
+ * libraries have been initialized. OS services and driver
+ * services are available.
+ *
+ ************************************************************/
+
+void up_initialize(void)
+{
+ /* Initialize global variables */
+
+ current_regs = NULL;
+
+ /* Initialize the interrupt subsystem */
+
+ up_irqinitialize();
+
+ /* Attach and enable the timer interrupt */
+
+ up_disable_irq(C5471_IRQ_SYSTIMER);
+ irq_attach(C5471_IRQ_SYSTIMER, (xcpt_t)up_timerisr);
+ up_enable_irq(C5471_IRQ_SYSTIMER);
+}
diff --git a/nuttx/arch/c5471/src/up_initialstate.c b/nuttx/arch/c5471/src/up_initialstate.c
new file mode 100644
index 0000000000..3a26c45f1a
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_initialstate.c
@@ -0,0 +1,85 @@
+/************************************************************
+ * up_initialstate.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_initial_state
+ *
+ * Description:
+ * A new thread is being started and a new TCB
+ * has been created. This function is called to initialize
+ * the processor specific portions of the new TCB.
+ *
+ * This function must setup the intial architecture registers
+ * and/or stack so that execution will begin at tcb->start
+ * on the next context switch.
+ *
+ ************************************************************/
+
+void up_initial_state(_TCB *tcb)
+{
+ struct xcptcontext *xcp = &tcb->xcp;
+
+ /* Initialize the initial exception register context structure */
+
+ memset(xcp, 0, sizeof(struct xcptcontext));
+ xcp->regs[JB_SP] = (uint32)tcb->adj_stack_ptr;
+ xcp->regs[JB_LR] = (uint32)tcb->start;
+}
diff --git a/nuttx/arch/c5471/src/up_internal.h b/nuttx/arch/c5471/src/up_internal.h
new file mode 100644
index 0000000000..d3e806d12b
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_internal.h
@@ -0,0 +1,99 @@
+/************************************************************
+ * up_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __UP_INTERNAL_H
+#define __UP_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+typedef void (*up_vector_t)(void);
+#endif
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+extern uint32 *current_regs;
+#endif
+
+/************************************************************
+ * Inline Functions
+ ************************************************************/
+
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+
+/* Defined in files with the same name as the function */
+
+extern void up_copystate(uint32 *dest, uint32 *src);
+extern void up_dataabort(uint32 *regs);
+extern void up_fullcontextrestore(uint32 *regs) __attribute__ ((noreturn));
+extern void up_irqinitialize(void);
+extern void up_prefetchabort(uint32 *regs);
+extern int up_saveusercontext(uint32 *regs);
+extern void up_sigdeliver(void);
+extern void up_syscall(uint32 *regs);
+extern int up_timerisr(int irq, uint32 *regs);
+extern void up_undefinedinsn(uint32 *regs);
+
+/* Defined in up_vectors.S */
+
+extern void up_vectorundefinsn(void);
+extern void up_vectorswi(void);
+extern void up_vectorprefetch(void);
+extern void up_vectordata(void);
+extern void up_vectoraddrexcptn(void);
+extern void up_vectorirq(void);
+extern void up_vectorfiq(void);
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __UP_INTERNAL_H */
diff --git a/nuttx/arch/c5471/src/up_interruptcontext.c b/nuttx/arch/c5471/src/up_interruptcontext.c
new file mode 100644
index 0000000000..b6df69ea4c
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_interruptcontext.c
@@ -0,0 +1,68 @@
+/************************************************************
+ * up_interruptcontext.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Types
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_interrupt_context
+ *
+ * Description: Return TRUE is we are currently executing in
+ * the interrupt handler context.
+ ************************************************************/
+
+boolean up_interrupt_context(void)
+{
+ return current_regs != NULL;
+}
diff --git a/nuttx/arch/c5471/src/up_irq.c b/nuttx/arch/c5471/src/up_irq.c
new file mode 100644
index 0000000000..d388f6446a
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_irq.c
@@ -0,0 +1,243 @@
+/************************************************************
+ * up_irq.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/irq.h>
+#include "c5471.h"
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define EdgeSensitive 0x00000020
+#define Priority 0x0000001E
+
+/************************************************************
+ * Public Data
+ ************************************************************/
+
+uint32 *current_regs;
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/* The value of _vflashstart is defined in ld.script. It
+ * could be hard-coded because we know that correct IRAM
+ * area is 0xffc00000.
+ */
+
+extern int _svectors; /* Type does not matter */
+
+/* The C5471 has FLASH at the low end of memory. The
+ * rrload bootloaer will catch all interrupts and re-vector
+ * them to vectors stored in IRAM. The following table is
+ * used to initialize those vectors.
+ */
+
+static up_vector_t g_vectorinittab[] =
+{
+ (up_vector_t)NULL,
+ up_vectorundefinsn,
+ up_vectorswi,
+ up_vectorprefetch,
+ up_vectordata,
+ up_vectoraddrexcptn,
+ up_vectorirq,
+ up_vectorfiq
+};
+#define NVECTORS ((sizeof(g_vectorinittab)) / sizeof(up_vector_t))
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_ackirq
+ *
+ * Description:
+ * Acknowlede the IRQ.Bit 0 of the Interrupt Control
+ * Register == New IRQ agreement (NEW_IRQ_AGR). Reset IRQ
+ * output. Clear source IRQ register. Enables a new IRQ
+ * generation. Reset by internal logic.
+ *
+ ************************************************************/
+
+static inline void up_ackirq(unsigned int irq)
+{
+ uint32 reg;
+ reg = getreg32(SRC_IRQ_REG); /* Insure appropriate IT_REG bit clears */
+ putreg32(reg | 0x00000001, INT_CTRL_REG); /* write the NEW_IRQ_AGR bit. */
+}
+
+/************************************************************
+ * Name: up_ackfiq
+ *
+ * Description:
+ * Acknowledge the FIQ. Bit 1 of the Interrupt Control
+ * Register == New FIQ agreement (NEW_FIQ_AGR). Reset FIQ
+ * output. Clear source FIQ register. Enables a new FIQ
+ * generation. Reset by internal logic.
+ *
+ ************************************************************/
+
+static inline void up_ackfiq(unsigned int irq)
+{
+ uint32 reg;
+ reg = getreg32(SRC_FIQ_REG); /* Insure appropriate IT_REG bit clears */
+ putreg32(reg | 0x00000002, INT_CTRL_REG); /* write the NEW_FIQ_AGR bit. */
+}
+
+/************************************************************
+ * Name: up_vectorinitialize
+ ************************************************************/
+
+static inline void up_vectorinitialize(void)
+{
+ up_vector_t *src = g_vectorinittab;
+ up_vector_t *dest = (up_vector_t*)&_svectors;
+ int i;
+ for (i = 0; i < NVECTORS; i++)
+ {
+ *dest++ = *src++;
+ }
+}
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: irq_initialize
+ ************************************************************/
+
+void up_irqinitialize(void)
+{
+ /* Disable all interrupts. */
+
+ putreg32(0x0000ffff, MASK_IT_REG);
+
+ /* Clear any pending interrupts */
+
+ up_ackirq(0);
+ up_ackfiq(0);
+ putreg32(0x00000000, IT_REG);
+
+ /* Override hardware defaults */
+
+ putreg32(EdgeSensitive | Priority, ILR_IRQ2_REG);
+ putreg32(EdgeSensitive | Priority, ILR_IRQ4_REG);
+ putreg32(Priority, ILR_IRQ6_REG);
+ putreg32(EdgeSensitive | Priority, ILR_IRQ15_REG);
+
+ /* Initialize hardware interrupt vectors */
+
+ up_vectorinitialize();
+ current_regs = NULL;
+
+ /* And finally, enable interrupts */
+
+ irqrestore(SVC_MODE | F_BIT);
+}
+
+/************************************************************
+ * Name: up_disable_irq
+ *
+ * Description:
+ * Disable the IRQ specified by 'irq'
+ *
+ ************************************************************/
+
+void up_disable_irq(int irq)
+{
+ if (irq < NR_IRQS)
+ {
+ uint32 reg = getreg32(MASK_IT_REG);
+ putreg32(reg | (1 << irq), MASK_IT_REG);
+ }
+}
+
+/************************************************************
+ * Name: up_enable_irq
+ *
+ * Description:
+ * Enable the IRQ specified by 'irq'
+ *
+ ************************************************************/
+
+void up_enable_irq(int irq)
+{
+ if (irq < NR_IRQS)
+ {
+ uint32 reg = getreg32(MASK_IT_REG);
+ putreg32(reg & ~(1 << irq), MASK_IT_REG);
+ }
+}
+
+/************************************************************
+ * Name: up_acknowledge_irq
+ *
+ * Description:
+ * Disable the IRQ specified by 'irq'
+ *
+ ************************************************************/
+
+/* Bit 0 of the Interrupt Control Rigster == New IRQ
+ * agreement (NEW_IRQ_AGR). Reset IRQ output. Clear source
+ * IRQ register. Enables a new IRQ generation. Reset by
+ * internal logic.
+ *
+ * IRQ (FIQ) output and SRC_IRQ_REG and SRC_IRQ_BIN_REG
+ * (SRC_FIQ_REG) registers are reset only if the bit in the
+ * Interrupt register (IT_REG) corresponding to the interrupt
+ * having requested MCU action is already cleared or masked.
+ *
+ * For an edge-sensitive interrupt, the Interrupt register bit is
+ * deactivated when reading the SRC_IRQ_REG or SRC_IRQ_BIN_REG
+ * (SRC_FIQ_REG) registers.
+ */
+
+void up_acknowledge_irq(int irq)
+{
+ uint32 reg = getreg32(INT_CTRL_REG);
+ putreg32(reg | 0x00000001, INT_CTRL_REG); /* write the NEW_IRQ_AGR bit. */
+}
diff --git a/nuttx/arch/c5471/src/up_prefetchabort.c b/nuttx/arch/c5471/src/up_prefetchabort.c
new file mode 100644
index 0000000000..7756b7c351
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_prefetchabort.c
@@ -0,0 +1,69 @@
+/************************************************************
+ * up_prefetchabort.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/irq.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_prefetchabort
+ ************************************************************/
+
+void up_prefetchabort(uint32 *regs)
+{
+ PANIC(OSERR_ERREXCEPTION);
+}
diff --git a/nuttx/arch/c5471/src/up_releasepending.c b/nuttx/arch/c5471/src/up_releasepending.c
new file mode 100644
index 0000000000..c1b5a88509
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_releasepending.c
@@ -0,0 +1,131 @@
+/************************************************************
+ * up_releasepending.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_release_pending
+ *
+ * Description:
+ * Release and ready-to-run tasks that have
+ * collected in the pending task list. This can call a
+ * context switch if a new task is placed at the head of
+ * the ready to run list.
+ *
+ ************************************************************/
+
+void up_release_pending(void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+
+ dbg("From TCB=%p\n", rtcb);
+
+ /* Merge the g_pendingtasks list into the g_readytorun task list */
+
+ /* sched_lock(); */
+ if (sched_mergepending())
+ {
+ /* The currently active task has changed! We will need to
+ * switch contexts. First check if we are operating in
+ * interrupt context:
+ */
+
+ if (current_regs)
+ {
+ /* Yes, then we have to do things differently.
+ * Just copy the current_regs into the OLD rtcb.
+ */
+
+ up_copystate(rtcb->xcp.regs, current_regs);
+
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* Then switch contexts */
+
+ up_copystate(current_regs, rtcb->xcp.regs);
+ }
+
+ /* Copy the exception context into the TCB of the task that
+ * was currently active. if up_saveusercontext returns a non-zero
+ * value, then this is really the previously running task
+ * restarting!
+ */
+
+ else if (!up_saveusercontext(rtcb->xcp.regs))
+ {
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* Then switch contexts */
+
+ up_fullcontextrestore(rtcb->xcp.regs);
+ }
+ }
+}
diff --git a/nuttx/arch/c5471/src/up_releasestack.c b/nuttx/arch/c5471/src/up_releasestack.c
new file mode 100644
index 0000000000..e300c93bf5
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_releasestack.c
@@ -0,0 +1,78 @@
+/************************************************************
+ * up_releasestack.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Types
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_release_stack
+ *
+ * Description:
+ * A task has been stopped. Free all stack
+ * related resources retained int the defunct TCB.
+ *
+ ************************************************************/
+
+void up_release_stack(_TCB *dtcb)
+{
+ if (dtcb->stack_alloc_ptr)
+ {
+ sched_free(dtcb->stack_alloc_ptr);
+ dtcb->stack_alloc_ptr = NULL;
+ }
+
+ dtcb->adj_stack_size = 0;
+}
diff --git a/nuttx/arch/c5471/src/up_reprioritizertr.c b/nuttx/arch/c5471/src/up_reprioritizertr.c
new file mode 100644
index 0000000000..414b410d06
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_reprioritizertr.c
@@ -0,0 +1,178 @@
+/************************************************************
+ * up_reprioritizertr.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_reprioritize_rtr
+ *
+ * Description:
+ * Called when the priority of a running or
+ * ready-to-run task changes and the reprioritization will
+ * cause a context switch. Two cases:
+ *
+ * 1) The priority of the currently running task drops and the next
+ * task in the ready to run list has priority.
+ * 2) An idle, ready to run task's priority has been raised above the
+ * the priority of the current, running task and it now has the
+ * priority.
+ *
+ * Inputs:
+ * tcb: The TCB of the task that has been reprioritized
+ * priority: The new task priority
+ *
+ ************************************************************/
+
+void up_reprioritize_rtr(_TCB *tcb, ubyte priority)
+{
+ /* Verify that the caller is sane */
+
+ if (tcb->task_state < FIRST_READY_TO_RUN_STATE ||
+ tcb->task_state > LAST_READY_TO_RUN_STATE ||
+ priority < SCHED_PRIORITY_MIN ||
+ priority > SCHED_PRIORITY_MAX)
+ {
+ PANIC(OSERR_BADREPRIORITIZESTATE);
+ }
+ else
+ {
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ boolean switch_needed;
+
+ dbg("TCB=%p PRI=%d\n", tcb, priority);
+
+ /* Remove the tcb task from the ready-to-run list.
+ * sched_removereadytorun will return TRUE if we just
+ * remove the head of the ready to run list.
+ */
+
+ switch_needed = sched_removereadytorun(tcb);
+
+ /* Setup up the new task priority */
+
+ tcb->sched_priority = (ubyte)priority;
+
+ /* Return the task to the specified blocked task list.
+ * sched_addreadytorun will return TRUE if the task was
+ * added to the new list. We will need to perform a context
+ * switch only if the EXCLUSIVE or of the two calls is non-zero
+ * (i.e., one and only one the calls changes the head of the
+ * ready-to-run list).
+ */
+
+ switch_needed ^= sched_addreadytorun(tcb);
+
+ /* Now, perform the context switch if one is needed */
+
+ if (switch_needed)
+ {
+ /* If we are going to do a context switch, then now is the right
+ * time to add any pending tasks back into the ready-to-run list.
+ * task list now
+ */
+
+ if (g_pendingtasks.head)
+ {
+ sched_mergepending();
+ }
+
+ /* Are we in an interrupt handler? */
+
+ if (current_regs)
+ {
+ /* Yes, then we have to do things differently.
+ * Just copy the current_regs into the OLD rtcb.
+ */
+
+ up_copystate(rtcb->xcp.regs, current_regs);
+
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* Then switch contexts */
+
+ up_copystate(current_regs, rtcb->xcp.regs);
+ }
+ /* Copy the exception context into the TCB at the (old) head of the
+ * g_readytorun Task list. if up_saveusercontext returns a non-zero
+ * value, then this is really the previously running task restarting!
+ */
+
+ if (!up_saveusercontext(rtcb->xcp.regs))
+ {
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* Then switch contexts */
+
+ up_fullcontextrestore(rtcb->xcp.regs);
+ }
+ }
+ }
+}
diff --git a/nuttx/arch/c5471/src/up_saveusercontext.S b/nuttx/arch/c5471/src/up_saveusercontext.S
new file mode 100644
index 0000000000..b9a9c2501d
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_saveusercontext.S
@@ -0,0 +1,121 @@
+/**************************************************************************
+ * up_saveusercontext.S
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **************************************************************************/
+
+/**************************************************************************
+ * Included Files
+ **************************************************************************/
+
+#include <nuttx/irq.h>
+#include "up_internal.h"
+
+/**************************************************************************
+ * Private Definitions
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Types
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Function Prototypes
+ **************************************************************************/
+
+/**************************************************************************
+ * Global Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Functions
+ **************************************************************************/
+
+/**************************************************************************
+ * Public Functions
+ **************************************************************************/
+
+/**************************************************************************
+ * Name: up_saveusercontext
+ **************************************************************************/
+
+ .text
+ .globl up_saveusercontext
+ .type up_saveusercontext, function
+up_saveusercontext:
+ /* On entry, a1 (r0) holds address of struct xcptcontext.
+ * Offset to the user region.
+ */
+
+ /* Make sure that the return value will be non-zero (the
+ * value of the other volatile registers don't matter --
+ * r1-r3, ip). This function is called throught the
+ * noraml C calling conventions and the values of these
+ * registers cannot be assumed at the point of setjmp
+ * return.
+ */
+
+ mov ip, #1
+ str ip, [r0, #JB_R0]
+
+ /* Get the offset to the user save area */
+
+ add r0, r0, #XCPTCONTEXT_UOFFSET
+
+ /* Get the current cpsr as well */
+
+ mrs r3, cpsr /* R3 = CPSR value */
+
+ /* We need to save:
+ *
+ * Volatile register: r3 (holds the cpsr value)
+ * Static registers: v1-v7 (aka r4-r10)
+ * Frame pointer: fp (aka r11)
+ * Stack pointer: sp (aka r13)
+ * Return address: lr (aka r14)
+ *
+ * These have to be save in the same order as is done
+ * by the interrupt handling logic.
+ */
+
+ stmia r0, {r3-r11, r13-r14}
+
+ /* Return 0 */
+
+ mov r0, #0 /* Return value == 0 */
+ mov pc, lr /* Return */
+ .size up_saveusercontext, . - up_saveusercontext
+
diff --git a/nuttx/arch/c5471/src/up_schedulesigaction.c b/nuttx/arch/c5471/src/up_schedulesigaction.c
new file mode 100644
index 0000000000..7fa92e49ff
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_schedulesigaction.c
@@ -0,0 +1,186 @@
+/************************************************************
+ * up_schedulesigaction.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+#include "c5471.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_schedule_sigaction
+ *
+ * Description:
+ * This function is called by the OS when one or more
+ * signal handling actions have been queued for execution.
+ * The architecture specific code must configure things so
+ * that the 'igdeliver' callback is executed on the thread
+ * specified by 'tcb' as soon as possible.
+ *
+ * This function may be called from interrupt handling logic.
+ *
+ * This operation should not cause the task to be unblocked
+ * nor should it cause any immediate execution of sigdeliver.
+ * Typically, a few cases need to be considered:
+ *
+ * (1) This function may be called from an interrupt handler
+ * During interrupt processing, all xcptcontext structures
+ * should be valid for all tasks. That structure should
+ * be modified to invoke sigdeliver() either on return
+ * from (this) interrupt or on some subsequent context
+ * switch to the recipient task.
+ * (2) If not in an interrupt handler and the tcb is NOT
+ * the currently executing task, then again just modify
+ * the saved xcptcontext structure for the recipient
+ * task so it will invoke sigdeliver when that task is
+ * later resumed.
+ * (3) If not in an interrupt handler and the tcb IS the
+ * currently executing task -- just call the signal
+ * handler now.
+ *
+ ************************************************************/
+
+void up_schedule_sigaction(_TCB *tcb, sig_deliver_t sigdeliver)
+{
+ /* Refuse to handle nested signal actions */
+
+ if (!tcb->xcp.sigdeliver)
+ {
+ uint32 flags;
+
+ /* Make sure that interrupts are disabled */
+
+ flags = irqsave();
+
+ /* First, handle some special cases when the signal is
+ * being delivered to the currently executing task.
+ */
+
+ if (tcb == (_TCB*)g_readytorun.head)
+ {
+ /* CASE 1: We are not in an interrupt handler and
+ * a task is signalling itself for some reason.
+ */
+
+ if (!current_regs)
+ {
+ /* In this case just deliver the signal now. */
+
+ sigdeliver(tcb);
+ }
+
+ /* CASE 2: We are in an interrupt handler AND the
+ * interrupted task is the same as the one that
+ * must receive the signal, then we will have to modify
+ * the return state as well as the state in the TCB.
+ */
+
+ else
+ {
+ /* Save the return lr and cpsr and one scratch register
+ * These will be restored by the signal trampoline after
+ * the signals have been delivered.
+ */
+
+ tcb->xcp.sigdeliver = sigdeliver;
+ tcb->xcp.saved_lr = current_regs[JB_LR];
+ tcb->xcp.saved_cpsr = current_regs[JB_CPSR];
+
+ /* Then set up to vector to the trampoline with interrupts
+ * disabled
+ */
+
+ current_regs[JB_LR] = (uint32)up_sigdeliver;
+ current_regs[JB_CPSR] = SVC_MODE | I_BIT | F_BIT;
+
+ /* And make sure that the saved context in the TCB
+ * is the same as the interrupt return context.
+ */
+
+ up_copystate(tcb->xcp.regs, current_regs);
+ }
+ }
+
+ /* Otherwise, we are (1) signaling a task is not running
+ * from an interrupt handler or (2) we are not in an
+ * interrupt handler and the running task is signalling
+ * some non-running task.
+ */
+
+ else
+ {
+ /* Save the return lr and cpsr and one scratch register
+ * These will be restored by the signal trampoline after
+ * the signals have been delivered.
+ */
+
+ tcb->xcp.sigdeliver = sigdeliver;
+ tcb->xcp.saved_lr = tcb->xcp.regs[JB_LR];
+ tcb->xcp.saved_cpsr = tcb->xcp.regs[JB_CPSR];
+
+ /* Then set up to vector to the trampoline with interrupts
+ * disabled
+ */
+
+ tcb->xcp.regs[JB_LR] = (uint32)up_sigdeliver;
+ tcb->xcp.regs[JB_CPSR] = SVC_MODE | I_BIT | F_BIT;
+ }
+
+ irqrestore(flags);
+ }
+}
diff --git a/nuttx/arch/c5471/src/up_sigdeliver.c b/nuttx/arch/c5471/src/up_sigdeliver.c
new file mode 100644
index 0000000000..00b38a9dd4
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_sigdeliver.c
@@ -0,0 +1,117 @@
+/************************************************************
+ * up_sigdeliver.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+#include "c5471.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_sigdeliver
+ *
+ * Description:
+ * This is the a signal handling trampoline. When a
+ * signal action was posted. The task context was mucked
+ * with and forced to branch to this location with interrupts
+ * disabled.
+ *
+ ************************************************************/
+
+void up_sigdeliver(void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ uint32 regs[XCPTCONTEST_REGS];
+ sig_deliver_t sigdeliver;
+
+ ASSERT(rtcb->xcp.sigdeliver);
+
+ /* Save the real return state on the stack. */
+
+ up_copystate(regs, rtcb->xcp.regs);
+ regs[JB_LR] = rtcb->xcp.saved_lr;
+ regs[JB_CPSR] = rtcb->xcp.saved_cpsr;
+
+ /* Get a local copy of the sigdeliver function pointer.
+ * we do this so that we can nullify the sigdeliver
+ * function point in the TCB and accept more signal
+ * deliveries while processing the current pending
+ * signals.
+ */
+
+ sigdeliver = rtcb->xcp.sigdeliver;
+ rtcb->xcp.sigdeliver = NULL;
+
+ /* Then enable interrupts. We should still be safe from
+ * any further signal handling actions until we also
+ * nullify tcb->xcp.sigdeliver.
+ */
+
+ irqrestore(SVC_MODE | F_BIT);
+
+ /* Deliver the signals */
+
+ sigdeliver(rtcb);
+
+ /* Then restore the correct state for this thread of
+ * execution.
+ */
+
+ up_fullcontextrestore(regs);
+}
diff --git a/nuttx/arch/c5471/src/up_syscall.c b/nuttx/arch/c5471/src/up_syscall.c
new file mode 100644
index 0000000000..be4cee7459
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_syscall.c
@@ -0,0 +1,82 @@
+/************************************************************
+ * up_syscall.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include "c5471.h"
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * vectors
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_syscall
+ *
+ * Description:
+ * SWI interrupts will vection here with insn=the SWI
+ * instruction and xcp=the interrupt context
+ *
+ * The handler may get the SWI number be de-referencing
+ * the return address saved in the xcp and decoding
+ * the SWI instruction
+ *
+ ************************************************************/
+
+void up_syscall(uint32 *regs)
+{
+ PANIC(OSERR_ERREXCEPTION);
+}
diff --git a/nuttx/arch/c5471/src/up_timerisr.c b/nuttx/arch/c5471/src/up_timerisr.c
new file mode 100644
index 0000000000..b636d10844
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_timerisr.c
@@ -0,0 +1,93 @@
+/************************************************************
+ * up_timerisr.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Types
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: timer_isr
+ *
+ * Description:
+ * The timer ISR will perform a variety of services for
+ * various portions of the systems.
+ *
+ ************************************************************/
+
+int up_timerisr(int irq, uint32 *regs)
+{
+ uint32 *saved_regs;
+
+ /* Save the pointer to the interrupted context (exercising some
+ * logic for the unexpected case of nested interrupts).
+ */
+
+ if (!current_regs)
+ {
+ saved_regs = NULL;
+ current_regs = regs;
+ }
+ else
+ {
+ saved_regs = current_regs;
+ }
+
+ /* Process timer interrupt */
+
+ sched_process_timer();
+
+ /* Restore the previous context */
+
+ current_regs = saved_regs;
+ return 0;
+}
diff --git a/nuttx/arch/c5471/src/up_unblocktask.c b/nuttx/arch/c5471/src/up_unblocktask.c
new file mode 100644
index 0000000000..98cc042baa
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_unblocktask.c
@@ -0,0 +1,160 @@
+/************************************************************
+ * up_unblocktask.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_unblock_task
+ *
+ * Description:
+ * A task is currently in an inactive task list
+ * but has been prepped to execute. Move the TCB to the
+ * ready-to-run list, restore its context, and start execution.
+ *
+ * Inputs:
+ * tcb: Refers to the tcb to be unblocked. This tcb is
+ * in one of the waiting tasks lists. It must be moved to
+ * the ready-to-run list and, if it is the highest priority
+ * ready to run taks, executed.
+ *
+ ************************************************************/
+
+void up_unblock_task(_TCB *tcb)
+{
+ /* Verify that the context switch can be performed */
+ if ((tcb->task_state < FIRST_BLOCKED_STATE) ||
+ (tcb->task_state > LAST_BLOCKED_STATE))
+ {
+ PANIC(OSERR_BADUNBLOCKSTATE);
+ }
+ else
+ {
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+
+ dbg("Unblocking TCB=%p\n", tcb);
+
+ /* Remove the task from the blocked task list */
+
+ sched_removeblocked(tcb);
+
+ /* Reset its timeslice. This is only meaningful for round
+ * robin tasks but it doesn't here to do it for everything
+ */
+
+#if CONFIG_RR_INTERVAL > 0
+ tcb->timeslice = CONFIG_RR_INTERVAL;
+#endif
+
+ /* Add the task in the correct location in the prioritized
+ * g_readytorun task list
+ */
+
+ if (sched_addreadytorun(tcb))
+ {
+ /* The currently active task has changed! We need to do
+ * a context switch to the new task.
+ *
+ * Are we in an interrupt handler?
+ */
+
+ if (current_regs)
+ {
+ /* Yes, then we have to do things differently.
+ * Just copy the current_regs into the OLD rtcb.
+ */
+
+ up_copystate(rtcb->xcp.regs, current_regs);
+
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* Then switch contexts */
+
+ up_copystate(current_regs, rtcb->xcp.regs);
+ }
+
+ /* We are not in an interrupt andler. Copy the user C context
+ * into the TCB of the task that was previously active. if
+ * up_saveusercontext returns a non-zero value, then this is really the
+ * previously running task restarting!
+ */
+
+ else if (!up_saveusercontext(rtcb->xcp.regs))
+ {
+ /* Restore the exception context of the new task that is ready to
+ * run (probably tcb). This is the new rtcb at the head of the
+ * g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* Then switch contexts */
+
+ up_fullcontextrestore(rtcb->xcp.regs);
+ }
+ }
+ }
+}
diff --git a/nuttx/arch/c5471/src/up_undefinedinsn.c b/nuttx/arch/c5471/src/up_undefinedinsn.c
new file mode 100644
index 0000000000..3c213eb31c
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_undefinedinsn.c
@@ -0,0 +1,68 @@
+/************************************************************
+ * up_undefinedinsn.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_undefinedinsn
+ ************************************************************/
+
+void up_undefinedinsn(uint32 *regs)
+{
+ PANIC(OSERR_UNDEFINEDINSN);
+}
diff --git a/nuttx/arch/c5471/src/up_usestack.c b/nuttx/arch/c5471/src/up_usestack.c
new file mode 100644
index 0000000000..4cab9209fc
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_usestack.c
@@ -0,0 +1,118 @@
+/************************************************************
+ * up_usestack.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Types
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_use_stack
+ *
+ * Description:
+ * Setup up stack-related information in the TCB
+ * using pre-allocated stack memory
+ *
+ * The following TCB fields must be initialized:
+ * adj_stack_size: Stack size after adjustment for hardware,
+ * processor, etc. This value is retained only for debug
+ * purposes.
+ * stack_alloc_ptr: Pointer to allocated stack
+ * adj_stack_ptr: Adjusted StatckAllocPtr for HW. The
+ * initial value of the stack pointer.
+ *
+ * Inputs:
+ * tcb: The TCB of new task
+ * stack_size: The allocated stack size.
+ *
+ ************************************************************/
+
+STATUS up_use_stack(_TCB *tcb, uint32 *stack, uint32 stack_size)
+{
+ uint32 top_of_stack;
+ uint32 size_of_stack;
+
+ if (tcb->stack_alloc_ptr)
+ {
+ sched_free(tcb->stack_alloc_ptr);
+ }
+
+ /* Save the stack allocation */
+
+ tcb->stack_alloc_ptr = stack;
+
+ /* The Arm7Tdmi uses a push-down stack: the stack grows
+ * toward loweraddresses in memory. The stack pointer
+ * register, points to the lowest, valid work address
+ * (the "top" of the stack). Items on the stack are
+ * referenced as positive word offsets from sp.
+ */
+
+ top_of_stack = (uint32)tcb->stack_alloc_ptr + stack_size - 4;
+
+ /* The Arm7Tdmi stack must be aligned at word (4 byte)
+ * boundaries. If necessary top_of_stack must be rounded
+ * down to the next boundary
+ */
+
+ top_of_stack &= ~3;
+ size_of_stack = top_of_stack - (uint32)tcb->stack_alloc_ptr + 4;
+
+ /* Save the adjusted stack values in the _TCB */
+
+ tcb->adj_stack_size = top_of_stack;
+ tcb->adj_stack_size = size_of_stack;
+
+ return OK;
+}
diff --git a/nuttx/arch/c5471/src/up_vectors.S b/nuttx/arch/c5471/src/up_vectors.S
new file mode 100644
index 0000000000..f1c8467504
--- /dev/null
+++ b/nuttx/arch/c5471/src/up_vectors.S
@@ -0,0 +1,449 @@
+/************************************************************
+ * up_vectors.S
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/irq.h>
+#include "c5471.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Data
+ ************************************************************/
+
+ .data
+up_irqtmp:
+ .word 0 /* Saved lr */
+ .word 0 /* Saved spsr */
+up_undeftmp:
+ .word 0 /* Saved lr */
+ .word 0 /* Saved spsr */
+up_aborttmp:
+ .word 0 /* Saved lr */
+ .word 0 /* Saved spsr */
+
+/************************************************************
+ * Macros
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+ .text
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+ .text
+
+/************************************************************
+ * Name: up_vectorirq
+ *
+ * Description:
+ * Interrupt excetpion. Entered in IRQ mode with spsr = SVC
+ * CPSR, lr = SVC PC
+ ************************************************************/
+
+ .global up_vectorirq
+ .type up_vectorirq, %function
+up_vectorirq:
+ /* On entry, we are in IRQ mode. We are free to use
+ * the IRQ mode r13 and r14.
+ *
+ */
+
+ ldr r13, .Lirqtmp
+ sub lr, lr, #4
+ str lr, [r13] @ save lr_IRQ
+ mrs lr, spsr
+ str lr, [r13, #4] @ save spsr_IRQ
+
+ /* Then switch back to SVC mode */
+
+ bic lr, lr, #MODE_MASK /* Keep F and T bits */
+ orr lr, lr, #I_BIT | SVC_MODE
+ msr spsr_c, lr /* Swith to SVC mode */
+
+ /* Create a context structure */
+
+ sub sp, sp, #XCPTCONTEXT_SIZE
+ stmia sp, {r0-r3, r12} /* Save volatile regs */
+ ldr r0, .Lirqtmp /* Points to temp storage */
+ ldr lr, [r0] /* Recover lr */
+ ldr r3, [r0, $4] /* Recover SPSR */
+ add r1, sp, #XCPTCONTEXT_UOFFSET
+ stmia r1, {r3-r11, r13-r14} /* Save SPSR+r4-r11+lr+sp */
+
+ /* Now decode the interrupt */
+
+#if 0
+ ldr lr, =SRC_IRQ_BIN_REG /* Fetch encoded IRQ */
+ ldr r0, [lr]
+ and r0, r0, #0x0f /* Valid range is 0..15 */
+
+ /* Problems here... cannot read SRC_IRQ_BIN_REQ (and/or
+ * SRC_IRQ_REQ because this will clear edge triggered
+ * interrupts. Plus, no way to validate spurious
+ * interrupt.
+ */
+#else
+ ldr r6, =SRC_IRQ_REG
+ ldr r6, [r6] /* Get source IRQ reg */
+ mov r0, #0 /* Assume IRQ0_IRQ set */
+.Lmorebits:
+ tst r6, #1 /* Is IRQ set? */
+ bne .Lhaveirq /* Yes... we have the IRQ */
+ add r0, r0, #1 /* Setup next IRQ */
+ mov r6, r6, lsr #1 /* Shift right one */
+ cmp r0, #16 /* Only 16 valid bits */
+ bcc .Lmorebits /* Keep until we have looked
+ * at all bits */
+ b .Lnoirqset /* If we get here, there is
+ * no pending interrupt */
+.Lhaveirq:
+#endif
+ /* Then call the data abort handler with interrupt disabled.
+ * rq_dispatch(int irq, struct xcptcontext *xcp)
+ */
+
+ mov fp, #0 /* Init frame pointer */
+ mov r1, sp /* Get r1=xcp */
+ bl up_prefetchabort /* Call the handler */
+
+ /* Recover the SVC_MODE registers */
+.Lnoirqset:
+ add r0, sp, #XCPTCONTEXT_UOFFSET
+ ldmia r0, {r3-r11, r13-r14}
+ msr spsr, r3
+ ldmia sp, {r0-r3, r12} /* recover volatile regs */
+ add sp, sp, #XCPTCONTEXT_SIZE
+ movs pc, lr /* return & move spsr into cpsr */
+
+ @
+ @ now branch to the relevent MODE handling routine
+ @
+
+ and lr, lr, #15
+ ldr lr, [pc, lr, lsl #2]
+ movs pc, lr @ Changes mode and branches
+
+.Lirqtmp:
+ .word up_irqtmp
+
+ .align 5
+
+/************************************************************
+ * Function: up_vectorswi
+ *
+ * Description:
+ * SWI interrupt. We enter the SWI in SVC mode
+ ************************************************************/
+
+ .align 5
+ .global up_vectorswi
+ .type up_vectorswi, %function
+up_vectorswi:
+
+ /* The c547x rrload bootloader intemediates all
+ * interrupts. For the* case of the SWI, it mucked
+ * with the stack to create some temporary registers.
+ * We'll have to recover from this mucking here.
+ */
+
+ ldr r14, [sp,#-0x4] /* rrload workaround */
+
+ /* Create a context structure */
+
+ sub sp, sp, #XCPTCONTEXT_SIZE
+ stmia sp, {r0-r3, r12} /* Save volatile regs */
+ mrs r3, spsr /* Get r3=interrupted CPSR */
+ add r0, sp, #XCPTCONTEXT_UOFFSET
+ stmia r0, {r3-r11, r13-r14} /* Save CPSR+r4-r11+lr+sp */
+
+ /* Then call the SWI handler with interrupt disabled.
+ * void up_syscall(struct xcptcontext *xcp)
+ */
+
+ mov fp, #0 /* Init frame pointer */
+ mov r0, sp /* Get r0=xcp */
+ bl up_syscall /* Call the handler */
+
+.LignoreSWI:
+ /* Recover the SVC_MODE registers */
+
+ add r0, sp, #XCPTCONTEXT_UOFFSET
+ ldmia r0, {r3-r11, r13-r14}
+ msr spsr, r3
+ ldmia sp, {r0-r3, r12} /* recover volatile regs */
+ add sp, sp, #XCPTCONTEXT_SIZE
+ movs pc, lr /* return & move spsr into cpsr */
+
+/************************************************************
+ * Name: up_vectordata
+ *
+ * Description:
+ * Data abort Exception dispatcher. Give control to data
+ * abort handler. This function is entered in ABORT mode
+ * with spsr = SVC CPSR, lr = SVC PC
+ *
+ ************************************************************/
+
+ .text
+ .global up_vectordata
+ .type up_vectordata, %function
+up_vectordata:
+ /* On entry we are free to use the ABORT mode registers
+ * r13 and r14
+ */
+
+ ldr r13, .Ldaborttmp /* Points to temp storage */
+ sub lr, lr, #8 /* Fixup return */
+ str lr, [r13] /* Save in temp storage */
+ mrs lr, spsr /* Get SPSR */
+ str lr, [r13, #4] /* Save in temp storage */
+
+ /* Then switch back to SVC mode */
+
+ bic lr, lr, #MODE_MASK /* Keep F and T bits */
+ orr lr, lr, #I_BIT | SVC_MODE
+ msr spsr_c, lr /* Swith to SVC mode */
+
+ /* Create a context structure */
+
+ sub sp, sp, #XCPTCONTEXT_SIZE
+ stmia sp, {r0-r3, r12} /* Save volatile regs */
+ ldr r0, .Ldaborttmp /* Points to temp storage */
+ ldr lr, [r0] /* Recover lr */
+ ldr r3, [r0, $4] /* Recover SPSR */
+ add r1, sp, #XCPTCONTEXT_UOFFSET
+ stmia r1, {r3-r11, r13-r14} /* Save SPSR+r4-r11+lr+sp */
+
+ /* Then call the data abort handler with interrupt disabled.
+ * void up_dataabort(struct xcptcontext *xcp)
+ */
+
+ mov fp, #0 /* Init frame pointer */
+ mov r0, sp /* Get r0=xcp */
+ bl up_dataabort /* Call the handler */
+
+ /* Recover the SVC_MODE registers */
+
+ add r0, sp, #XCPTCONTEXT_UOFFSET
+ ldmia r0, {r3-r11, r13-r14}
+ msr spsr, r3
+ ldmia sp, {r0-r3, r12} /* recover volatile regs */
+ add sp, sp, #XCPTCONTEXT_SIZE
+ movs pc, lr /* return & move spsr into cpsr */
+
+ @
+ @ now branch to the relevent MODE handling routine
+ @
+
+ and lr, lr, #15
+ ldr lr, [pc, lr, lsl #2]
+ movs pc, lr @ Changes mode and branches
+
+.Ldaborttmp:
+ .word up_aborttmp
+
+ .align 5
+
+/************************************************************
+ * Name: up_vectorprefetch
+ *
+ * Description:
+ * Prefetch abort exception. Entered in ABT mode with
+ * spsr = SVC CPSR, lr = SVC PC
+ ************************************************************/
+
+ .global up_vectorprefetch
+ .type up_vectorprefetch, %function
+up_vectorprefetch:
+ /* On entry we are free to use the ABORT mode registers
+ * r13 and r14
+ */
+
+ ldr r13, .Lpaborttmp /* Points to temp storage */
+ sub lr, lr, #4 /* Fixup return */
+ str lr, [r13] /* Save in temp storage */
+ mrs lr, spsr /* Get SPSR */
+ str lr, [r13, #4] /* Save in temp storage */
+
+ /* Then switch back to SVC mode */
+
+ bic lr, lr, #MODE_MASK /* Keep F and T bits */
+ orr lr, lr, #I_BIT | SVC_MODE
+ msr spsr_c, lr /* Swith to SVC mode */
+
+ /* Create a context structure */
+
+ sub sp, sp, #XCPTCONTEXT_SIZE
+ stmia sp, {r0-r3, r12} /* Save volatile regs */
+ ldr r0, .Lpaborttmp /* Points to temp storage */
+ ldr lr, [r0] /* Recover lr */
+ ldr r3, [r0, $4] /* Recover SPSR */
+ add r1, sp, #XCPTCONTEXT_UOFFSET
+ stmia r1, {r3-r11, r13-r14} /* Save SPSR+r4-r11+lr+sp */
+
+ /* Then call the data abort handler with interrupt disabled.
+ * void up_prefetchabort(struct xcptcontext *xcp)
+ */
+
+ mov fp, #0 /* Init frame pointer */
+ mov r0, sp /* Get r0=xcp */
+ bl up_prefetchabort /* Call the handler */
+
+ /* Recover the SVC_MODE registers */
+
+ add r0, sp, #XCPTCONTEXT_UOFFSET
+ ldmia r0, {r3-r11, r13-r14}
+ msr spsr, r3
+ ldmia sp, {r0-r3, r12} /* recover volatile regs */
+ add sp, sp, #XCPTCONTEXT_SIZE
+ movs pc, lr /* return & move spsr into cpsr */
+
+ @
+ @ now branch to the relevent MODE handling routine
+ @
+
+ and lr, lr, #15
+ ldr lr, [pc, lr, lsl #2]
+ movs pc, lr @ Changes mode and branches
+
+.Lpaborttmp:
+ .word up_aborttmp
+
+ .align 5
+
+/************************************************************
+ * Name: up_vectorundefinsn
+ *
+ * Description:
+ * Undefined instruction entry exception. Entered in
+ * UND mode, spsr = SVC CPSR, lr = SVC PC
+ *
+ ************************************************************/
+
+ .global up_vectorundefinsn
+ .type up_vectorundefinsn, %function
+up_vectorundefinsn:
+ /* On entry we are free to use the UND mode registers
+ * r13 and r14
+ */
+
+ ldr r13, .Lundeftmp /* Points to temp storage */
+ str lr, [r13] /* Save in temp storage */
+ mrs lr, spsr /* Get SPSR */
+ str lr, [r13, #4] /* Save in temp storage */
+
+ /* Then switch back to SVC mode */
+
+ bic lr, lr, #MODE_MASK /* Keep F and T bits */
+ orr lr, lr, #I_BIT | SVC_MODE
+ msr spsr_c, lr /* Swith to SVC mode */
+
+ /* Create a context structure */
+
+ sub sp, sp, #XCPTCONTEXT_SIZE
+ stmia sp, {r0-r3, r12} /* Save volatile regs */
+ ldr r0, .Lundeftmp /* Points to temp storage */
+ ldr lr, [r0] /* Recover lr */
+ ldr r3, [r0, $4] /* Recover SPSR */
+ add r1, sp, #XCPTCONTEXT_UOFFSET
+ stmia r1, {r3-r11, r13-r14} /* Save SPSR+r4-r11+lr+sp */
+
+ /* Then call the data abort handler with interrupt disabled.
+ * void up_undefinedinsn(struct xcptcontext *xcp)
+ */
+
+ mov fp, #0 /* Init frame pointer */
+ mov r0, sp /* Get r0=xcp */
+ bl up_undefinedinsn /* Call the handler */
+
+ /* Recover the SVC_MODE registers */
+
+ add r0, sp, #XCPTCONTEXT_UOFFSET
+ ldmia r0, {r3-r11, r13-r14}
+ msr spsr, r3
+ ldmia sp, {r0-r3, r12} /* recover volatile regs */
+ add sp, sp, #XCPTCONTEXT_SIZE
+ movs pc, lr /* return & move spsr into cpsr */
+
+ @
+ @ now branch to the relevent MODE handling routine
+ @
+
+ and lr, lr, #15
+ ldr lr, [pc, lr, lsl #2]
+ movs pc, lr @ Changes mode and branches
+
+.Lundeftmp:
+ .word up_undeftmp
+
+ .align 5
+
+/************************************************************
+ * Name: up_vectorfiq
+ *
+ * Description:
+ * Shouldn't happen
+ ************************************************************/
+
+ .global up_vectorfiq
+ .type up_vectorfiq, %function
+up_vectorfiq:
+ subs pc, lr, #4
+
+/************************************************************
+ * Name: up_vectoraddrexcption
+ *
+ * Description:
+ * Shouldn't happen
+ *
+ ************************************************************/
+
+ .global up_vectoraddrexcptn
+ .type up_vectoraddrexcptn, %function
+up_vectoraddrexcptn:
+ b up_vectoraddrexcptn
+ .end
diff --git a/nuttx/arch/sim/Make.defs b/nuttx/arch/sim/Make.defs
new file mode 100644
index 0000000000..12e288a573
--- /dev/null
+++ b/nuttx/arch/sim/Make.defs
@@ -0,0 +1,69 @@
+############################################################
+# Make.defs
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+include ${TOPDIR}/.config
+
+ifeq ("${CONFIG_DEBUG}","y")
+ ARCHOPTIMIZATION = -g
+else
+ ARCHOPTIMIZATION = -O2
+endif
+
+ARCHCPUFLAGS =
+ARCHPICFLAGS = -fpic
+ARCHWARNINGS = -Wall -Wstrict-prototypes -Wshadow
+ARCHDEFINES =
+ARCHINCLUDES = -I. -isystem $(TOPDIR)/include
+ARCHSCRIPT =
+
+CROSSDEV =
+CC = $(CROSSDEV)gcc
+LD = $(CROSSDEV)ld
+AR = $(CROSSDEV)ar
+NM = $(CROSSDEV)nm
+OBJCOPY = $(CROSSDEV)objcopy
+OBJDUMP = $(CROSSDEV)objdump
+
+CFLAGS = $(ARCHWARNINGS) $(ARCHOPTIMIZATION) \
+ $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) -pipe
+
+LDFLAGS = $(ARCHSCRIPT)
+EXTRA_LIBS = -lc
+
+ifeq ("${CONFIG_DEBUG}","y")
+ LDFLAGS += -g
+endif
+
+
diff --git a/nuttx/arch/sim/defconfig b/nuttx/arch/sim/defconfig
new file mode 100644
index 0000000000..60656fce38
--- /dev/null
+++ b/nuttx/arch/sim/defconfig
@@ -0,0 +1,154 @@
+############################################################
+# defconfig
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+#
+# architecture selection
+#
+# CONFIG_ARCH - identifies the arch subdirectory
+# CONFIG_ARCH_name - for use in C code
+#
+CONFIG_ARCH=sim
+CONFIG_ARCH_SIM=y
+
+#
+# General OS setup
+#
+# CONFIG_EXAMPLE - identifies the subdirectgory in examples
+# that will be used in the build
+# CONFIG_DEBUG - enables built-in debug options
+# CONFIG_DEBUG_VERBOSE - enables verbose debug output
+# CONFIG_HAVE_LOWPUTC - architecture supports low-level, boot
+# time console output
+# CONFIG_RR_INTERVAL - The round robin timeslice will be set
+# this number of milliseconds; Round robin scheduling can
+# be disabled by setting this value to zero.
+# CONFIG_SCHED_INSTRUMENTATION - enables instrumentation in
+# scheduler to monitor system performance
+# CONFIG_TASK_NAME_SIZE - Spcifies that maximum size of a
+# task name to save in the TCB. Useful if scheduler
+# instrumentation is selected. Set to zero to disable.
+# CONFIG_JULIAN_TIME - Enables Julian time conversions
+# CONFIG_START_YEAR, CONFIG_START_MONTH, CONFIG_START_DAY -
+# Used to initialize the internal time logic.
+# CONFIG_DEV_CONSOLE - Set if architecture-specific logic
+# provides /dev/console. Enables stdout, stderr, stdin.
+#
+CONFIG_EXAMPLE=ostest
+CONFIG_DEBUG=y
+CONFIG_DEBUG_VERBOSE=y
+CONFIG_ARCH_LOWPUTC=y
+CONFIG_RR_INTERVAL=200
+CONFIG_SCHED_INSTRUMENTATION=n
+CONFIG_TASK_NAME_SIZE=32
+CONFIG_START_YEAR=2007
+CONFIG_START_MONTH=2
+CONFIG_START_DAY=13
+CONFIG_JULIAN_TIME=n
+CONFIG_DEV_CONSOLE=y
+
+#
+# Allow for artchitecture optimized implementations
+#
+# The architecture can provide optimized versions of the
+# following to improve sysem performance
+#
+CONFIG_ARCH_MEMCPY=n
+CONFIG_ARCH_MEMCMP=n
+CONFIG_ARCH_MEMMOVE=n
+CONFIG_ARCH_MEMSET=n
+CONFIG_ARCH_STRCMP=n
+CONFIG_ARCH_STRCPY=n
+CONFIG_ARCH_STRNCPY=n
+CONFIG_ARCH_STRLEN=n
+CONFIG_ARCH_BZERO=n
+CONFIG_ARCH_KMALLOC=n
+CONFIG_ARCH_KZMALLOC=n
+CONFIG_ARCH_KFREE=n
+
+# General Compile environment setup
+#
+# CONFIG_HAVE_LONG_LONG - enable if your architecture supports
+# long long types and if you plan to use them
+CONFIG_HAVE_LONG_LONG=n
+
+#
+# Sizes of configurable things (0 disables)
+#
+# CONFIG_NPTHREAD_KEYS - The number of items of thread-
+# specific data that can be retained
+# CONFIG_NFILE_DESCRIPTORS - The maximum number of file
+# descriptors (one for each open)
+# CONFIG_NFILE_STREAMS - The maximum number of streams that
+# can be fopen'ed
+# CONFIG_STDIO_BUFFER_SIZE - Size of the buffer to allocate
+# on fopen. (Only if CONFIG_NFILE_STREAMS > 0)
+# CONFIG_NUNGET_CHARS - Number of characters that can be
+# buffered by ungetc() (Only if CONFIG_NFILE_STREAMS > 0)
+# CONFIG_PREALLOC_MQ_MSGS - The number of pre-allocated message
+# structures. The system manages a pool of preallocated
+# message structures to minimize dynamic allocations
+# CONFIG_MQ_MAXMSGSIZE - Message structures are allocated with
+# a fixed payload size given by this settin (does not include
+# other message structure overhead.
+# CONFIG_PREALLOC_WDOGS - The number of pre-allocated watchdog
+# structures. The system manages a pool of preallocated
+# watchdog structures to minimize dynamic allocations
+#
+CONFIG_NPTHREAD_KEYS=4
+CONFIG_NFILE_DESCRIPTORS=32
+CONFIG_NFILE_STREAMS=16
+CONFIG_STDIO_BUFFER_SIZE=1024
+CONFIG_NUNGET_CHARS=2
+CONFIG_PREALLOC_MQ_MSGS=32
+CONFIG_MQ_MAXMSGSIZE=32
+CONFIG_PREALLOC_WDOGS=32
+
+#
+# Stack and heap information
+#
+# CONFIG_BOOT_FROM_FLASH - Some configurations support XIP
+# operation from FLASH.
+# CONFIG_STACK_POINTER - The initial stack pointer
+# CONFIG_PROC_STACK_SIZE - The size of the initial stack
+# CONFIG_PTHREAD_STACK_MIN - Minimum pthread stack size
+# CONFIG_PTHREAD_STACK_DEFAULT - Default pthread stack size
+# CONFIG_HEAP_BASE - The beginning of the heap
+# CONFIG_HEAP_SIZE - The size of the heap
+#
+CONFIG_BOOT_FROM_FLASH=n
+CONFIG_PROC_STACK_SIZE=0x00001000
+CONFIG_PTHREAD_STACK_MIN=256
+CONFIG_PTHREAD_STACK_DEFAULT=8192
+CONFIG_HEAP_BASE=
+CONFIG_HEAP_SIZE=
diff --git a/nuttx/arch/sim/include/arch.h b/nuttx/arch/sim/include/arch.h
new file mode 100644
index 0000000000..32eadcd178
--- /dev/null
+++ b/nuttx/arch/sim/include/arch.h
@@ -0,0 +1,80 @@
+/************************************************************
+ * arch.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/* This file should never be included directed but, rather,
+ * only indirectly through nuttx/arch.h
+ */
+
+#ifndef __ARCH_ARCH_H
+#define __ARCH_ARCH_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Inline functions
+ ************************************************************/
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ARCH_ARCH_H */
+
diff --git a/nuttx/arch/sim/include/irq.h b/nuttx/arch/sim/include/irq.h
new file mode 100644
index 0000000000..3ae46aa42d
--- /dev/null
+++ b/nuttx/arch/sim/include/irq.h
@@ -0,0 +1,106 @@
+/************************************************************
+ * irq.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/* This file should never be included directed but, rather,
+ * only indirectly through nuttx/irq.h
+ */
+
+#ifndef __ARCH_IRQ_H
+#define __ARCH_IRQ_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define NR_IRQS 0
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/* This struct defines the way the registers are stored */
+
+#ifndef __ASSEMBLY__
+struct xcptcontext
+{
+ void *sigdeliver; /* Actual type is sig_deliver_t */
+
+ /* Storage order: %ebx, $esi, %edi, %ebp, sp, and return PC */
+
+ int regs[6];
+};
+#endif
+
+/************************************************************
+ * Inline functions
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+static inline uint32 irqsave(void)
+{
+ return 0;
+}
+
+static inline void irqrestore(uint32 flags)
+{
+}
+#endif
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ARCH_IRQ_H */
+
diff --git a/nuttx/arch/sim/include/types.h b/nuttx/arch/sim/include/types.h
new file mode 100644
index 0000000000..39232af7d7
--- /dev/null
+++ b/nuttx/arch/sim/include/types.h
@@ -0,0 +1,70 @@
+/************************************************************
+ * types.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/* This file should never be included directed but, rather,
+ * only indirectly through sys/types.h
+ */
+
+#ifndef __ARCH_TYPES_H
+#define __ARCH_TYPES_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Type Declarations
+ ************************************************************/
+
+typedef char sbyte;
+typedef unsigned char ubyte;
+typedef unsigned char uint8;
+typedef unsigned char boolean;
+typedef short sint16;
+typedef unsigned short uint16;
+typedef int sint32;
+typedef unsigned int uint32;
+typedef long long sint64;
+typedef unsigned long long uint64;
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#endif /* __ARCH_TYPES_H */
diff --git a/nuttx/arch/sim/setenv.sh b/nuttx/arch/sim/setenv.sh
new file mode 100755
index 0000000000..d0a05bfdf9
--- /dev/null
+++ b/nuttx/arch/sim/setenv.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+# setenv.sh
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+if [ "$(basename $0)" = "setenv" ] ; then
+ echo "You must source this script, not run it!" 1>&2
+ exit 1
+fi
+
+if [ -z ${PATH_ORIG} ]; then export PATH_ORIG=${PATH}; fi
+
+#export NUTTX_BIN=
+#export PATH=${NUTTX_BIN}:/sbin:/usr/sbin:${PATH_ORIG}
+
+echo "PATH : ${PATH}"
diff --git a/nuttx/arch/sim/src/Makefile b/nuttx/arch/sim/src/Makefile
new file mode 100644
index 0000000000..780cff5968
--- /dev/null
+++ b/nuttx/arch/sim/src/Makefile
@@ -0,0 +1,76 @@
+############################################################
+# Makefile
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+-include $(TOPDIR)/Make.defs
+
+MKDEP = $(TOPDIR)/tools/mkdeps.sh
+CFLAGS += -I$(TOPDIR)/sched
+
+ASRCS = up_setjmp.S
+AOBJS = $(ASRCS:.S=.o)
+CSRCS = up_initialize.c up_idle.c up_interruptcontext.c up_initialstate.c \
+ up_createstack.c up_usestack.c up_releasestack.c \
+ up_unblocktask.c up_blocktask.c up_releasepending.c up_reprioritizertr.c \
+ up_exit.c up_schedulesigaction.c up_allocateheap.c up_devconsole.c
+COBJS = $(CSRCS:.c=.o)
+
+SRCS = $(ASRCS) $(CSRCS)
+OBJS = $(AOBJS) $(COBJS)
+
+all: up_head.o libarch.a
+
+$(AOBJS): %.o: %.S
+ $(CC) -c $(CFLAGS) -D__ASSEMBLY__ $< -o $@
+
+$(COBJS) up_head.o: %.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
+libarch.a: $(OBJS)
+ $(AR) rcs $@ $(OBJS)
+
+.depend: Makefile $(SRCS)
+ $(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
+ touch $@
+
+depend: .depend
+
+clean:
+ rm -f libarch.a *.o *~
+
+distclean: clean
+ rm -f Make.dep .depend
+
+
+-include Make.dep
diff --git a/nuttx/arch/sim/src/up_allocateheap.c b/nuttx/arch/sim/src/up_allocateheap.c
new file mode 100644
index 0000000000..b464719d41
--- /dev/null
+++ b/nuttx/arch/sim/src/up_allocateheap.c
@@ -0,0 +1,81 @@
+/************************************************************
+ * up_allocateheap.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+static ubyte sim_heap[SIM_HEAP_SIZE];
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_allocate_heap
+ *
+ * Description:
+ * The heap may be statically allocated by
+ * defining CONFIG_HEAP_BASE and CONFIG_HEAP_SIZE. If these
+ * are not defined, then this function will be called to
+ * dynamically set aside the heap region.
+ *
+ ************************************************************/
+
+void up_allocate_heap(void **heap_start, size_t *heap_size)
+{
+ *heap_start = sim_heap;
+ *heap_size = SIM_HEAP_SIZE;
+}
diff --git a/nuttx/arch/sim/src/up_blocktask.c b/nuttx/arch/sim/src/up_blocktask.c
new file mode 100644
index 0000000000..66cd893723
--- /dev/null
+++ b/nuttx/arch/sim/src/up_blocktask.c
@@ -0,0 +1,157 @@
+/************************************************************
+ * up_blocktask.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_block_task
+ *
+ * Description:
+ * The currently executing task at the head of
+ * the ready to run list must be stopped. Save its context
+ * and move it to the inactive list specified by task_state.
+ *
+ * Inputs:
+ * tcb: Refers to a task in the ready-to-run list (normally
+ * the task at the the head of the list). It most be
+ * stopped, its context saved and moved into one of the
+ * waiting task lists. It it was the task at the head
+ * of the ready-to-run list, then a context to the new
+ * ready to run task must be performed.
+ * task_state: Specifies which waiting task list should be
+ * hold the blocked task TCB.
+ *
+ ************************************************************/
+
+void up_block_task(_TCB *tcb, tstate_t task_state)
+{
+ /* Verify that the context switch can be performed */
+ if ((tcb->task_state < FIRST_READY_TO_RUN_STATE) ||
+ (tcb->task_state > LAST_READY_TO_RUN_STATE))
+ {
+ PANIC(OSERR_BADBLOCKSTATE);
+ }
+ else
+ {
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ boolean switch_needed;
+
+ dbg("Blocking TCB=%p\n", tcb);
+
+ /* Remove the tcb task from the ready-to-run list. If we
+ * are blocking the task at the head of the task list (the
+ * most likely case), then a context switch to the next
+ * ready-to-run task is needed. In this case, it should
+ * also be true that rtcb == tcb.
+ */
+
+ switch_needed = sched_removereadytorun(tcb);
+
+ /* Add the task to the specified blocked task list */
+
+ sched_addblocked(tcb, (tstate_t)task_state);
+
+ /* If there are any pending tasks, then add them to the g_readytorun
+ * task list now
+ */
+
+ if (g_pendingtasks.head)
+ {
+ switch_needed |= sched_mergepending();
+ }
+
+ /* Now, perform the context switch if one is needed */
+
+ if (switch_needed)
+ {
+ /* Copy the exception context into the TCB at the (old) head of the
+ * g_readytorun Task list. if up_setjmp returns a non-zero
+ * value, then this is really the previously running task restarting!
+ */
+
+ if (!up_setjmp(rtcb->xcp.regs))
+ {
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* The way that we handle signals in the simulation is kind of
+ * a kludge. This would be unsafe in a truly multi-threaded, interrupt
+ * driven environment.
+ */
+
+ if (rtcb->xcp.sigdeliver)
+ {
+ dbg("Delivering signals TCB=%p\n", rtcb);
+ ((sig_deliver_t)rtcb->xcp.sigdeliver)(rtcb);
+ rtcb->xcp.sigdeliver = NULL;
+ }
+
+ /* Then switch contexts */
+
+ up_longjmp(rtcb->xcp.regs, 1);
+ }
+ }
+ }
+}
diff --git a/nuttx/arch/sim/src/up_createstack.c b/nuttx/arch/sim/src/up_createstack.c
new file mode 100644
index 0000000000..0e2c63e4d1
--- /dev/null
+++ b/nuttx/arch/sim/src/up_createstack.c
@@ -0,0 +1,109 @@
+/************************************************************
+ * up_createstack.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include <nuttx/kmalloc.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_create_stack
+ *
+ * Description:
+ * Allocate a stack for a new thread and setup
+ * up stack-related information in the TCB.
+ *
+ * The following TCB fields must be initialized:
+ * adj_stack_size: Stack size after adjustment for hardware,
+ * processor, etc. This value is retained only for debug
+ * purposes.
+ * stack_alloc_ptr: Pointer to allocated stack
+ * adj_stack_ptr: Adjusted StatckAllocPtr for HW. The
+ * initial value of the stack pointer.
+ *
+ * Inputs:
+ * tcb: The TCB of new task
+ * stack_size: The requested stack size. At least this much
+ * must be allocated.
+ *
+ ************************************************************/
+
+STATUS up_create_stack(_TCB *tcb, uint32 stack_size)
+{
+ STATUS ret = ERROR;
+
+ /* Move up to next even word boundary if necessary */
+
+ uint32 adj_stack_size = (stack_size + 3) & ~3;
+ uint32 adj_stack_words = adj_stack_size >> 2;
+
+ /* Allocate the memory for the stack */
+
+ uint32 *stack_alloc_ptr = (uint32*)kmalloc(adj_stack_size);
+ if (stack_alloc_ptr)
+ {
+ /* This is the address of the last word in the allocation */
+
+ uint32 *adj_stack_ptr = &stack_alloc_ptr[adj_stack_words - 1];
+
+ /* Save the values in the TCB */
+ tcb->adj_stack_size = adj_stack_size;
+ tcb->stack_alloc_ptr = stack_alloc_ptr;
+ tcb->adj_stack_ptr = adj_stack_ptr;
+ ret = OK;
+ }
+ return ret;
+}
diff --git a/nuttx/arch/sim/src/up_devconsole.c b/nuttx/arch/sim/src/up_devconsole.c
new file mode 100644
index 0000000000..b55df53dff
--- /dev/null
+++ b/nuttx/arch/sim/src/up_devconsole.c
@@ -0,0 +1,136 @@
+/************************************************************
+ * up_devconsole.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+
+#include <stdio.h>
+#include <errno.h>
+
+#include <nuttx/fs.h>
+
+#include "up_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define READ 3
+#define WRITE 4
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+static ssize_t devconsole_read(struct file *, char *, size_t);
+static ssize_t devconsole_write(struct file *, const char *, size_t);
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+static struct file_operations devconsole_fops =
+{
+ .read = devconsole_read,
+ .write = devconsole_write,
+};
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static inline int up_read(int fd, void* buf, size_t count)
+{
+ uint32 result;
+
+ __asm__ volatile ("int $0x80" \
+ : "=a" (result) \
+ : "0" (READ), "b" ((uint32)(fd)), "c" ((uint32)(buf)), "d" ((uint32)(count)) \
+ : "memory");
+
+ return (int)result;
+}
+
+static inline int up_write(int fd, const void* buf, size_t count)
+{
+ uint32 result;
+
+ __asm__ volatile ("int $0x80" \
+ : "=a" (result) \
+ : "0" (WRITE), "b" ((uint32)(fd)), "c" ((uint32)(buf)), "d" ((uint32)(count)) \
+ : "memory");
+
+ return (int)result;
+}
+
+static inline int up_check_result(int result)
+{
+ if (result >= (uint32)(-(128 + 1)))
+ {
+ *get_errno_ptr() = -result;
+ result = ERROR;
+ }
+ return result;
+}
+
+static ssize_t devconsole_read(struct file *filp, char *buffer, size_t len)
+{
+ return up_check_result(up_read(1, buffer, len));
+}
+
+static ssize_t devconsole_write(struct file *filp, const char *buffer, size_t len)
+{
+ return up_check_result(up_write(1, buffer, len));
+}
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+void up_devconsole(void)
+{
+ (void)register_inode("/dev/console", &devconsole_fops, 0666, NULL);
+}
+
+int up_putc(int ch)
+{
+ char b = ch;
+ (void)up_write(1, &b, 1);
+ return ch;
+}
diff --git a/nuttx/arch/sim/src/up_exit.c b/nuttx/arch/sim/src/up_exit.c
new file mode 100644
index 0000000000..a0a3bdc9ab
--- /dev/null
+++ b/nuttx/arch/sim/src/up_exit.c
@@ -0,0 +1,123 @@
+/************************************************************
+ * up_exit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: _exit
+ *
+ * Description:
+ * This function causes the currently executing task to cease
+ * to exist. This is a special case of task_delete().
+ *
+ ************************************************************/
+
+void _exit(int status)
+{
+ _TCB* tcb = (_TCB*)g_readytorun.head;
+
+ dbg("TCB=%p exitting\n", tcb);
+
+ /* Remove the tcb task from the ready-to-run list. We can
+ * ignore the return value because we know that a context
+ * switch is needed.
+ */
+
+ (void)sched_removereadytorun(tcb);
+
+ /* Move the TCB to the specified blocked task list and delete it */
+
+ sched_addblocked(tcb, TSTATE_TASK_INACTIVE);
+ task_delete(tcb->pid);
+
+ /* If there are any pending tasks, then add them to the g_readytorun
+ * task list now
+ */
+
+ if (g_pendingtasks.head)
+ {
+ (void)sched_mergepending();
+ }
+
+ /* Now, perform the context switch to the new ready-to-run task at the
+ * head of the list.
+ */
+
+ tcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", tcb);
+
+ /* The way that we handle signals in the simulation is kind of
+ * a kludge. This would be unsafe in a truly multi-threaded, interrupt
+ * driven environment.
+ */
+
+ if (tcb->xcp.sigdeliver)
+ {
+ dbg("Delivering signals TCB=%p\n", tcb);
+ ((sig_deliver_t)tcb->xcp.sigdeliver)(tcb);
+ tcb->xcp.sigdeliver = NULL;
+ }
+
+ /* Then switch contexts */
+
+ up_longjmp(tcb->xcp.regs, 1);
+}
+
diff --git a/nuttx/arch/sim/src/up_head.c b/nuttx/arch/sim/src/up_head.c
new file mode 100644
index 0000000000..9d1ccf1533
--- /dev/null
+++ b/nuttx/arch/sim/src/up_head.c
@@ -0,0 +1,77 @@
+/************************************************************
+ * up_head.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <setjmp.h>
+#include <assert.h>
+#include <nuttx/os_external.h>
+#include <nuttx/arch.h>
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+static jmp_buf sim_abort;
+
+/************************************************************
+ * Global Funtions
+ ************************************************************/
+
+int main(int argc, char **argv, char **envp)
+{
+ if (setjmp(sim_abort) == 0)
+ {
+ os_start();
+ }
+ return 0;
+}
+
+void up_assert(const ubyte *filename, uint32 line)
+{
+ fprintf(stderr, "Assertion failed at file:%s line: %d\n", filename, line);
+ longjmp(sim_abort, 1);
+}
+
+void up_assert_code(const ubyte *filename, uint32 line, uint16 code)
+{
+ fprintf(stderr, "Assertion failed at file:%s line: %d error code: %d\n", filename, line, code);
+ longjmp(sim_abort, 1);
+}
diff --git a/nuttx/arch/sim/src/up_idle.c b/nuttx/arch/sim/src/up_idle.c
new file mode 100644
index 0000000000..b40b2e3d87
--- /dev/null
+++ b/nuttx/arch/sim/src/up_idle.c
@@ -0,0 +1,84 @@
+/************************************************************
+ * up_idle.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_idle
+ *
+ * Description:
+ * up_idle() is the logic that will be executed when their
+ * is no other ready-to-run task. This is processor idle
+ * time and will continue until some interrupt occurs to
+ * cause a context switch from the idle task.
+ *
+ * Processing in this state may be processor-specific. e.g.,
+ * this is where power management operations might be
+ * performed.
+ *
+ ************************************************************/
+
+void up_idle(void)
+{
+ /* If the system, then process timer interrupts. Hopefully
+ * something will wake up.
+ */
+
+ sched_process_timer();
+}
+
diff --git a/nuttx/arch/sim/src/up_initialize.c b/nuttx/arch/sim/src/up_initialize.c
new file mode 100644
index 0000000000..90b0b49e47
--- /dev/null
+++ b/nuttx/arch/sim/src/up_initialize.c
@@ -0,0 +1,88 @@
+/************************************************************
+ * up_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include <nuttx/fs.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_initialize
+ *
+ * Description:
+ * up_initialize will be called once during OS
+ * initialization after the basic OS services have been
+ * initialized. The architecture specific details of
+ * initializing the OS will be handled here. Such things as
+ * setting up interrupt service routines, starting the
+ * clock, and registering device drivers are some of the
+ * things that are different for each processor and hardware
+ * platform.
+ *
+ * up_initialize is called after the OS initialized but
+ * before the init process has been started and before the
+ * libraries have been initialized. OS services and driver
+ * services are available.
+ *
+ ************************************************************/
+
+void up_initialize(void)
+{
+ /* Register devices */
+
+ devnull_register(); /* Standard /dev/null */
+ up_devconsole(); /* Our private /dev/console */
+}
diff --git a/nuttx/arch/sim/src/up_initialstate.c b/nuttx/arch/sim/src/up_initialstate.c
new file mode 100644
index 0000000000..ec3e50991b
--- /dev/null
+++ b/nuttx/arch/sim/src/up_initialstate.c
@@ -0,0 +1,81 @@
+/************************************************************
+ * up_initialstate.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_initial_state
+ *
+ * Description:
+ * A new thread is being started and a new TCB
+ * has been created. This function is called to initialize
+ * the processor specific portions of the new TCB.
+ *
+ * This function must setup the intial architecture registers
+ * and/or stack so that execution will begin at tcb->start
+ * on the next context switch.
+ *
+ ************************************************************/
+
+void up_initial_state(_TCB *tcb)
+{
+ memset(&tcb->xcp, 0, sizeof(struct xcptcontext));
+ tcb->xcp.regs[JB_SP] = (uint32)tcb->adj_stack_ptr;
+ tcb->xcp.regs[JB_PC] = (uint32)tcb->start;
+}
diff --git a/nuttx/arch/sim/src/up_internal.h b/nuttx/arch/sim/src/up_internal.h
new file mode 100644
index 0000000000..29defe0296
--- /dev/null
+++ b/nuttx/arch/sim/src/up_internal.h
@@ -0,0 +1,91 @@
+/**************************************************************************
+ * up_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **************************************************************************/
+
+#ifndef __ARCH_UP_INTERNAL_H
+#define __ARCH_UP_INTERNAL_H
+
+/**************************************************************************
+ * Included Files
+ **************************************************************************/
+
+#include <nuttx/irq.h>
+
+/**************************************************************************
+ * Public Definitions
+ **************************************************************************/
+
+/* Storage order: %ebx, $esi, %edi, %ebp, sp, and return PC */
+#ifdef __ASSEMBLY__
+# define JB_EBX (0*4)
+# define JB_ESI (1*4)
+# define JB_EDI (2*4)
+# define JB_EBP (3*4)
+# define JB_SP (4*4)
+# define JB_PC (5*4)
+#else
+# define JB_EBX (0)
+# define JB_ESI (1)
+# define JB_EDI (2)
+# define JB_EBP (3)
+# define JB_SP (4)
+# define JB_PC (5)
+#endif /* __ASSEMBLY__ */
+
+#define SIM_HEAP_SIZE (4*1024*1024)
+
+/**************************************************************************
+ * Public Types
+ **************************************************************************/
+
+/**************************************************************************
+ * Public Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Public Function Prototypes
+ **************************************************************************/
+
+#ifndef __ASSEMBLY__
+/* up_setjmp.S ************************************************************/
+
+extern int up_setjmp(int *jb);
+extern void up_longjmp(int *jb, int val) __attribute__ ((noreturn));
+
+/* up_devconsole **********************************************************/
+
+extern void up_devconsole(void);
+
+#endif /* __ASSEMBLY__ */
+#endif /* __ARCH_UP_INTERNAL_H */
diff --git a/nuttx/arch/sim/src/up_interruptcontext.c b/nuttx/arch/sim/src/up_interruptcontext.c
new file mode 100644
index 0000000000..ad1eb74d7e
--- /dev/null
+++ b/nuttx/arch/sim/src/up_interruptcontext.c
@@ -0,0 +1,74 @@
+/************************************************************
+ * up_interruptcontext.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_interrupt_context
+ *
+ * Description:
+ * Return TRUE is we are currently executing in
+ * the interrupt handler context.
+ ************************************************************/
+
+boolean up_interrupt_context(void)
+{
+ /* The simulation is never in the interrupt state */
+ return FALSE;
+}
+
diff --git a/nuttx/arch/sim/src/up_releasepending.c b/nuttx/arch/sim/src/up_releasepending.c
new file mode 100644
index 0000000000..8a494a3542
--- /dev/null
+++ b/nuttx/arch/sim/src/up_releasepending.c
@@ -0,0 +1,118 @@
+/************************************************************
+ * up_releasepending.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_release_pending
+ *
+ * Description:
+ * Release and ready-to-run tasks that have
+ * collected in the pending task list. This can call a
+ * context switch if a new task is placed at the head of
+ * the ready to run list.
+ *
+ ************************************************************/
+
+void up_release_pending(void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+
+ dbg("From TCB=%p\n", rtcb);
+
+ /* Merge the g_pendingtasks list into the g_readytorun task list */
+
+ /* sched_lock(); */
+ if (sched_mergepending())
+ {
+ /* The currently active task has changed! Copy the exception context
+ * into the TCB of the task that was currently active. if up_setjmp
+ * returns a non-zero value, then this is really the previously
+ * running task restarting!
+ */
+
+ if (!up_setjmp(rtcb->xcp.regs))
+ {
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* The way that we handle signals in the simulation is kind of
+ * a kludge. This would be unsafe in a truly multi-threaded, interrupt
+ * driven environment.
+ */
+
+ if (rtcb->xcp.sigdeliver)
+ {
+ dbg("Delivering signals TCB=%p\n", rtcb);
+ ((sig_deliver_t)rtcb->xcp.sigdeliver)(rtcb);
+ rtcb->xcp.sigdeliver = NULL;
+ }
+
+ /* Then switch contexts */
+
+ up_longjmp(rtcb->xcp.regs, 1);
+ }
+ }
+}
diff --git a/nuttx/arch/sim/src/up_releasestack.c b/nuttx/arch/sim/src/up_releasestack.c
new file mode 100644
index 0000000000..5fc950c6ab
--- /dev/null
+++ b/nuttx/arch/sim/src/up_releasestack.c
@@ -0,0 +1,81 @@
+/************************************************************
+ * up_releasestack.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <nuttx/arch.h>
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_release_stack
+ *
+ * Description:
+ * A task has been stopped. Free all stack
+ * related resources retained int the defunct TCB.
+ *
+ ************************************************************/
+
+void up_release_stack(_TCB *dtcb)
+{
+ if (dtcb->stack_alloc_ptr)
+ {
+ free(dtcb->stack_alloc_ptr);
+ }
+
+ dtcb->stack_alloc_ptr = NULL;
+ dtcb->adj_stack_size = 0;
+ dtcb->adj_stack_ptr = NULL;
+}
diff --git a/nuttx/arch/sim/src/up_reprioritizertr.c b/nuttx/arch/sim/src/up_reprioritizertr.c
new file mode 100644
index 0000000000..d4d91a88e1
--- /dev/null
+++ b/nuttx/arch/sim/src/up_reprioritizertr.c
@@ -0,0 +1,169 @@
+/************************************************************
+ * up_reprioritizertr.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_reprioritize_rtr
+ *
+ * Description:
+ * Called when the priority of a running or
+ * ready-to-run task changes and the reprioritization will
+ * cause a context switch. Two cases:
+ *
+ * 1) The priority of the currently running task drops and the next
+ * task in the ready to run list has priority.
+ * 2) An idle, ready to run task's priority has been raised above the
+ * the priority of the current, running task and it now has the
+ * priority.
+ *
+ * Inputs:
+ * tcb: The TCB of the task that has been reprioritized
+ * priority: The new task priority
+ *
+ ************************************************************/
+
+void up_reprioritize_rtr(_TCB *tcb, ubyte priority)
+{
+ /* Verify that the caller is sane */
+
+ if (tcb->task_state < FIRST_READY_TO_RUN_STATE ||
+ tcb->task_state > LAST_READY_TO_RUN_STATE ||
+ priority < SCHED_PRIORITY_MIN ||
+ priority > SCHED_PRIORITY_MAX)
+ {
+ PANIC(OSERR_BADREPRIORITIZESTATE);
+ }
+ else
+ {
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ boolean switch_needed;
+
+ dbg("TCB=%p PRI=%d\n", tcb, priority);
+
+ /* Remove the tcb task from the ready-to-run list.
+ * sched_removereadytorun will return TRUE if we just
+ * remove the head of the ready to run list.
+ */
+
+ switch_needed = sched_removereadytorun(tcb);
+
+ /* Setup up the new task priority */
+
+ tcb->sched_priority = (ubyte)priority;
+
+ /* Return the task to the specified blocked task list.
+ * sched_addreadytorun will return TRUE if the task was
+ * added to the new list. We will need to perform a context
+ * switch only if the EXCLUSIVE or of the two calls is non-zero
+ * (i.e., one and only one the calls changes the head of the
+ * ready-to-run list).
+ */
+
+ switch_needed ^= sched_addreadytorun(tcb);
+
+ /* Now, perform the context switch if one is needed */
+
+ if (switch_needed)
+ {
+ /* If we are going to do a context switch, then now is the right
+ * time to add any pending tasks back into the ready-to-run list.
+ * task list now
+ */
+
+ if (g_pendingtasks.head)
+ {
+ sched_mergepending();
+ }
+
+ /* Copy the exception context into the TCB at the (old) head of the
+ * g_readytorun Task list. if up_setjmp returns a non-zero
+ * value, then this is really the previously running task restarting!
+ */
+
+ if (!up_setjmp(rtcb->xcp.regs))
+ {
+ /* Restore the exception context of the rtcb at the (new) head
+ * of the g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* The way that we handle signals in the simulation is kind of
+ * a kludge. This would be unsafe in a truly multi-threaded, interrupt
+ * driven environment.
+ */
+
+ if (rtcb->xcp.sigdeliver)
+ {
+ dbg("Delivering signals TCB=%p\n", rtcb);
+ ((sig_deliver_t)rtcb->xcp.sigdeliver)(rtcb);
+ rtcb->xcp.sigdeliver = NULL;
+ }
+
+ /* Then switch contexts */
+
+ up_longjmp(rtcb->xcp.regs, 1);
+ }
+ }
+ }
+}
diff --git a/nuttx/arch/sim/src/up_schedulesigaction.c b/nuttx/arch/sim/src/up_schedulesigaction.c
new file mode 100644
index 0000000000..6e6d13911b
--- /dev/null
+++ b/nuttx/arch/sim/src/up_schedulesigaction.c
@@ -0,0 +1,109 @@
+/************************************************************
+ * up_schedulesigaction.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_schedule_sigaction
+ *
+ * Description:
+ * This function is called by the OS when one or more
+ * signal handling actions have been queued for execution.
+ * The architecture specific code must configure things so
+ * that the 'igdeliver' callback is executed on the thread
+ * specified by 'tcb' as soon as possible.
+ *
+ * This function may be called from interrupt handling logic.
+ *
+ * This operation should not cause the task to be unblocked
+ * nor should it cause any immediate execution of sigdeliver.
+ * Typically, a few cases need to be considered:
+ *
+ * (1) This function may be called from an interrupt handler
+ * During interrupt processing, all xcptcontext structures
+ * should be valid for all tasks. That structure should
+ * be modified to invoke sigdeliver() either on return
+ * from (this) interrupt or on some subsequent context
+ * switch to the recipient task.
+ * (2) If not in an interrupt handler and the tcb is NOT
+ * the currently executing task, then again just modify
+ * the saved xcptcontext structure for the recipient
+ * task so it will invoke sigdeliver when that task is
+ * later resumed.
+ * (3) If not in an interrupt handler and the tcb IS the
+ * currently executing task -- just call the signal
+ * handler now.
+ *
+ ************************************************************/
+
+void up_schedule_sigaction(_TCB *tcb, sig_deliver_t sigdeliver)
+{
+ /* We don't have to anything complex of the simulated target */
+
+ if (tcb == (_TCB*)g_readytorun.head)
+ {
+ sigdeliver(tcb);
+ }
+ else
+ {
+ tcb->xcp.sigdeliver = sigdeliver;
+ }
+}
diff --git a/nuttx/arch/sim/src/up_setjmp.S b/nuttx/arch/sim/src/up_setjmp.S
new file mode 100644
index 0000000000..96b9bfee88
--- /dev/null
+++ b/nuttx/arch/sim/src/up_setjmp.S
@@ -0,0 +1,129 @@
+/**************************************************************************
+ * up_setjmp.S
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **************************************************************************/
+
+/**************************************************************************
+ * Conditional Compilation Options
+ **************************************************************************/
+
+/**************************************************************************
+ * Included Files
+ **************************************************************************/
+
+#include "up_internal.h"
+
+/**************************************************************************
+ * Private Definitions
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Types
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Function Prototypes
+ **************************************************************************/
+
+/**************************************************************************
+ * Global Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Functions
+ **************************************************************************/
+
+/**************************************************************************
+ * Public Functions
+ **************************************************************************/
+
+ .text
+ .globl up_setjmp
+ .type up_setjmp, @function
+up_setjmp:
+
+ /* %ebx, %esi, %edi, and %ebp must be preserved.
+ * save %ebx, $esi, and %edi now... */
+
+ movl 4(%esp), %eax
+ movl %ebx, (JB_EBX)(%eax)
+ movl %esi, (JB_ESI)(%eax)
+ movl %edi, (JB_EDI)(%eax)
+
+ /* Save the value of SP as will be after we return */
+
+ leal 4(%esp), %ecx
+ movl %ecx, (JB_SP)(%eax)
+
+ /* Save the return PC */
+
+ movl 0(%esp), %ecx
+ movl %ecx, (JB_PC)(%eax)
+
+ /* Save the framepointer */
+
+ movl %ebp, (JB_EBP)(%eax)
+
+ /* And return 0 */
+
+ xorl %eax, %eax
+ ret
+ .size up_setjmp, . - up_setjmp
+
+ .globl up_longjmp
+ .type up_longjmp, @function
+up_longjmp:
+ movl 4(%esp), %ecx /* U_pthread_jmpbuf in %ecx. */
+ movl 8(%esp), %eax /* Second argument is return value. */
+
+ /* Save the return address now. */
+
+ movl (JB_PC)(%ecx), %edx
+
+ /* Restore registers. */
+
+ movl (JB_EBX)(%ecx), %ebx
+ movl (JB_ESI)(%ecx), %esi
+ movl (JB_EDI)(%ecx), %edi
+ movl (JB_EBP)(%ecx), %ebp
+ movl (JB_SP)(%ecx), %esp
+
+ /* Jump to saved PC. */
+
+ jmp *%edx
+ .size up_longjmp, . - up_longjmp
+
diff --git a/nuttx/arch/sim/src/up_unblocktask.c b/nuttx/arch/sim/src/up_unblocktask.c
new file mode 100644
index 0000000000..bf3148506a
--- /dev/null
+++ b/nuttx/arch/sim/src/up_unblocktask.c
@@ -0,0 +1,146 @@
+/************************************************************
+ * up_unblocktask.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_unblock_task
+ *
+ * Description:
+ * A task is currently in an inactive task list
+ * but has been prepped to execute. Move the TCB to the
+ * ready-to-run list, restore its context, and start execution.
+ *
+ * Inputs:
+ * tcb: Refers to the tcb to be unblocked. This tcb is
+ * in one of the waiting tasks lists. It must be moved to
+ * the ready-to-run list and, if it is the highest priority
+ * ready to run taks, executed.
+ *
+ ************************************************************/
+
+void up_unblock_task(_TCB *tcb)
+{
+ /* Verify that the context switch can be performed */
+ if ((tcb->task_state < FIRST_BLOCKED_STATE) ||
+ (tcb->task_state > LAST_BLOCKED_STATE))
+ {
+ PANIC(OSERR_BADUNBLOCKSTATE);
+ }
+ else
+ {
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+
+ dbg("Unblocking TCB=%p\n", tcb);
+
+ /* Remove the task from the blocked task list */
+
+ sched_removeblocked(tcb);
+
+ /* Reset its timeslice. This is only meaningful for round
+ * robin tasks but it doesn't here to do it for everything
+ */
+
+#if CONFIG_RR_INTERVAL > 0
+ tcb->timeslice = CONFIG_RR_INTERVAL;
+#endif
+
+ /* Add the task in the correct location in the prioritized
+ * g_readytorun task list
+ */
+
+ if (sched_addreadytorun(tcb))
+ {
+ /* The currently active task has changed! Copy the exception context
+ * into the TCB of the task that was previously active. if
+ * up_setjmp returns a non-zero value, then this is really the
+ * previously running task restarting!
+ */
+
+ if (!up_setjmp(rtcb->xcp.regs))
+ {
+ /* Restore the exception context of the new task that is ready to
+ * run (probably tcb). This is the new rtcb at the head of the
+ * g_readytorun task list.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ dbg("New Active Task TCB=%p\n", rtcb);
+
+ /* The way that we handle signals in the simulation is kind of
+ * a kludge. This would be unsafe in a truly multi-threaded, interrupt
+ * driven environment.
+ */
+
+ if (rtcb->xcp.sigdeliver)
+ {
+ dbg("Delivering signals TCB=%p\n", rtcb);
+ ((sig_deliver_t)rtcb->xcp.sigdeliver)(rtcb);
+ rtcb->xcp.sigdeliver = NULL;
+ }
+
+ /* Then switch contexts */
+
+ up_longjmp(rtcb->xcp.regs, 1);
+ }
+ }
+ }
+}
diff --git a/nuttx/arch/sim/src/up_usestack.c b/nuttx/arch/sim/src/up_usestack.c
new file mode 100644
index 0000000000..26bf14b211
--- /dev/null
+++ b/nuttx/arch/sim/src/up_usestack.c
@@ -0,0 +1,100 @@
+/************************************************************
+ * up_usestack.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "up_internal.h"
+
+/************************************************************
+ * Private Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Funtions
+ ************************************************************/
+
+/************************************************************
+ * Public Funtions
+ ************************************************************/
+
+/************************************************************
+ * Name: up_use_stack
+ *
+ * Description:
+ * Setup up stack-related information in the TCB
+ * using pre-allocated stack memory
+ *
+ * The following TCB fields must be initialized:
+ * adj_stack_size: Stack size after adjustment for hardware,
+ * processor, etc. This value is retained only for debug
+ * purposes.
+ * stack_alloc_ptr: Pointer to allocated stack
+ * adj_stack_ptr: Adjusted StatckAllocPtr for HW. The
+ * initial value of the stack pointer.
+ *
+ * Inputs:
+ * tcb: The TCB of new task
+ * stack_size: The allocated stack size.
+ *
+ ************************************************************/
+
+STATUS up_use_stack(_TCB *tcb, uint32 *stack, uint32 stack_size)
+{
+ /* Move up to next even word boundary if necessary */
+
+ uint32 adj_stack_size = stack_size & ~3;
+ uint32 adj_stack_words = adj_stack_size >> 2;
+
+ /* This is the address of the last word in the allocation */
+
+ uint32 *adj_stack_ptr = &stack[adj_stack_words - 1];
+
+ /* Save the values in the TCB */
+ tcb->adj_stack_size = adj_stack_size;
+ tcb->stack_alloc_ptr = stack;
+ tcb->adj_stack_ptr = adj_stack_ptr;
+ return OK;
+}
diff --git a/nuttx/drivers/Makefile b/nuttx/drivers/Makefile
new file mode 100644
index 0000000000..dcd4dec625
--- /dev/null
+++ b/nuttx/drivers/Makefile
@@ -0,0 +1,74 @@
+############################################################
+# Makefile
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+-include $(TOPDIR)/Make.defs
+
+MKDEP = $(TOPDIR)/tools/mkdeps.sh
+
+ASRCS =
+AOBJS = $(ASRCS:.S=.o)
+
+CSRCS = dev_null.c
+COBJS = $(CSRCS:.c=.o)
+
+SRCS = $(ASRCS) $(CSRCS)
+OBJS = $(AOBJS) $(COBJS)
+
+BIN = libdrivers.a
+
+all: $(BIN)
+
+$(AOBJS): %.o: %.S
+ $(CC) -c $(CFLAGS) -D__ASSEMBLY__ $< -o $@
+
+$(cOBJS): %.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(BIN): $(OBJS)
+ $(AR) rcs $@ $(OBJS)
+
+.depend: Makefile $(SRCS)
+ $(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
+ touch $@
+
+depend: .depend
+
+clean:
+ rm -f $(BIN) *.o *~
+
+distclean: clean
+ rm -f Make.dep .depend
+
+-include Make.dep
diff --git a/nuttx/drivers/dev_null.c b/nuttx/drivers/dev_null.c
new file mode 100644
index 0000000000..f4211b5138
--- /dev/null
+++ b/nuttx/drivers/dev_null.c
@@ -0,0 +1,89 @@
+/************************************************************
+ * dev_null.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <string.h>
+#include <errno.h>
+#include <nuttx/fs.h>
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+static ssize_t devnull_read(struct file *, char *, size_t);
+static ssize_t devnull_write(struct file *, const char *, size_t);
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+static struct file_operations devnull_fops =
+{
+ .read = devnull_read,
+ .write = devnull_write,
+};
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static ssize_t devnull_read(struct file *filp, char *buffer, size_t len)
+{
+ return 0; /* Return EOF */
+}
+
+static ssize_t devnull_write(struct file *filp, const char *buffer, size_t len)
+{
+ return len; /* Say that everything was written */
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+void devnull_register(void)
+{
+ (void)register_inode("/dev/null", &devnull_fops, 0666, NULL);
+}
diff --git a/nuttx/examples/ostest/Makefile b/nuttx/examples/ostest/Makefile
new file mode 100644
index 0000000000..85612a9921
--- /dev/null
+++ b/nuttx/examples/ostest/Makefile
@@ -0,0 +1,75 @@
+############################################################
+# Makefile
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+-include $(TOPDIR)/.config
+-include $(TOPDIR)/Make.defs
+
+MKDEP = $(TOPDIR)/tools/mkdeps.sh
+
+ASRCS =
+AOBJS = $(ASRCS:.S=.o)
+CSRCS = main.c dev_null.c mutex.c cancel.c sem.c cond.c \
+ timedwait.c mqueue.c sighand.c
+COBJS = $(CSRCS:.c=.o)
+
+SRCS = $(ASRCS) $(CSRCS)
+OBJS = $(AOBJS) $(COBJS)
+
+BIN = lib$(CONFIG_EXAMPLE).a
+
+all: $(BIN)
+
+$(AOBJS): %.o: %.S
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(COBJS): %.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(BIN): $(OBJS)
+ $(AR) rcs $@ $(OBJS)
+
+.depend: Makefile $(SRCS)
+ $(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
+ touch $@
+
+depend: .depend
+
+clean:
+ rm -f $(BIN) *.o *~
+
+distclean: clean
+ rm -f Make.dep .depend
+
+-include Make.dep
diff --git a/nuttx/examples/ostest/cancel.c b/nuttx/examples/ostest/cancel.c
new file mode 100644
index 0000000000..d52c596fcf
--- /dev/null
+++ b/nuttx/examples/ostest/cancel.c
@@ -0,0 +1,308 @@
+/***********************************************************************
+ * cancel.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ***********************************************************************/
+
+#include <stdio.h>
+#include <time.h>
+#include <pthread.h>
+#include <errno.h>
+#include "ostest.h"
+
+static pthread_mutex_t mutex;
+static pthread_cond_t cond;
+
+static void *thread_waiter(void *parameter)
+{
+ int status;
+
+ /* Take the mutex */
+
+ printf("%s: Taking mutex\n", __FUNCTION__);
+ status = pthread_mutex_lock(&mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_lock failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ printf("%s: Starting wait for condition\n", __FUNCTION__);
+
+ /* Are we a non-cancelable thread? Yes, set the non-cancelable state */
+
+ if (!parameter)
+ {
+ printf("%s: Setting non-cancelable\n", __FUNCTION__);
+ status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_setcancelstate failed, status=%d\n", __FUNCTION__, status);
+ }
+ }
+
+ /* The wait -- we will never awaken from this. */
+
+ status = pthread_cond_wait(&cond, &mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cond_wait failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Release the mutex */
+
+ printf("%s: Releasing mutex\n", __FUNCTION__);
+ status = pthread_mutex_unlock(&mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_unlock failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Set the cancelable state */
+
+ printf("%s: Setting cancelable\n", __FUNCTION__);
+ status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_setcancelstate failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ printf("%s: Exit with status 0x12345678\n", __FUNCTION__);
+ pthread_exit((void*)0x12345678);
+ return NULL;
+}
+
+static void start_thread(pthread_t *waiter, int cancelable)
+{
+ pthread_attr_t attr;
+ int status;
+
+ /* Initialize the mutex */
+
+ printf("%s: Initializing mutex\n", __FUNCTION__);
+ status = pthread_mutex_init(&mutex, NULL);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Initialize the condition variable */
+
+ printf("%s: Initializing cond\n", __FUNCTION__);
+ status = pthread_cond_init(&cond, NULL);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cond_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Set up attributes */
+
+ status = pthread_attr_init(&attr);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ status = pthread_attr_setstacksize(&attr, 16384);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_setstacksize failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Start the waiter thread */
+
+ printf("%s: Starting thread\n", __FUNCTION__);
+ status = pthread_create(waiter, NULL, thread_waiter, (void*)cancelable);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_create failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Make sure that the waiter thread gets a chance to run */
+
+ printf("%s: Yielding\n", __FUNCTION__);
+ pthread_yield();
+
+}
+
+static void restart_thread(pthread_t *waiter, int cancelable)
+{
+ int status;
+
+ /* Destroy the condition variable */
+
+ printf("%s: Destroying cond\n", __FUNCTION__);
+ status = pthread_cond_destroy(&cond);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cond_destroy failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Destroy the mutex */
+
+ printf("%s: Destroying mutex\n", __FUNCTION__);
+ status = pthread_cond_destroy(&cond);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_destroy failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Then restart the thread */
+
+ printf("%s: Re-starting thread\n", __FUNCTION__);
+ start_thread(waiter, cancelable);
+}
+
+void cancel_test(void)
+{
+ pthread_t waiter;
+ void *result;
+ int status;
+
+ /* Test 1: Normal Cancel *********************************************/
+ /* Start the waiter thread */
+
+ printf("%s: Test 1: Normal Cancelation\n", __FUNCTION__);
+ printf("%s: Starting thread\n", __FUNCTION__);
+ start_thread(&waiter, 1);
+
+ /* Then cancel it. It should be in the pthread_cond_wait now */
+
+ printf("%s: Canceling thread\n", __FUNCTION__);
+ status = pthread_cancel(waiter);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cancel failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Then join to the thread to pick up the result */
+
+ printf("%s: Joining\n", __FUNCTION__);
+ status = pthread_join(waiter, &result);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_join failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: waiter exited with result=%p\n", __FUNCTION__, result);
+ if (result != PTHREAD_CANCELED)
+ {
+ printf("%s: ERROR expected result=%p\n", __FUNCTION__, PTHREAD_CANCELED);
+ }
+ else
+ {
+ printf("%s: PASS thread terminated with PTHREAD_CANCELED\n", __FUNCTION__);
+ }
+ }
+
+ /* Test 2: Cancel Detached Thread ************************************/
+
+ printf("%s: Test 2: Cancelation of detached thread\n", __FUNCTION__);
+ printf("%s: Re-starting thread\n", __FUNCTION__);
+ restart_thread(&waiter, 1);
+
+ /* Detach the thread */
+
+ status = pthread_detach(waiter);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_detach, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Then cancel it. It should be in the pthread_cond_wait now */
+
+ printf("%s: Canceling thread\n", __FUNCTION__);
+ status = pthread_cancel(waiter);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cancel failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Join should now fail */
+
+ printf("%s: Joining\n", __FUNCTION__);
+ status = pthread_join(waiter, &result);
+ if (status == 0)
+ {
+ printf("%s: ERROR pthread_join succeeded\n", __FUNCTION__);
+ }
+ else if (status != ESRCH)
+ {
+ printf("%s: ERROR pthread_join failed but with wrong status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: PASS pthread_join failed with status=ESRCH\n", __FUNCTION__);
+ }
+
+ /* Test 3: Non-cancelable threads ************************************/
+
+ printf("%s: Test 3: Non-cancelable threads\n", __FUNCTION__);
+ printf("%s: Re-starting thread (non-cancelable)\n", __FUNCTION__);
+ restart_thread(&waiter, 0);
+
+ /* Then cancel it. It should be in the pthread_cond_wait now. The
+ * behavior here is non-standard: when the thread is at a cancelation
+ * point, it should be cancelable, even when cancelation is disable.
+ *
+ * The cancelation should succeed, because the cancelation is pending.
+ */
+
+ printf("%s: Canceling thread\n", __FUNCTION__);
+ status = pthread_cancel(waiter);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cancel failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Signal the thread. It should wake up an restore the cancelable state.
+ * When the cancelable state is re-enabled, the thread should be canceled.
+ */
+
+ status = pthread_mutex_lock(&mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_lock failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ status = pthread_cond_signal(&cond);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cond_signal failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ status = pthread_mutex_unlock(&mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_unlock failed, status=%d\n", __FUNCTION__, status);
+ }
+}
diff --git a/nuttx/examples/ostest/cond.c b/nuttx/examples/ostest/cond.c
new file mode 100644
index 0000000000..be995fa080
--- /dev/null
+++ b/nuttx/examples/ostest/cond.c
@@ -0,0 +1,284 @@
+/***********************************************************************
+ * cond.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ***********************************************************************/
+
+#include <stdio.h>
+#include <pthread.h>
+#include <unistd.h>
+#include "ostest.h"
+
+#ifndef NULL
+# define NULL (void*)0
+#endif
+
+static volatile enum { RUNNING, MUTEX_WAIT, COND_WAIT} waiter_state;
+
+static pthread_mutex_t mutex;
+static pthread_cond_t cond;
+static volatile int data_available = 0;
+static int waiter_nloops = 0;
+static int waiter_waits = 0;
+static int waiter_nerrors = 0;
+static int signaler_nloops = 0;
+static int signaler_already = 0;
+static int signaler_state = 0;
+static int signaler_nerrors = 0;
+
+static void *thread_waiter(void *parameter)
+{
+ int status;
+
+ printf("%s: Started\n", __FUNCTION__);
+
+ for(;;)
+ {
+ /* Take the mutex */
+
+ waiter_state = MUTEX_WAIT;
+ status = pthread_mutex_lock(&mutex);
+ waiter_state = RUNNING;
+
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_lock failed, status=%d\n", __FUNCTION__, status);
+ waiter_nerrors++;
+ }
+
+ /* Check if data is available -- if data is not available then
+ * wait for it
+ */
+
+ if (!data_available)
+ {
+ /* We are higher priority than the signaler thread so the
+ * only time that the signaler thread will have a chance to run is when
+ * we are waiting for the condition variable. In this case, pthread_cond_wait
+ * will automatically release the mutex for the signaler (then re-acquire
+ * the mutex before returning.
+ */
+
+ waiter_state = COND_WAIT;
+ status = pthread_cond_wait(&cond, &mutex);
+ waiter_state = RUNNING;
+
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cond_wait failed, status=%d\n", __FUNCTION__, status);
+ waiter_nerrors++;
+ }
+ waiter_waits++;
+ }
+
+ /* Now data should be available */
+
+ if (!data_available)
+ {
+ printf("%s: ERROR data not available after wait\n", __FUNCTION__);
+ waiter_nerrors++;
+ }
+
+ /* Clear data available */
+
+ data_available = 0;
+
+ /* Release the mutex */
+
+ status = pthread_mutex_unlock(&mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR waiter: pthread_mutex_unlock failed, status=%d\n", __FUNCTION__, status);
+ waiter_nerrors++;
+ }
+
+ waiter_nloops++;
+ }
+}
+
+static void *thread_signaler(void *parameter)
+{
+ int status;
+ int i;
+
+ printf("%s: Started\n", __FUNCTION__);
+ for (i = 0; i < 32; i++)
+ {
+ /* Take the mutex. The waiter is higher priority and should
+ * run until it waits for the condition. So, at this point
+ * signaler should be waiting for the condition.
+ */
+
+ status = pthread_mutex_lock(&mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_lock failed, status=%d\n", __FUNCTION__, status);
+ signaler_nerrors++;
+ }
+
+ /* Verify the state */
+
+ if (waiter_state != COND_WAIT)
+ {
+ printf("%s: ERROR waiter state = %d != COND_WAITING\n", __FUNCTION__, waiter_state);
+ signaler_state++;
+ }
+
+ if (data_available)
+ {
+ printf("%s: ERROR data already available, waiter_state=%d\n", __FUNCTION__, waiter_state);
+ signaler_already++;
+ }
+
+ /* Set data available and signal the waiter */
+
+ data_available = 1;
+ status = pthread_cond_signal(&cond);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cond_signal failed, status=%d\n", __FUNCTION__, status);
+ signaler_nerrors++;
+ }
+
+ /* Release the mutex */
+
+ status = pthread_mutex_unlock(&mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_unlock failed, status=%d\n", __FUNCTION__, status);
+ signaler_nerrors++;
+ }
+
+ signaler_nloops++;
+ }
+
+ printf("%s: Terminating\n", __FUNCTION__);
+ pthread_exit(NULL);
+}
+
+void cond_test(void)
+{
+ pthread_t waiter;
+ pthread_t signaler;
+ pthread_attr_t attr;
+ struct sched_param sparam;
+ int prio_min;
+ int prio_max;
+ int prio_mid;
+ int status;
+
+ /* Initialize the mutex */
+
+ printf("%s: Initializing mutex\n", __FUNCTION__);
+ status = pthread_mutex_init(&mutex, NULL);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Initialize the condition variable */
+
+ printf("%s: Initializing cond\n", __FUNCTION__);
+ status = pthread_cond_init(&cond, NULL);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_condinit failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Start the waiter thread at higher priority */
+
+ printf("%s: Starting waiter\n", __FUNCTION__);
+ status = pthread_attr_init(&attr);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ prio_min = sched_get_priority_min(SCHED_FIFO);
+ prio_max = sched_get_priority_max(SCHED_FIFO);
+ prio_mid = (prio_min + prio_max) / 2;
+
+ sparam.sched_priority = prio_mid;
+ status = pthread_attr_setschedparam(&attr,&sparam);
+ if (status != OK)
+ {
+ printf("%s: pthread_attr_setschedparam failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: Set thread 1 priority to %d\n", __FUNCTION__, sparam.sched_priority);
+ }
+
+ status = pthread_create(&waiter, &attr, thread_waiter, NULL);
+ if (status != 0)
+ {
+ printf("%s: pthread_create failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ printf("%s: Starting signaler\n", __FUNCTION__);
+ status = pthread_attr_init(&attr);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ sparam.sched_priority = (prio_min + prio_mid) / 2;
+ status = pthread_attr_setschedparam(&attr,&sparam);
+ if (status != OK)
+ {
+ printf("%s: pthread_attr_setschedparam failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: Set thread 2 priority to %d\n", __FUNCTION__, sparam.sched_priority);
+ }
+
+ status = pthread_create(&signaler, &attr, thread_signaler, NULL);
+ if (status != 0)
+ {
+ printf("%s: pthread_create failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Wait for the threads to stop */
+
+ pthread_join(signaler, NULL);
+ printf("%s: signaler terminated, now cancel the waiter\n", __FUNCTION__);
+ pthread_detach(waiter);
+ pthread_cancel(waiter);
+
+ printf("%s: \tWaiter\tSignaler\n", __FUNCTION__);
+ printf("%s: Loops\t%d\t%d\n", __FUNCTION__, waiter_nloops, signaler_nloops);
+ printf("%s: Errors\t%d\t%d\n", __FUNCTION__, waiter_nerrors, signaler_nerrors);
+ printf("%s: \n%d times, waiter did not have to wait for data\n", __FUNCTION__, waiter_nloops - waiter_waits);
+ printf("%s: %d times, data was already available when the signaler run\n", __FUNCTION__, signaler_already);
+ printf("%s: %d times, the waiter was in an unexpected state when the signaler ran\n", __FUNCTION__, signaler_state);
+}
diff --git a/nuttx/examples/ostest/dev_null.c b/nuttx/examples/ostest/dev_null.c
new file mode 100644
index 0000000000..11d6056c84
--- /dev/null
+++ b/nuttx/examples/ostest/dev_null.c
@@ -0,0 +1,91 @@
+/************************************************************
+ * dev_null.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <stdio.h>
+#include <nuttx/os_external.h>
+#include "ostest.h"
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+static char buffer[1024];
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+int dev_null(void)
+{
+ int nbytes;
+ int fd;
+
+ fd = open("/dev/null", O_RDWR);
+ if (fd < 0)
+ {
+ fprintf(stderr, "%s: Failed to open /dev/null\n", __FUNCTION__);
+ return -1;
+ }
+
+ nbytes = read(fd, buffer, 1024);
+ if (nbytes < 0)
+ {
+ fprintf(stderr, "%s: Failed to read from /dev/null\n", __FUNCTION__);
+ close(fd);
+ return -1;
+ }
+ printf("%s: Read %d bytes from /dev/null\n", __FUNCTION__, nbytes);
+
+ nbytes = write(fd, buffer, 1024);
+ if (nbytes < 0)
+ {
+ fprintf(stderr, "%s: Failed to write to /dev/null\n", __FUNCTION__);
+ close(fd);
+ return -1;
+ }
+ printf("%s: Wrote %d bytes to /dev/null\n", __FUNCTION__, nbytes);
+
+ close(fd);
+ return 0;
+}
diff --git a/nuttx/examples/ostest/main.c b/nuttx/examples/ostest/main.c
new file mode 100644
index 0000000000..f6c0c65caf
--- /dev/null
+++ b/nuttx/examples/ostest/main.c
@@ -0,0 +1,178 @@
+/************************************************************
+ * main.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/os_external.h>
+#include <stdio.h>
+#include <string.h>
+#include <sched.h>
+#include "ostest.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define PRIORITY 100
+#define STACKSIZE 8192
+#define NARGS 4
+
+#define ARG1 "Arg1"
+#define ARG2 "Arg2"
+#define ARG3 "Arg3"
+#define ARG4 "Arg4"
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+static char write_data1[] = "Standard I/O Check: write fd=1\n";
+static char write_data2[] = "Standard I/O Check: write fd=2\n";
+static char *args[NARGS] = { ARG1, ARG2, ARG3, ARG4 };
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static int user_main(int argc, char *argv[])
+{
+ int i;
+
+ printf("%s: Started with argc=%d\n", __FUNCTION__, argc);
+
+ /* Verify passed arguments */
+
+ if (argc != NARGS + 1)
+ {
+ fprintf(stderr, "%s: Error expected argc=%d got argc=%d\n",
+ __FUNCTION__, NARGS+1, argc);
+ }
+
+ for (i = 0; i <= NARGS; i++)
+ {
+ printf("%s: argv[%d]=\"%s\"\n", __FUNCTION__, i, argv[i]);
+ }
+
+ for (i = 1; i <= NARGS; i++)
+ {
+ if (strcmp(argv[i], args[i-1]) != 0)
+ {
+ fprintf(stderr, "%s: ERROR argv[%d]: Expected \"%s\" found \"%s\"\n",
+ __FUNCTION__, i, argv[i], args[i-1]);
+ }
+ }
+
+ /* Checkout /dev/null */
+
+ dev_null();
+
+ /* Verify pthreads and pthread mutex */
+
+ mutex_test();
+
+ /* Verify pthread cancellation */
+
+ cancel_test();
+
+ /* Verify pthreads and semaphores */
+
+ sem_test();
+
+ /* Verify pthreads and condition variables */
+
+ cond_test();
+
+ /* Verify pthreads and condition variable timed waits */
+
+ timedwait_test();
+
+ /* Verify pthreads and message queues */
+
+ mqueue_test();
+
+ /* Verify signal handlers */
+
+ sighand_test();
+ return 0;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * user_initialize
+ ************************************************************/
+
+void user_initialize(void)
+{
+ /* stub */
+}
+
+/************************************************************
+ * user_start
+ ************************************************************/
+
+int user_start(int parm1, int parm2, int parm3, int parm4)
+{
+ int result;
+
+ /* Verify that we can communicate */
+
+ write(1, write_data1, sizeof(write_data1));
+ printf("%s: Standard I/O Check: printf\n", __FUNCTION__);
+ write(2, write_data2, sizeof(write_data2));
+ fprintf(stderr, "%s: Standard I/O Check: fprintf to stderr\n", __FUNCTION__);
+
+ /* Verify that we can spawn a new task */
+
+ result = task_create("ostest", PRIORITY, STACKSIZE, user_main,
+ ARG1, ARG2, ARG3, ARG4);
+ if (result == ERROR)
+ {
+ fprintf(stderr, "%s: Failed to start user_main\n", __FUNCTION__);
+ }
+ else
+ {
+ printf("%s: Started user_main at PID=%d\n", __FUNCTION__, result);
+ }
+ return 0;
+}
diff --git a/nuttx/examples/ostest/mqueue.c b/nuttx/examples/ostest/mqueue.c
new file mode 100644
index 0000000000..f9e6b6e034
--- /dev/null
+++ b/nuttx/examples/ostest/mqueue.c
@@ -0,0 +1,333 @@
+/**************************************************************************
+ * mqueue.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **************************************************************************/
+
+/**************************************************************************
+ * Included Files
+ **************************************************************************/
+
+#include <stdio.h>
+#include <string.h>
+#include <pthread.h>
+#include <mqueue.h>
+#include <sched.h>
+
+#include "ostest.h"
+
+/**************************************************************************
+ * Private Definitions
+ **************************************************************************/
+
+#define TEST_MESSAGE "This is a test and only a test"
+#define TEST_MSGLEN (strlen(TEST_MESSAGE)+1)
+
+/**************************************************************************
+ * Private Types
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Function Prototypes
+ **************************************************************************/
+
+/**************************************************************************
+ * Global Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Functions
+ **************************************************************************/
+
+/**************************************************************************
+ * Public Functions
+ **************************************************************************/
+
+static void *sender_thread(void *arg)
+{
+ mqd_t mqfd;
+ char msg_buffer[TEST_MSGLEN];
+ struct mq_attr attr;
+ int status = 0;
+ int nerrors = 0;
+ int i;
+
+ printf("%s: Starting\n", __FUNCTION__);
+
+ /* Fill in attributes for message queue */
+
+ attr.mq_maxmsg = 20;
+ attr.mq_msgsize = TEST_MSGLEN;
+ attr.mq_flags = 0;
+
+ /* Set the flags for the open of the queue.
+ * Make it a blocking open on the queue, meaning it will block if
+ * this process tries to send to the queue and the queue is full.
+ *
+ * O_CREAT - the queue will get created if it does not already exist.
+ * O_WRONLY - we are only planning to write to the queue.
+ *
+ * Open the queue, and create it if the receiving process hasn't
+ * already created it.
+ */
+
+ mqfd = mq_open("testmq", O_WRONLY|O_CREAT, 0666, &attr);
+ if (mqfd < 0)
+ {
+ printf("%s: ERROR mq_open failed\n", __FUNCTION__);
+ pthread_exit((void*)1);
+ }
+
+ /* Fill in a test message buffer to send */
+
+ memcpy(msg_buffer, TEST_MESSAGE, TEST_MSGLEN);
+
+ /* Perform the send 10 times */
+
+ for (i = 0; i < 10; i++)
+ {
+ status = mq_send(mqfd, msg_buffer, TEST_MSGLEN, 42);
+ if (status < 0)
+ {
+ printf("%s: ERROR mq_send failure=%d on msg %d\n", __FUNCTION__, status, i);
+ nerrors++;
+ }
+ else
+ {
+ printf("%s: mq_send succeeded on msg %d\n", __FUNCTION__, i);
+ }
+ }
+
+ /* Close the queue and return success */
+
+ if (mq_close(mqfd) < 0)
+ {
+ printf("%s: ERROR mq_close failed\n", __FUNCTION__);
+ }
+
+ printf("%s: returning nerrors=%d\n", __FUNCTION__, nerrors);
+ return (void*)nerrors;
+}
+
+static void *receiver_thread(void *arg)
+{
+ mqd_t mqfd;
+ char msg_buffer[TEST_MSGLEN];
+ struct mq_attr attr;
+ int nbytes;
+ int nerrors = 0;
+ int i;
+
+ printf("%s: Starting\n", __FUNCTION__);
+
+ /* Fill in attributes for message queue */
+
+ attr.mq_maxmsg = 20;
+ attr.mq_msgsize = TEST_MSGLEN;
+ attr.mq_flags = 0;
+
+ /* Set the flags for the open of the queue.
+ * Make it a blocking open on the queue, meaning it will block if
+ * this process tries to* send to the queue and the queue is full.
+ *
+ * O_CREAT - the queue will get created if it does not already exist.
+ * O_RDONLY - we are only planning to write to the queue.
+ *
+ * Open the queue, and create it if the sending process hasn't
+ * already created it.
+ */
+
+ mqfd = mq_open("testmq", O_RDONLY|O_CREAT, 0666, &attr);
+ if (mqfd < 0)
+ {
+ printf("%s: ERROR mq_open failed\n", __FUNCTION__);
+ pthread_exit((void*)1);
+ }
+
+ /* Perform the receive 10 times */
+
+ for (i = 0; i < 10; i++)
+ {
+ nbytes = mq_receive(mqfd, msg_buffer, TEST_MSGLEN, 0);
+ if (nbytes < 0)
+ {
+ printf("%s: ERROR mq_receive failure on msg %d\n", __FUNCTION__, i);
+ nerrors++;
+ }
+ else if (nbytes != TEST_MSGLEN)
+ {
+ printf("%s: mq_receive return bad size %d on msg %d\n", __FUNCTION__, nbytes, i);
+ nerrors++;
+ }
+ else if (memcmp(TEST_MESSAGE, msg_buffer, nbytes) != 0)
+ {
+ int j;
+
+ printf("%s: mq_receive returned corrupt message on msg %d\n", __FUNCTION__, i);
+ printf("%s: i Expected Received\n", __FUNCTION__);
+
+ for (j = 0; j < TEST_MSGLEN-1; j++)
+ {
+ printf("%s: %2d %02x (%c) %02x\n",
+ __FUNCTION__,
+ j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j] & 0x0ff);
+ }
+ printf("%s: %2d 00 %02x\n",
+ __FUNCTION__, j, msg_buffer[j] & 0xff);
+ }
+ else
+ {
+ printf("%s: mq_receive succeeded on msg %d\n", __FUNCTION__, i);
+ }
+ }
+
+ /* Close the queue and return success */
+
+ if (mq_close(mqfd) < 0)
+ {
+ printf("%s: ERROR mq_close failed\n", __FUNCTION__);
+ nerrors++;
+ }
+
+ pthread_exit((void*)nerrors);
+
+ /* Destroy the queue */
+
+ if (mq_unlink("testmq") < 0)
+ {
+ printf("%s: ERROR mq_close failed\n", __FUNCTION__);
+ nerrors++;
+ }
+
+ printf("%s: returning nerrors=%d\n", __FUNCTION__, nerrors);
+ return (void*)nerrors;
+}
+
+void mqueue_test(void)
+{
+ pthread_t sender;
+ pthread_t receiver;
+ void *result;
+ pthread_attr_t attr;
+ struct sched_param sparam;
+ int prio_min;
+ int prio_max;
+ int prio_mid;
+ int status;
+
+ /* Start the sending thread at higher priority */
+
+ printf("%s: Starting receiver\n", __FUNCTION__);
+ status = pthread_attr_init(&attr);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ status = pthread_attr_setstacksize(&attr, 16384);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_setstacksize failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ prio_min = sched_get_priority_min(SCHED_FIFO);
+ prio_max = sched_get_priority_max(SCHED_FIFO);
+ prio_mid = (prio_min + prio_max) / 2;
+
+ sparam.sched_priority = prio_mid;
+ status = pthread_attr_setschedparam(&attr,&sparam);
+ if (status != OK)
+ {
+ printf("%s: pthread_attr_setschedparam failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: Set receiver priority to %d\n", __FUNCTION__, sparam.sched_priority);
+ }
+
+ status = pthread_create(&receiver, &attr, receiver_thread, NULL);
+ if (status != 0)
+ {
+ printf("%s: pthread_create failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Start the sending thread at lower priority */
+
+ printf("%s: Starting sender\n", __FUNCTION__);
+ status = pthread_attr_init(&attr);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ status = pthread_attr_setstacksize(&attr, 16384);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_setstacksize failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ sparam.sched_priority = (prio_min + prio_mid) / 2;
+ status = pthread_attr_setschedparam(&attr,&sparam);
+ if (status != OK)
+ {
+ printf("%s: pthread_attr_setschedparam failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: Set sender thread priority to %d\n", __FUNCTION__, sparam.sched_priority);
+ }
+
+ status = pthread_create(&sender, &attr, sender_thread, NULL);
+ if (status != 0)
+ {
+ printf("%s: pthread_create failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ pthread_join(sender, &result);
+ if (result != (void*)0)
+ {
+ printf("%s: ERROR sender thread exited with %d errors\n", __FUNCTION__, (int)result);
+ }
+
+ pthread_cancel(receiver);
+ pthread_join(receiver, &result);
+ if (result != (void*)0)
+ {
+ printf("%s: ERROR receiver thread exited with %d errors\n", __FUNCTION__, (int)result);
+ }
+}
+
+
diff --git a/nuttx/examples/ostest/mutex.c b/nuttx/examples/ostest/mutex.c
new file mode 100644
index 0000000000..d6cad9e39f
--- /dev/null
+++ b/nuttx/examples/ostest/mutex.c
@@ -0,0 +1,120 @@
+/***********************************************************************
+ * mutex.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ***********************************************************************/
+
+#include <stdio.h>
+#include <pthread.h>
+#include "ostest.h"
+
+#ifndef NULL
+# define NULL (void*)0
+#endif
+
+#define NLOOPS 32
+
+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 void *thread_func(void *parameter)
+{
+ int id = (int)parameter;
+ int ndx = id - 1;
+ int i;
+
+ for (nloops[ndx] = 0; nloops[ndx] < NLOOPS; nloops[ndx]++)
+ {
+ int status = pthread_mutex_lock(&mut);
+ if (status != 0)
+ {
+ printf("ERROR thread %d: pthread_mutex_lock failed, status=%d\n",
+ id, status);
+ }
+
+ if (my_mutex == 1)
+ {
+ printf("ERROR thread=%d: "
+ "my_mutex should be zero, instead my_mutex=%d\n",
+ id, my_mutex);
+ nerrors[ndx]++;
+ }
+
+ my_mutex = 1;
+ for (i = 0; i < 10; i++)
+ {
+ pthread_yield();
+ }
+ my_mutex = 0;
+
+ status = pthread_mutex_unlock(&mut);
+ if (status != 0)
+ {
+ printf("ERROR thread %d: pthread_mutex_unlock failed, status=%d\n",
+ id, status);
+ }
+ }
+ pthread_exit(NULL);
+}
+
+void mutex_test(void)
+{
+ pthread_t thread1, thread2;
+
+ /* Initialize the mutex */
+
+ printf("Initializing mutex\n");
+ pthread_mutex_init(&mut, NULL);
+
+ /* Start two thread instances */
+
+ printf("Starting thread 1\n");
+ if ((pthread_create(&thread1, NULL, thread_func, (void*)1)) != 0)
+ {
+ printf("Error in thread#1 creation\n");
+ }
+
+ printf("Starting thread 2\n");
+ if ((pthread_create(&thread2, NULL, thread_func, (void*)2)) != 0)
+ {
+ printf("Error in thread#2 creation\n");
+ }
+
+ pthread_join(thread1, NULL);
+ pthread_join(thread2, NULL);
+
+ printf("%s:\t\tThread1\tThread2\n", __FUNCTION__);
+ printf("%s:\tLoops\t%ld\t%ld\n", __FUNCTION__, nloops[0], nloops[1]);
+ printf("%s:\tErrors\t%ld\t%ld\n", __FUNCTION__, nerrors[0], nerrors[1]);
+}
diff --git a/nuttx/examples/ostest/ostest.h b/nuttx/examples/ostest/ostest.h
new file mode 100644
index 0000000000..3871ecc67b
--- /dev/null
+++ b/nuttx/examples/ostest/ostest.h
@@ -0,0 +1,91 @@
+/************************************************************
+ * ostest.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __OSTEST_H
+#define __OSTEST_H
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+/* dev_null.c ***********************************************/
+
+extern int dev_null(void);
+
+/* mutex.c **************************************************/
+
+extern void mutex_test(void);
+
+/* sem.c ****************************************************/
+
+extern void sem_test(void);
+
+/* cond.c ***************************************************/
+
+extern void cond_test(void);
+
+/* queue.c **************************************************/
+
+extern void mqueue_test(void);
+
+/* cancel.c *************************************************/
+
+extern void cancel_test(void);
+
+/* timedwait.c **********************************************/
+
+extern void timedwait_test(void);
+
+#endif /* __OSTEST_H */
diff --git a/nuttx/examples/ostest/sem.c b/nuttx/examples/ostest/sem.c
new file mode 100644
index 0000000000..0b532982dc
--- /dev/null
+++ b/nuttx/examples/ostest/sem.c
@@ -0,0 +1,238 @@
+/***********************************************************************
+ * sem.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ***********************************************************************/
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <sched.h>
+#include "ostest.h"
+
+#ifndef NULL
+# define NULL (void*)0
+#endif
+
+static sem_t sem;
+
+static void *waiter_func(void *parameter)
+{
+ int id = (int)parameter;
+ int status;
+ int value;
+
+ printf("%s: Thread %d Started\n", __FUNCTION__, id);
+
+ /* Take the semaphore */
+
+ status = sem_getvalue(&sem, &value);
+ if (status < 0)
+ {
+ printf("%s: ERROR thread %d could not get semaphore value\n", __FUNCTION__, id);
+ }
+ else
+ {
+ printf("%s: Thread %d initial semaphore value = %d\n", __FUNCTION__, id, value);
+ }
+
+ printf("%s: Thread %d aiting on semaphore\n", __FUNCTION__, id);
+ status = sem_wait(&sem);
+ if (status != 0)
+ {
+ printf("%s: ERROR thread %d sem_wait failed\n", __FUNCTION__, id);
+ }
+ printf("%s: Thread %d awakened\n", __FUNCTION__, id);
+
+ status = sem_getvalue(&sem, &value);
+ if (status < 0)
+ {
+ printf("%s: ERROR thread %d could not get semaphore value\n", __FUNCTION__, id);
+ }
+ else
+ {
+ printf("%s: Thread %d new semaphore value = %d\n", __FUNCTION__, id, value);
+ }
+
+ printf("%s: Thread %d done\n", __FUNCTION__, id);
+ return NULL;
+}
+
+static void *poster_func(void *parameter)
+{
+ int id = (int)parameter;
+ int status;
+ int value;
+
+ printf("%s: Thread %d started\n", __FUNCTION__, id);
+
+ /* Take the semaphore */
+
+ do
+ {
+ status = sem_getvalue(&sem, &value);
+ if (status < 0)
+ {
+ printf("%s: ERROR thread %d could not get semaphore value\n", __FUNCTION__, id);
+ }
+ else
+ {
+ printf("%s: Thread %d semaphore value = %d\n", __FUNCTION__, id, value);
+ }
+
+ if (value < 0)
+ {
+ printf("%s: Thread %d posting semaphore\n", __FUNCTION__, id);
+ status = sem_post(&sem);
+ if (status != 0)
+ {
+ printf("%s: ERROR thread %d sem_wait failed\n", __FUNCTION__, id);
+ }
+
+ pthread_yield();
+
+ status = sem_getvalue(&sem, &value);
+ if (status < 0)
+ {
+ printf("%s: ERROR thread %d could not get semaphore value\n", __FUNCTION__, id);
+ }
+ else
+ {
+ printf("%s: Thread %d new semaphore value = %d\n", __FUNCTION__, id, value);
+ }
+ }
+ }
+ while (value < 0);
+
+ printf("%s: Thread %d done\n", __FUNCTION__, id);
+ return NULL;
+
+}
+
+void sem_test(void)
+{
+ pthread_t waiter_thread1;
+ pthread_t waiter_thread2;
+ pthread_t poster_thread;
+ struct sched_param sparam;
+ int prio_min;
+ int prio_max;
+ int prio_mid;
+ pthread_attr_t attr;
+ int status;
+
+ printf("%s: Initializing semaphore to 0\n", __FUNCTION__);
+ sem_init(&sem, 0, 0);
+
+ /* Start two waiter thread instances */
+
+ printf("%s: Starting waiter thread 1\n", __FUNCTION__);
+ status = pthread_attr_init(&attr);
+ if (status != OK)
+ {
+ printf("%s: pthread_attr_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ prio_min = sched_get_priority_min(SCHED_FIFO);
+ prio_max = sched_get_priority_max(SCHED_FIFO);
+ prio_mid = (prio_min + prio_max) / 2;
+
+ sparam.sched_priority = (prio_mid + prio_max) / 2;
+ status = pthread_attr_setschedparam(&attr,&sparam);
+ if (status != OK)
+ {
+ printf("%s: pthread_attr_setschedparam failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: Set thread 1 priority to %d\n", __FUNCTION__, sparam.sched_priority);
+ }
+
+ status = pthread_create(&waiter_thread1, &attr, waiter_func, (void*)1);
+ if (status != 0)
+ {
+ printf("%s: Error in thread 1 creation, status=%d\n", __FUNCTION__, status);
+ }
+
+ printf("%s: Starting waiter thread 2\n", __FUNCTION__);
+ status = pthread_attr_init(&attr);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ sparam.sched_priority = prio_mid;
+ status = pthread_attr_setschedparam(&attr,&sparam);
+ if (status != OK)
+ {
+ printf("%s: pthread_attr_setschedparam failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: Set thread 2 priority to %d\n", __FUNCTION__, sparam.sched_priority);
+ }
+
+ status = pthread_create(&waiter_thread2, &attr, waiter_func, (void*)2);
+ if (status != 0)
+ {
+ printf("%s: Error in thread 2 creation, status=%d\n", __FUNCTION__, status);
+ }
+
+ printf("%s: Starting poster thread 3\n", __FUNCTION__);
+ status = pthread_attr_init(&attr);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ sparam.sched_priority = (prio_min + prio_mid) / 2;
+ status = pthread_attr_setschedparam(&attr,&sparam);
+ if (status != OK)
+ {
+ printf("%s: pthread_attr_setschedparam failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: Set thread 3 priority to %d\n", __FUNCTION__, sparam.sched_priority);
+ }
+
+ status = pthread_create(&poster_thread, &attr, poster_func, (void*)3);
+ if (status != 0)
+ {
+ printf("%s: Error in thread 3 creation, status=%d\n", __FUNCTION__, status);
+ }
+
+ pthread_join(waiter_thread1, NULL);
+ pthread_join(waiter_thread2, NULL);
+ pthread_join(poster_thread, NULL);
+}
diff --git a/nuttx/examples/ostest/sighand.c b/nuttx/examples/ostest/sighand.c
new file mode 100644
index 0000000000..9ffd166f46
--- /dev/null
+++ b/nuttx/examples/ostest/sighand.c
@@ -0,0 +1,254 @@
+/***********************************************************************
+ * sighand.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ***********************************************************************/
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <signal.h>
+#include <sched.h>
+#include <errno.h>
+#include "ostest.h"
+
+#ifndef NULL
+# define NULL (void*)0
+#endif
+
+#define WAKEUP_SIGNAL 17
+#define SIGVALUE_INT 42
+
+static sem_t sem;
+static boolean sigreceived = FALSE;
+static boolean threadexited = FALSE;
+
+static void wakeup_action(int signo, siginfo_t *info, void *ucontext)
+{
+ sigset_t oldset;
+ sigset_t allsigs;
+ int status;
+
+ printf("%s: Received signal %d\n", __FUNCTION__, signo);
+
+ sigreceived = TRUE;
+
+ /* Check signo */
+
+ if (signo != WAKEUP_SIGNAL)
+ {
+ printf("%s: ERROR expected signo=%d\n", __FUNCTION__, WAKEUP_SIGNAL);
+ }
+
+ /* Check siginfo */
+
+ if (info->si_value.sival_int != SIGVALUE_INT)
+ {
+ printf("%s: ERROR sival_int=%d expected %d\n",
+ __FUNCTION__, info->si_value.sival_int, SIGVALUE_INT);
+ }
+ else
+ {
+ printf("%s: sival_int=%d\n", __FUNCTION__, info->si_value.sival_int);
+ }
+
+ if (info->si_signo != WAKEUP_SIGNAL)
+ {
+ printf("%s: ERROR expected si_signo=%d, got=%d\n",
+ __FUNCTION__, WAKEUP_SIGNAL, info->si_signo);
+ }
+
+ printf("%s: si_code=%d\n", __FUNCTION__, info->si_code);
+
+ /* Check ucontext_t */
+
+ printf("%s: ucontext=%p\n", __FUNCTION__, ucontext);
+
+ /* Check sigprocmask */
+
+ (void)sigfillset(&allsigs);
+ status = sigprocmask(SIG_SETMASK, NULL, &oldset);
+ if (status != OK)
+ {
+ printf("%s: ERROR sigprocmask failed, status=%d\n",
+ __FUNCTION__, status);
+ }
+
+ if (oldset != allsigs)
+ {
+ printf("%s: ERROR sigprocmask=%x expected=%x\n",
+ __FUNCTION__, oldset, allsigs);
+ }
+
+}
+
+static int waiter_main(int argc, char *argv[])
+{
+ sigset_t sigset;
+ struct sigaction act;
+ struct sigaction oact;
+ int status;
+
+ printf("%s: Waiter started\n", __FUNCTION__);
+
+ printf("%s: Unmasking signal %d\n", __FUNCTION__, WAKEUP_SIGNAL);
+ (void)sigemptyset(&sigset);
+ (void)sigaddset(&sigset, WAKEUP_SIGNAL);
+ status = sigprocmask(SIG_UNBLOCK, &sigset, NULL);
+ if (status != OK)
+ {
+ printf("%s: ERROR sigprocmask failed, status=%d\n",
+ __FUNCTION__, status);
+ }
+
+ printf("%s: Registering signal handler\n", __FUNCTION__);
+ act.sa_sigaction = wakeup_action;
+ act.sa_flags = SA_SIGINFO;
+
+ (void)sigfillset(&act.sa_mask);
+ (void)sigdelset(&act.sa_mask, WAKEUP_SIGNAL);
+
+ status = sigaction(WAKEUP_SIGNAL, &act, &oact);
+ if (status != OK)
+ {
+ printf("%s: ERROR sigaction failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ printf("%s: oact.sigaction=%p oact.sa_flags=%x oact.sa_mask=%x\n",
+ __FUNCTION__, oact.sa_sigaction, oact.sa_flags, oact.sa_mask);
+
+ /* Take the semaphore */
+
+ printf("%s: Waiting on semaphore\n", __FUNCTION__);
+ fflush(stdout);
+ status = sem_wait(&sem);
+ if (status != 0)
+ {
+ int error = *get_errno_ptr();
+ if (error == EINTR)
+ {
+ printf("%s: sem_wait() successfully interrupted by signal\n", __FUNCTION__);
+ }
+ else
+ {
+ printf("%s: ERROR sem_wait failed, errno=%d\n", __FUNCTION__, error);
+ }
+ }
+ else
+ {
+ printf("%s: ERROR awakened with no error!\n", __FUNCTION__);
+ }
+
+ printf("%s: done\n", __FUNCTION__);
+ fflush(stdout);
+ threadexited = TRUE;
+ return 0;
+}
+
+void sighand_test(void)
+{
+ struct sched_param param;
+ union sigval sigvalue;
+ pid_t waiterpid;
+ int policy;
+ int status;
+
+ printf("%s: Initializing semaphore to 0\n", __FUNCTION__);
+ sem_init(&sem, 0, 0);
+
+ /* Start waiter thread */
+
+ printf("%s: Starting waiter task\n", __FUNCTION__);
+
+
+ status = sched_getparam (0, &param);
+ if (status != OK)
+ {
+ printf("%s: ERROR sched_getparam() failed\n", __FUNCTION__);
+ param.sched_priority = PTHREAD_DEFAULT_PRIORITY;
+ }
+
+ policy = sched_getscheduler(0);
+ if (policy == ERROR)
+ {
+ printf("%s: ERROR sched_getscheduler() failed\n", __FUNCTION__);
+ policy = SCHED_FIFO;
+ }
+
+ waiterpid = task_create("waiter", param.sched_priority,
+ PTHREAD_STACK_DEFAULT, waiter_main, 0, 0, 0, 0);
+ if (waiterpid == ERROR)
+ {
+ printf("%s: ERROR failed to start waiter_main\n", __FUNCTION__);
+ }
+ else
+ {
+ printf("%s: Started waiter_main pid=%d\n", __FUNCTION__, waiterpid);
+ }
+
+ /* Wait a bit */
+
+ fflush(stdout);
+ usleep(500*1000);
+
+ /* Then signal the waiter thread. */
+
+ sigvalue.sival_int = SIGVALUE_INT;
+ status = sigqueue(waiterpid, WAKEUP_SIGNAL, sigvalue);
+ if (status != OK)
+ {
+ printf("%s: ERROR sigqueue failed\n", __FUNCTION__);
+ task_delete(waiterpid);
+ }
+
+ /* Wait a bit */
+
+ fflush(stdout);
+ usleep(500*1000);
+
+ /* Then check the result */
+
+ if (!threadexited)
+ {
+ printf("%s: ERROR waiter task did not exit\n", __FUNCTION__);
+ }
+
+ if (!sigreceived)
+ {
+ printf("%s: ERROR signal handler did not run\n", __FUNCTION__);
+ }
+
+ printf("%s: done\n", __FUNCTION__);
+ fflush(stdout);
+}
diff --git a/nuttx/examples/ostest/timedwait.c b/nuttx/examples/ostest/timedwait.c
new file mode 100644
index 0000000000..9f3e61eae2
--- /dev/null
+++ b/nuttx/examples/ostest/timedwait.c
@@ -0,0 +1,161 @@
+/***********************************************************************
+ * timedwait.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ***********************************************************************/
+
+#include <stdio.h>
+#include <time.h>
+#include <pthread.h>
+#include <unistd.h>
+#include "ostest.h"
+
+static pthread_mutex_t mutex;
+static pthread_cond_t cond;
+
+static void *thread_waiter(void *parameter)
+{
+ struct timespec time;
+ int status;
+
+ /* Take the mutex */
+
+ printf("%s: Taking mutex\n", __FUNCTION__);
+ status = pthread_mutex_lock(&mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_lock failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ printf("%s: Starting 5 second wait for condition\n", __FUNCTION__);
+
+ status = clock_gettime(CLOCK_REALTIME, &time);
+ if (status != 0)
+ {
+ printf("%s: ERROR clock_gettime failed\n", __FUNCTION__);
+ }
+ time.tv_sec += 5;
+
+ /* The wait -- no-one is ever going to awaken us */
+
+ status = pthread_cond_timedwait(&cond, &mutex, &time);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_cond_timedwait failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Release the mutex */
+
+ printf("%s: Releasing mutex\n", __FUNCTION__);
+ status = pthread_mutex_unlock(&mutex);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_unlock failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ printf("%s: Exit with status 0x12345678\n", __FUNCTION__);
+ pthread_exit((void*)0x12345678);
+ return NULL;
+}
+
+void timedwait_test(void)
+{
+ pthread_t waiter;
+ pthread_attr_t attr;
+ struct sched_param sparam;
+ void *result;
+ int prio_max;
+ int status;
+
+ /* Initialize the mutex */
+
+ printf("%s: Initializing mutex\n", __FUNCTION__);
+ status = pthread_mutex_init(&mutex, NULL);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_mutex_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Initialize the condition variable */
+
+ printf("%s: Initializing cond\n", __FUNCTION__);
+ status = pthread_cond_init(&cond, NULL);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_condinit failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ /* Start the waiter thread at higher priority */
+
+ printf("%s: Starting waiter\n", __FUNCTION__);
+ status = pthread_attr_init(&attr);
+ if (status != 0)
+ {
+ printf("%s: pthread_attr_init failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ prio_max = sched_get_priority_max(SCHED_FIFO);
+ status = sched_getparam (getpid(), &sparam);
+ if (status != 0)
+ {
+ printf("%s: sched_getparam failed\n", __FUNCTION__);
+ sparam.sched_priority = PTHREAD_DEFAULT_PRIORITY;
+ }
+
+ sparam.sched_priority = (prio_max + sparam.sched_priority) / 2;
+ status = pthread_attr_setschedparam(&attr,&sparam);
+ if (status != OK)
+ {
+ printf("%s: pthread_attr_setschedparam failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: Set thread 2 priority to %d\n", __FUNCTION__, sparam.sched_priority);
+ }
+
+ status = pthread_create(&waiter, &attr, thread_waiter, NULL);
+ if (status != 0)
+ {
+ printf("%s: pthread_create failed, status=%d\n", __FUNCTION__, status);
+ }
+
+ printf("%s: Joining\n", __FUNCTION__);
+ status = pthread_join(waiter, &result);
+ if (status != 0)
+ {
+ printf("%s: ERROR pthread_join failed, status=%d\n", __FUNCTION__, status);
+ }
+ else
+ {
+ printf("%s: waiter exited with result=%p\n", __FUNCTION__, result);
+ }
+}
diff --git a/nuttx/fs/Makefile b/nuttx/fs/Makefile
new file mode 100644
index 0000000000..fa69e2b4b7
--- /dev/null
+++ b/nuttx/fs/Makefile
@@ -0,0 +1,75 @@
+############################################################
+# Makefile
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+-include $(TOPDIR)/Make.defs
+
+MKDEP = $(TOPDIR)/tools/mkdeps.sh
+
+ASRCS =
+AOBJS = $(ASRCS:.S=.o)
+
+CSRCS = fs_open.c fs_close.c fs_read.c fs_write.c fs_ioctl.c fs_dup.c \
+ fs_files.c fs_inode.c
+COBJS = $(CSRCS:.c=.o)
+
+SRCS = $(ASRCS) $(CSRCS)
+OBJS = $(AOBJS) $(COBJS)
+
+BIN = libfs.a
+
+all: $(BIN)
+
+$(AOBJS): %.o: %.S
+ $(CC) -c $(CFLAGS) -D__ASSEMBLY__ $< -o $@
+
+$(cOBJS): %.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(BIN): $(OBJS)
+ $(AR) rcs $@ $(OBJS)
+
+.depend: Makefile $(SRCS)
+ $(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
+ touch $@
+
+depend: .depend
+
+clean:
+ rm -f $(BIN) *.o *~
+
+distclean: clean
+ rm -f Make.dep .depend
+
+-include Make.dep
diff --git a/nuttx/fs/fs_close.c b/nuttx/fs/fs_close.c
new file mode 100644
index 0000000000..a6a81d2545
--- /dev/null
+++ b/nuttx/fs/fs_close.c
@@ -0,0 +1,88 @@
+/************************************************************
+ * fs_close.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS >0
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <sched.h>
+#include <errno.h>
+
+#include <nuttx/fs.h>
+
+#include "fs_internal.h"
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+int close(int fd)
+{
+ struct filelist *list;
+
+ /* Get the thread-specific file list */
+
+ list = sched_getfiles();
+ if (!list)
+ {
+ *get_errno_ptr() = EMFILE;
+ return ERROR;
+ }
+
+ if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS)
+ {
+ struct inode *inode = list->fl_files[fd].f_inode;
+ if (inode)
+ {
+ files_release(fd);
+ inode_release(inode);
+ return OK;
+ }
+ }
+ *get_errno_ptr() = EBADF;
+ return ERROR;
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/fs/fs_dup.c b/nuttx/fs/fs_dup.c
new file mode 100644
index 0000000000..02a62daf61
--- /dev/null
+++ b/nuttx/fs/fs_dup.c
@@ -0,0 +1,158 @@
+/************************************************************
+ * fs_dup.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <sched.h>
+#include <errno.h>
+#include "fs_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static inline boolean dup_isopen(int fd, struct filelist *list)
+{
+ if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS ||
+ list->fl_files[fd].f_inode == NULL)
+ {
+ return FALSE;
+ }
+ else
+ {
+ return TRUE;
+ }
+}
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+int dup(int fildes)
+{
+ struct filelist *list;
+ int fildes2;
+
+ /* Get the thread-specific file list */
+
+ list = sched_getfiles();
+ if (!list)
+ {
+ *get_errno_ptr() = EMFILE;
+ return ERROR;
+ }
+
+ /* Verify that fildes is a valid, open file descriptor */
+
+ if (!dup_isopen(fildes, list))
+ {
+ *get_errno_ptr() = EBADF;
+ return ERROR;
+ }
+
+ /* Increment the reference count on the contained inode */
+
+ inode_addref(list->fl_files[fildes].f_inode);
+
+ /* Then allocate a new file descriptor for the inode */
+
+ fildes2 = files_allocate(list->fl_files[fildes].f_inode,
+ list->fl_files[fildes].f_oflags,
+ list->fl_files[fildes].f_pos);
+ if (fildes2 < 0)
+ {
+ *get_errno_ptr() = EMFILE;
+ inode_release(list->fl_files[fildes].f_inode);
+ return ERROR;
+ }
+ return fildes2;
+}
+
+int dup2(int fildes1, int fildes2)
+{
+ struct filelist *list;
+
+ /* Get the thread-specific file list */
+
+ list = sched_getfiles();
+ if (!list)
+ {
+ *get_errno_ptr() = EMFILE;
+ return ERROR;
+ }
+
+ /* Verify that fildes is a valid, open file descriptor */
+
+ if (!dup_isopen(fildes1, list))
+ {
+ *get_errno_ptr() = EBADF;
+ return ERROR;
+ }
+
+ /* Handle a special case */
+
+ if (fildes1 == fildes2)
+ {
+ return fildes1;
+ }
+
+ /* Verify fildes2 */
+
+ if ((unsigned int)fildes2 >= CONFIG_NFILE_DESCRIPTORS)
+ {
+ *get_errno_ptr() = EBADF;
+ return ERROR;
+ }
+
+ return files_dup(&list->fl_files[fildes1], &list->fl_files[fildes2]);
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/fs/fs_files.c b/nuttx/fs/fs_files.c
new file mode 100644
index 0000000000..7ffede4605
--- /dev/null
+++ b/nuttx/fs/fs_files.c
@@ -0,0 +1,273 @@
+/************************************************************
+ * fs_files.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+
+#if CONFIG_NFILE_DESCRIPTORS >0
+
+#include <string.h>
+#include <semaphore.h>
+#include <assert.h>
+#include <sched.h>
+#include <errno.h>
+#include <nuttx/fs.h>
+#include <nuttx/kmalloc.h>
+
+#include "fs_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static void _files_semtake(struct filelist *list)
+{
+ /* Take the semaphore (perhaps waiting) */
+
+ while (sem_wait(&list->fl_sem) != 0)
+ {
+ /* The only case that an error should occr here is if
+ * the wait was awakened by a signal.
+ */
+
+ ASSERT(*get_errno_ptr() == EINTR);
+ }
+}
+
+static inline void _files_semgive(struct filelist *list)
+{
+ sem_post(&list->fl_sem);
+}
+
+/************************************************************
+ * Pulblic Functions
+ ************************************************************/
+
+/* This is called from the FS initialization logic to configure
+ * the files.
+ */
+
+void files_initialize(void)
+{
+}
+
+/* Allocate a list of files for a new task */
+
+struct filelist *files_alloclist(void)
+{
+ struct filelist *list;
+ list = (struct filelist*)kzmalloc(sizeof(struct filelist));
+ if (list)
+ {
+ /* Start with a reference count of one */
+
+ list->fl_crefs = 1;
+
+ /* Initialize the list access mutex */
+
+ (void)sem_init(&list->fl_sem, 0, 1);
+ }
+ return list;
+}
+
+/* Increase the reference count on a file list */
+
+int files_addreflist(struct filelist *list)
+{
+ if (list)
+ {
+ /* Increment the reference count on the list */
+
+ _files_semtake(list);
+ list->fl_crefs++;
+ _files_semgive(list);
+ }
+ return OK;
+}
+
+/* Release a reference to the file list */
+
+int files_releaselist(struct filelist *list)
+{
+ int crefs;
+ if (list)
+ {
+ /* Decrement the reference count */
+
+ _files_semtake(list);
+ crefs = --list->fl_crefs;
+ _files_semgive(list);
+
+ /* If the count decrements to zero, then there is no reference
+ * to the structure and it should be deallocated.
+ */
+
+ if (crefs <= 0)
+ {
+ int i;
+
+ /* close each file descriptor */
+
+ for (i = 0; i < CONFIG_NFILE_DESCRIPTORS; i++)
+ {
+ struct inode *inode = list->fl_files[i].f_inode;
+ if (inode)
+ {
+ inode_release(inode);
+ }
+ }
+
+ /* Destroy the semaphore and release the filelist */
+
+ (void)sem_destroy(&list->fl_sem);
+ sched_free(list);
+ }
+ }
+ return OK;
+}
+
+/* Assign an inode to a specific files structure. this is the
+ * heart of dup2.
+ */
+
+int files_dup(struct file *filep1, struct file *filep2)
+{
+ struct filelist *list;
+
+ if (!filep1 || !filep1->f_inode || !filep2)
+ {
+ *get_errno_ptr() = EBADF;
+ return ERROR;
+ }
+
+ list = sched_getfiles();
+ if (!list)
+ {
+ *get_errno_ptr() = EMFILE;
+ return ERROR;
+ }
+
+ _files_semtake(list);
+
+ /* If there is already an inode contained in the new file structure,
+ * release it (effectively closing the file).
+ */
+
+ if (filep2->f_inode)
+ {
+ inode_release(filep2->f_inode);
+ }
+
+ /* Increment the reference count on the contained inode */
+
+ inode_addref(filep1->f_inode);
+
+ /* Then clone the file structure */
+
+ filep2->f_oflags = filep1->f_oflags;
+ filep2->f_pos = filep1->f_pos;
+ filep2->f_inode = filep1->f_inode;
+ _files_semgive(list);
+ return OK;
+}
+
+/* Allocate a struct files instance and associate it with an
+ * inode instance. Returns the file descriptor == index into
+ * the files array.
+ */
+
+int files_allocate(struct inode *inode, int oflags, off_t pos)
+{
+ struct filelist *list;
+ int i;
+
+ list = sched_getfiles();
+ if (list)
+ {
+ _files_semtake(list);
+ for (i = 0; i < CONFIG_NFILE_DESCRIPTORS; i++)
+ {
+ if (!list->fl_files[i].f_inode)
+ {
+ list->fl_files[i].f_oflags = oflags;
+ list->fl_files[i].f_pos = pos;
+ list->fl_files[i].f_inode = inode;
+ _files_semgive(list);
+ return i;
+ }
+ }
+ _files_semgive(list);
+ }
+ return ERROR;
+}
+
+void files_release(int filedes)
+{
+ struct filelist *list;
+
+ list = sched_getfiles();
+ if (list)
+ {
+ if (filedes >=0 && filedes < CONFIG_NFILE_DESCRIPTORS)
+ {
+ _files_semtake(list);
+ list->fl_files[filedes].f_oflags = 0;
+ list->fl_files[filedes].f_pos = 0;
+ list->fl_files[filedes].f_inode = NULL;
+ _files_semgive(list);
+ }
+ }
+}
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/fs/fs_inode.c b/nuttx/fs/fs_inode.c
new file mode 100644
index 0000000000..b080a4e43f
--- /dev/null
+++ b/nuttx/fs/fs_inode.c
@@ -0,0 +1,568 @@
+/************************************************************
+ * fs_inode.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+
+#include <stdlib.h>
+#include <assert.h>
+#include <semaphore.h>
+#include <errno.h>
+
+#include <nuttx/fs.h>
+#include "fs_internal.h"
+
+/************************************************************
+ * Private types
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+static sem_t tree_sem;
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+struct inode *root_inode = NULL;
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static void _inode_semtake(void)
+{
+ /* Take the semaphore (perhaps waiting) */
+
+ while (sem_wait(&tree_sem) != 0)
+ {
+ /* The only case that an error should occr here is if
+ * the wait was awakened by a signal.
+ */
+
+ ASSERT(*get_errno_ptr() == EINTR);
+ }
+}
+
+static inline void _inode_semgive(void)
+{
+ sem_post(&tree_sem);
+}
+
+static int _inode_compare(const char *fname,
+ struct inode *node)
+{
+ char *nname = node->i_name;
+
+ if (!nname)
+ {
+ return 1;
+ }
+
+ if (!fname)
+ {
+ return -1;
+ }
+
+ for (;;)
+ {
+ /* At end of node name? */
+ if (!*nname)
+ {
+ /* Yes.. also end of find name? */
+ if (!*fname || *fname == '/')
+ {
+ /* Yes.. return match */
+ return 0;
+ }
+ else
+ {
+ /* No... return find name > node name */
+ return 1;
+ }
+ }
+
+ /* At end of find name?*/
+ else if (!*fname || *fname == '/')
+ {
+ /* Yes... return find name < node name */
+ return -1;
+ }
+
+ /* check for non-matching characters */
+ else if (*fname > *nname)
+ {
+ return 1;
+ }
+ else if (*fname < *nname)
+ {
+ return -1;
+ }
+
+ /* Not at the end of either string and all of the
+ * characters still match. keep looking.
+ */
+ else
+ {
+ fname++;
+ nname++;
+ }
+ }
+}
+
+static inline int _inode_namelen(const char *name)
+{
+ const char *tmp = name;
+ while(*tmp && *tmp != '/') tmp++;
+ return tmp - name;
+}
+
+static inline void _inode_namecpy(char *dest, const char *src)
+{
+ while(*src && *src != '/') *dest++ = *src++;
+ *dest='\0';
+}
+
+static inline const char *_inode_nextname(const char *name)
+{
+ while (*name && *name != '/') name++;
+ if (*name) name++;
+ return name;
+}
+
+static struct inode *_inode_alloc(const char *name,
+ struct file_operations *fops,
+ mode_t mode, void *private)
+{
+ int namelen = _inode_namelen(name);
+ struct inode *node = malloc(FSNODE_SIZE(namelen));
+ if (node)
+ {
+ node->i_peer = NULL;
+ node->i_child = NULL;
+ node->i_ops = fops;
+#ifdef CONFIG_FILE_MODE
+ node->i_mode = mode;
+#endif
+ node->i_private = private;
+ _inode_namecpy(node->i_name, name);
+ }
+ return node;
+}
+
+static struct inode *_inode_find(const char **path,
+ struct inode **peer,
+ struct inode **parent)
+{
+ const char *name = *path + 1; /* Skip over leading '/' */
+ struct inode *node = root_inode;
+ struct inode *left = NULL;
+ struct inode *above = NULL;
+
+ while (node)
+ {
+ int result = _inode_compare(name, node);
+
+ /* Case 1: The name is less than the name of the node.
+ * Since the names are ordered, these means that there
+ * is no peer node with this name and that there can be
+ * no match in the fileystem.
+ */
+
+ if (result < 0)
+ {
+ node = NULL;
+ break;
+ }
+
+ /* Case 2: the name is greater than the name of the node.
+ * In this case, the name may still be in the list to the
+ * "right"
+ */
+
+ else if (result > 0)
+ {
+ left = node;
+ node = node->i_peer;
+ }
+
+ /* The names match */
+
+ else
+ {
+ /* Now there are two more possibilities:
+ * (1) This is the node that we are looking for or,
+ * (2) the node we are looking for is "blow" this one.
+ */
+
+ name = _inode_nextname(name);
+ if (!*name)
+ {
+ /* We are at the end of the path, so this must be
+ * the node we are looking for.
+ */
+
+ break;
+ }
+ else
+ {
+ /* More to go, keep looking at the next level "down" */
+
+ above = node;
+ left = NULL;
+ node = node->i_child;
+ }
+ }
+ }
+
+ /* node is null. This can happen in one of four cases:
+ * With node = NULL
+ * (1) We went left past the final peer: The new node
+ * name is larger than any existing node name at
+ * that level.
+ * (2) We broke out in the middle of the list of peers
+ * because the name was not found in the ordered
+ * list.
+ * (3) We went down past the final parent: The new node
+ * name is "deeper" than anything that we currently
+ * have in the tree.
+ * with node != NULL
+ * (4) When the node matching the full path is found
+ */
+
+ if (peer) *peer = left;
+ if (parent) *parent = above;
+ *path = name;
+ return node;
+}
+
+static void _inode_insert(struct inode *node,
+ struct inode *peer,
+ struct inode *parent)
+{
+ /* If peer is non-null, then new node simply goes to the right
+ * of that peer node.
+ */
+
+ if (peer)
+ {
+ node->i_peer = peer->i_peer;
+ peer->i_peer = node;
+ }
+
+ /* If parent is non-null, then it must go at the head of its
+ * list of children.
+ */
+
+ else if (parent)
+ {
+ node->i_peer = parent->i_child;
+ parent->i_child = node;
+ }
+
+ /* Otherwise, this must be the new root_inode */
+
+ else
+ {
+ node->i_peer = root_inode;
+ root_inode = node;
+ }
+}
+
+static void _inode_remove(struct inode *node,
+ struct inode *peer,
+ struct inode *parent)
+{
+ /* If peer is non-null, then remove the node from the right of
+ * of that peer node.
+ */
+
+ if (peer)
+ {
+ peer->i_peer = node->i_peer;
+ }
+
+ /* If parent is non-null, then remove the node from head of
+ * of the list of children.
+ */
+
+ else if (parent)
+ {
+ parent->i_child = node->i_peer;
+ }
+
+ /* Otherwise, we must be removing the root inode. */
+
+ else
+ {
+ root_inode = node->i_peer;
+ }
+ node->i_peer = NULL;
+}
+
+static void _inode_free(struct inode *node)
+{
+ if (node)
+ {
+ _inode_free(node->i_peer);
+ _inode_free(node->i_child);
+ free(node);
+ }
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/* This is called from the OS initialization logic to configure
+ * the file system.
+ */
+
+void fs_initialize(void)
+{
+ /* Initialize the semaphore to one (to support one-at-
+ * a-time access to the inode tree).
+ */
+
+ (void)sem_init(&tree_sem, 0, 1);
+
+ /* Initialize files array (if it is used) */
+
+ if (files_initialize != NULL)
+ {
+ files_initialize();
+ }
+}
+
+/* This is called from the open() logic to get a reference
+ * to the inode associatged with a path.
+ */
+
+struct inode *inode_find(const char *path)
+{
+ struct inode *node;
+
+ if (!*path || path[0] != '/')
+ {
+ return NULL;
+ }
+
+ /* Find the node matching the path. If found,
+ * increment the count of references on the node.
+ */
+
+ _inode_semtake();
+ node = _inode_find(&path, NULL, NULL);
+ if (node) node->i_crefs++;
+ _inode_semgive();
+ return node;
+}
+
+/* Increment the reference count on an inode (as when a file
+ * descriptor is dup'ed.
+ */
+
+void inode_addref(struct inode *inode)
+{
+ if (inode)
+ {
+ _inode_semtake();
+ inode->i_crefs++;
+ _inode_semgive();
+ }
+}
+
+/* This is called from close() logic when it no longer refers
+ * to the inode.
+ */
+
+void inode_release(struct inode *node)
+{
+ if (node)
+ {
+ /* Decrement the references of the inode */
+
+ _inode_semtake();
+ if (node->i_crefs) node->i_crefs--;
+
+ /* If the subtree was previously deleted and the reference
+ * count has decrement to zero, then delete the inode
+ * now.
+ */
+
+ if (node->i_crefs <= 0 && (node->i_flags & FSNODEFLAG_DELETED) != 0)
+ {
+ _inode_semgive();
+ _inode_free(node->i_child);
+ free(node);
+ }
+ else
+ {
+ _inode_semgive();
+ }
+ }
+}
+
+
+STATUS register_inode(const char *path,
+ struct file_operations *fops,
+ mode_t mode, void *private)
+{
+ const char *name = path;
+ struct inode *left;
+ struct inode *parent;
+
+ if (!*path || path[0] != '/')
+ {
+ return ERROR;
+ }
+
+ /* Find the location to insert the new subtree */
+
+ _inode_semtake();
+ if (_inode_find(&name, &left, &parent) != NULL)
+ {
+ /* Is is an error if the node already exists in the tree */
+
+ _inode_semgive();
+ return ERROR;
+ }
+
+ /* Now we now where to insert the subtree */
+
+ for (;;)
+ {
+ struct inode *node;
+
+ /* Create a new node -- we need to know if this is the
+ * the leaf node or some intermediary. We can find this
+ * by looking at the next name.
+ */
+
+ const char *next_name = _inode_nextname(name);
+ if (*next_name)
+ {
+ /* Insert an operationless node */
+
+ node = _inode_alloc(name, NULL, mode, NULL);
+ if (node)
+ {
+ _inode_insert(node, left, parent);
+
+ /* Set up for the next time through the loop */
+
+ name = next_name;
+ left = NULL;
+ parent = node;
+ continue;
+ }
+ }
+ else
+ {
+ node = _inode_alloc(name, fops, mode, private);
+ if (node)
+ {
+ _inode_insert(node, left, parent);
+ _inode_semgive();
+ return 0;
+ }
+ }
+
+ /* We get here on failures to allocate node memory */
+
+ _inode_semgive();
+ return ERROR;
+ }
+}
+
+STATUS unregister_inode(const char *path)
+{
+ const char *name = path;
+ struct inode *node;
+ struct inode *left;
+ struct inode *parent;
+
+ if (*path && path[0] == '/')
+ {
+ return ERROR;
+ }
+
+ /* Find the node to delete */
+
+ _inode_semtake();
+ node = _inode_find(&name, &left, &parent);
+ if (node)
+ {
+ /* Found it, now remove it from the tree */
+
+ _inode_remove(node, left, parent);
+
+ /* We cannot delete it if there reference to the inode */
+
+ if (node->i_crefs)
+ {
+ /* In that case, we will mark it deleted, when the FS
+ * releases the inode, we will then, finally delete
+ * the subtree.
+ */
+
+ node->i_flags |= FSNODEFLAG_DELETED;
+ _inode_semgive();
+ }
+ else
+ {
+ /* And delete it now -- recursively to delete all of its children */
+
+ _inode_semgive();
+ _inode_free(node->i_child);
+ free(node);
+ return OK;
+ }
+ }
+
+ /* The node does not exist or it has references */
+ _inode_semgive();
+ return ERROR;
+}
diff --git a/nuttx/fs/fs_internal.h b/nuttx/fs/fs_internal.h
new file mode 100644
index 0000000000..14265895f7
--- /dev/null
+++ b/nuttx/fs/fs_internal.h
@@ -0,0 +1,103 @@
+/************************************************************
+ * fs_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __FS_INTERNAL_H
+#define __FS_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/fs.h>
+#include <nuttx/compiler.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define FSNODEFLAG_DELETED 0x00000001
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+#if CONFIG_NFILE_DESCRIPTORS >0
+extern struct file files[CONFIG_NFILE_DESCRIPTORS];
+#endif
+extern struct inode *root_inode;
+
+/************************************************************
+ * Inline Functions
+ ************************************************************/
+
+/************************************************************
+ * Pulblic Function Prototypes
+ ************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/* fs_inode.c ***********************************************/
+
+EXTERN struct inode *inode_find(const char *path);
+EXTERN void inode_addref(struct inode *inode);
+EXTERN void inode_release(struct inode *inode);
+
+/* fs_files.c ***********************************************/
+
+#if CONFIG_NFILE_DESCRIPTORS >0
+EXTERN void weak_function files_initialize(void);
+EXTERN int files_allocate(struct inode *inode, int oflags, off_t pos);
+EXTERN void files_release(int filedes);
+#endif
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+
+
+#endif /* __FS_INTERNAL_H */
diff --git a/nuttx/fs/fs_ioctl.c b/nuttx/fs/fs_ioctl.c
new file mode 100644
index 0000000000..483a2d9299
--- /dev/null
+++ b/nuttx/fs/fs_ioctl.c
@@ -0,0 +1,75 @@
+/************************************************************
+ * fs_ioctl.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <errno.h>
+#include "fs_internal.h"
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+int ioctl(int fd, int req, unsigned long arg)
+{
+ int ret = EBADF;
+
+ /* We we give a valid file descriptor? */
+
+ if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS)
+ {
+ struct file *this_file = &files[fd];
+ struct inode *inode = this_file->f_inode;
+
+ /* Is a driver registered? Does it support the ioctl method? */
+
+ if (inode && inode->i_ops && inode->i_ops->ioctl)
+ {
+ /* Yes, then let it perform the ioctl */
+
+ ret = (int)inode->i_ops->ioctl(this_file, req, arg);
+ }
+ }
+ return ret;
+}
diff --git a/nuttx/fs/fs_open.c b/nuttx/fs/fs_open.c
new file mode 100644
index 0000000000..deedd60feb
--- /dev/null
+++ b/nuttx/fs/fs_open.c
@@ -0,0 +1,167 @@
+/************************************************************
+ * fs_open.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS >0
+
+#include <sys/types.h>
+
+#include <stdio.h>
+#include <sched.h>
+#include <errno.h>
+
+#ifdef CONFIG_FILE_MODE
+#include <stdarg.h>
+#endif
+
+#include <nuttx/fs.h>
+#include <errno.h>
+
+#include "fs_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+int inode_checkflags(struct inode *inode, int oflags)
+{
+ if (((oflags & O_RDOK) != 0 && !inode->i_ops->read) ||
+ ((oflags & O_WROK) != 0 && !inode->i_ops->write))
+ {
+ *get_errno_ptr() = EACCES;
+ return ERROR;
+ }
+ else
+ {
+ return OK;
+ }
+}
+
+int open(const char *path, int oflags, ...)
+{
+ struct filelist *list;
+ struct inode *inode;
+ int status;
+ int fd;
+
+ /* Get the thread-specific file list */
+
+ list = sched_getfiles();
+ if (!list)
+ {
+ *get_errno_ptr() = EMFILE;
+ return ERROR;
+ }
+
+#ifdef CONFIG_FILE_MODE
+# warning "File creation not implemented"
+ mode_t mode = 0;
+
+ /* If the file is opened for creation, then get the mode bits */
+
+ if (oflags & (O_WRONLY|O_CREAT) != 0)
+ {
+ va_list ap;
+ va_start(ap, oflags);
+ mode = va_arg(ap, mode_t);
+ va_end(ap);
+ }
+#endif
+
+ /* Get an inode for this file */
+
+ inode = inode_find(path);
+ if (!inode)
+ {
+ /* "O_CREAT is not set and the named file does not exist. Or,
+ * a directory component in pathname does not exist or is a
+ * dangling symbolic link.
+ */
+
+ *get_errno_ptr() = ENOENT;
+ return ERROR;
+ }
+
+ /* Make sure that the inode supports the requested access */
+
+ if (inode_checkflags(inode, oflags) != OK)
+ {
+ inode_release(inode);
+ return ERROR;
+ }
+
+ /* Associate the inode with a file structure */
+
+ fd = files_allocate(inode, oflags, 0);
+ if (fd < 0)
+ {
+ inode_release(inode);
+ *get_errno_ptr() = EMFILE;
+ return ERROR;
+ }
+
+ /* Perform the driver open operation */
+
+ status = OK;
+ if (inode->i_ops && inode->i_ops->open)
+ {
+ status = inode->i_ops->open(&list->fl_files[fd]);
+ }
+
+ if (status != OK || !inode->i_ops)
+ {
+ files_release(fd);
+ inode_release(inode);
+ *get_errno_ptr() = ENODEV;
+ return ERROR;
+ }
+
+ return fd;
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/fs/fs_read.c b/nuttx/fs/fs_read.c
new file mode 100644
index 0000000000..a092d0db63
--- /dev/null
+++ b/nuttx/fs/fs_read.c
@@ -0,0 +1,97 @@
+/************************************************************
+ * fs_read.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS >0
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <sched.h>
+#include <errno.h>
+#include "fs_internal.h"
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+int read(int fd, void *buf, unsigned int nbytes)
+{
+ struct filelist *list;
+ int ret = EBADF;
+
+ /* Get the thread-specific file list */
+
+ list = sched_getfiles();
+ if (!list)
+ {
+ *get_errno_ptr() = EMFILE;
+ return ERROR;
+ }
+
+ /* Were we given a valid file descriptor? */
+
+ if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS)
+ {
+ struct file *this_file = &list->fl_files[fd];
+
+ /* Was this file opened for read access? */
+
+ if ((this_file->f_oflags & O_RDOK) != 0)
+ {
+ struct inode *inode = this_file->f_inode;
+
+ /* Is a driver registered? Does it support the read method? */
+
+ if (inode && inode->i_ops && inode->i_ops->read)
+ {
+ /* Yes, then let it perform the read */
+
+ ret = (int)inode->i_ops->read(this_file, (char*)buf, (size_t)nbytes);
+ }
+ }
+ }
+ return ret;
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/fs/fs_write.c b/nuttx/fs/fs_write.c
new file mode 100644
index 0000000000..d85ba61e6e
--- /dev/null
+++ b/nuttx/fs/fs_write.c
@@ -0,0 +1,98 @@
+/************************************************************
+ * fs_write.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS >0
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <sched.h>
+#include <errno.h>
+#include "fs_internal.h"
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+int write(int fd, const void *buf, unsigned int nbytes)
+{
+ struct filelist *list;
+ int ret = EBADF;
+
+ /* Get the thread-specific file list */
+
+ list = sched_getfiles();
+ if (!list)
+ {
+ *get_errno_ptr() = EMFILE;
+ return ERROR;
+ }
+
+ /* Did we get a valid file descriptor? */
+
+ if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS)
+ {
+ struct file *this_file = &list->fl_files[fd];
+
+ /* Was this file opened for write access? */
+
+ if ((this_file->f_oflags & O_WROK) != 0)
+ {
+ struct inode *inode = this_file->f_inode;
+
+ /* Is a driver registered? Does it support the write method? */
+
+ if (inode && inode->i_ops && inode->i_ops->write)
+ {
+ /* Yes, then let it perform the write */
+
+ ret = inode->i_ops->write(this_file, buf, nbytes);
+ }
+ }
+ }
+ return ret;
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS */
+
diff --git a/nuttx/include/assert.h b/nuttx/include/assert.h
new file mode 100644
index 0000000000..831b574852
--- /dev/null
+++ b/nuttx/include/assert.h
@@ -0,0 +1,96 @@
+/************************************************************
+ * assert.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __ASSERT_H
+#define __ASSERT_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* Macro Name: ASSERT, ASSERTCODE, et al. */
+
+#undef ASSERT
+#undef ASSERTFILE
+#undef ASSERTCODE
+#undef DEBUGASSERT
+
+#define ASSERT(f) \
+ { if (!(f)) up_assert((const ubyte *)__FILE__, (uint32)__LINE__); }
+
+#define ASSERTCODE(f, errCode) \
+ { if (!(f)) up_assert_code((const ubyte *)__FILE__, (uint32)__LINE__, errCode); }
+
+#ifdef CONFIG_DEBUG
+#define DEBUGASSERT(f) \
+ { if (!(f)) up_assert((const ubyte *)__FILE__, (uint32)__LINE__); }
+#else
+#define DEBUGASSERT(f)
+#endif /* CONFIG_DEBUG */
+
+#define PANIC(errCode) \
+ up_assert_code((const ubyte *)__FILE__, (uint32)__LINE__, ((errCode)|(0x8000)))
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN void up_assert(const ubyte *fileName, uint32 lineNum);
+EXTERN void up_assert_code(const ubyte *fileName, uint32 lineNum,
+ uint16 errorCode);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ASSERT_H */
diff --git a/nuttx/include/ctype.h b/nuttx/include/ctype.h
new file mode 100644
index 0000000000..c9acca9e70
--- /dev/null
+++ b/nuttx/include/ctype.h
@@ -0,0 +1,187 @@
+/************************************************************
+ * ctype.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __CTYPE_H
+#define __CTYPE_H
+
+/* There is no consistent ctype implementation, just a
+ * smattering of functions. Individually, they are okay, but
+ * a more standard, data lookup approach would make more sense
+ * if used extensively.
+ */
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+
+/************************************************************
+ * Public Type Definitions
+ ************************************************************/
+
+/************************************************************
+ * Function: isspace
+ *
+ * Description:
+ * Checks for white-space characters. In the "C" and "POSIX"
+ * locales, these are: space, form-feed ('\f'), newline ('\n'),
+ * carriage return ('\r'), horizontal tab ('\t'), and vertical
+ * tab ('\v').
+ *
+ ************************************************************/
+
+static inline int isspace(int c)
+{
+ if (c == ' ' || c == '\t' || c == '\n' || \
+ c == '\r' || c == '\f' || c == '\v')
+ {
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+/************************************************************
+ * Function: isdigit
+ *
+ * Description:
+ * ANSI standard isdigit implementation.
+ *
+ ************************************************************/
+
+static inline int isdigit(int c)
+{
+ return (c >= '0' && c <= '9');
+}
+
+/************************************************************
+ * Function: isascii
+ *
+ * Description:
+ * Checks whether c is a 7-bit unsigned char value that
+ * fits into the ASCII character set.
+ *
+ ************************************************************/
+
+static inline int isascii(int c)
+{
+ return (c >= 0 && c <= 0177);
+}
+
+
+/************************************************************
+ * Function: isascii
+ *
+ * Description:
+ * isxdigit() checks for a hexadecimal digits, i.e. one of
+ * {0-9,a-f,A-F}
+ *
+ ************************************************************/
+
+static inline int isxdigit(int c)
+{
+ if ((c >= '0' && c <= '9') ||
+ (c >= 'a' && c <= 'f') ||
+ (c >= 'A' && c <= 'F'))
+ {
+ return 1;
+ }
+ else
+ {
+
+ return 0;
+ }
+}
+
+/************************************************************
+ * Function: isascii
+ *
+ * Description:
+ * toupper() converts the letter c to upper case, if possible.
+ *
+ ************************************************************/
+
+static inline int toupper(int c)
+{
+ if (c >= 'a' && c <= 'z')
+ {
+ return c - 'a' + 'A';
+ }
+ else
+ {
+ return c;
+ }
+}
+
+/************************************************************
+ * Function: isascii
+ *
+ * Description:
+ * tolower() converts the letter c to lower case, if possible.
+ *
+ ************************************************************/
+
+static inline int tolower(int c)
+{
+ if (c >= 'A' && c <= 'Z')
+ {
+ return c - 'A' + 'a';
+ }
+ else
+ {
+ return c;
+ }
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CTYPE_H */
diff --git a/nuttx/include/debug.h b/nuttx/include/debug.h
new file mode 100644
index 0000000000..db47bf6b12
--- /dev/null
+++ b/nuttx/include/debug.h
@@ -0,0 +1,103 @@
+/************************************************************
+ * debug.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __DEBUG_H
+#define __DEBUG_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* Debug macros to runtime filter the opsys debug messages */
+
+#ifdef CONFIG_DEBUG
+# define dbg(format, arg...) lib_rawprintf(format, ##arg)
+
+# ifdef CONFIG_ARCH_LOWPUTC
+# define lldbg(format, arg...) lib_lowprintf(format, ##arg)
+# else
+# define lldbg(x...)
+# endif
+
+# ifdef CONFIG_DEBUG_VERBOSE
+# define vdbg(format, arg...) lib_rawprintf(format, ##arg)
+# else
+# define vdbg(x...)
+# endif
+
+#else
+# define dbg(x...)
+# define lldbg(x...)
+# define vdbg(x...)
+#endif
+
+/************************************************************
+ * Public Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN int lib_rawprintf(const char *format, ...);
+
+#ifdef CONFIG_ARCH_LOWPUTC
+EXTERN int lib_lowprintf(const char *format, ...);
+#endif
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __DEBUG_H */
diff --git a/nuttx/include/errno.h b/nuttx/include/errno.h
new file mode 100644
index 0000000000..50fddfc9f0
--- /dev/null
+++ b/nuttx/include/errno.h
@@ -0,0 +1,198 @@
+/************************************************************
+ * errno.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __ERRNO_H
+#define __ERRNO_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define EPERM 1 /* Operation not permitted */
+#define ENOENT 2 /* No such file or directory */
+#define ESRCH 3 /* No such process */
+#define EINTR 4 /* Interrupted system call */
+#define EIO 5 /* I/O error */
+#define ENXIO 6 /* No such device or address */
+#define E2BIG 7 /* Arg list too long */
+#define ENOEXEC 8 /* Exec format error */
+#define EBADF 9 /* Bad file number */
+#define ECHILD 10 /* No child processes */
+#define EAGAIN 11 /* Try again */
+#define ENOMEM 12 /* Out of memory */
+#define EACCES 13 /* Permission denied */
+#define EFAULT 14 /* Bad address */
+#define ENOTBLK 15 /* Block device required */
+#define EBUSY 16 /* Device or resource busy */
+#define EEXIST 17 /* File exists */
+#define EXDEV 18 /* Cross-device link */
+#define ENODEV 19 /* No such device */
+#define ENOTDIR 20 /* Not a directory */
+#define EISDIR 21 /* Is a directory */
+#define EINVAL 22 /* Invalid argument */
+#define ENFILE 23 /* File table overflow */
+#define EMFILE 24 /* Too many open files */
+#define ENOTTY 25 /* Not a typewriter */
+#define ETXTBSY 26 /* Text file busy */
+#define EFBIG 27 /* File too large */
+#define ENOSPC 28 /* No space left on device */
+#define ESPIPE 29 /* Illegal seek */
+#define EROFS 30 /* Read-only file system */
+#define EMLINK 31 /* Too many links */
+#define EPIPE 32 /* Broken pipe */
+#define EDOM 33 /* Math argument out of domain of func */
+#define ERANGE 34 /* Math result not representable */
+#define EDEADLK 35 /* Resource deadlock would occur */
+#define ENAMETOOLONG 36 /* File name too long */
+#define ENOLCK 37 /* No record locks available */
+#define ENOSYS 38 /* Function not implemented */
+#define ENOTEMPTY 39 /* Directory not empty */
+#define ELOOP 40 /* Too many symbolic links encountered */
+#define EWOULDBLOCK EAGAIN /* Operation would block */
+#define ENOMSG 42 /* No message of desired type */
+#define EIDRM 43 /* Identifier removed */
+#define ECHRNG 44 /* Channel number out of range */
+#define EL2NSYNC 45 /* Level 2 not synchronized */
+#define EL3HLT 46 /* Level 3 halted */
+#define EL3RST 47 /* Level 3 reset */
+#define ELNRNG 48 /* Link number out of range */
+#define EUNATCH 49 /* Protocol driver not attached */
+#define ENOCSI 50 /* No CSI structure available */
+#define EL2HLT 51 /* Level 2 halted */
+#define EBADE 52 /* Invalid exchange */
+#define EBADR 53 /* Invalid request descriptor */
+#define EXFULL 54 /* Exchange full */
+#define ENOANO 55 /* No anode */
+#define EBADRQC 56 /* Invalid request code */
+#define EBADSLT 57 /* Invalid slot */
+
+#define EDEADLOCK EDEADLK
+
+#define EBFONT 59 /* Bad font file format */
+#define ENOSTR 60 /* Device not a stream */
+#define ENODATA 61 /* No data available */
+#define ETIME 62 /* Timer expired */
+#define ENOSR 63 /* Out of streams resources */
+#define ENONET 64 /* Machine is not on the network */
+#define ENOPKG 65 /* Package not installed */
+#define EREMOTE 66 /* Object is remote */
+#define ENOLINK 67 /* Link has been severed */
+#define EADV 68 /* Advertise error */
+#define ESRMNT 69 /* Srmount error */
+#define ECOMM 70 /* Communication error on send */
+#define EPROTO 71 /* Protocol error */
+#define EMULTIHOP 72 /* Multihop attempted */
+#define EDOTDOT 73 /* RFS specific error */
+#define EBADMSG 74 /* Not a data message */
+#define EOVERFLOW 75 /* Value too large for defined data type */
+#define ENOTUNIQ 76 /* Name not unique on network */
+#define EBADFD 77 /* File descriptor in bad state */
+#define EREMCHG 78 /* Remote address changed */
+#define ELIBACC 79 /* Can not access a needed shared library */
+#define ELIBBAD 80 /* Accessing a corrupted shared library */
+#define ELIBSCN 81 /* .lib section in a.out corrupted */
+#define ELIBMAX 82 /* Attempting to link in too many shared libraries */
+#define ELIBEXEC 83 /* Cannot exec a shared library directly */
+#define EILSEQ 84 /* Illegal byte sequence */
+#define ERESTART 85 /* Interrupted system call should be restarted */
+#define ESTRPIPE 86 /* Streams pipe error */
+#define EUSERS 87 /* Too many users */
+#define ENOTSOCK 88 /* Socket operation on non-socket */
+#define EDESTADDRREQ 89 /* Destination address required */
+#define EMSGSIZE 90 /* Message too long */
+#define EPROTOTYPE 91 /* Protocol wrong type for socket */
+#define ENOPROTOOPT 92 /* Protocol not available */
+#define EPROTONOSUPPORT 93 /* Protocol not supported */
+#define ESOCKTNOSUPPORT 94 /* Socket type not supported */
+#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
+#define EPFNOSUPPORT 96 /* Protocol family not supported */
+#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
+#define EADDRINUSE 98 /* Address already in use */
+#define EADDRNOTAVAIL 99 /* Cannot assign requested address */
+#define ENETDOWN 100 /* Network is down */
+#define ENETUNREACH 101 /* Network is unreachable */
+#define ENETRESET 102 /* Network dropped connection because of reset */
+#define ECONNABORTED 103 /* Software caused connection abort */
+#define ECONNRESET 104 /* Connection reset by peer */
+#define ENOBUFS 105 /* No buffer space available */
+#define EISCONN 106 /* Transport endpoint is already connected */
+#define ENOTCONN 107 /* Transport endpoint is not connected */
+#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */
+#define ETOOMANYREFS 109 /* Too many references: cannot splice */
+#define ETIMEDOUT 110 /* Connection timed out */
+#define ECONNREFUSED 111 /* Connection refused */
+#define EHOSTDOWN 112 /* Host is down */
+#define EHOSTUNREACH 113 /* No route to host */
+#define EALREADY 114 /* Operation already in progress */
+#define EINPROGRESS 115 /* Operation now in progress */
+#define ESTALE 116 /* Stale NFS file handle */
+#define EUCLEAN 117 /* Structure needs cleaning */
+#define ENOTNAM 118 /* Not a XENIX named type file */
+#define ENAVAIL 119 /* No XENIX semaphores available */
+#define EISNAM 120 /* Is a named type file */
+#define EREMOTEIO 121 /* Remote I/O error */
+#define EDQUOT 122 /* Quota exceeded */
+
+#define ENOMEDIUM 123 /* No medium found */
+#define EMEDIUMTYPE 124 /* Wrong medium type */
+
+/************************************************************
+ * Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+extern int *get_errno_ptr(void);
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __ERRNO_H */
diff --git a/nuttx/include/mqueue.h b/nuttx/include/mqueue.h
new file mode 100644
index 0000000000..3e54ba6e80
--- /dev/null
+++ b/nuttx/include/mqueue.h
@@ -0,0 +1,120 @@
+/************************************************************
+ * mqueue.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __MQUEUE_H
+#define __MQUEUE_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include "queue.h"
+
+/************************************************************
+ * Compilations Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Type Declarations
+ ************************************************************/
+
+/* Message queue attributes */
+
+struct mq_attr
+{
+ size_t mq_maxmsg; /* Max number of messages in queue */
+ size_t mq_msgsize; /* Max message size */
+ unsigned mq_flags; /* Queue flags */
+ size_t mq_curmsgs; /* Number of messages currently in queue */
+};
+
+/* The following is used to attach a signal to a message queue
+ * to notify a task when a message is available on a queue
+ */
+
+struct sigevent {
+ int sigev_signo;
+ union sigval sigev_value;
+ int sigev_notify;
+};
+
+/* Message queue descriptor */
+
+typedef struct mq_des *mqd_t;
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN mqd_t mq_open(const char *mq_name,
+ int oflags, ... );
+EXTERN int mq_close(mqd_t mqdes );
+EXTERN int mq_unlink(const char *mq_name );
+EXTERN int mq_send(mqd_t mqdes, const void *msg,
+ size_t msglen, int prio );
+EXTERN int mq_receive(mqd_t mqdes, void *msg,
+ size_t msglen, int *prio );
+EXTERN int mq_notify(mqd_t mqdes,
+ const struct sigevent *notification );
+EXTERN int mq_setattr(mqd_t mqdes,
+ const struct mq_attr *mq_stat,
+ struct mq_attr *oldstat);
+EXTERN int mq_getattr(mqd_t mqdes,
+ struct mq_attr *mq_stat);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MQUEUE_H */
+
diff --git a/nuttx/include/nuttx/arch.h b/nuttx/include/nuttx/arch.h
new file mode 100644
index 0000000000..30f47029a3
--- /dev/null
+++ b/nuttx/include/nuttx/arch.h
@@ -0,0 +1,433 @@
+/************************************************************
+ * arch.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __ARCH_H
+#define __ARCH_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <arch/arch.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Inline functions
+ ************************************************************/
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+typedef void (*sig_deliver_t)(_TCB *tcb);
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/************************************************************
+ * These are standard interfaces that must be exported to the
+ * scheduler from architecture-specific code.
+ ************************************************************/
+
+/************************************************************
+ * Name: up_initialize
+ *
+ * Description:
+ * up_initialize will be called once during OS
+ * initialization after the basic OS services have been
+ * initialized. The architecture specific details of
+ * initializing the OS will be handled here. Such things as
+ * setting up interrupt service routines, starting the
+ * clock, and registering device drivers are some of the
+ * things that are different for each processor and hardware
+ * platform.
+ *
+ * up_initialize is called after the OS initialized but
+ * before the init process has been started and before the
+ * libraries have been initialized. OS services and driver
+ * services are available.
+ *
+ ************************************************************/
+
+EXTERN void up_initialize(void);
+
+/************************************************************
+ * Name: up_idle
+ *
+ * Description:
+ * up_idle() is the logic that will be executed
+ * when their is no other ready-to-run task. This is processor
+ * idle time and will continue until some interrupt occurs to
+ * cause a context switch from the idle task.
+ *
+ * Processing in this state may be processor-specific. e.g.,
+ * this is where power management operations might be performed.
+ *
+ ************************************************************/
+
+EXTERN void up_idle(void);
+
+/************************************************************
+ * Name: up_initial_state
+ *
+ * Description:
+ * A new thread is being started and a new TCB
+ * has been created. This function is called to initialize
+ * the processor specific portions of the new TCB.
+ *
+ * This function must setup the intial architecture registers
+ * and/or stack so that execution will begin at tcb->start
+ * on the next context switch.
+ *
+ ************************************************************/
+
+EXTERN void up_initial_state(_TCB *tcb);
+
+/************************************************************
+ * Name: up_create_stack
+ *
+ * Description:
+ * Allocate a stack for a new thread and setup
+ * up stack-related information in the TCB.
+ *
+ * The following TCB fields must be initialized:
+ * adj_stack_size: Stack size after adjustment for hardware,
+ * processor, etc. This value is retained only for debug
+ * purposes.
+ * stack_alloc_ptr: Pointer to allocated stack
+ * adj_stack_ptr: Adjusted StatckAllocPtr for HW. The
+ * initial value of the stack pointer.
+ *
+ * Inputs:
+ * tcb: The TCB of new task
+ * stack_size: The requested stack size. At least this much
+ * must be allocated.
+ ************************************************************/
+
+EXTERN STATUS up_create_stack(_TCB *tcb, uint32 stack_size);
+
+/************************************************************
+ * Name: up_use_stack
+ *
+ * Description:
+ * Setup up stack-related information in the TCB
+ * using pre-allocated stack memory
+ *
+ * The following TCB fields must be initialized:
+ * adj_stack_size: Stack size after adjustment for hardware,
+ * processor, etc. This value is retained only for debug
+ * purposes.
+ * stack_alloc_ptr: Pointer to allocated stack
+ * adj_stack_ptr: Adjusted StatckAllocPtr for HW. The
+ * initial value of the stack pointer.
+ *
+ * Inputs:
+ * tcb: The TCB of new task
+ * stack_size: The allocated stack size.
+ *
+ ************************************************************/
+
+EXTERN STATUS up_use_stack(_TCB *tcb, uint32 *stack, uint32 stack_size);
+
+/************************************************************
+ * Name: up_release_stack
+ *
+ * Description:
+ * A task has been stopped. Free all stack
+ * related resources retained int the defunct TCB.
+ *
+ ************************************************************/
+
+EXTERN void up_release_stack(_TCB *dtcb);
+
+/************************************************************
+ * Name: up_unblock_task
+ *
+ * Description:
+ * A task is currently in an inactive task list
+ * but has been prepped to execute. Move the TCB to the
+ * ready-to-run list, restore its context, and start execution.
+ *
+ * Inputs:
+ * tcb: Refers to the tcb to be unblocked. This tcb is
+ * in one of the waiting tasks lists. It must be moved to
+ * the ready-to-run list and, if it is the highest priority
+ * ready to run taks, executed.
+ *
+ ************************************************************/
+
+EXTERN void up_unblock_task(_TCB *tcb);
+
+/************************************************************
+ * Name: up_block_task
+ *
+ * Description:
+ * The currently executing task at the head of
+ * the ready to run list must be stopped. Save its context
+ * and move it to the inactive list specified by task_state.
+ *
+ * Inputs:
+ * tcb: Refers to a task in the ready-to-run list (normally
+ * the task at the the head of the list). It most be
+ * stopped, its context saved and moved into one of the
+ * waiting task lists. It it was the task at the head
+ * of the ready-to-run list, then a context to the new
+ * ready to run task must be performed.
+ * task_state: Specifies which waiting task list should be
+ * hold the blocked task TCB.
+ *
+ ************************************************************/
+
+EXTERN void up_block_task(_TCB *tcb, tstate_t task_state);
+
+/************************************************************
+ * Name: up_release_pending
+ *
+ * Description:
+ * Release and ready-to-run tasks that have
+ * collected in the pending task list. This can call a
+ * context switch if a new task is placed at the head of
+ * the ready to run list.
+ *
+ ************************************************************/
+
+EXTERN void up_release_pending(void);
+
+/************************************************************
+ * Name: up_reprioritize_rtr
+ *
+ * Description:
+ * Called when the priority of a running or
+ * ready-to-run task changes and the reprioritization will
+ * cause a context switch. Two cases:
+ *
+ * 1) The priority of the currently running task drops and the next
+ * task in the ready to run list has priority.
+ * 2) An idle, ready to run task's priority has been raised above the
+ * the priority of the current, running task and it now has the
+ * priority.
+ *
+ * Inputs:
+ * tcb: The TCB of the task that has been reprioritized
+ * priority: The new task priority
+ *
+ ************************************************************/
+
+EXTERN void up_reprioritize_rtr(_TCB *tcb, ubyte priority);
+
+/************************************************************
+ * Name: _exit
+ *
+ * Description:
+ * This function causes the currently executing task to cease
+ * to exist. This is a special case of task_delete().
+ *
+ ************************************************************/
+/* Prototype is in unistd.h */
+
+/************************************************************
+ * Name: ip_assert and up_assert_code
+ *
+ * Description:
+ * Assertions may be handled in an architecture-specific
+ * way.
+ *
+ ************************************************************/
+/* Prototype is in assert.h */
+
+/************************************************************
+ * Name: up_schedule_sigaction
+ *
+ * Description:
+ * This function is called by the OS when one or more
+ * signal handling actions have been queued for execution.
+ * The architecture specific code must configure things so
+ * that the 'igdeliver' callback is executed on the thread
+ * specified by 'tcb' as soon as possible.
+ *
+ * This function may be called from interrupt handling logic.
+ *
+ * This operation should not cause the task to be unblocked
+ * nor should it cause any immediate execution of sigdeliver.
+ * Typically, a few cases need to be considered:
+ *
+ * (1) This function may be called from an interrupt handler
+ * During interrupt processing, all xcptcontext structures
+ * should be valid for all tasks. That structure should
+ * be modified to invoke sigdeliver() either on return
+ * from (this) interrupt or on some subsequent context
+ * switch to the recipient task.
+ * (2) If not in an interrupt handler and the tcb is NOT
+ * the currently executing task, then again just modify
+ * the saved xcptcontext structure for the recipient
+ * task so it will invoke sigdeliver when that task is
+ * later resumed.
+ * (3) If not in an interrupt handler and the tcb IS the
+ * currently executing task -- just call the signal
+ * handler now.
+ *
+ ************************************************************/
+
+EXTERN void up_schedule_sigaction(_TCB *tcb, sig_deliver_t sigdeliver);
+
+/************************************************************
+ * Name: up_allocate_heap
+ *
+ * Description:
+ * The heap may be statically allocated by
+ * defining CONFIG_HEAP_BASE and CONFIG_HEAP_SIZE. If these
+ * are not defined, then this function will be called to
+ * dynamically set aside the heap region.
+ *
+ ************************************************************/
+
+#ifndef CONFIG_HEAP_BASE
+EXTERN void up_allocate_heap(void **heap_start, size_t *heap_size);
+#endif
+
+/************************************************************
+ * Name: up_interrupt_context
+ *
+ * Description:
+ * Return TRUE is we are currently executing in
+ * the interrupt handler context.
+ *
+ ************************************************************/
+
+EXTERN boolean up_interrupt_context(void);
+
+/************************************************************
+ * Name: up_disable_irq
+ *
+ * Description:
+ * Disable the IRQ specified by 'irq'
+ *
+ ************************************************************/
+
+EXTERN void up_disable_irq(int irq);
+
+/************************************************************
+ * Name: up_enable_irq
+ *
+ * Description:
+ * Enable the IRQ specified by 'irq'
+ *
+ ************************************************************/
+
+EXTERN void up_enable_irq(int irq);
+
+/************************************************************
+ * Name: up_acknowledge_irq
+ *
+ * Description:
+ * Disable the IRQ specified by 'irq'
+ *
+ ************************************************************/
+
+EXTERN void up_disable_irq(int irq);
+
+/************************************************************
+ * These are standard interfaces that are exported by the OS
+ * for use by the architecture specific logic
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_process_timer
+ *
+ * Description:
+ * This function handles system timer events.
+ * The timer interrupt logic itself is implemented in the
+ * architecture specific code, but must call the following OS
+ * function periodically -- the calling interval must be
+ * MSEC_PER_TICK.
+ *
+ ************************************************************/
+
+EXTERN void sched_process_timer(void);
+
+/************************************************************
+ * Name: irq_dispatch
+ *
+ * Description:
+ * This function must be called from the achitecture-
+ * specific logic in order to dispaly an interrupt to
+ * the appropriate, registered handling logic.
+ *
+ ***********************************************************/
+
+EXTERN void irq_dispatch(int irq, struct xcptcontext *xcp);
+
+/************************************************************
+ * Debug interfaces exported by the architecture-specific
+ * logic
+ ************************************************************/
+
+/************************************************************
+ * Name: up_putc
+ *
+ * Description:
+ * Output one character on the console
+ *
+ ************************************************************/
+
+# ifdef CONFIG_ARCH_LOWPUTC
+EXTERN int up_putc(int ch);
+#endif
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ARCH_H */
+
diff --git a/nuttx/include/nuttx/compiler.h b/nuttx/include/nuttx/compiler.h
new file mode 100644
index 0000000000..6204faa7b7
--- /dev/null
+++ b/nuttx/include/nuttx/compiler.h
@@ -0,0 +1,73 @@
+/************************************************************
+ * compiler.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __COMPILER_H
+#define __COMPILER_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define weak_alias(name, aliasname) \
+ extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
+#define weak_function __attribute__ ((weak))
+#define weak_const_function __attribute__ ((weak, __const__))
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __COMPILER_H */
diff --git a/nuttx/include/nuttx/fs.h b/nuttx/include/nuttx/fs.h
new file mode 100644
index 0000000000..085c050ef6
--- /dev/null
+++ b/nuttx/include/nuttx/fs.h
@@ -0,0 +1,215 @@
+/************************************************************
+ * fs.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __FS_H
+#define __FS_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include <nuttx/compiler.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Type Definitions
+ ************************************************************/
+
+/* This structure is provided by filesystems when they are
+ * registered with the system. It is used to call back to
+ * perform fs/device specific operations.
+ */
+
+struct file;
+struct file_operations
+{
+ int (*open)(struct file *);
+ int (*close)(struct file *);
+// off_t (*llseek)(struct file *, off_t, int);
+ ssize_t (*read)(struct file *, char *, size_t);
+ ssize_t (*write)(struct file *, const char *, size_t);
+// unsigned int (*poll)(struct file *, struct poll_table_struct *);
+ int (*ioctl)(struct file *, int, unsigned long);
+};
+
+/* This structure represents one inode in the Nuttx psuedo-file system */
+
+struct inode
+{
+ struct inode *i_peer; /* Pointer to inode at same level */
+ struct inode *i_child; /* Pointer to inode at lower level */
+ struct file_operations *i_ops; /* Driver file operations for inode */
+ sint16 i_crefs; /* References to inode */
+ uint16 i_flags; /* flags for inode */
+#ifdef CONFIG_FILE_MODE
+ mode_t i_mode; /* Access mode flags */
+#endif
+ void *i_private; /* Driver private data */
+ char i_name[1]; /* Name of inode (variable length) */
+};
+#define FSNODE_SIZE(n) (sizeof(struct inode) + (n))
+
+/* This is the underlying representation of a ropen file.
+ * A file descriptor is an index into an array of such types.
+ * The type associates the file descriptor to the file state
+ * and to a set of inode operations.
+ */
+
+struct file
+{
+ int f_oflags; /* Open mode flags */
+ off_t f_pos; /* File position */
+ struct inode *f_inode; /* Driver interface */
+};
+
+/* This defines a list of files indexed by the file descriptor */
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+struct filelist
+{
+ sem_t fl_sem; /* Manage access to the file list */
+ sint16 fl_crefs; /* Reference count */
+ struct file fl_files[CONFIG_NFILE_DESCRIPTORS];
+};
+#endif
+
+/* This defines the list of files used for standard C I/O
+ * We can support the standard C APIs without or without buffering
+ */
+
+/* Buffered file I/O structure */
+
+#if CONFIG_NFILE_STREAMS > 0
+struct file_struct
+{
+ int fs_filedes; /* File descriptor associated with stream */
+ mode_t fs_oflags; /* Open mode flags */
+#if CONFIG_NUNGET_CHARS > 0
+ uint8 fs_nungotten; /* The number of characters buffered for ungetc */
+ unsigned char fs_ungotten[CONFIG_NUNGET_CHARS];
+#endif
+#if CONFIG_STDIO_BUFFER_SIZE > 0
+ sem_t fs_sem; /* For thread safety */
+ pid_t fs_holder; /* Holder of sem */
+ int fs_counts; /* Number of times sem is held */
+ unsigned char *fs_bufstart; /* Pointer to start of buffer */
+ unsigned char *fs_bufend; /* Pointer to 1 past end of buffer */
+ unsigned char *fs_bufpos; /* Current position in buffer */
+ unsigned char *fs_bufread; /* Pointer to 1 past last buffered read char. */
+#endif
+};
+
+struct streamlist
+{
+ int sl_crefs; /* Reference count */
+ sem_t sl_sem; /* For thread safety */
+ struct file_struct sl_streams[CONFIG_NFILE_STREAMS];
+};
+#endif /* CONFIG_NFILE_STREAMS */
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/* fs_inode.c ***********************************************/
+
+/* These interfaces are used by drivers to register their
+ * inodes in the inode tree.
+ */
+
+EXTERN void weak_function fs_initialize(void);
+EXTERN STATUS register_inode(const char *path,
+ struct file_operations *fops,
+ mode_t mode, void *private);
+EXTERN STATUS unregister_inode(const char *path);
+
+/* fs_open.c ************************************************/
+
+EXTERN int inode_checkflags(struct inode *inode, int oflags);
+
+/* fs_files.c ***********************************************/
+
+#if CONFIG_NFILE_DESCRIPTORS >0
+EXTERN struct filelist *files_alloclist(void);
+EXTERN int files_addreflist(struct filelist *list);
+EXTERN int files_releaselist(struct filelist *list);
+EXTERN int files_dup(struct file *filep1, struct file *filep2);
+#endif
+
+/* lib_fopen.c **********************************************/
+
+/* Used by the OS to clone stdin, stdout, stderr */
+
+#if CONFIG_NFILE_STREAMS > 0
+EXTERN struct file_struct *lib_fdopen(int fd,
+ const char *mode,
+ struct filelist *flist,
+ struct streamlist *slist);
+#endif
+
+/* lib_fflush.c *********************************************/
+
+#if CONFIG_NFILE_STREAMS > 0
+EXTERN void lib_flushall(struct streamlist *list);
+#endif
+
+/* drivers **************************************************/
+
+/* Call in of these to register the corresponding default
+ * default drivers in the drivers/ subdirectory
+ */
+
+EXTERN void devnull_register(void);
+
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __FS_H */
diff --git a/nuttx/include/nuttx/irq.h b/nuttx/include/nuttx/irq.h
new file mode 100644
index 0000000000..e1b1c5a65c
--- /dev/null
+++ b/nuttx/include/nuttx/irq.h
@@ -0,0 +1,107 @@
+/************************************************************
+ * irq.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __IRQ_H
+#define __IRQ_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+# include <sys/types.h>
+# include <assert.h>
+#endif
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+# define irq_detach(isr) irq_attach(isr, NULL)
+#endif
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/* This struct defines the way the registers are stored */
+
+#ifndef __ASSEMBLY__
+struct xcptcontext; /* forward reference */
+
+typedef int (*xcpt_t)(int irq, struct xcptcontext *xcp);
+typedef int (*swint_t)(uint32 code, uint32 parm2, uint32 parm3,
+ struct xcptcontext *xcp);
+#endif
+
+/* Now include architecture-specific types */
+
+#include <arch/irq.h>
+
+/************************************************************
+ * Inline functions
+ ************************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+struct xcptcontext *current_xcp;
+#endif
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN int irq_attach(int irq, xcpt_t isr);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+#endif
+
+#endif /* __IRQ_H */
+
diff --git a/nuttx/include/nuttx/kmalloc.h b/nuttx/include/nuttx/kmalloc.h
new file mode 100644
index 0000000000..5a48f33d2e
--- /dev/null
+++ b/nuttx/include/nuttx/kmalloc.h
@@ -0,0 +1,94 @@
+/************************************************************
+ * kmalloc.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __KMALLOC_H
+#define __KMALLOC_H
+
+/************************************************************
+ * Included Functions
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/************************************************************
+ * Pulblic Function Prototypes
+ ************************************************************/
+
+#undef KMALLOC_EXTERN
+#if defined(__cplusplus)
+# define KMALLOC_EXTERN extern "C"
+extern "C" {
+#else
+# define KMALLOC_EXTERN extern
+#endif
+
+#ifndef CONFIG_ARCH_KMALLOC
+# include <stdlib.h>
+# define kmalloc(s) malloc(s)
+#else
+KMALLOC_EXTERN void *kmalloc(size_t);
+#endif
+
+#ifndef CONFIG_ARCH_KZMALLOC
+# include <stdlib.h>
+# define kzmalloc(s) zalloc(s)
+#else
+KMALLOC_EXTERN void *kzalloc(size_t);
+#endif
+
+#ifndef CONFIG_ARCH_KFREE
+# include <stdlib.h>
+# define kfree(p) free(p)
+#else
+KMALLOC_EXTERN void kfree(void*);
+#endif
+
+/* Functions defined in os_list.c ***************************/
+
+/* Handles memory freed from an interrupt handler */
+
+KMALLOC_EXTERN void sched_free(void *address);
+
+#undef KMALLOC_EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __KMALLOC_H */
diff --git a/nuttx/include/nuttx/lib.h b/nuttx/include/nuttx/lib.h
new file mode 100644
index 0000000000..52f506897b
--- /dev/null
+++ b/nuttx/include/nuttx/lib.h
@@ -0,0 +1,79 @@
+/************************************************************
+ * lib.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __LIB_H
+#define __LIB_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/fs.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/* Functions contained in lib_init.c ************************/
+
+EXTERN void weak_function lib_initialize(void);
+#if CONFIG_NFILE_STREAMS > 0
+EXTERN struct streamlist *lib_alloclist(void);
+EXTERN void lib_addreflist(struct streamlist *list);
+EXTERN void lib_releaselist(struct streamlist *list);
+#endif
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __LIB_H */
diff --git a/nuttx/include/nuttx/os_external.h b/nuttx/include/nuttx/os_external.h
new file mode 100644
index 0000000000..6d96b5afb4
--- /dev/null
+++ b/nuttx/include/nuttx/os_external.h
@@ -0,0 +1,85 @@
+/************************************************************
+ * os_external.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __OS_EXTERNAL_H
+#define __OS_EXTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <pthread.h>
+#include <sched.h>
+#include <nuttx/compiler.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/* These are functions that must be supplied by the application */
+
+EXTERN void weak_function user_initialize(void);
+EXTERN int user_start(int parm1, int parm2, int parm3, int parm4);
+
+/* Functions contained in os_task.c *************************/
+
+EXTERN void os_start(void); /* OS entry point called by boot logic */
+
+/* Functions contained in mm_init.c *************************/
+
+EXTERN void mm_initialize(void *heap_start, size_t heap_size);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __OS_EXTERNAL_H */
diff --git a/nuttx/include/pthread.h b/nuttx/include/pthread.h
new file mode 100644
index 0000000000..c15cf3e0f6
--- /dev/null
+++ b/nuttx/include/pthread.h
@@ -0,0 +1,326 @@
+/************************************************************
+ * pthread.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __PTHREAD_H
+#define __PTHREAD_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h> /* Default settings */
+#include <sys/types.h> /* Needed for general types */
+#include <semaphore.h> /* Needed for sem_t */
+#include <time.h> /* Needed for struct timespec */
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/* Standard POSIX switches */
+
+#ifndef _POSIX_THREADS
+#define _POSIX_THREADS
+#endif
+#ifndef _POSIX_THREAD_ATTR_STACKSIZE
+#define _POSIX_THREAD_ATTR_STACKSIZE
+#endif
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define PTHREAD_PROCESS_PRIVATE 0
+#define PTHREAD_PROCESS_SHARED 1
+
+#define PTHREAD_STACK_MIN CONFIG_PTHREAD_STACK_MIN
+#define PTHREAD_STACK_DEFAULT CONFIG_PTHREAD_STACK_DEFAULT
+
+#define PTHREAD_INHERIT_SCHED 0
+#define PTHREAD_EXPLICIT_SCHED 1
+
+#define PTHREAD_PRIO_NONE 0
+#define PTHREAD_PRIO_INHERIT 1
+#define PTHREAD_PRIO_PROTECT 2
+
+#define PTHREAD_DEFAULT_PRIORITY 100
+
+/* Cancellation states returned by pthread_cancelstate() */
+
+#define PTHREAD_CANCEL_ENABLE (0)
+#define PTHREAD_CANCEL_DISABLE (1)
+
+/* Thread return value when a pthread is canceled */
+
+#define PTHREAD_CANCELED ((void*)-1)
+
+/************************************************************
+ * Global Type Declarations
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/*----------------------------------------------------------*
+ PTHREAD-SPECIFIC TYPES
+ *----------------------------------------------------------*/
+
+typedef int pthread_key_t;
+typedef void *pthread_addr_t;
+typedef pthread_addr_t any_t;
+
+typedef pthread_addr_t (*pthread_startroutine_t)(pthread_addr_t);
+typedef pthread_startroutine_t pthread_func_t;
+
+struct pthread_addr_s
+{
+ unsigned long stacksize; /* Size of the stack allocated for the pthead */
+ short priority; /* Priority of the pthread */
+ ubyte policy; /* Pthread scheduler policy */
+ ubyte inheritsched; /* Inherit parent prio/policy? */
+};
+typedef struct pthread_addr_s pthread_attr_t;
+
+struct pthread_s
+{
+ int pid;
+};
+typedef struct pthread_s pthread_t;
+
+typedef int pthread_condattr_t;
+
+struct pthread_cond_s
+{
+ sem_t sem;
+};
+typedef struct pthread_cond_s pthread_cond_t;
+#define PTHREAD_COND_INITIALIZER {{0, 0xffff}}
+
+struct pthread_mutexattr_s
+{
+ int pshared;
+};
+typedef struct pthread_mutexattr_s pthread_mutexattr_t;
+
+struct pthread_mutex_s
+{
+ int pid;
+ sem_t sem;
+};
+typedef struct pthread_mutex_s pthread_mutex_t;
+#define PTHREAD_MUTEX_INITIALIZER {0, {1, 0xffff}}
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+/*----------------------------------------------------------*
+ * Initializes a thread attributes object (attr) with default
+ * values for all of the individual attributes used by a
+ * given implementation.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_attr_init(pthread_attr_t *attr);
+
+/*----------------------------------------------------------*
+ * An attributes object can be deleted when it is no longer
+ * needed.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_attr_destroy(pthread_attr_t *attr);
+
+/*----------------------------------------------------------*
+ * Set or obtain the default scheduling algorithm
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
+EXTERN int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
+EXTERN int pthread_attr_setschedparam(pthread_attr_t *attr,
+ const struct sched_param *param);
+EXTERN int pthread_attr_getschedparam(pthread_attr_t *attr,
+ struct sched_param *param);
+EXTERN int pthread_attr_setinheritsched(pthread_attr_t *attr,
+ int inheritsched);
+EXTERN int pthread_attr_getinheritsched(const pthread_attr_t *attr,
+ int *inheritsched);
+
+/*----------------------------------------------------------*
+ * Set or obtain the default stack size
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_attr_setstacksize(pthread_attr_t *attr, long stacksize);
+EXTERN int pthread_attr_getstacksize(pthread_attr_t *attr, long *stackaddr);
+
+/*----------------------------------------------------------*
+ * To create a thread object and runnable thread, a routine
+ * must be specified as the new thread's start routine. An
+ * argument may be passed to this routine, as an untyped
+ * address; an untyped address may also be returned as the
+ * routine's value. An attributes object may be used to
+ * specify details about the kind of thread being created.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_create(pthread_t *thread, pthread_attr_t *attr,
+ pthread_startroutine_t startRoutine,
+ pthread_addr_t arg);
+
+/*----------------------------------------------------------*
+ * A thread object may be "detached" to specify that the
+ * return value and completion status will not be requested.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_detach(pthread_t thread);
+
+/*----------------------------------------------------------*
+ * A thread may terminate it's own execution or the
+ * execution of another thread.
+ *----------------------------------------------------------*/
+
+EXTERN void pthread_exit(pthread_addr_t pvValue) __attribute__ ((noreturn));
+EXTERN int pthread_cancel(pthread_t thread);
+EXTERN int pthread_setcancelstate(int state, int *oldstate);
+EXTERN void pthread_testcancel(void);
+
+/*----------------------------------------------------------*
+ * A thread can await termination of another thread and retrieve
+ * the return value of the thread.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_join(pthread_t thread, pthread_addr_t *ppvValue);
+
+/*----------------------------------------------------------*
+ * A thread may tell the scheduler that its processor can be
+ * made available.
+ *----------------------------------------------------------*/
+
+EXTERN void pthread_yield(void);
+
+/*----------------------------------------------------------*
+ * A thread may obtain a copy of its own thread handle.
+ *----------------------------------------------------------*/
+
+EXTERN pthread_t pthread_self(void);
+
+/*----------------------------------------------------------*
+ * Thread scheduling parameters
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_getschedparam(pthread_t thread, int *policy,
+ struct sched_param *param);
+EXTERN int pthread_setschedparam(pthread_t thread, int policy,
+ const struct sched_param *param);
+
+/*----------------------------------------------------------*
+ * Thread-specific Data Interfaces
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_key_create(pthread_key_t *key,
+ void (*destructor)(void*));
+EXTERN int pthread_setspecific(pthread_key_t key, void *value);
+EXTERN void *pthread_getspecific(pthread_key_t key);
+EXTERN int pthread_key_delete(pthread_key_t key);
+
+/*----------------------------------------------------------*
+ * Create, operate on, and destroy mutex attributes.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_mutexattr_init(pthread_mutexattr_t *attr);
+EXTERN int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
+EXTERN int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr,
+ int *pshared);
+EXTERN int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr,
+ int pshared);
+
+/*----------------------------------------------------------*
+ * The following routines create, delete, lock and unlock
+ * mutexes.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_mutex_init(pthread_mutex_t *mutex,
+ pthread_mutexattr_t *attr);
+EXTERN int pthread_mutex_destroy(pthread_mutex_t *mutex);
+EXTERN int pthread_mutex_lock(pthread_mutex_t *mutex);
+EXTERN int pthread_mutex_trylock(pthread_mutex_t *mutex);
+EXTERN int pthread_mutex_unlock(pthread_mutex_t *mutex);
+
+/*----------------------------------------------------------*
+ * Operations on condition variables
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_condattr_init(pthread_condattr_t *attr);
+EXTERN int pthread_condattr_destroy(pthread_condattr_t *attr);
+
+/*----------------------------------------------------------*
+ * A thread can create and delete condition variables.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
+EXTERN int pthread_cond_destroy(pthread_cond_t *cond);
+
+/*----------------------------------------------------------*
+ * A thread can signal to and broadcast on a condition variable.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_cond_broadcast(pthread_cond_t *cond);
+EXTERN int pthread_cond_signal(pthread_cond_t *dond);
+
+/*----------------------------------------------------------*
+ * A thread can wait for a condition variable to be signalled
+ * or broadcast.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
+
+/*----------------------------------------------------------*
+ * A thread can perform a timed wait on a condition variable.
+ *----------------------------------------------------------*/
+
+EXTERN int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
+ const struct timespec *abstime);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __PTHREAD_H */
+
diff --git a/nuttx/include/queue.h b/nuttx/include/queue.h
new file mode 100644
index 0000000000..17f4ddec02
--- /dev/null
+++ b/nuttx/include/queue.h
@@ -0,0 +1,118 @@
+/************************************************************************
+ * queue.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************************/
+
+#ifndef __QUEUE_H
+#define __QUEUE_H
+
+/************************************************************************
+ * Included Files
+ ************************************************************************/
+
+#include <sys/types.h>
+
+/************************************************************************
+ * Definitions
+ ************************************************************************/
+
+#define sq_init(q) do { (q)->head = NULL; (q)->tail = NULL; } while (0)
+#define dq_init(q) do { (q)->head = NULL; (q)->tail = NULL; } while (0)
+#define sq_next(p) ((p)->flink)
+#define dq_next(p) ((p)->flink)
+#define dq_prev(p) ((p)->prev)
+
+/************************************************************************
+ * Global Type Declarations
+ ************************************************************************/
+
+struct sq_entry_s
+{
+ struct sq_entry_s *flink;
+};
+typedef struct sq_entry_s sq_entry_t;
+
+struct dq_entry_s
+{
+ struct dq_entry_s *flink, *blink;
+};
+typedef struct dq_entry_s dq_entry_t;
+
+struct sq_queue_s
+{
+ sq_entry_t *head, *tail;
+};
+typedef struct sq_queue_s sq_queue_t;
+
+struct dq_queue_s
+{
+ dq_entry_t *head, *tail;
+};
+typedef struct dq_queue_s dq_queue_t;
+
+/************************************************************************
+ * Global Function Prototypes
+ ************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN void sq_addfirst(sq_entry_t *node, sq_queue_t *queue);
+EXTERN void dq_addfirst(dq_entry_t *node, dq_queue_t *queue);
+EXTERN void sq_addlast(sq_entry_t *node, sq_queue_t *queue);
+EXTERN void dq_addlast(dq_entry_t *node, dq_queue_t *queue);
+EXTERN void sq_addafter(sq_entry_t *prev, sq_entry_t *node,
+ sq_queue_t *queue);
+EXTERN void dq_addafter(dq_entry_t *prev, dq_entry_t *node,
+ dq_queue_t *queue);
+EXTERN void dq_addbefore(dq_entry_t *next, dq_entry_t *node,
+ dq_queue_t *queue);
+EXTERN sq_entry_t *sq_remafter(sq_entry_t *node, sq_queue_t *queue);
+EXTERN void sq_rem(sq_entry_t *node, sq_queue_t *queue);
+EXTERN void dq_rem(dq_entry_t *node, dq_queue_t *queue);
+EXTERN sq_entry_t *sq_remlast(sq_queue_t *queue);
+EXTERN dq_entry_t *dq_remlast(dq_queue_t *queue);
+EXTERN sq_entry_t *sq_remfirst(sq_queue_t *queue);
+EXTERN dq_entry_t *dq_remfirst(dq_queue_t *queue);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __QUEUE_H_ */
+
diff --git a/nuttx/include/sched.h b/nuttx/include/sched.h
new file mode 100644
index 0000000000..c5d7e1163a
--- /dev/null
+++ b/nuttx/include/sched.h
@@ -0,0 +1,308 @@
+/************************************************************
+ * sched.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __SCHED_H
+#define __SCHED_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <queue.h>
+#include <signal.h>
+#include <semaphore.h>
+#include <pthread.h>
+#include <mqueue.h>
+#include <time.h>
+#include <nuttx/irq.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* Task Management Definitins *******************************/
+
+/* This is the number of arguments that are passed to tasks
+ * on start-up. This number was selected because this is the
+ * number of parameters that can be passed to a MIPS function
+ * in registers
+ . */
+
+#define NUM_TASK_ARGS 4
+
+/* This is the maximum number of times that a lock can be set */
+
+#define MAX_LOCK_COUNT 127
+
+/* Values for the _TCB flags flag bits */
+
+#define TCB_FLAG_PTHREAD 0x0001 /* Thread is a pthread */
+#define TCB_FLAG_NONCANCELABLE 0x0002 /* Pthread is non-cancelable */
+#define TCB_FLAG_CANCEL_PENDING 0x0004 /* Pthread cancel is pending */
+#define TCB_FLAG_ROUND_ROBIN 0x0008 /* Round robin sched enabled */
+
+/* Pthread definitions **************************************/
+
+#define PTHREAD_KEYS_MAX CONFIG_NPTHREAD_KEYS
+
+/************************************************************
+ * Global Type Definitions
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+
+/* General Task Management Types ****************************/
+
+/* This is the type of the task_state field of the TCB.
+ * NOTE: the order and content of this enumeration is
+ * critical since there are some OS tables indexed by these
+ * values.
+ */
+
+typedef enum tstate_e
+{
+ TSTATE_TASK_INVALID = 0, /* INVALID - TCB has not yet been initialized */
+
+ TSTATE_TASK_PENDING = 1, /* READY_TO_RUN - Pending preemption unlock */
+ TSTATE_TASK_READYTORUN = 2, /* READY-TO-RUN - But not running */
+ TSTATE_TASK_RUNNING = 3, /* READY_TO_RUN - Aand running */
+
+ TSTATE_TASK_INACTIVE = 4, /* BLOCKED - Initialized but not yet activated */
+ TSTATE_WAIT_SEM = 5, /* BLOCKED - Waiting for a semaphore */
+ TSTATE_WAIT_SIG = 6, /* BLOCKED - Waiting for a signal */
+ TSTATE_WAIT_MQNOTEMPTY = 7, /* BLOCKED - Waiting for a MQ to become not empty. */
+ TSTATE_WAIT_MQNOTFULL = 8 /* BLOCKED - Waiting for a MQ to become not full. */
+};
+typedef enum tstate_e tstate_t;
+
+/* The following definitions are determined by tstate_t */
+
+#define FIRST_READY_TO_RUN_STATE TSTATE_TASK_READYTORUN
+#define LAST_READY_TO_RUN_STATE TSTATE_TASK_RUNNING
+#define FIRST_BLOCKED_STATE TSTATE_TASK_INACTIVE
+#define LAST_BLOCKED_STATE TSTATE_WAIT_MQNOTFULL
+#define NUM_TASK_STATES 9
+
+/* The following is the form of a thread start-up function */
+
+typedef void (*start_t)(void);
+
+/* This is the entry point into the main thread of the task
+ * or into a created pthread within the task.
+ */
+
+union entry_u
+{
+ pthread_startroutine_t pthread;
+ main_t main;
+};
+typedef union entry_u entry_t;
+
+/* This is the type of the function that is executed with
+ * exit() is called (if registered via atexit()).
+ */
+
+typedef void (*exitfunc_t)(void);
+
+/* POSIX Message queue */
+
+typedef struct msgq_s msgq_t;
+
+/* This is the task control block (TCB) */
+
+struct _TCB
+{
+ /* Fields used to support list management ***************************/
+
+ struct _TCB *flink, *blink; /* link in DQ of TCBs */
+
+ /* Task Management Fields *******************************************/
+
+ pid_t pid; /* This is the ID of the thread */
+ start_t start; /* Thread start function */
+ entry_t entry; /* Entry Point into the thread */
+ exitfunc_t exitfunc; /* Called if exit is called. */
+ ubyte sched_priority; /* Current priority of the thread */
+ tstate_t task_state; /* Current state of the thread */
+ uint16 flags; /* Misc. general status flags */
+ sint16 lockcount; /* 0=preemptable (not-locked) */
+ void *joininfo; /* Detach-able info to support join */
+#if CONFIG_RR_INTERVAL > 0
+ int timeslice; /* RR timeslice interval remaining */
+#endif
+
+ /* Values needed to restart a task **********************************/
+
+ ubyte init_priority; /* Initial priority of the task */
+ char *argv[NUM_TASK_ARGS+1]; /* Name + start-up parameters */
+
+ /* Stack-Related Fields *********************************************/
+
+ uint32 adj_stack_size; /* Stack size after adjustment */
+ /* for hardware, processor, etc. */
+ /* (for debug purposes only) */
+ uint32 *stack_alloc_ptr; /* Pointer to allocated stack */
+ /* Need to deallocate stack */
+ uint32 *adj_stack_ptr; /* Adjusted StatckAllocPtr for HW */
+ /* The initial stack pointer value */
+
+ /* POSIX thread Specific Data ***************************************/
+
+ void *pthread_data[CONFIG_NPTHREAD_KEYS];
+
+ /* POSIX Semaphore Control Fields ***********************************/
+
+ sem_t *waitsem; /* Semaphore ID waiting on */
+
+ /* POSIX Signal Control Fields **************************************/
+
+ sigset_t sigprocmask; /* Signals that are blocked */
+ sigset_t sigwaitmask; /* Waiting for pending signals */
+ sq_queue_t sigactionq; /* List of actions for signals */
+ sq_queue_t sigpendingq; /* List of Pending Signals */
+ sq_queue_t sigpendactionq; /* List of pending signal actions */
+ sq_queue_t sigpostedq; /* List of posted signals */
+ siginfo_t sigunbinfo; /* Signal info when task unblocked */
+
+ /* POSIX Named Message Queue Fields *********************************/
+
+ sq_queue_t msgdesq; /* List of opened message queues */
+ msgq_t *msgwaitq; /* Waiting for this message queue */
+
+ /* Library related fields *******************************************/
+
+ int errno; /* Current per-thread errno */
+
+ /* File system support **********************************************/
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+ struct filelist *filelist; /* Maps file descriptor to file */
+#endif
+
+#if CONFIG_NFILE_STREAMS > 0
+ struct streamlist *streams; /* Holds C buffered I/O info */
+#endif
+
+ /* State save areas *************************************************/
+ /* The form and content of these fields are processor-specific. */
+
+ struct xcptcontext xcp; /* Interrupt register save area */
+
+#if CONFIG_TASK_NAME_SIZE > 0
+ char name[CONFIG_TASK_NAME_SIZE]; /* Task name */
+#endif
+
+};
+typedef struct _TCB _TCB;
+#endif /* __ASSEMBLY__ */
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#ifndef __ASSEMBLY__
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/* Task Control Interfaces (non-standard) */
+
+EXTERN STATUS task_init(_TCB *tcb , char *name, int priority,
+ uint32 *stack, uint32 stack_size, main_t entry,
+ char *arg1, char *arg2, char *arg3, char *arg4);
+EXTERN STATUS task_activate(_TCB *tcb);
+EXTERN int task_create(char *name, int priority, int stack_size, main_t main,
+ char *arg1, char *arg2, char *arg3, char *arg4);
+EXTERN STATUS task_delete(pid_t pid);
+EXTERN STATUS task_restart(pid_t pid);
+
+/* Task Scheduling Interfaces (based on POSIX APIs) */
+
+EXTERN int sched_setparam(pid_t pid,
+ const struct sched_param * param);
+EXTERN int sched_getparam(pid_t pid,
+ struct sched_param * param);
+EXTERN int sched_setscheduler(pid_t pid, int policy,
+ const struct sched_param * param);
+EXTERN int sched_getscheduler(pid_t pid);
+EXTERN int sched_yield(void);
+EXTERN int sched_get_priority_max(int policy);
+EXTERN int sched_get_priority_min(int policy);
+EXTERN int sched_rr_get_interval(pid_t pid,
+ struct timespec * interval);
+
+/* Task Switching Interfaces (non-standard) */
+
+EXTERN STATUS sched_lock(void);
+EXTERN STATUS sched_unlock(void);
+EXTERN sint32 sched_lockcount(void);
+
+/* If instrumentation of the scheduler is enabled, then some
+ * outboard logic must provide the following interfaces.
+ */
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION
+
+EXTERN void sched_note_start(_TCB *tcb );
+EXTERN void sched_note_stop(_TCB *tcb );
+EXTERN void sched_note_switch(_TCB *pFromTcb, _TCB *pToTcb);
+
+#else
+# define sched_note_start(t)
+# define sched_note_stop(t)
+# define sched_note_switch(t1, t2)
+#endif /* CONFIG_SCHED_INSTRUMENTATION */
+
+/* File system helpers */
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+EXTERN struct filelist *sched_getfiles(void);
+#if CONFIG_NFILE_STREAMS > 0
+EXTERN struct streamlist *sched_getstreams(void);
+#endif /* CONFIG_NFILE_STREAMS */
+#endif /* CONFIG_NFILE_DESCRIPTORS */
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+#endif /* __ASSEMBLY__ */
+
+#endif /* __SCHED_H */
diff --git a/nuttx/include/semaphore.h b/nuttx/include/semaphore.h
new file mode 100644
index 0000000000..92ac7ab898
--- /dev/null
+++ b/nuttx/include/semaphore.h
@@ -0,0 +1,103 @@
+/************************************************************
+ * semaphore.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __SEMAPHORE_H
+#define __SEMAPHORE_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* The maximum value that a semaphore may have. */
+
+#define SEM_MAX_VALUE 0x7fff /* Max value POSIX counting semaphore */
+
+/* The maximum number of semaphores that a task may have */
+
+#define SEM_NSEMS_MAX 0x7fffffff
+
+/************************************************************
+ * Public Type Declarations
+ ************************************************************/
+
+/* This is the generic semaphore structure. */
+
+struct sem_s
+{
+ sint16 semcount; /* >0 -> Num counts available */
+ /* <0 -> Num tasks waiting for semaphore */
+};
+typedef struct sem_s sem_t;
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+/* Counting Semaphore Interfaces (based on POSIX APIs) */
+
+EXTERN int sem_init(sem_t *sem, int pshared, unsigned int value);
+EXTERN int sem_destroy(sem_t *sem);
+EXTERN sem_t *sem_open(const char *name, int oflag, ...);
+EXTERN int sem_close(sem_t *sem);
+EXTERN int sem_unlink(const char *name);
+EXTERN int sem_wait(sem_t *sem);
+EXTERN int sem_trywait(sem_t *sem);
+EXTERN int sem_post(sem_t *sem);
+EXTERN int sem_getvalue(sem_t *sem, int *sval);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SEMAPHORE_H */
+
diff --git a/nuttx/include/signal.h b/nuttx/include/signal.h
new file mode 100644
index 0000000000..67236fd6c5
--- /dev/null
+++ b/nuttx/include/signal.h
@@ -0,0 +1,176 @@
+/************************************************************
+ * signal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __SIGNAL_H
+#define __SIGNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <time.h> /* Needed for struct timespec */
+#include <sys/types.h> /* Needed for, e.g., sigset_t */
+
+/************************************************************
+ * Compilations Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* Signal set management definitions and macros. */
+
+#define NULL_SIGNAL_SET ((sigset_t)0x00000000)
+#define ALL_SIGNAL_SET ((sigset_t)0xffffffff)
+#define MIN_SIGNO 0
+#define MAX_SIGNO 31
+#define GOOD_SIGNO(s) (((s)>=MIN_SIGNO)&&((s)<=MAX_SIGNO))
+#define SIGNO2SET(s) ((sigset_t)1 << (s))
+
+/* sigprocmask() "how" definitions. Only one of the following
+ * can be specified:
+ */
+
+#define SIG_BLOCK 1
+#define SIG_UNBLOCK 2
+#define SIG_SETMASK 3
+
+/* struct sigaction flag values */
+
+#define SA_NOCLDSTOP 1 /* Do not generate SIGCHILD when
+ * children stop (ignored) */
+#define SA_SIGINFO 2 /* Invoke the signal-catching function
+ * with 3 args instead of 1
+ * (always assumed) */
+
+/* Dummy value for the sigev_notify field of struct sigevent */
+
+#define SIGEV_SIGNAL 0
+
+/* These are the possible values of the signfo si_code field */
+
+#define SI_QUEUE 0 /* Signal sent from sigqueue */
+#define SI_MESGQ 1 /* Signal generated by arrival of a message on an
+ * empty message queue */
+#define SI_NOWAIT 2 /* Signal already pending -- don't know how sent */
+#define SI_TIMEOUT 3 /* No-signal, unblocked by timeout */
+
+/************************************************************
+ * Global Type Declarations
+ ************************************************************/
+
+/* This defines a set of 32 signals (numbered 0 through 31). */
+
+typedef uint32 sigset_t;
+
+/* This defines the type of the siginfo si_value field */
+
+union sigval
+{
+ int sival_int;
+ void *sival_ptr;
+};
+
+/* The following types is used to pass parameters to/from
+ * signal handlers
+ */
+
+typedef struct siginfo
+{
+ int si_signo;
+ int si_code;
+ union sigval si_value;
+} siginfo_t;
+
+/* The following structure defines the action to take for given signal */
+
+typedef void saHandType(int signo);
+typedef void saVxHandType(int signo, siginfo_t *info, void *context);
+struct sigaction
+{
+ union
+ {
+ saHandType *_sa_handler;
+ saVxHandType *_sa_sigaction;
+ } sa_u;
+ sigset_t sa_mask;
+ int sa_flags;
+};
+#define sa_handler sa_u._sa_handler
+#define sa_sigaction sa_u._sa_sigaction
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN int sigemptyset(sigset_t *set);
+EXTERN int sigfillset(sigset_t *set);
+EXTERN int sigaddset(sigset_t *set, int signo);
+EXTERN int sigdelset(sigset_t *set, int signo);
+EXTERN int sigismember(const sigset_t *set, int signo);
+EXTERN int sigaction(int sig,
+ const struct sigaction *act,
+ struct sigaction *oact);
+EXTERN int sigprocmask(int how, const sigset_t *set,
+ sigset_t *oset);
+EXTERN int sigpending(sigset_t *set);
+EXTERN int sigsuspend(const sigset_t *sigmask);
+EXTERN int sigwaitinfo(const sigset_t *set,
+ struct siginfo *value);
+EXTERN int sigtimedwait(const sigset_t *set,
+ struct siginfo *value,
+ const struct timespec *timeout);
+EXTERN int sigqueue(int tid, int signo,
+ const union sigval value);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SIGNAL_H */
+
diff --git a/nuttx/include/stddef.h b/nuttx/include/stddef.h
new file mode 100644
index 0000000000..09cfa53698
--- /dev/null
+++ b/nuttx/include/stddef.h
@@ -0,0 +1,49 @@
+/************************************************************
+ * stddef.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __STDDEF_H
+#define __STDDEF_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+
+/************************************************************
+ * Type Definitions
+ ************************************************************/
+
+#endif /* __STDDEF_H */
diff --git a/nuttx/include/stdio.h b/nuttx/include/stdio.h
new file mode 100644
index 0000000000..b210dc708e
--- /dev/null
+++ b/nuttx/include/stdio.h
@@ -0,0 +1,285 @@
+/************************************************************
+ * stdio.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __STDIO_H
+#define __STDIO_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdarg.h>
+#include <sched.h>
+#include <semaphore.h>
+#include <time.h>
+
+#include <nuttx/fs.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* File System Definitions **********************************/
+
+/* File system error values *********************************/
+
+#define EOF (-1)
+
+/* File I/O constants ***************************************/
+
+#define MAXNAMLEN 100
+#define NAME_MAX 100
+#define PATH_MAX 101
+#define S_IFMT 0170000
+#define S_IFIFO 0010000
+#define S_IFCHR 0020000
+#define S_IFDIR 0040000
+#define S_IFBLK 0060000
+#define S_IFREG 0100000
+#define S_IFLNK 0120000
+#define S_IFSOCK 0140000
+#define SEEK_SET 0
+#define SEEK_CUR 1
+#define SEEK_END 2
+
+/* Wind-River IOCTL constants */
+
+#define FIOFLUSH 2
+#define FIONBIO 16
+#define FIOSELECT 28
+#define FIOUNSELECT 29
+#define FIOTRUNC 42
+#define FIOHANDLETONAME 45
+#define FIOLABELGET 33
+#define SET_HIDDEN 0x1004
+#define PHYS_BLK_IO 255
+
+#define SECTOR_ALIGN_BYTES 512
+#define FILE_BUF_SIZE (4 * SECTOR_ALIGN_BYTES)
+#define FILE_BUF_ALIGN_BYTES 16
+
+/* File type code for the EntryType field in dirent struct */
+
+#define FS_FILE_TYPE 0
+#define FS_DIRECTORY_TYPE 1
+
+/* The first three _iob entries are reserved for standard I/O */
+
+#define stdin (__stdfile(0))
+#define stdout (__stdfile(1))
+#define stderr (__stdfile(2))
+
+/* These APIs are not implemented and/or can be synthesized from
+ * supported APIs.
+ */
+
+#define putc(c,s) fputc((c),(s))
+#define putchar(c) fputc(c, stdout)
+#define getc(s) fgetc(s)
+#define getchar() fgetc(stdin)
+#define ftell(s) fseek((s),0,SEEK_CUR)
+#define rewind(s) ((void)fseek((s),0,SEEK_SET))
+#define fsync(f)
+
+/************************************************************
+ * Public Type Definitions
+ ************************************************************/
+
+/* The POSIX specification requires that the caller of readdir_r
+ * provide storage "large enough for a dirent with the d_name
+ * member and an array of char containing at least {NAME_MAX}
+ * plus one elements. The legacy dirent structure does not
+ * contain such an array. The legacy dirent structure is
+ * renamed _dirent below.
+ */
+
+struct _dirent
+{
+ char *d_name; /* name of directory entry */
+};
+struct dirent
+{
+ char *d_name; /* A pointer to szName */
+ char szName[NAME_MAX+1]; /* name of the directory entry */
+};
+
+typedef struct
+{
+ unsigned char EntryType; /* FS_FILE_TYPE or FS_DIRECTORY_TYPE */
+ char szName[NAME_MAX]; /* name of the directory entry */
+} fsdirent;
+
+typedef struct
+{
+ unsigned long inode;
+ int generation;
+ char *fileName;
+} HANDLE_TO_NAME_IOCTL;
+
+struct stat
+{
+ dev_t st_dev; /* ID of device containing a */
+ /* directory entry for this file */
+ ino_t st_ino; /* Inode number */
+ unsigned short st_mode; /* File type, attributes, and */
+ /* access control summary */
+ unsigned short st_nlink; /* Number of links */
+ uid_t st_uid; /* User ID of file owner */
+ gid_t st_gid; /* Group ID of file group */
+ dev_t st_rdev; /* Device ID; this entry defined */
+ /* only for char or blk spec files */
+ off_t st_size; /* File size (bytes) */
+ time_t st_atime; /* Time of last access */
+ time_t st_mtime; /* Last modification time */
+ time_t st_ctime; /* Last file status change time */
+ /* Measured in secs since */
+ /* 00:00:00 GMT, Jan 1, 1970 */
+ long st_blksize; /* Non-standard, Wind-River field */
+ unsigned long st_blocks; /* Non-standard, Wind-River field */
+ long st_gen; /* file generation value: Non-standard, Wind-River field */
+};
+
+struct statfs
+{
+ long f_bavail; /* free blocks available to non-superuser */
+ long f_bfree; /* free blocks */
+ long f_blocks; /* total blocks in file system */
+ long f_bsize; /* fundamental file system block (bytes) */
+ long f_ffree; /* free file nodes in file system */
+ long f_files; /* total file nodes in file system */
+ long f_type; /* type of info, zero for now */
+};
+
+/* Streams */
+
+typedef struct file_struct FILE;
+
+typedef void DIR;
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Inline Functions
+ ************************************************************/
+
+/* Used to reference stdin, stdout, and stderr */
+
+static inline FILE *__stdfile(int fd)
+{
+ if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS)
+ {
+ struct streamlist *streams = sched_getstreams();
+ if (streams)
+ {
+ return &streams->sl_streams[fd];
+ }
+ }
+ return NULL;
+}
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/* ANSI-like File System Interfaces */
+
+EXTERN int fclose(FILE *stream);
+EXTERN int fflush(FILE *stream);
+EXTERN int feof(FILE *stream);
+EXTERN int ferror(FILE *stream);
+EXTERN int fgetc(FILE *stream);
+EXTERN char *fgets(char *s, int n, FILE *stream);
+EXTERN FILE *fopen(const char *path, const char *type);
+EXTERN int fprintf(FILE *stream, const char *format, ...);
+EXTERN int fputc(int c, FILE *stream);
+EXTERN int fputs(const char *s, FILE *stream);
+EXTERN size_t fread(void *ptr, size_t size, size_t n_items,
+ FILE *stream);
+EXTERN int fseek(FILE *stream, long int offset, int whence);
+EXTERN size_t fwrite(const void *ptr, size_t size,
+ size_t n_items, FILE *stream);
+EXTERN int printf(const char *format, ...);
+EXTERN int puts(const char *s);
+EXTERN int rename(const char *source, const char *target);
+EXTERN int sprintf(char *dest, const char *format, ...);
+EXTERN int ungetc(int c, FILE *stream);
+EXTERN int vprintf(const char *s, va_list ap);
+EXTERN int vfprintf(FILE *stream, const char *s, va_list ap);
+EXTERN int vsprintf(char *buf, const char *s, va_list ap);
+
+/* POSIX-like File System Interfaces */
+
+EXTERN int chdir(const char *path);
+EXTERN int close(int fd);
+EXTERN int closedir(DIR *dirp);
+EXTERN int creat(const char *path, mode_t mode);
+EXTERN FILE *fdopen(int fd, const char *type);
+EXTERN int fstat(int fd, struct stat *buf);
+EXTERN char *getcwd(char *buf, size_t size);
+EXTERN int ioctl(int fd, int req, unsigned long arg);
+EXTERN off_t lseek(int fd, off_t offset, int whence);
+EXTERN int mkdir(const char *path, mode_t mode);
+EXTERN int open( const char *path, int oflag, ... );
+EXTERN DIR *opendir(const char *path);
+EXTERN int read(int fd, void *buf, unsigned int nbytes);
+EXTERN struct _dirent *readdir(DIR *dirp);
+EXTERN int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
+EXTERN void rewinddir(DIR *dirp);
+EXTERN int rmdir(const char *path);
+EXTERN void seekdir(DIR *dirp, int loc);
+EXTERN int stat(const char *path, struct stat *buf);
+EXTERN int statfs(const char *path, struct statfs *buf);
+EXTERN int telldir(DIR *dirp);
+EXTERN int unlink(const char *path);
+EXTERN int write(int fd, const void *buf, unsigned int nbytes);
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __STDIO_H */
diff --git a/nuttx/include/stdlib.h b/nuttx/include/stdlib.h
new file mode 100644
index 0000000000..c5cf3edda3
--- /dev/null
+++ b/nuttx/include/stdlib.h
@@ -0,0 +1,116 @@
+/************************************************************
+ * stdlib.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __STDLIB_H
+#define __STDLIB_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* The C standard specifies two constants, EXIT_SUCCESS and
+ * EXIT_FAILURE, that may be passed to exit() to indicate
+ * successfuol or unsucessful termination, respectively.
+ */
+
+#define EXIT_SUCCESS 0
+#define EXIT_FAILURE 1
+
+/************************************************************
+ * Global Type Definitions
+ ************************************************************/
+
+struct mallinfo
+{
+ int arena; /* This is the total size of memory allocated
+ * for use by malloc in bytes. */
+ int ordblks; /* This is the number of free (not in use) chunks */
+ int mxordblk; /* Size of the largest free (not in use) chunk */
+ int uordblks; /* This is the total size of memory occupied by
+ * chunks handed out by malloc. */
+ int fordblks; /* This is the total size of memory occupied
+ * by free (not in use) chunks.*/
+};
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/* Random number generation */
+EXTERN void srand(unsigned int seed);
+EXTERN int rand(void);
+
+/* Environment variable support */
+EXTERN char *getenv(const char *name);
+
+/* Process exit functions */
+EXTERN void exit(int status);
+EXTERN void abort(void);
+EXTERN int atexit(void (*func)(void));
+
+/* String to binary conversions */
+#define atoi(nptr) strtol((nptr), (char**)NULL, 10)
+EXTERN long strtol(const char *, char **, int);
+EXTERN double strtod(const char *, char **);
+
+/* Memory Management */
+EXTERN void *malloc(size_t);
+EXTERN void free(void*);
+EXTERN void *realloc(void*, size_t);
+EXTERN void *memalign(size_t, size_t);
+EXTERN void *zalloc(size_t);
+EXTERN void *calloc(size_t, size_t);
+EXTERN struct mallinfo mallinfo(void);
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __STDLIB_H */
diff --git a/nuttx/include/string.h b/nuttx/include/string.h
new file mode 100644
index 0000000000..da77502fea
--- /dev/null
+++ b/nuttx/include/string.h
@@ -0,0 +1,94 @@
+/************************************************************
+ * string.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __STRING_H
+#define __STRING_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <stddef.h> /* For size_t */
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define bzero(s,n) memset((s),0,(n))
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN char *strchr(const char *s, int c);
+EXTERN char *strdup(const char *s);
+EXTERN char *strerror(int);
+EXTERN size_t strlen(const char *);
+EXTERN char *strncat(char *, const char *, size_t);
+EXTERN int strcmp(const char *, const char *);
+EXTERN int strncmp(const char *, const char *, size_t);
+EXTERN char *strcpy(char *dest, const char *src);
+EXTERN char *strncpy(char *, const char *, size_t);
+EXTERN char *strpbrk(const char *, const char *);
+EXTERN char *strchr(const char *, int);
+EXTERN char *strrchr(const char *, int);
+EXTERN size_t strspn(const char *, const char *);
+EXTERN size_t strcspn(const char *, const char *);
+EXTERN char *strstr(const char *, const char *);
+EXTERN char *strtok(char *, const char *);
+
+EXTERN void *memset(void *s, int c, size_t n);
+EXTERN void *memcpy(void *dest, const void *src, size_t n);
+EXTERN int memcmp(const void *s1, const void *s2, size_t n);
+EXTERN void *memmove(void *dest, const void *src, size_t count);
+
+#ifndef CONFIG_ARCH_BZERO
+# define bzero(s,n) (void)memset(s,0,n)
+#endif
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+#endif /* __STRING_H */
diff --git a/nuttx/include/sys/types.h b/nuttx/include/sys/types.h
new file mode 100644
index 0000000000..4cebb48a94
--- /dev/null
+++ b/nuttx/include/sys/types.h
@@ -0,0 +1,143 @@
+/************************************************************
+ * types.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __SYS_TYPES_H
+#define __SYS_TYPES_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <arch/types.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* Values for type boolean */
+
+#define TRUE 1
+#define FALSE 0
+
+/* NULL is usually defined in stddef.h */
+
+#ifndef NULL
+#define NULL (void*)0L
+#endif
+
+/* POSIX-like OS return values: */
+
+#if !defined(__cplusplus)
+#undef ERROR
+#define ERROR -1
+#endif
+
+#undef OK
+#define OK 0
+
+/* POSIX-like scheduling policies (only SCHED_FIFO is supported) */
+
+#define SCHED_FIFO 1 /* FIFO per priority scheduling policy */
+#define SCHED_RR 2 /* Round robin scheduling policy */
+#define SCHED_OTHER 4 /* Not used */
+
+/* HPUX-like MIN/MAX value */
+
+#define PRIOR_RR_MIN 0
+#define PRIOR_RR_MAX 255
+#define PRIOR_FIFO_MIN 0
+#define PRIOR_FIFO_MAX 255
+#define PRIOR_OTHER_MIN 0
+#define PRIOR_OTHER_MAX 255
+
+/* Scheduling Priorities. NOTE: Only the idle task can take
+ * the TRUE minimum priority. */
+
+#define SCHED_PRIORITY_MAX 255
+#define SCHED_PRIORITY_DEFAULT 100
+#define SCHED_PRIORITY_MIN 1
+#define SCHED_PRIORITY_IDLE 0
+
+/* oflag bit settings for sem_open and mq_open */
+
+#define O_RDONLY 0x01 /* Open for read access */
+#define O_WRONLY 0x02 /* Open for write access */
+#define O_RDWR 0x03 /* Open for both read & write access */
+#define O_CREAT 0x04 /* Create semaphore/message queue */
+#define O_EXCL 0x08 /* Name must not exist when opened */
+#define O_APPEND 0x10
+#define O_TRUNC 0x20
+#define O_NONBLOCK 0x40 /* Don't wait for data */
+#define O_NDELAY O_NONBLOCK
+#define O_LOCK 0x80
+
+#define O_RDOK O_RDONLY /* Not POSIX */
+#define O_WROK O_WRONLY /* Not POSIX */
+
+/************************************************************
+ * Type Declarations
+ ************************************************************/
+
+/* Misc. scalar types */
+
+typedef uint32 mode_t;
+typedef uint32 size_t;
+typedef sint32 ssize_t;
+//typedef sint32 time_t;
+typedef sint32 off_t;
+typedef sint32 uid_t;
+typedef sint32 gid_t;
+typedef uint32 dev_t;
+typedef uint32 ino_t;
+typedef unsigned int sig_atomic_t;
+typedef int pid_t;
+typedef int STATUS;
+
+/* Process entry point */
+
+typedef int (*main_t)(int argc, char *argv[]);
+
+/* This is the POSIX-like scheduling parameter structure */
+
+struct sched_param
+{
+ int sched_priority;
+};
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#endif /* __SYS_TYPES_H */
diff --git a/nuttx/include/time.h b/nuttx/include/time.h
new file mode 100644
index 0000000000..0d0399e833
--- /dev/null
+++ b/nuttx/include/time.h
@@ -0,0 +1,126 @@
+/************************************************************
+ * time.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef _TIME_H_
+#define _TIME_H_
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/************************************************************
+ * Compilations Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* This must be set to the last known date */
+
+#define CURRENT_YEAR 2000
+#define CURRENT_MONTH 5
+#define CURRENT_DAY 22
+
+/* This is the only clock_id supported by the "Clock and Timer
+ * Functions."
+ */
+
+#define CLOCK_REALTIME 0
+
+/************************************************************
+ * Global Type Declarations
+ ************************************************************/
+
+typedef long time_t;
+typedef long clockid_t;
+
+struct timespec
+{
+ time_t tv_sec; /* Seconds */
+ long tv_nsec; /* Nanoseconds */
+};
+
+struct timeval
+{
+ time_t tv_sec; /* Seconds */
+ long tv_usec; /* Microseconds */
+};
+
+struct tm
+{
+ int tm_sec; /* second (0-61, allows for leap seconds) */
+ int tm_min; /* minute (0-59) */
+ int tm_hour; /* hour (0-23) */
+ int tm_mday; /* day of the month (1-31) */
+ int tm_mon; /* month (0-11) */
+ int tm_year; /* years since 1900 */
+#if 0 /* not supported */
+ int tm_wday; /* day of the week (0-6) */
+ int tm_yday; /* day of the year (0-365) */
+ int tm_isdst; /* non-0 if daylight savings time is in effect */
+#endif
+};
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+EXTERN int clock_settime(clockid_t clock_id, const struct timespec *tp);
+EXTERN int clock_gettime(clockid_t clock_id, struct timespec *tp);
+EXTERN int clock_getres(clockid_t clock_id, struct timespec *res);
+
+EXTERN time_t mktime(struct tm *tp);
+EXTERN struct tm *gmtime_r(const time_t *clock, struct tm *result);
+#define localtime_r(c,r) gmtime_r(c,r)
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* _TIME_H_ */
diff --git a/nuttx/include/unistd.h b/nuttx/include/unistd.h
new file mode 100644
index 0000000000..d68b0e28b2
--- /dev/null
+++ b/nuttx/include/unistd.h
@@ -0,0 +1,84 @@
+/************************************************************
+ * unistd.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __UNISTD_H
+#define __UNISTD_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* The number of functions that may be registerd to be called
+ * at program exit.
+ */
+
+#define ATEXIT_MAX 1
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/* Task Control Interfaces (based on ANSII APIs) */
+
+EXTERN pid_t getpid( void );
+EXTERN void _exit(int status) __attribute__ ((noreturn));
+EXTERN unsigned int sleep(unsigned int seconds);
+EXTERN void usleep(unsigned long usec);
+
+/* File descriptor operations */
+
+EXTERN int dup(int fildes);
+EXTERN int dup2(int fildes1, int fildes2);
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __UNISTD_H */
diff --git a/nuttx/include/wdog.h b/nuttx/include/wdog.h
new file mode 100644
index 0000000000..9ef16256c6
--- /dev/null
+++ b/nuttx/include/wdog.h
@@ -0,0 +1,95 @@
+/************************************************************
+ * wdog.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __WDOG_H
+#define __WDOG_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sched.h>
+
+/************************************************************
+ * Compilations Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Type Declarations
+ ************************************************************/
+
+/* This is the form of the function that is called when the
+ * watchdog function expires. Up to four parameters may be passed.
+ */
+
+typedef void (*wdentry_t)(int arg1, ...);
+
+/* Watchdog 'handle' */
+
+typedef struct wdog_s *WDOG_ID;
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN WDOG_ID wd_create(void);
+EXTERN STATUS wd_delete(WDOG_ID wdId);
+EXTERN STATUS wd_start(WDOG_ID wdId, int delay, wdentry_t wdentry,
+ int parm1, int parm2, int parm3, int parm4);
+EXTERN STATUS wd_cancel(WDOG_ID wdId);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _WDOG_H_ */
+
diff --git a/nuttx/lib/Makefile b/nuttx/lib/Makefile
new file mode 100644
index 0000000000..2dfda7e819
--- /dev/null
+++ b/nuttx/lib/Makefile
@@ -0,0 +1,96 @@
+############################################################
+# Makefile
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+-include $(TOPDIR)/Make.defs
+
+MKDEP = $(TOPDIR)/tools/mkdeps.sh
+
+ASRCS =
+AOBJS = $(ASRCS:.S=.o)
+
+MISC_SRCS = lib_init.c lib_streamsem.c lib_filesem.c
+STRING_SRCS = lib_memset.c lib_memcpy.c lib_memcmp.c lib_memmove.c \
+ lib_strcpy.c lib_strncpy.c lib_strcmp.c \
+ lib_strlen.c lib_strdup.c lib_strtol.c lib_strchr.c
+CTYPE_SRCS =
+STDIO_SRCS = lib_fopen.c lib_fclose.c \
+ lib_fread.c lib_libfread.c lib_fgetc.c\
+ lib_fwrite.c lib_libfwrite.c lib_fflush.c\
+ lib_fputc.c lib_puts.c lib_fputs.c \
+ lib_ungetc.c \
+ lib_printf.c lib_vprintf.c lib_fprintf.c lib_rawprintf.c lib_lowprintf.c \
+ lib_vfprintf.c lib_sprintf.c lib_libsprintf.c lib_vsprintf.c \
+ lib_libvsprintf.c lib_stdstream.c lib_memstream.c lib_rawstream.c lib_lowstream.c \
+ lib_sscanf.c
+STDLIB_SRCS = lib_getenv.c lib_rand.c
+MATH_SRCS = lib_rint.c
+SQ_SRCS = sq_addlast.c sq_addfirst.c sq_addafter.c \
+ sq_rem.c sq_remlast.c sq_remfirst.c sq_remafter.c
+DQ_SRCS = dq_addlast.c dq_addfirst.c dq_addafter.c dq_addbefore.c \
+ dq_rem.c dq_remlast.c dq_remfirst.c
+
+CSRCS = $(MISC_SRCS) $(STRING_SRCS) $(CTYPE_SRCS) $(STDIO_SRCS) \
+ $(STDLIB_SRCS) $(MATH_SRCS) $(SQ_SRCS) $(DQ_SRCS)
+COBJS = $(CSRCS:.c=.o)
+
+SRCS = $(ASRCS) $(CSRCS)
+OBJS = $(AOBJS) $(COBJS)
+
+BIN = liblib.a
+
+all: $(BIN)
+
+$(AOBJS): %.o: %.S
+ $(CC) -c $(CFLAGS) -D__ASSEMBLY__ $< -o $@
+
+$(COBJS): %.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(BIN): $(OBJS)
+ $(AR) rcs $@ $(OBJS)
+
+.depend: Makefile $(SRCS)
+ $(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
+ touch $@
+
+depend: .depend
+
+clean:
+ rm -f $(BIN) *.o *~
+
+distclean: clean
+ rm -f Make.dep .depend
+
+-include Make.dep
diff --git a/nuttx/lib/dq_addafter.c b/nuttx/lib/dq_addafter.c
new file mode 100644
index 0000000000..5533d57a1c
--- /dev/null
+++ b/nuttx/lib/dq_addafter.c
@@ -0,0 +1,74 @@
+/************************************************************
+ * dq_addafter.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: dq_addafter
+ *
+ * Description:
+ * dq_addafter function adds 'node' after 'qqqq' in the
+ * 'queue.'
+ *
+ ************************************************************/
+
+void dq_addafter(dq_entry_t *prev, dq_entry_t *node,
+ dq_queue_t *queue)
+{
+ if (!queue->head || prev == queue->tail)
+ {
+ dq_addlast(node, queue);
+ }
+ else
+ {
+ dq_entry_t *next = prev->flink;
+ node->blink = prev;
+ node->flink = next;
+ next->blink = node;
+ prev->flink = node;
+ }
+}
diff --git a/nuttx/lib/dq_addbefore.c b/nuttx/lib/dq_addbefore.c
new file mode 100644
index 0000000000..916a7db616
--- /dev/null
+++ b/nuttx/lib/dq_addbefore.c
@@ -0,0 +1,73 @@
+/************************************************************
+ * dq_addbefore.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: dq_addbefore
+ *
+ * Description:
+ * dq_addbefore adds 'node' before 'next' in 'queue'
+ *
+ ************************************************************/
+
+void dq_addbefore(dq_entry_t *next, dq_entry_t *node,
+ dq_queue_t *queue)
+{
+ if (!queue->head || next == queue->head)
+ {
+ dq_addfirst(node, queue);
+ }
+ else
+ {
+ dq_entry_t *prev = next->blink;
+ node->flink = next;
+ node->blink = prev;
+ prev->flink = node;
+ next->blink = node;
+ }
+}
diff --git a/nuttx/lib/dq_addfirst.c b/nuttx/lib/dq_addfirst.c
new file mode 100644
index 0000000000..a11b1f8c3f
--- /dev/null
+++ b/nuttx/lib/dq_addfirst.c
@@ -0,0 +1,73 @@
+/************************************************************
+ * dq_addfirst.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: dq_addfirst
+ *
+ * Description:
+ * dq_addfirst affs 'node' at the beginning of 'queue'
+ *
+ ************************************************************/
+
+void dq_addfirst(dq_entry_t *node, dq_queue_t *queue)
+{
+ node->blink = NULL;
+ node->flink = queue->head;
+
+ if (!queue->head)
+ {
+ queue->head = node;
+ queue->tail = node;
+ }
+ else
+ {
+ queue->head->blink = node;
+ queue->head = node;
+ }
+}
diff --git a/nuttx/lib/dq_addlast.c b/nuttx/lib/dq_addlast.c
new file mode 100644
index 0000000000..85c6c72a93
--- /dev/null
+++ b/nuttx/lib/dq_addlast.c
@@ -0,0 +1,73 @@
+/************************************************************
+ * dq_addlast.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: dq_addlast
+ *
+ * Description
+ * dq_addlast adds 'node' to the end of 'queue'
+ *
+ ************************************************************/
+
+void dq_addlast(dq_entry_t *node, dq_queue_t *queue)
+{
+ node->flink = NULL;
+ node->blink = queue->tail;
+
+ if (!queue->head)
+ {
+ queue->head = node;
+ queue->tail = node;
+ }
+ else
+ {
+ queue->tail->flink = node;
+ queue->tail = node;
+ }
+}
diff --git a/nuttx/lib/dq_rem.c b/nuttx/lib/dq_rem.c
new file mode 100644
index 0000000000..d78ac8b6d8
--- /dev/null
+++ b/nuttx/lib/dq_rem.c
@@ -0,0 +1,83 @@
+/************************************************************
+ * dq_rem.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: dq_rem
+ *
+ * Descripton:
+ * dq_rem removes 'node' from 'queue'
+ *
+ ************************************************************/
+
+void dq_rem(dq_entry_t *node, dq_queue_t *queue)
+{
+ dq_entry_t *prev = node->blink;
+ dq_entry_t *next = node->flink;
+
+ if (!prev)
+ {
+ queue->head = next;
+ }
+ else
+ {
+ prev->flink = next;
+ }
+
+ if (!next)
+ {
+ queue->tail = prev;
+ }
+ else
+ {
+ next->blink = prev;
+ }
+
+ node->flink = NULL;
+ node->blink = NULL;
+}
diff --git a/nuttx/lib/dq_remfirst.c b/nuttx/lib/dq_remfirst.c
new file mode 100644
index 0000000000..0c7381c779
--- /dev/null
+++ b/nuttx/lib/dq_remfirst.c
@@ -0,0 +1,81 @@
+/************************************************************
+ * dq_remfirst.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: dq_remfirst
+ *
+ * Description:
+ * dq_remfirst removes 'node' from the head of 'queue'
+ *
+ ************************************************************/
+
+dq_entry_t *dq_remfirst(dq_queue_t *queue)
+{
+ dq_entry_t *ret = queue->head;
+
+ if (ret)
+ {
+ dq_entry_t *next = ret->flink;
+ if (!next)
+ {
+ queue->head = NULL;
+ queue->tail = NULL;
+ }
+ else
+ {
+ queue->head = next;
+ next->blink = NULL;
+ }
+
+ ret->flink = NULL;
+ ret->blink = NULL;
+ }
+
+ return ret;
+}
diff --git a/nuttx/lib/dq_remlast.c b/nuttx/lib/dq_remlast.c
new file mode 100644
index 0000000000..4764260212
--- /dev/null
+++ b/nuttx/lib/dq_remlast.c
@@ -0,0 +1,81 @@
+/************************************************************
+ * dq_remlast.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/***********************************(************************
+ * Name: dq_remlast
+ *
+ * Description:
+ * dq_remlast removes the last entry from 'queue'
+ *
+ ************************************************************/
+
+dq_entry_t *dq_remlast(dq_queue_t *queue)
+{
+ dq_entry_t *ret = queue->tail;
+
+ if (ret)
+ {
+ dq_entry_t *prev = ret->blink;
+ if (!prev)
+ {
+ queue->head = NULL;
+ queue->tail = NULL;
+ }
+ else
+ {
+ queue->tail = prev;
+ prev->flink = NULL;
+ }
+
+ ret->flink = NULL;
+ ret->blink = NULL;
+ }
+
+ return ret;
+}
diff --git a/nuttx/lib/lib_fclose.c b/nuttx/lib/lib_fclose.c
new file mode 100644
index 0000000000..c1cddeb691
--- /dev/null
+++ b/nuttx/lib/lib_fclose.c
@@ -0,0 +1,98 @@
+/************************************************************
+ * lib_fclose.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_STREAMS > 0
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+int fclose(FILE *stream)
+{
+ int ret = OK;
+ if (stream)
+ {
+ if (stream->fs_filedes > 0)
+ {
+ ret = close(stream->fs_filedes);
+ }
+#warning REVIEW for race conditions
+#if CONFIG_NFILE_STREAMS > 0
+ /* Destroy the semaphore */
+ sem_destroy(&stream->fs_sem);
+
+ /* release the buffer */
+ if (stream->fs_bufstart)
+ {
+ free(stream->fs_bufstart);
+ }
+
+ /* Clear the whole structure */
+
+ memset(stream, 0, sizeof(FILE));
+#else
+#if CONFIG_NUNGET_CHARS > 0
+ /* Reset the number of ungetc characters */
+
+ stream->fs_nungotten = 0;
+#endif
+ /* Reset the flags */
+
+ stream->fs_oflags = 0;
+#endif
+ /* Setting the fs_filedescriptor to -1 makes the stream available for reuse */
+
+ stream->fs_filedes = -1;
+ }
+ return ret;
+}
+
+#endif /* CONFIG_NFILE_STREAMS */
diff --git a/nuttx/lib/lib_fflush.c b/nuttx/lib/lib_fflush.c
new file mode 100644
index 0000000000..b3dbb11096
--- /dev/null
+++ b/nuttx/lib/lib_fflush.c
@@ -0,0 +1,171 @@
+/************************************************************
+ * lib_fflush.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h> /* for CONFIG_STDIO_BUFFER_SIZE */
+
+#if CONFIG_NFILE_STREAMS > 0
+
+#include <stdio.h>
+#include <errno.h>
+#include <nuttx/fs.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * fflush
+ ************************************************************/
+
+/* Called by the OS when a task exits */
+
+void lib_flushall(struct streamlist *list)
+{
+ /* Make sure that there are streams associated with this thread */
+ if (list)
+ {
+ int i;
+
+ /* Process each stream in the thread's stream list */
+
+ stream_semtake(list);
+ for (i = 0; i < CONFIG_NFILE_STREAMS; i++)
+ {
+ /* If the stream is open (i.e., assigned a non-
+ * negative file descriptor), then flush the
+ * stream.
+ */
+
+ if (list->sl_streams[i].fs_filedes >= 0)
+ {
+ (void)fflush(&list->sl_streams[i]);
+ }
+ }
+ stream_semgive(list);
+ }
+}
+
+int fflush(FILE *stream)
+{
+#if CONFIG_STDIO_BUFFER_SIZE > 0
+ const unsigned char *src;
+ size_t bytes_written;
+ size_t nbuffer;
+
+ if (stream->fs_filedes < 0 || (stream->fs_oflags & O_WROK) == 0)
+ {
+ *get_errno_ptr() = EBADF;
+ return ERROR;
+ }
+
+ /* How many bytes are used in the buffer now */
+
+ nbuffer = stream->fs_bufpos - stream->fs_bufstart;
+
+ /* Try to write that amount */
+
+ src = stream->fs_bufstart;
+ bytes_written = write(stream->fs_filedes, src, nbuffer);
+ if (bytes_written < 0)
+ {
+ return bytes_written;
+ }
+
+ /* Update pointers and counts */
+
+ src += bytes_written;
+ nbuffer -= bytes_written;
+
+ /* Reset the buffer position to the beginning of the buffer */
+
+ stream->fs_bufpos = stream->fs_bufstart;
+
+ /* For the case of an incomplete write, nbuffer will be non-zero
+ * It will hold the number of bytes that were not written.
+ * Move the data down in the buffer to handle this (rare) case
+ */
+
+ while (nbuffer)
+ {
+ *stream->fs_bufpos++ = *src++;
+ --nbuffer;
+ }
+
+ /* Return the number of bytes remaining in the buffer */
+
+ return stream->fs_bufpos - stream->fs_bufstart;
+#else
+ return 0;
+#endif
+}
+
+#endif /* CONFIG_NFILE_STREAMS */
diff --git a/nuttx/lib/lib_fgetc.c b/nuttx/lib/lib_fgetc.c
new file mode 100644
index 0000000000..3fd2a5a2b8
--- /dev/null
+++ b/nuttx/lib/lib_fgetc.c
@@ -0,0 +1,98 @@
+/************************************************************
+ * lib_fgetc.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Global Functions
+ **********************************************************/
+
+/************************************************************
+ * fgetc
+ **********************************************************/
+
+int fgetc(FILE *stream)
+{
+ unsigned char c;
+ if (lib_fread(&c, 1, stream) > 0)
+ {
+ return c;
+ }
+ else
+ {
+ return EOF;
+ }
+}
diff --git a/nuttx/lib/lib_filesem.c b/nuttx/lib/lib_filesem.c
new file mode 100644
index 0000000000..eb2279b9d1
--- /dev/null
+++ b/nuttx/lib/lib_filesem.c
@@ -0,0 +1,143 @@
+/************************************************************
+ * lib_filesem.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_STDIO_BUFFER_SIZE > 0
+
+#include <unistd.h>
+#include <semaphore.h>
+#include <errno.h>
+#include <assert.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * lib_sem_initialize
+ ************************************************************/
+
+void lib_sem_initialize(struct file_struct *stream)
+{
+ /* Initialize the LIB semaphore to one (to support one-at-
+ * a-time access to private data sets.
+ */
+
+ (void)sem_init(&stream->fs_sem, 0, 1);
+
+ stream->fs_holder = -1;
+ stream->fs_counts = 0;
+}
+
+/************************************************************
+ * lib_take_semaphore
+ ************************************************************/
+
+void lib_take_semaphore(struct file_struct *stream)
+{
+ pid_t my_pid = getpid();
+
+ /* Do I already have the semaphore? */
+
+ if (stream->fs_holder == my_pid)
+ {
+ /* Yes, just increment the number of references that I have */
+
+ stream->fs_counts++;
+ }
+ else
+ {
+ /* Take the semaphore (perhaps waiting) */
+
+ while (sem_wait(&stream->fs_sem) != 0)
+ {
+ /* The only case that an error should occr here is if
+ * the wait was awakened by a signal.
+ */
+
+ ASSERT(*get_errno_ptr() == EINTR);
+ }
+
+ /* We have it. Claim the stak and return */
+
+ stream->fs_holder = my_pid;
+ stream->fs_counts = 1;
+ }
+}
+
+/************************************************************
+ * lib_give_semaphore
+ ************************************************************/
+
+void lib_give_semaphore(struct file_struct *stream)
+{
+ pid_t my_pid = getpid();
+
+ /* I better be holding at least one reference to the semaphore */
+
+ ASSERT(stream->fs_holder == my_pid);
+
+ /* Do I hold multiple references to the semphore */
+
+ if (stream->fs_counts > 1)
+ {
+ /* Yes, just release one count and return */
+
+ stream->fs_counts--;
+ }
+ else
+ {
+ /* Nope, this is the last reference I have */
+
+ stream->fs_holder = -1;
+ stream->fs_counts = 0;
+ ASSERT(sem_post(&stream->fs_sem) == 0);
+ }
+}
+#endif /* CONFIG_STDIO_BUFFER_SIZE */
diff --git a/nuttx/lib/lib_fopen.c b/nuttx/lib/lib_fopen.c
new file mode 100644
index 0000000000..0a935ddd23
--- /dev/null
+++ b/nuttx/lib/lib_fopen.c
@@ -0,0 +1,232 @@
+/************************************************************
+ * lib_fopen.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_STREAMS > 0
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "lib_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static int lib_mode2oflags(const char *mode)
+{
+ int oflags = 0;
+ if (mode)
+ {
+ while(*mode)
+ {
+ switch (*mode)
+ {
+ /* Open for read access */
+
+ case 'r' :
+ if (*(mode + 1) == '+')
+ {
+ /* Open for read/write access */
+ oflags |= O_RDWR;
+ mode++;
+ }
+ else
+ {
+ /* Open for read access */
+ oflags |= O_RDOK;
+ }
+ break;
+
+ /* Open for write access? */
+
+ case 'w' :
+ if (*(mode + 1) == '+')
+ {
+ /* Open for write read/access, truncating any existing file */
+ oflags |= O_RDWR|O_CREAT|O_TRUNC;
+ mode++;
+ }
+ else
+ {
+ /* Open for write access, truncating any existing file */
+ oflags |= O_WROK|O_CREAT|O_TRUNC;
+ }
+ break;
+
+ /* Open for write/append access? */
+
+ case 'a' :
+ if (*(mode + 1) == '+')
+ {
+ /* Read from the beginning of the file; write to the end */
+ oflags |= O_RDWR|O_CREAT;
+ mode++;
+ }
+ else
+ {
+ /* Write to the end of the file */
+ oflags |= O_WROK|O_CREAT;
+ }
+ break;
+
+ /* Open for binary access? */
+
+ case 'b' :
+ default:
+ break;
+ }
+ mode++;
+ }
+ }
+ return oflags;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+struct file_struct *lib_fdopen(int fd, const char *mode,
+ struct filelist *flist,
+ struct streamlist *slist)
+{
+ struct inode *inode = flist->fl_files[fd].f_inode;
+ FILE *stream;
+ int oflags = lib_mode2oflags(mode);
+ int i;
+
+ if (fd < 0 || !flist || !slist)
+ {
+ *get_errno_ptr() = EBADF;
+ return NULL;
+ }
+
+ /* Make sure that the inode supports the requested access. In
+ * the case of fdopen, we are not actually creating the file -- in
+ * particular w and w+ do not truncate the file and any files have
+ * already been created.
+ */
+
+ if (inode_checkflags(inode, oflags) != OK)
+ {
+ return NULL;
+ }
+
+ /* Find an unallocated FILE structure */
+
+ stream_semtake(slist);
+ for (i = 0 ; i < CONFIG_NFILE_STREAMS; i++)
+ {
+ stream = &slist->sl_streams[i];
+ if (stream->fs_filedes < 0)
+ {
+ /* Zero the structure */
+#if CONFIG_STDIO_BUFFER_SIZE > 0
+ memset(stream, 0, sizeof(FILE));
+#elif CONFIG_NUNGET_CHARS > 0
+ stream->fs_nungotten = 0;
+#endif
+
+#if CONFIG_STDIO_BUFFER_SIZE > 0
+ /* Initialize the semaphore the manages access to the buffer */
+
+ (void)sem_init(&stream->fs_sem, 0, 1);
+
+ /* Allocate the IO buffer */
+
+ stream->fs_bufstart = malloc(CONFIG_STDIO_BUFFER_SIZE);
+ if (!stream)
+ {
+ *get_errno_ptr() = ENOMEM;
+ return NULL;
+ }
+
+ /* Set up pointers */
+
+ stream->fs_bufend = &stream->fs_bufstart[CONFIG_STDIO_BUFFER_SIZE];
+ stream->fs_bufpos = stream->fs_bufstart;
+ stream->fs_bufpos = stream->fs_bufstart;
+ stream->fs_bufread = stream->fs_bufstart;
+ stream_semgive(slist);
+#endif
+ /* Save the file description and open flags. Setting the
+ * file descriptor locks this stream.
+ */
+
+ stream->fs_filedes = fd;
+ stream->fs_oflags = oflags;
+
+ return stream;
+ }
+ }
+ stream_semgive(slist);
+ return NULL;
+}
+
+FILE *fdopen(int fd, const char *mode)
+{
+ struct filelist *flist = sched_getfiles();
+ struct streamlist *slist = sched_getstreams();
+ return lib_fdopen(fd, mode, flist, slist);
+}
+
+FILE *fopen(const char *path, const char *mode)
+{
+ struct filelist *flist = sched_getfiles();
+ struct streamlist *slist = sched_getstreams();
+ int oflags = lib_mode2oflags(mode);
+ int fd = open(path, oflags, 0666);
+
+ FILE *ret = lib_fdopen(fd, mode, flist, slist);
+ if (!ret)
+ {
+ (void)close(fd);
+ }
+ return ret;
+}
+
+#endif /* CONFIG_NFILE_STREAMS */
diff --git a/nuttx/lib/lib_fprintf.c b/nuttx/lib/lib_fprintf.c
new file mode 100644
index 0000000000..a0e3d53307
--- /dev/null
+++ b/nuttx/lib/lib_fprintf.c
@@ -0,0 +1,92 @@
+/************************************************************
+ * lib_fprintf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * fprintf
+ ************************************************************/
+
+int fprintf(FILE *stream, const char *fmt, ...)
+{
+ va_list ap;
+ int n;
+
+ /* vfprintf into the stream */
+
+ va_start(ap, fmt);
+ n = vfprintf(stream, fmt, ap);
+ va_end(ap);
+ return n;
+}
diff --git a/nuttx/lib/lib_fputc.c b/nuttx/lib/lib_fputc.c
new file mode 100644
index 0000000000..e8033a54e7
--- /dev/null
+++ b/nuttx/lib/lib_fputc.c
@@ -0,0 +1,98 @@
+/************************************************************
+ * lib_fputc.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Global Functions
+ **********************************************************/
+
+/************************************************************
+ * fputc
+ **********************************************************/
+
+int fputc(int c, FILE *stream)
+{
+ unsigned char buf = (unsigned char)c;
+ if (lib_fwrite(&buf, 1, stream) > 0)
+ {
+ return c;
+ }
+ else
+ {
+ return EOF;
+ }
+}
diff --git a/nuttx/lib/lib_fputs.c b/nuttx/lib/lib_fputs.c
new file mode 100644
index 0000000000..61e5a03c4c
--- /dev/null
+++ b/nuttx/lib/lib_fputs.c
@@ -0,0 +1,126 @@
+/************************************************************
+ * lib_fputs.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Global Functions
+ **********************************************************/
+
+/************************************************************
+ * fputs
+ **********************************************************/
+
+/* fputs() writes the string s to stream, without its
+ * trailing '\0'.
+ */
+
+int fputs(const char *s, FILE *stream)
+{
+ int ntowrite;
+ int nwritten;
+ int nput = EOF;
+
+ /* Make sure that a string was provided. */
+
+ if (!s)
+ {
+ *get_errno_ptr() = EINVAL;
+ }
+ else
+ {
+ /* Get the length of the string. */
+
+ ntowrite = strlen(s);
+ if (ntowrite == 0)
+ {
+ nput = 0;
+ }
+ else
+ {
+ /* Write the string */
+
+ nwritten = lib_fwrite(s, ntowrite, stream);
+ if (nwritten > 0)
+ {
+ nput = nwritten;
+ }
+ }
+ }
+ return nput;
+}
diff --git a/nuttx/lib/lib_fread.c b/nuttx/lib/lib_fread.c
new file mode 100644
index 0000000000..b40febe3d0
--- /dev/null
+++ b/nuttx/lib/lib_fread.c
@@ -0,0 +1,98 @@
+/************************************************************
+ * lib_fread.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * fwrite
+ ************************************************************/
+
+size_t fread(void *ptr, size_t size, size_t n_items, FILE *stream)
+{
+ size_t full_size = n_items * (size_t)size;
+ ssize_t bytes_read;
+ size_t items_read = 0;
+
+ /* Write the data into the stream buffer */
+
+ bytes_read = lib_fread(ptr, full_size, stream);
+ if (bytes_read > 0)
+ {
+ /* Return the number of full items read */
+
+ items_read = bytes_read / size;
+ }
+ return items_read;
+}
diff --git a/nuttx/lib/lib_fwrite.c b/nuttx/lib/lib_fwrite.c
new file mode 100644
index 0000000000..3dd22ba8be
--- /dev/null
+++ b/nuttx/lib/lib_fwrite.c
@@ -0,0 +1,98 @@
+/************************************************************
+ * lib_fwrite.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * fwrite
+ ************************************************************/
+
+size_t fwrite(const void *ptr, size_t size, size_t n_items, FILE *stream)
+{
+ size_t full_size = n_items * (size_t)size;
+ ssize_t bytes_written;
+ size_t items_written = 0;
+
+ /* Write the data into the stream buffer */
+
+ bytes_written = lib_fwrite(ptr, full_size, stream);
+ if (bytes_written > 0)
+ {
+ /* Return the number of full items written */
+
+ items_written = bytes_written / size;
+ }
+ return items_written;
+}
diff --git a/nuttx/lib/lib_getenv.c b/nuttx/lib/lib_getenv.c
new file mode 100644
index 0000000000..2c75a4f162
--- /dev/null
+++ b/nuttx/lib/lib_getenv.c
@@ -0,0 +1,163 @@
+/************************************************************
+ * lib_getenv.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <debug.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+#ifdef ENVIRONMENT_STRINGS
+static const char environment[] = ENVIRONMENT_STRINGS;
+#else
+static const char environment[] = "";
+#endif
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Function: getenv
+ *
+ * Description:
+ *
+ * getenv() searches the environment list for a string of
+ * the form name=value, and returns a pointer to the value
+ * in the current environment if such a string is present,
+ * otherwise a NULL pointer. name can be either the desired
+ * name, null-terminated, or of the form name=value, in which
+ * case getenv() uses the portion to the left of the = as the
+ * search key.
+ *
+ * getenv() is (by convention) declared in stdlib.h
+ *
+ ************************************************************/
+
+char *getenv(const char *name)
+{
+ const char *penv = environment;
+ int size = sizeof(environment);
+ const char *pend = &environment[size-1];
+ const char *ptmp;
+
+ dbg("getenv(ge): name=\"%s\"\n", name);
+
+ if (name) {
+
+ /* Process each string in the environment. */
+ while (penv < pend) {
+
+ vdbg("(ge):\tCompare to=\"%s\"\n", penv);
+
+ /* The logic below basically implements a version of
+ * strcmp where the strings may be terminated with = signs. */
+ ptmp = name;
+ for (;;) {
+
+ /* Are we at the end of the name-to-matching? */
+ if ((!*ptmp) || (*ptmp == '=')) {
+
+ /* Yes.. are we also at the end of the matching-name? */
+ if (*penv == '=') {
+
+ /* Yes.. return the pointer to the value. */
+ dbg("(ge):\tReturning \"%s\"\n", penv+1);
+ return ((char*)penv+1);
+
+ } /* end if */
+ else {
+
+ /* No.. Skip to the next name matching name candidate. */
+ while(*penv++);
+ break;
+
+ } /* end else */
+ } /* end if */
+
+ /* NO.. are we at the end of the matching name candidate? */
+ /* OR.. do the corresponding characters not match. */
+ else if (*penv != *ptmp) {
+
+ /* Yes.. Skip to the next name matching name candidate. */
+ while(*penv++);
+ break;
+
+ } /* end else if */
+ else {
+
+ /* No.. try the next characters. */
+ penv++; ptmp++;
+
+ } /* end else */
+ } /* end for */
+ } /* end while */
+ } /* end if */
+
+ /* If we got here, then no matching string was found. */
+ dbg("(ge):\tReturning NULL\n");
+ return NULL;
+
+} /* end getenv */
diff --git a/nuttx/lib/lib_init.c b/nuttx/lib/lib_init.c
new file mode 100644
index 0000000000..216f7b780a
--- /dev/null
+++ b/nuttx/lib/lib_init.c
@@ -0,0 +1,203 @@
+/************************************************************
+ * lib_init.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs.h>
+#include <nuttx/os_external.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static void _lib_semtake(struct streamlist *list)
+{
+ /* Take the semaphore (perhaps waiting) */
+
+ while (sem_wait(&list->sl_sem) != 0)
+ {
+ /* The only case that an error should occr here is if
+ * the wait was awakened by a signal.
+ */
+
+ ASSERT(*get_errno_ptr() == EINTR);
+ }
+}
+
+static inline void _lib_semgive(struct streamlist *list)
+{
+ sem_post(&list->sl_sem);
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * lib_initialize
+ ************************************************************/
+
+/* General library initialization hook */
+
+void weak_const_function lib_initialize(void)
+{
+}
+
+#if CONFIG_NFILE_STREAMS > 0
+/* The following function is called when a new TCB is allocated. It
+ * creates the streamlist instance that is stored in the TCB.
+ */
+
+struct streamlist *lib_alloclist(void)
+{
+ struct streamlist *list;
+ list = (struct streamlist*)kzmalloc(sizeof(struct streamlist));
+ if (list)
+ {
+ int i;
+
+ /* Start with a reference count of one */
+
+ list->sl_crefs = 1;
+
+ /* Initialize the list access mutex */
+
+ (void)sem_init(&list->sl_sem, 0, 1);
+
+ /* Initialize each FILE structure */
+
+ for (i = 0; i < CONFIG_NFILE_STREAMS; i++)
+ {
+ /* Clear the IOB */
+
+ memset(&list->sl_streams[i], 0, sizeof(FILE));
+
+ /* Indicate not opened */
+
+ list->sl_streams[i].fs_filedes = -1;
+
+ /* Initialize the stream semaphore to one to support one-at-
+ * a-time access to private data sets.
+ */
+
+ lib_sem_initialize(&list->sl_streams[i]);
+ }
+ }
+ return list;
+
+}
+
+/* This function is called when a TCB is closed (such as with
+ * pthread_create(). It increases the reference count on the stream
+ * list.
+ */
+
+void lib_addreflist(struct streamlist *list)
+{
+ if (list)
+ {
+ /* Increment the reference count on the list */
+
+ _lib_semtake(list);
+ list->sl_crefs++;
+ _lib_semgive(list);
+ }
+}
+
+/* this function is called when a TCB is destroyed. Note that is
+ * does not close the file by release this inode. This happens
+ * separately when the file descriptor list is freed.
+ */
+
+void lib_releaselist(struct streamlist *list)
+{
+ int crefs;
+ if (list)
+ {
+ /* Decrement the reference count */
+
+ _lib_semtake(list);
+ crefs = --list->sl_crefs;
+ _lib_semgive(list);
+
+ /* If the count decrements to zero, then there is no reference
+ * to the structure and it should be deallocated.
+ */
+
+ if (crefs <= 0)
+ {
+ int i;
+
+ /* Destroy the semaphore and release the filelist */
+
+ (void)sem_destroy(&list->sl_sem);
+ sched_free(list);
+
+ /* Initialize each FILE structure */
+
+ for (i = 0; i < CONFIG_NFILE_STREAMS; i++)
+ {
+ /* Destroy the semaphore that protects the IO buffer */
+
+ (void)sem_destroy(&list->sl_streams[i].fs_sem);
+
+ /* Release the IO buffer */
+ if (&list->sl_streams[i].fs_bufstart)
+ {
+ free(list->sl_streams[i].fs_bufstart);
+ }
+ }
+ }
+ }
+}
+
+#endif /* CONFIG_NFILE_STREAMS */
+
+
diff --git a/nuttx/lib/lib_internal.h b/nuttx/lib/lib_internal.h
new file mode 100644
index 0000000000..41d40d4288
--- /dev/null
+++ b/nuttx/lib/lib_internal.h
@@ -0,0 +1,166 @@
+/************************************************************
+ * lib_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __LIB_INTERNAL_H
+#define __LIB_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <stdio.h>
+#include <semaphore.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#if CONFIG_STDIO_BUFFER_SIZE <= 0
+# define lib_sem_initialize(s)
+# define lib_take_semaphore(s)
+# define lib_give_semaphore(s)
+#endif
+
+#define LIB_BUFLEN_UNKNOWN (0x7fffffff)
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/* This is the generic for of a stream used by the library
+ * to manage variable sized output.
+ */
+
+struct lib_stream_s;
+
+typedef void (*lib_putc_t)(struct lib_stream_s *this, int ch);
+
+struct lib_stream_s
+{
+ lib_putc_t put; /* Pointer to function to put one character */
+ int nput; /* Total number of characters put. Written
+ * by put method, readable by user */
+};
+
+struct lib_memstream_s
+{
+ struct lib_stream_s public;
+ char *buffer; /* Address of first byte in the buffer */
+ int buflen; /* Size of the buffer in bytes */
+};
+
+struct lib_stdstream_s
+{
+ struct lib_stream_s public;
+ FILE *stream;
+};
+
+struct lib_rawstream_s
+{
+ struct lib_stream_s public;
+ int fd;
+};
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Inline Functions
+ ************************************************************/
+
+/************************************************************
+ * Pulblic Function Prototypes
+ ************************************************************/
+
+/* Defined in lib_streamsem.c */
+
+extern void stream_semtake(struct streamlist *list);
+extern void stream_semgive(struct streamlist *list);
+
+/* Defined in lib_memstream.c */
+
+extern void lib_memstream(struct lib_memstream_s *memstream,
+ char *bufstart, int buflen);
+
+/* Defined in lib_stdstream.c */
+
+extern void lib_stdstream(struct lib_stdstream_s *stdstream,
+ FILE *stream);
+
+/* Defined in lib_rawstream.c */
+
+extern void lib_rawstream(struct lib_rawstream_s *rawstream,
+ int fd);
+
+/* Defined in lib_lowstream.c */
+
+#ifdef CONFIG_ARCH_LOWPUTC
+extern void lib_lowstream(struct lib_stream_s *rawstream);
+#endif
+
+/* Defined in lib_libsprintf.c */
+
+extern int lib_sprintf (struct lib_stream_s *obj,
+ const char *fmt, ...);
+
+/* Defined lib_libvsprintf.c */
+
+extern int lib_vsprintf(struct lib_stream_s *obj,
+ const char *src, va_list ap);
+
+/* Defined in lib_libwrite.c */
+
+extern ssize_t lib_fwrite(const void *ptr, size_t count, FILE *stream);
+
+/* Defined in lib_libfread.c */
+
+extern ssize_t lib_fread(void *ptr, size_t count, FILE *stream);
+
+/* Defined in lib_sem.c */
+
+#if CONFIG_STDIO_BUFFER_SIZE > 0
+extern void lib_sem_initialize(struct file_struct *stream);
+extern void lib_take_semaphore(struct file_struct *stream);
+extern void lib_give_semaphore(struct file_struct *stream);
+#endif
+
+/* Defined in lib_libgetbase.c */
+
+extern int lib_getbase(const char *nptr, const char **endptr);
+
+
+#endif /* __LIB_INTERNAL_H */
diff --git a/nuttx/lib/lib_libfread.c b/nuttx/lib/lib_libfread.c
new file mode 100644
index 0000000000..8c3deb6559
--- /dev/null
+++ b/nuttx/lib/lib_libfread.c
@@ -0,0 +1,257 @@
+/************************************************************
+ * lib_libfread.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h> /* for CONFIG_STDIO_BUFFER_SIZE */
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * lib_fread
+ ************************************************************/
+
+ssize_t lib_fread(void *ptr, size_t count, FILE *stream)
+{
+ unsigned char *dest = (unsigned char*)ptr;
+ ssize_t bytes_read;
+
+ /* Make sure that reading from this stream is allowed */
+
+ if ((!stream) || ((stream->fs_oflags & O_RDOK) == 0))
+ {
+ *get_errno_ptr() = EBADF;
+ bytes_read = -1;
+ }
+ else
+ {
+ /* The stream must be stable until we complete the read */
+
+ lib_take_semaphore(stream);
+
+#if CONFIG_NUNGET_CHARS > 0
+ /* First, re-read any previously ungotten characters */
+
+ while ((stream->fs_nungotten > 0) && (count > 0))
+ {
+ /* Decrement the count of ungotten bytes to get an index */
+
+ stream->fs_nungotten--;
+
+ /* Return the last ungotten byte */
+
+ *dest++ = stream->fs_ungotten[stream->fs_nungotten];
+
+ /* That's one less byte that we have to read */
+
+ count--;
+ }
+#endif
+
+#if CONFIG_STDIO_BUFFER_SIZE > 0
+ /* Now get any other needed chars from the buffer or the file. */
+
+ while (count > 0)
+ {
+ /* Is there readable data in the buffer? */
+
+ while ((count > 0) && (stream->fs_bufpos < stream->fs_bufread))
+ {
+ /* Yes, copy a byte into the user buffer */
+
+ *dest++ = *stream->fs_bufpos++;
+ count--;
+ }
+
+ /* The buffer is empty OR we have already supplied the number of
+ * bytes requested in the read. Check if we need to read
+ * more from the file.
+ */
+
+ if (count > 0)
+ {
+ size_t buffer_available;
+
+ /* We need to read more data into the buffer from the file */
+
+ /* Mark the buffer empty */
+
+ stream->fs_bufpos = stream->fs_bufread = stream->fs_bufstart;
+
+ /* How much space is available in the buffer? */
+
+ buffer_available = stream->fs_bufend - stream->fs_bufread;
+
+ /* Will the number of bytes that we need to read fit into
+ * the buffer space that is available? If the read size is
+ * larger than the buffer, then read some of the data
+ * directly into the user's buffer.
+ */
+
+ if (count > buffer_available)
+ {
+ bytes_read = read(stream->fs_filedes, dest, count);
+ if (bytes_read < 0)
+ {
+ /* An error occurred on the read */
+
+ goto err_out;
+ }
+ else if (bytes_read == 0)
+ {
+ /* We are at the end of the file */
+
+ goto short_read;
+ }
+ else
+ {
+ /* Some bytes were read. Adjust the dest pointer */
+
+ dest += bytes_read;
+
+ /* Were all of the requested bytes read? */
+
+ if (bytes_read < count)
+ {
+ /* No. We must be at the end of file. */
+
+ goto short_read;
+ }
+ else
+ {
+ /* Yes. We are done. */
+
+ count = 0;
+ }
+ }
+ }
+ else
+ {
+ /* The number of bytes required to satisfy the read
+ * is less than or equal to the size of the buffer
+ * space that we have left. Read as much as we can
+ * into the buffer.
+ */
+
+ bytes_read = read(stream->fs_filedes, stream->fs_bufread, buffer_available);
+ if (bytes_read < 0)
+ {
+ /* An error occurred on the read */
+
+ goto err_out;
+ }
+ else if (bytes_read == 0)
+ {
+ /* We are at the end of the file */
+
+ goto short_read;
+ }
+ else
+ {
+ /* Some bytes were read */
+
+ stream->fs_bufread += bytes_read;
+ }
+ }
+ }
+ }
+#else
+ /* Now get any other needed chars from the file. */
+
+ while (count > 0)
+ {
+ bytes_read = read(stream->fs_filedes, dest, count);
+ if (bytes_read < 0)
+ {
+ goto err_out;
+ }
+ else if (bytes_read == 0)
+ {
+ break;
+ }
+ else
+ {
+ dest += bytes_read;
+ count -= bytes_read;
+ }
+ }
+#endif
+ short_read:
+ bytes_read = dest - (unsigned char*)ptr;
+ err_out:
+ lib_give_semaphore(stream);
+ }
+ return bytes_read;
+}
diff --git a/nuttx/lib/lib_libfwrite.c b/nuttx/lib/lib_libfwrite.c
new file mode 100644
index 0000000000..cee26b1940
--- /dev/null
+++ b/nuttx/lib/lib_libfwrite.c
@@ -0,0 +1,162 @@
+/************************************************************
+ * lib_libfwrite.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h> /* for CONFIG_STDIO_BUFFER_SIZE */
+#include <stdio.h>
+#include <errno.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * lib_fwrite
+ ************************************************************/
+
+ssize_t lib_fwrite(const void *ptr, size_t count, FILE *stream)
+#if CONFIG_STDIO_BUFFER_SIZE > 0
+{
+ const unsigned char *start = ptr;
+ const unsigned char *src = ptr;
+ ssize_t bytes_written;
+ unsigned char *dest;
+
+ /* Make sure that writing to this stream is allowed */
+
+ if ((!stream) || ((stream->fs_oflags & O_WROK) == 0))
+ {
+ *get_errno_ptr() = EBADF;
+ bytes_written = -1;
+ }
+ else
+ {
+ /* Loop until all of the bytes have been buffered */
+
+ lib_take_semaphore(stream);
+ while (count > 0)
+ {
+ /* Determine the number of bytes left in the buffer */
+
+ size_t gulp_size = stream->fs_bufend - stream->fs_bufpos;
+
+ /* Will the user data fit into the amount of buffer space
+ * that we have left?
+ */
+
+ if (gulp_size > count)
+ {
+ /* Yes, clip the gulp to the size of the user data */
+
+ gulp_size = count;
+ }
+
+ /* Adjust the number of bytes remaining to be transferred
+ * on the next pass through the loop (might be zero).
+ */
+
+ count -= gulp_size;
+
+ /* Transfer the data into the buffer */
+
+ for (dest = stream->fs_bufpos; gulp_size > 0; gulp_size--)
+ {
+ *dest++ = *src++;
+ }
+ stream->fs_bufpos = dest;
+
+ /* Is the buffer full? */
+
+ if (dest >= stream->fs_bufend)
+ {
+ /* flush the buffered data to the IO stream */
+
+ int bytes_buffered = fflush(stream);
+ if (bytes_buffered < 0)
+ {
+ bytes_written = bytes_buffered;
+ goto err_out;
+ }
+ }
+ }
+
+ bytes_written = src - start;
+
+ err_out:
+ lib_give_semaphore(stream);
+ }
+ return bytes_written;
+}
+#else
+{
+ return write(stream->fs_filedes, ptr, count);
+}
+#endif
diff --git a/nuttx/lib/lib_libsprintf.c b/nuttx/lib/lib_libsprintf.c
new file mode 100644
index 0000000000..20078b7b5e
--- /dev/null
+++ b/nuttx/lib/lib_libsprintf.c
@@ -0,0 +1,90 @@
+/************************************************************
+ * lib_libsprintf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * lib_sprintf
+ ************************************************************/
+
+int lib_sprintf (struct lib_stream_s *obj, const char *fmt, ...)
+{
+ va_list ap;
+ int n;
+
+ /* Let lib_vsprintf do the real work */
+
+ va_start(ap, fmt);
+ n = lib_vsprintf(obj, fmt, ap);
+ va_end(ap);
+ return n;
+}
diff --git a/nuttx/lib/lib_libvsprintf.c b/nuttx/lib/lib_libvsprintf.c
new file mode 100644
index 0000000000..da03f7f318
--- /dev/null
+++ b/nuttx/lib/lib_libvsprintf.c
@@ -0,0 +1,854 @@
+/************************************************************
+ * lib_libvsprintf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+enum
+{
+ FMT_RJUST = 0, /* Default */
+ FMT_LJUST,
+ FMT_RJUST0,
+ FMT_CENTER
+};
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+static void utodec(char **pp, unsigned int n);
+static void _utodec(char **pp, unsigned int n);
+static void utohex(char **pp, unsigned int n);
+static void _utohex(char **pp, unsigned int n);
+static void utooct(char **pp, unsigned int n);
+static void _utooct(char **pp, unsigned int n);
+static void utobin(char **pp, unsigned int n);
+static void _utobin(char **pp, unsigned int n);
+
+#ifdef CONFIG_HAVE_LONG_LONG
+static void llutodec(char **pp, unsigned long long n);
+static void _llutodec(char **pp, unsigned long long n);
+static void llutohex(char **pp, unsigned long long n);
+static void _llutohex(char **pp, unsigned long long n);
+static void llutooct(char **pp, unsigned long long n);
+static void _llutooct(char **pp, unsigned long long n);
+static void llutobin(char **pp, unsigned long long n);
+static void _llutobin(char **pp, unsigned long long n);
+#endif
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+static const char g_nullstring[] = "(null)";
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static void utodec(char **pp, unsigned int n)
+{
+ _utodec(pp, n);
+ **pp = 0;
+}
+
+static void _utodec(char **pp, unsigned int n)
+{
+ unsigned int remainder = n % 10;
+ unsigned int dividend = n / 10;
+
+ if (dividend)
+ {
+ _utodec(pp, dividend);
+ }
+
+ **pp = (char)(remainder + (unsigned int)'0');
+ (*pp)++;
+}
+
+static void utohex(char **pp, unsigned int n)
+{
+ _utohex(pp, n);
+ **pp = 0;
+}
+
+static void _utohex(char **pp, unsigned int n)
+{
+ unsigned int remainder = n & 0xf;
+ unsigned int dividend = n >> 4;
+
+ if (dividend)
+ {
+ _utohex(pp, dividend);
+ }
+
+ if (remainder < 10)
+ {
+ **pp = (char)(remainder + (unsigned int)'0');
+ }
+ else
+ {
+ **pp = (char)(remainder + ((unsigned int)'a' - 10));
+ }
+ (*pp)++;
+}
+
+static void utooct(char **pp, unsigned int n)
+{
+ _utooct(pp, n);
+ **pp = 0;
+}
+
+static void _utooct(char **pp, unsigned int n)
+{
+ unsigned int remainder = n & 0x7;
+ unsigned int dividend = n >> 3;
+
+ if (dividend)
+ {
+ _utooct(pp, dividend);
+ }
+
+ **pp = (char)(remainder + (unsigned int)'0');
+ (*pp)++;
+}
+
+static void utobin(char **pp, unsigned int n)
+{
+ _utobin(pp, n);
+ **pp = 0;
+}
+
+static void _utobin(char **pp, unsigned int n)
+{
+ unsigned int remainder = n & 1;
+ unsigned int dividend = n >> 1;
+
+ if (dividend)
+ {
+ _utobin(pp, dividend);
+ }
+
+ **pp = (char)(remainder + (unsigned int)'0');
+ (*pp)++;
+}
+
+#ifdef CONFIG_HAVE_LONG_LONG
+static void llutodec(char **pp, unsigned long long n)
+{
+ _llutodec(pp, n);
+ **pp = 0;
+}
+
+static void _llutodec(char **pp, unsigned long long n)
+{
+ unsigned int remainder = n % 10;
+ unsigned long long dividend = n / 10;
+
+ if (dividend)
+ {
+ _llutodec(pp, dividend);
+ }
+
+ **pp = (char)(remainder + (unsigned int)'0');
+ (*pp)++;
+}
+
+static void llutohex(char **pp, unsigned long long n)
+{
+ _llutohex(pp, n);
+ **pp = 0;
+}
+
+static void _llutohex(char **pp, unsigned long long n)
+{
+ unsigned int remainder = n & 0xf;
+ unsigned long long dividend = n >> 4;
+ if (dividend)
+ {
+ _llutohex(pp, dividend);
+ }
+
+ if (remainder < 10)
+ {
+ **pp = (char)(remainder + (unsigned int)'0');
+ }
+ else
+ {
+ **pp = (char)(remainder + ((unsigned int)'a' - 10));
+ }
+ (*pp)++;
+}
+
+static void llutooct(char **pp, unsigned long long n)
+{
+ _llutooct(pp, n);
+ **pp = 0;
+}
+
+static void _llutooct(char **pp, unsigned long long n)
+{
+ unsigned int remainder = n & 0x7;
+ unsigned long long dividend = n >> 3;
+
+ if (dividend)
+ {
+ _llutooct(pp, dividend);
+ }
+
+ **pp = (char)(remainder + (unsigned int)'0');
+ (*pp)++;
+}
+
+static void llutobin(char **pp, unsigned long long n)
+{
+ _llutobin(pp, n);
+ **pp = 0;
+}
+
+static void _llutobin(char **pp, unsigned long long n)
+{
+ unsigned int remainder = n & 1;
+ unsigned long long dividend = n >> 1;
+
+ if (dividend)
+ {
+ _llutobin(pp, dividend);
+ }
+
+ **pp = (char)(remainder + (unsigned int)'0');
+ (*pp)++;
+}
+#endif /* CONFIG_HAVE_LONG_LONG */
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * lib_vsprintf
+ ************************************************************/
+
+int lib_vsprintf(struct lib_stream_s *obj, const char *src, va_list ap)
+{
+ char *ptmp;
+ char tmp[40];
+ const char *pfmt;
+ unsigned int n;
+ int fmt, width, trunc, tmpwidth;
+ int showplus, altform, longlong;
+ int hasdot, hasasteriskwidth, hasasterisktrunc;
+
+ for (; *src; src++)
+ {
+ /* Just copy regular characters */
+
+ if (*src != '%')
+ {
+ obj->put(obj, *src);
+ }
+ else
+ {
+ /* We have found a format specifier. Move past it. */
+
+ pfmt = src;
+ src++;
+
+ fmt = FMT_RJUST; /* Assume right justification. */
+ width = trunc = 0;
+ showplus = altform = longlong = 0;
+ hasdot = hasasteriskwidth = hasasterisktrunc = 0;
+
+ /* Process each format qualifier. */
+
+ for (; *src; src++)
+ {
+ /* Break out of the loop when the format is known. */
+
+ if (strchr("diuxXpobeEfgGlLsc%", *src))
+ {
+ break;
+ }
+
+ /* Check for left justification. */
+
+ else if (*src == '-')
+ {
+ fmt = FMT_LJUST;
+ }
+
+ /* Check for leading zero fill right justification. */
+
+ else if (*src == '0')
+ {
+ fmt = FMT_RJUST0;
+ }
+#if 0
+ /* Center justification. */
+
+ else if (*src == '~')
+ {
+ fmt = FMT_CENTER;
+ }
+#endif
+
+ else if (*src == '*')
+ {
+ int value = va_arg(ap, int);
+ if (hasdot)
+ {
+ trunc = value;
+ hasasterisktrunc = 1;
+ }
+ else
+ {
+ width = value;
+ hasasteriskwidth = 1;
+ }
+ }
+
+ /* Check for field width */
+
+ else if (((*src) >= '1') && ((*src) <= '9'))
+ {
+ /* Accumulate the field width integer. */
+
+ n = ((int)(*src)) - (int)'0';
+ for (src++; (((*src) >= '0') && ((*src) <= '9')); src++)
+ {
+ n = 10*n + (((int)(*src)) - (int)'0');
+ }
+
+ if (hasdot) trunc = n;
+ else width = n;
+
+ /* Back up to the last digit. */
+
+ src--;
+ }
+
+ /* Check for a decimal point. */
+
+ else if (*src == '.')
+ {
+ hasdot = 1;
+ }
+
+ /* Check for leading plus sign. */
+
+ else if (*src == '+')
+ {
+ showplus = 1;
+ }
+
+ /* Check for alternate form. */
+
+ else if (*src == '#')
+ {
+ altform = 1;
+ }
+ }
+
+ /* "%%" means that a literal '%' was intended (instead of a format
+ * specification).
+ */
+
+ if (*src == '%')
+ {
+ obj->put(obj, '%');
+ }
+
+ /* Check for the string format. */
+
+ else if (*src == 's')
+ {
+ /* Just concatenate the string into the output */
+
+ ptmp = va_arg(ap, char *);
+ if (!ptmp) ptmp = (char*)g_nullstring;
+
+ while(*ptmp)
+ {
+ obj->put(obj, *ptmp);
+ ptmp++;
+ }
+ }
+
+ /* Check for the character output */
+
+ else if (*src == 'c')
+ {
+ /* Just copy the character into the output. */
+
+ n = va_arg(ap, int);
+ obj->put(obj, n);
+ }
+ else
+ {
+ /* Check for the long long prefix. */
+
+ if (*src == 'L')
+ {
+ longlong = 1;
+ ++src;
+ }
+ else if (*src == 'l')
+ {
+ if (*++src == 'l')
+ {
+ longlong = 1;
+ ++src;
+ }
+ }
+
+ /* Get the ascii format into the tmp[] buffer. */
+
+ ptmp = tmp;
+
+ /* Handle integer conversions */
+
+ if (strchr("diuxXpob", *src))
+ {
+#ifdef CONFIG_HAVE_LONG_LONG
+ if ((longlong) && (*src != 'p'))
+ {
+ /* Extract the long long value. */
+
+ long long lln = va_arg(ap, long long);
+
+ /* Perform the integer conversion according to the
+ * format specifier
+ */
+
+ switch (*src)
+ {
+ case 'd':
+ case 'i':
+ /* Signed base 10 */
+ {
+ /* Check for leading +/- */
+
+ if (lln < 0)
+ {
+ *ptmp++ = '-';
+ *ptmp = 0;
+ lln = -lln;
+ }
+ else if (showplus)
+ {
+ *ptmp++ = '+';
+ *ptmp = 0;
+ }
+
+ /* Convert the unsigned value to a string. */
+
+ llutodec(&ptmp, (unsigned long long)lln);
+ }
+ break;
+
+ case 'u':
+ /* Unigned base 10 */
+ {
+ /* Check for leading + */
+
+ if (showplus)
+ {
+ *ptmp++ = '+';
+ *ptmp = 0;
+ }
+
+ /* Convert the unsigned value to a string. */
+
+ llutodec(&ptmp, (unsigned long long)lln);
+ }
+ break;
+
+ case 'p':
+ case 'x':
+ case 'X':
+ /* Hexadecimal */
+ {
+ /* Check for alternate form */
+
+ if (altform)
+ {
+ /* Prefix the number with "0x" */
+
+ *ptmp++ = '0';
+ *ptmp++ = 'x';
+ *ptmp = 0;
+ }
+
+ /* Convert the unsigned value to a string. */
+
+ llutohex(&ptmp, (unsigned long long)lln);
+
+ /* Check for upper case conversion. */
+
+ if ((*src) == 'X')
+ {
+ for (ptmp = tmp; *ptmp; ptmp++)
+ {
+ if (((*ptmp) >= 'a') && ((*ptmp) <= 'z'))
+ {
+ /* Convert to upper case. */
+
+ *ptmp += (char)((int)'A' - (int)'a');
+ }
+ }
+ }
+ }
+ break;
+
+ case 'o':
+ /* Octal */
+ {
+ /* Check for alternate form */
+
+ if (altform)
+ {
+ /* Prefix the number with '0' */
+
+ *ptmp++ = '0';
+ *ptmp = 0;
+ }
+
+ /* Convert the unsigned value to a string. */
+
+ llutooct(&ptmp, (unsigned long long)lln);
+ }
+ break;
+
+ case 'b':
+ /* Binary */
+ {
+ /* Convert the unsigned value to a string. */
+
+ llutobin(&ptmp, (unsigned long long)lln);
+ }
+ break;
+
+ default:
+ break;
+ }
+ }
+ else
+ {
+#endif /* CONFIG_HAVE_LONG_LONG */
+ /* Extract the integer value */
+
+ n = va_arg(ap, int);
+
+ /* Perform the integer conversion according to the
+ * format specifier
+ */
+
+ switch (*src)
+ {
+ case 'd':
+ case 'i':
+ /* Signed base 10 */
+ {
+ /* Check for leading +/- */
+
+ if ((int)n < 0)
+ {
+ *ptmp++ = '-';
+ *ptmp = 0;
+ n = -n;
+ }
+ else if (showplus)
+ {
+ *ptmp++ = '+';
+ *ptmp = 0;
+ }
+
+ /* Convert the unsigned value to a string. */
+
+ utodec(&ptmp, (unsigned int)n);
+ }
+ break;
+
+ case 'u':
+ /* Unigned base 10 */
+ {
+ /* Check for leading + */
+
+ if (showplus)
+ {
+ *ptmp++ = '+';
+ *ptmp = 0;
+ }
+
+ /* Convert the unsigned value to a string. */
+
+ utodec(&ptmp, (unsigned int)n);
+ }
+ break;
+
+ case 'x':
+ case 'X':
+ case 'p':
+ /* Hexadecimal */
+ {
+ /* Check for alternate form */
+
+ if (altform)
+ {
+ /* Prefix the number with "0x" */
+
+ *ptmp++ = '0';
+ *ptmp++ = 'x';
+ *ptmp = 0;
+ }
+
+ /* Convert the unsigned value to a string. */
+
+ utohex(&ptmp, (unsigned int)n);
+
+ /* Check for upper case conversion. */
+
+ if ((*src) == 'X')
+ {
+ for (ptmp = tmp; *ptmp; ptmp++)
+ {
+ if (((*ptmp) >= 'a') && ((*ptmp) <= 'z'))
+ {
+ /* Convert to upper case. */
+
+ *ptmp += (char)((int)'A' - (int)'a');
+ }
+ }
+ }
+ else if ((*src) == 'p')
+ {
+ if ((!width) && (fmt == FMT_RJUST))
+ {
+ if (altform) width = 10;
+ else width = 8;
+
+ fmt = FMT_RJUST0;
+ }
+ }
+ }
+ break;
+
+ case 'o':
+ /* Octal */
+ {
+ /* Check for alternate form */
+
+ if (altform)
+ {
+ /* Prefix the number with '0' */
+
+ *ptmp++ = '0';
+ *ptmp = 0;
+ }
+
+ /* Convert the unsigned value to a string. */
+
+ utooct(&ptmp, (unsigned int)n);
+ }
+ break;
+
+ case 'b':
+ /* Binary */
+ {
+ /* Convert the unsigned value to a string. */
+
+ utobin(&ptmp, (unsigned int)n);
+ }
+ break;
+
+ default:
+ break;
+ }
+#ifdef CONFIG_HAVE_LONG_LONG
+ }
+#endif /* CONFIG_HAVE_LONG_LONG */
+
+ /* Now, get the "real" field width of the integer value*/
+
+ tmpwidth = strlen(tmp);
+ if (width <= tmpwidth)
+ {
+ /* Just copy the string. */
+
+ for (ptmp = tmp; *ptmp; )
+ {
+ obj->put(obj, *ptmp);
+ ptmp++;
+ }
+ }
+ else
+ {
+ /* Apply field justification to the integer value. */
+
+ switch (fmt)
+ {
+ default:
+ case FMT_RJUST:
+ for (n = width - tmpwidth; n; n--)
+ {
+ obj->put(obj, ' ');
+ }
+
+ for (ptmp = tmp; *ptmp; )
+ {
+ obj->put(obj, *ptmp);
+ ptmp++;
+ }
+ break;
+
+ case FMT_RJUST0:
+ ptmp = tmp;
+ if (((*ptmp) == '-') || ((*ptmp) == '+'))
+ {
+ obj->put(obj, *ptmp);
+ ptmp++;
+ }
+
+ for (n = width - tmpwidth; n; n--)
+ {
+ obj->put(obj, '0');
+ }
+
+ while (*ptmp)
+ {
+ obj->put(obj, *ptmp);
+ ptmp++;
+ }
+ break;
+
+ case FMT_LJUST:
+ for (ptmp = tmp; *ptmp; )
+ {
+ obj->put(obj, *ptmp);
+ ptmp++;
+ }
+
+ for (n = width - tmpwidth; n; n--)
+ {
+ obj->put(obj, ' ');
+ }
+ break;
+ }
+ }
+ }
+
+ /* Handle floating point conversions */
+
+ else if (strchr("eEfgG", *src))
+ {
+ char tmpfmt[40];
+ const char *psrc;
+ char *pdst;
+ double dbl;
+
+ /* Reconstruct the floating point format. */
+
+ psrc = pfmt;
+ pdst = tmpfmt;
+ while (psrc <= src) *pdst++ = *psrc++;
+ *pdst = 0;
+
+ /* Extract the floating point number. */
+
+ dbl = va_arg(ap, double);
+
+ /* Then let the lib_sprintf do the work. */
+
+ if (hasasteriskwidth)
+ {
+ if (hasasterisktrunc)
+ {
+ lib_sprintf(obj, tmpfmt, width, trunc, dbl);
+ }
+ else
+ {
+ lib_sprintf(obj, tmpfmt, width, dbl);
+ }
+ }
+ else
+ {
+ if (hasasterisktrunc)
+ {
+ lib_sprintf(obj, tmpfmt, trunc, dbl);
+ }
+ else
+ {
+ lib_sprintf(obj, tmpfmt, dbl);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return obj->nput;
+}
+
diff --git a/nuttx/lib/lib_lowprintf.c b/nuttx/lib/lib_lowprintf.c
new file mode 100644
index 0000000000..ef9f9ff0fb
--- /dev/null
+++ b/nuttx/lib/lib_lowprintf.c
@@ -0,0 +1,106 @@
+/************************************************************
+ * lib_lowprintf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#ifdef CONFIG_ARCH_LOWPUTC
+
+#include <stdio.h>
+#include <debug.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Global Functions
+ **********************************************************/
+
+/************************************************************
+ * lib_lowprintf
+ **********************************************************/
+
+int lib_lowprintf(const char *fmt, ...)
+{
+ struct lib_stream_s stream;
+ va_list ap;
+ int ret;
+
+ /* Wrap the stdout in a stream object and let lib_vsprintf
+ * do the work.
+ */
+
+ lib_lowstream(&stream);
+
+ va_start(ap, fmt);
+ ret= lib_vsprintf(&stream, fmt, ap);
+ va_end(ap);
+ return ret;
+}
+
+#endif /* CONFIG_ARCH_LOWPUTC */
diff --git a/nuttx/lib/lib_lowstream.c b/nuttx/lib/lib_lowstream.c
new file mode 100644
index 0000000000..e0985d3f10
--- /dev/null
+++ b/nuttx/lib/lib_lowstream.c
@@ -0,0 +1,72 @@
+/************************************************************
+ * lib_lowstream.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#ifdef CONFIG_ARCH_LOWPUTC
+
+#include <stdio.h>
+#include <errno.h>
+#include <nuttx/arch.h>
+
+#include "lib_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static void lowstream_putc(struct lib_stream_s *this, int ch)
+{
+ if (this && up_putc(ch) != EOF)
+ {
+ this->nput++;
+ }
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+void lib_lowstream(struct lib_stream_s *stream)
+{
+ stream->put = lowstream_putc;
+ stream->nput = 0;
+}
+
+#endif /* CONFIG_ARCH_LOWPUTC */
diff --git a/nuttx/lib/lib_memcmp.c b/nuttx/lib/lib_memcmp.c
new file mode 100644
index 0000000000..b433eda90f
--- /dev/null
+++ b/nuttx/lib/lib_memcmp.c
@@ -0,0 +1,74 @@
+/************************************************************
+ * lib_memcmp.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+#ifndef CONFIG_ARCH_MEMCMP
+int memcmp(const void *s1, const void *s2, size_t n)
+{
+ unsigned char *p1 = (unsigned char *)s1;
+ unsigned char *p2 = (unsigned char *)s2;
+
+ while (n-- > 0)
+ {
+ if (*p1 < *p2)
+ {
+ return -1;
+ }
+ else if (*p1 > *p2)
+ {
+ return 0;
+ }
+
+ p1++;
+ p2++;
+ }
+ return 0;
+}
+#endif
diff --git a/nuttx/lib/lib_memcpy.c b/nuttx/lib/lib_memcpy.c
new file mode 100644
index 0000000000..0953b4d282
--- /dev/null
+++ b/nuttx/lib/lib_memcpy.c
@@ -0,0 +1,60 @@
+/************************************************************
+ * lib_memcpy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+#ifndef CONFIG_ARCH_MEMCPY
+void *memcpy(void *dest, const void *src, size_t n)
+{
+ unsigned char *pout = (unsigned char*)dest;
+ unsigned char *pin = (unsigned char*)src;
+ while (n-- > 0) *pout++ = *pin++;
+ return dest;
+}
+#endif
diff --git a/nuttx/lib/lib_memmove.c b/nuttx/lib/lib_memmove.c
new file mode 100644
index 0000000000..cad0fa2fc4
--- /dev/null
+++ b/nuttx/lib/lib_memmove.c
@@ -0,0 +1,72 @@
+/************************************************************
+ * lib_memmove.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+#ifndef CONFIG_ARCH_MEMMOVE
+void *memmove(void *dest, const void *src, size_t count)
+{
+ char *tmp, *s;
+ if (dest <= src)
+ {
+ tmp = (char*) dest;
+ s = (char*) src;
+ while (count--)
+ *tmp++ = *s++;
+ }
+ else
+ {
+ tmp = (char*) dest + count;
+ s = (char*) src + count;
+ while (count--)
+ *--tmp = *--s;
+ }
+ return dest;
+}
+#endif
diff --git a/nuttx/lib/lib_memset.c b/nuttx/lib/lib_memset.c
new file mode 100644
index 0000000000..25b4929ebd
--- /dev/null
+++ b/nuttx/lib/lib_memset.c
@@ -0,0 +1,59 @@
+/************************************************************
+ * lib_memset.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+#ifndef CONFIG_ARCH_MEMSET
+void *memset(void *s, int c, size_t n)
+{
+ unsigned char *p = (unsigned char*)s;
+ while (n-- > 0) *p++ = c;
+ return s;
+}
+#endif
diff --git a/nuttx/lib/lib_memstream.c b/nuttx/lib/lib_memstream.c
new file mode 100644
index 0000000000..713c77e16a
--- /dev/null
+++ b/nuttx/lib/lib_memstream.c
@@ -0,0 +1,70 @@
+/************************************************************
+ * lib_memstream.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include "lib_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static void memstream_putc(struct lib_stream_s *this, int ch)
+{
+ struct lib_memstream_s *mthis = (struct lib_memstream_s *)this;
+ if (this && this->nput < mthis->buflen - 1)
+ {
+ mthis->buffer[this->nput] = ch;
+ this->nput++;
+ mthis->buffer[this->nput] = '\0';
+ }
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+void lib_memstream(struct lib_memstream_s *memstream,
+ char *bufstart, int buflen)
+{
+ memstream->public.put = memstream_putc;
+ memstream->public.nput = 0; /* Will be buffer index */
+ memstream->buffer = bufstart; /* Start of buffer */
+ memstream->buflen = buflen - 1; /* Save space for null terminator */
+}
+
+
diff --git a/nuttx/lib/lib_printf.c b/nuttx/lib/lib_printf.c
new file mode 100644
index 0000000000..c57000fd0d
--- /dev/null
+++ b/nuttx/lib/lib_printf.c
@@ -0,0 +1,103 @@
+/************************************************************
+ * lib_printf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Global Functions
+ **********************************************************/
+
+/************************************************************
+ * printf
+ **********************************************************/
+
+int printf(const char *fmt, ...)
+{
+ struct lib_stdstream_s stdstream;
+ va_list ap;
+ int ret;
+
+ /* Wrap the stdout in a stream object and let lib_vsprintf
+ * do the work.
+ */
+
+ lib_stdstream(&stdstream, stdout);
+
+ va_start(ap, fmt);
+ ret= lib_vsprintf(&stdstream.public, fmt, ap);
+ va_end(ap);
+ return ret;
+}
diff --git a/nuttx/lib/lib_puts.c b/nuttx/lib/lib_puts.c
new file mode 100644
index 0000000000..711fd81950
--- /dev/null
+++ b/nuttx/lib/lib_puts.c
@@ -0,0 +1,111 @@
+/************************************************************
+ * lib_puts.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * puts
+ ************************************************************/
+
+/* puts() writes the string s and a trailing newline to stdout. */
+int puts(const char *s)
+{
+ int nwritten;
+ int nput = EOF;
+
+ /* Write the string (the next two steps must be atomic) */
+
+ lib_take_semaphore(stdout);
+
+ /* Write the string without its trailing '\0' */
+
+ nwritten = fputs(s, stdout);
+ if (nwritten > 0)
+ {
+ /* Followed by a newline */
+ char newline = '\n';
+ if (lib_fwrite(&newline, 1, stdout) > 0)
+ {
+ nput = nwritten + 1;
+ }
+ }
+ lib_give_semaphore(stdout);
+ return nput;
+}
diff --git a/nuttx/lib/lib_rand.c b/nuttx/lib/lib_rand.c
new file mode 100644
index 0000000000..5ce708bc82
--- /dev/null
+++ b/nuttx/lib/lib_rand.c
@@ -0,0 +1,212 @@
+/************************************************************
+ * lib_rand.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdlib.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#ifndef RND_ORDER
+#define RND_ORDER 1
+#endif
+
+/* Values needed by the random number generator */
+
+#define RND1_CONSTK 470001
+#define RND1_CONSTP 999563
+#define RND2_CONSTK1 366528
+#define RND2_CONSTK2 508531
+#define RND2_CONSTP 998917
+#define RND3_CONSTK1 360137
+#define RND3_CONSTK2 519815
+#define RND3_CONSTK3 616087
+#define RND3_CONSTP 997783
+
+#if RND_ORDER == 1
+# define RND_CONSTP RND1_CONSTP
+#elif RND_ORDER == 2
+# define RND_CONSTP RND2_CONSTP
+#else
+# define RND_CONSTP RND3_CONSTP
+#endif
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+static unsigned int nrand(unsigned int nLimit);
+static double frand1(void);
+#if (RND_ORDER > 1)
+static double frand2(void);
+#if (RND_ORDER > 2)
+static double frand3(void);
+#endif
+#endif
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+static unsigned long g_nRandInt1;
+#if (RND_ORDER > 1)
+static unsigned long g_nRandInt2;
+#if (RND_ORDER > 2)
+static unsigned long g_nRandInt3;
+#endif
+#endif
+
+/************************************************************
+ * Function: srand, rand
+ ************************************************************/
+
+void srand(unsigned int seed)
+{
+ g_nRandInt1 = seed;
+#if (RND_ORDER > 1)
+ g_nRandInt2 = seed;
+ (void)frand1();
+#if (RND_ORDER > 2)
+ g_nRandInt3 = seed;
+ (void)frand2();
+#endif
+#endif
+
+} /* end srand */
+
+int rand(void)
+{
+ return (int)nrand(32768);
+
+} /* end rand */
+
+static unsigned int nrand(unsigned int nLimit)
+{
+ unsigned long nResult;
+ double fRatio;
+
+ /* Loop to be sure a legal random number is generated */
+ do {
+
+ /* Get a random integer in the requested range */
+#if (RND_ORDER == 1)
+ fRatio = frand1();
+#elif (RND_ORDER == 2)
+ fRatio = frand2();
+#else
+ fRatio = frand3();
+#endif
+
+ /* Then, produce the return-able value */
+ nResult = (unsigned long)(((double)nLimit) * fRatio);
+
+ } while (nResult >= (unsigned long)nLimit);
+
+ return (unsigned int)nResult;
+
+} /* end nrand */
+
+static double frand1(void)
+{
+ unsigned long nRandInt;
+
+ /* First order congruential generator */
+ nRandInt = (RND1_CONSTK * g_nRandInt1) % RND1_CONSTP;
+ g_nRandInt1 = nRandInt;
+
+ /* Construct an floating point value in the range from 0.0 up to 1.0 */
+ return ((double)nRandInt) / ((double)RND_CONSTP);
+
+} /* end frand */
+
+#if (RND_ORDER > 1)
+static double frand2(void)
+{
+ unsigned long nRandInt;
+
+ /* Second order congruential generator */
+ nRandInt = (RND2_CONSTK1 * g_nRandInt1 + RND2_CONSTK2 * g_nRandInt2) %
+ RND2_CONSTP;
+ g_nRandInt2 = g_nRandInt1;
+ g_nRandInt1 = nRandInt;
+
+ /* Construct an floating point value in the range from 0.0 up to 1.0 */
+ return ((double)nRandInt) / ((double)RND_CONSTP);
+
+} /* end frand */
+
+#if (RND_ORDER > 2)
+static double frand(void)
+{
+ unsigned long nRandInt;
+
+ /* Third order congruential generator */
+ nRandInt = (RND3_CONSTK1 * g_nRandInt1 + RND3_CONSTK2 * g_nRandInt2 +
+ RND3_CONSTK2 * g_nRandInt3) % RND3_CONSTP;
+ g_nRandInt3 = g_nRandInt2;
+ g_nRandInt2 = g_nRandInt1;
+ g_nRandInt1 = nRandInt;
+
+ /* Construct an floating point value in the range from 0.0 up to 1.0 */
+ return ((double)nRandInt) / ((double)RND_CONSTP);
+
+} /* end frand */
+#endif
+#endif
diff --git a/nuttx/lib/lib_rawprintf.c b/nuttx/lib/lib_rawprintf.c
new file mode 100644
index 0000000000..5377776653
--- /dev/null
+++ b/nuttx/lib/lib_rawprintf.c
@@ -0,0 +1,100 @@
+/************************************************************
+ * lib_rawprintf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include <debug.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Global Functions
+ **********************************************************/
+
+/************************************************************
+ * lib_rawprintf
+ **********************************************************/
+
+int lib_rawprintf(const char *fmt, ...)
+{
+ struct lib_rawstream_s rawstream;
+ va_list ap;
+ int ret;
+
+ /* Wrap the stdout in a stream object and let lib_vsprintf
+ * do the work.
+ */
+
+ lib_rawstream(&rawstream, 1);
+
+ va_start(ap, fmt);
+ ret= lib_vsprintf(&rawstream.public, fmt, ap);
+ va_end(ap);
+ return ret;
+}
diff --git a/nuttx/lib/lib_rawstream.c b/nuttx/lib/lib_rawstream.c
new file mode 100644
index 0000000000..e7c194f477
--- /dev/null
+++ b/nuttx/lib/lib_rawstream.c
@@ -0,0 +1,78 @@
+/************************************************************
+ * lib_rawstream.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include <errno.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static void rawstream_putc(struct lib_stream_s *this, int ch)
+{
+ struct lib_rawstream_s *rthis = (struct lib_rawstream_s *)this;
+ char buffer = ch;
+ if (this && rthis->fd >= 0)
+ {
+ int nwritten;
+ do
+ {
+ nwritten = write(rthis->fd, &buffer, 1);
+ if (nwritten == 1)
+ {
+ this->nput++;
+ }
+ }
+ while (nwritten < 0 && *get_errno_ptr() == EINTR);
+ }
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+void lib_rawstream(struct lib_rawstream_s *rawstream, int fd)
+{
+ rawstream->public.put = rawstream_putc;
+ rawstream->public.nput = 0;
+ rawstream->fd = fd;
+}
+
+
diff --git a/nuttx/lib/lib_rint.c b/nuttx/lib/lib_rint.c
new file mode 100644
index 0000000000..f747eea863
--- /dev/null
+++ b/nuttx/lib/lib_rint.c
@@ -0,0 +1,123 @@
+/************************************************************
+ * lib_rint.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdlib.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+double rint(double x)
+{
+ double retValue;
+
+/* If the current rounding mode rounds toward negative
+ * infinity, rint() is identical to floor(). If the current
+ * rounding mode rounds toward positive infinity, rint() is
+ * identical to ceil(). */
+#if ((defined(FP_ROUND_POSITIVE)) && (FP_ROUNDING_POSITIVE != 0))
+ retValue = ceil(x);
+
+#elif ((defined(FP_ROUND_NEGATIVE)) && (FP_ROUNDING_NEGATIVE != 0))
+ retValue = floor(x);
+
+#else
+
+ /* In the default rounding mode (round to nearest), rint(x) is the
+ * integer nearest x with the additional stipulation that if
+ * |rint(x)-x|=1/2, then rint(x) is even. */
+
+ long dwInteger = (long)x;
+ double fRemainder = x - (double)dwInteger;
+
+ if (x < 0.0) {
+
+ /* fRemainder should be in range 0 .. -1 */
+ if (fRemainder == -0.5)
+ dwInteger = ((dwInteger+1)&~1);
+ else if (fRemainder < -0.5) {
+ dwInteger--;
+
+ } /* end if */
+ } /* end if */
+ else {
+
+ /* fRemainder should be in range 0 .. 1 */
+ if (fRemainder == 0.5)
+ dwInteger = ((dwInteger+1)&~1);
+ else if (fRemainder > 0.5) {
+ dwInteger++;
+
+ } /* end if */
+ } /* end else */
+
+ retValue = (double)dwInteger;
+#endif
+
+ return retValue;
+}
diff --git a/nuttx/lib/lib_sem.c b/nuttx/lib/lib_sem.c
new file mode 100644
index 0000000000..9c105dfca7
--- /dev/null
+++ b/nuttx/lib/lib_sem.c
@@ -0,0 +1,143 @@
+/************************************************************
+ * lib_sem.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#ifdef CONFIG_STDIO_BUFFERED_IO
+
+#include <unistd.h>
+#include <semaphore.h>
+#include <errno.h>
+#include <assert.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * lib_sem_initialize
+ ************************************************************/
+
+void lib_sem_initialize(FILE *stream)
+{
+ /* Initialize the LIB semaphore to one (to support one-at-
+ * a-time access to private data sets.
+ */
+
+ (void)sem_init(&stream->sem, 0, 1);
+
+ stream->holder = -1;
+ stream->counts = 0;
+}
+
+/************************************************************
+ * lib_take_semaphore
+ ************************************************************/
+
+void lib_take_semaphore(FILE *stream)
+{
+ pid_t my_pid = getpid();
+
+ /* Do I already have the semaphore? */
+
+ if (stream->holder == my_pid)
+ {
+ /* Yes, just increment the number of references that I have */
+
+ stream->counts++;
+ }
+ else
+ {
+ /* Take the semaphore (perhaps waiting) */
+
+ while (sem_wait(&stream->sem) != 0)
+ {
+ /* The only case that an error should occr here is if
+ * the wait was awakened by a signal.
+ */
+
+ ASSERT(*get_errno_ptr() == EINTR);
+ }
+
+ /* We have it. Claim the stak and return */
+
+ stream->holder = my_pid;
+ stream->counts = 1;
+ }
+}
+
+/************************************************************
+ * lib_give_semaphore
+ ************************************************************/
+
+void lib_give_semaphore(FILE *stream)
+{
+ pid_t my_pid = getpid();
+
+ /* I better be holding at least one reference to the semaphore */
+
+ ASSERT(stream->holder == my_pid);
+
+ /* Do I hold multiple references to the semphore */
+
+ if (stream->counts > 1)
+ {
+ /* Yes, just release one count and return */
+
+ stream->counts--;
+ }
+ else
+ {
+ /* Nope, this is the last reference I have */
+
+ stream->holder = -1;
+ stream->counts = 0;
+ ASSERT(sem_post(&stream->sem) == 0);
+ }
+}
+#endif /* CONFIG_STDIO_BUFFERED_IO */
diff --git a/nuttx/lib/lib_sprintf.c b/nuttx/lib/lib_sprintf.c
new file mode 100644
index 0000000000..47ec01301a
--- /dev/null
+++ b/nuttx/lib/lib_sprintf.c
@@ -0,0 +1,95 @@
+/************************************************************
+ * lib_sprintf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * sprintf
+ ************************************************************/
+
+int sprintf (char *buf, const char *fmt, ...)
+{
+ struct lib_memstream_s memstream;
+ va_list ap;
+ int n;
+
+ /* Initialize a memory stream to write to the buffer */
+
+ lib_memstream(&memstream, buf, LIB_BUFLEN_UNKNOWN);
+
+ /* Then let lib_vsprintf do the real work */
+
+ va_start(ap, fmt);
+ n = lib_vsprintf(&memstream.public, fmt, ap);
+ va_end(ap);
+ return n;
+}
diff --git a/nuttx/lib/lib_sscanf.c b/nuttx/lib/lib_sscanf.c
new file mode 100644
index 0000000000..4f100c8cab
--- /dev/null
+++ b/nuttx/lib/lib_sscanf.c
@@ -0,0 +1,328 @@
+/************************************************************
+ * lib_sscanf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <debug.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define MAXLN 128
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+int vsscanf(char *buf, const char *s, va_list ap);
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+static const char spaces[] = " \t\n\r\f\v";
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Function: sscanf
+ *
+ * Description:
+ * ANSI standard sscanf implementation.
+ *
+ ************************************************************/
+
+int sscanf(const char *buf, const char *fmt, ...)
+{
+ va_list ap;
+ int count;
+
+ va_start(ap, fmt);
+ count = vsscanf((char*)buf, fmt, ap);
+ va_end(ap);
+ return count;
+
+} /* end sscanf */
+
+/************************************************************
+ * Function: vsscanf
+ *
+ * Description:
+ * ANSI standard vsscanf implementation.
+ *
+ ************************************************************/
+int vsscanf(char *buf, const char *s, va_list ap)
+{
+ int count;
+ int noassign;
+ int width;
+ int base = 10;
+ int lflag;
+ char *tv;
+ const char *tc;
+ char tmp[MAXLN];
+
+ vdbg("vsscanf: buf=\"%s\" fmt=\"%s\"\n", buf, s);
+
+ count = noassign = width = lflag = 0;
+ while (*s && *buf)
+ {
+ /* Skip over white space */
+
+ while (isspace(*s))
+ s++;
+
+ /* Check for a conversion specifier */
+
+ if (*s == '%')
+ {
+ vdbg("vsscanf: Specifier found\n");
+
+ /* Check for qualifiers on the conversion specifier */
+ s++;
+ for (; *s; s++)
+ {
+ vdbg("vsscanf: Processing %c\n", *s);
+
+ if (strchr("dibouxcsefg%", *s))
+ break;
+ if (*s == '*')
+ noassign = 1;
+ else if (*s == 'l' || *s == 'L')
+ lflag = 1;
+ else if (*s >= '1' && *s <= '9') {
+ for (tc = s; isdigit(*s); s++);
+ strncpy(tmp, tc, s - tc);
+ tmp[s - tc] = '\0';
+ width = atoi(tmp);
+ /* atob(&width, tmp, 10); */
+ s--;
+ }
+ }
+
+ /* Process %s: String conversion */
+
+ if (*s == 's')
+ {
+ vdbg("vsscanf: Performing string conversion\n");
+
+ while (isspace(*buf))
+ buf++;
+ if (!width)
+ {
+ width = strcspn(buf, spaces);
+ }
+ if (!noassign)
+ {
+ tv = va_arg(ap, char*);
+ strncpy(tv, buf, width);
+ tv[width] = '\0';
+ }
+ buf += width;
+ }
+
+ /* Process %c: Character conversion */
+
+ else if (*s == 'c')
+ {
+ vdbg("vsscanf: Performing character conversion\n");
+
+ if (!width)
+ width = 1;
+ if (!noassign)
+ {
+ tv = va_arg(ap, char*);
+ strncpy(tv, buf, width);
+ tv[width] = '\0';
+ }
+ buf += width;
+ }
+
+ /* Process %d, %o, %b, %x, %u: Various integer conversions */
+
+ else if (strchr("dobxu", *s))
+ {
+ vdbg("vsscanf: Performing integer conversion\n");
+
+ /* Skip over any white space before the integer string */
+
+ while (isspace(*buf))
+ buf++;
+
+ /* The base of the integer conversion depends on the specific
+ * conversion specification.
+ */
+
+ if (*s == 'd' || *s == 'u')
+ base = 10;
+ else if (*s == 'x')
+ base = 16;
+ else if (*s == 'o')
+ base = 8;
+ else if (*s == 'b')
+ base = 2;
+
+ /* Copy the integer string into a temporary working buffer. */
+
+ if (!width)
+ {
+ if (isspace(*(s + 1)) || *(s + 1) == 0)
+ {
+ width = strcspn(buf, spaces);
+ }
+ else
+ {
+ width = strchr(buf, *(s + 1)) - buf;
+ }
+ }
+ strncpy(tmp, buf, width);
+ tmp[width] = '\0';
+
+ vdbg("vsscanf: tmp[]=\"%s\"\n", tmp);
+
+ /* Perform the integer conversion */
+
+ buf += width;
+ if (!noassign)
+ {
+ int *pint = va_arg(ap, int*);
+ int tmpint = strtol(tmp, NULL, base);
+ vdbg("vsscanf: Return %d to 0x%p\n", tmpint, pint);
+ *pint = tmpint;
+ }
+ }
+
+ /* Process %f: Floating point conversion */
+
+ else if (*s == 'f')
+ {
+ vdbg("vsscanf: Performing floating point conversion\n");
+
+ /* Skip over any white space before the real string */
+
+ while (isspace(*buf))
+ buf++;
+
+ /* Copy the real string into a temporary working buffer. */
+
+ if (!width)
+ {
+ if (isspace(*(s + 1)) || *(s + 1) == 0)
+ {
+ width = strcspn(buf, spaces);
+ }
+ else
+ {
+ width = strchr(buf, *(s + 1)) - buf;
+ }
+ }
+ strncpy(tmp, buf, width);
+ tmp[width] = '\0';
+ buf += width;
+
+ vdbg("vsscanf: tmp[]=\"%s\"\n", tmp);
+
+ /* Perform the floating point conversion */
+
+ if (!noassign)
+ {
+ /* strtod always returns a double */
+
+ double dvalue = strtod(tmp, NULL);
+ void *pv = va_arg(ap, void*);
+
+ vdbg("vsscanf: Return %f to 0x%p\n", dvalue, pv);
+
+ /* But we have to check whether we need to return a
+ * float or a double.
+ */
+
+ if (lflag)
+ {
+ *((double*)pv) = dvalue;
+ }
+ else
+ {
+ *((float*)pv) = (float)dvalue;
+ }
+ }
+ }
+
+ if (!noassign)
+ count++;
+ width = noassign = lflag = 0;
+ s++;
+ }
+
+ /* Its is not a conversion specifier */
+
+ else
+ {
+ while (isspace(*buf))
+ buf++;
+ if (*s != *buf)
+ break;
+ else
+ s++, buf++;
+ }
+ }
+ return count;
+}
diff --git a/nuttx/lib/lib_stdstream.c b/nuttx/lib/lib_stdstream.c
new file mode 100644
index 0000000000..a6fe7849ac
--- /dev/null
+++ b/nuttx/lib/lib_stdstream.c
@@ -0,0 +1,70 @@
+/************************************************************
+ * lib_stdstream.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include "lib_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static void stdstream_putc(struct lib_stream_s *this, int ch)
+{
+ struct lib_stdstream_s *sthis = (struct lib_stdstream_s *)this;
+ if (this)
+ {
+ if (putc(ch, sthis->stream) != EOF)
+ {
+ this->nput++;
+ }
+ }
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+void lib_stdstream(struct lib_stdstream_s *stdstream,
+ FILE *stream)
+{
+ stdstream->public.put = stdstream_putc;
+ stdstream->public.nput = 0;
+ stdstream->stream = stream;
+}
+
+
diff --git a/nuttx/lib/lib_strchr.c b/nuttx/lib/lib_strchr.c
new file mode 100644
index 0000000000..5b3426dbb9
--- /dev/null
+++ b/nuttx/lib/lib_strchr.c
@@ -0,0 +1,71 @@
+/************************************************************
+ * lib_strchr.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/* The strchr() function returns a pointer to the first
+ * occurrence of the character c in the string s.
+ */
+
+char *strchr(const char *s, int c)
+{
+ if (s)
+ {
+ for (; *s; s++)
+ {
+ if (*s == c)
+ {
+ return (char*)s;
+ }
+ }
+ }
+
+ return NULL;
+}
+
diff --git a/nuttx/lib/lib_strcmp.c b/nuttx/lib/lib_strcmp.c
new file mode 100644
index 0000000000..0b1ce89d52
--- /dev/null
+++ b/nuttx/lib/lib_strcmp.c
@@ -0,0 +1,63 @@
+/************************************************************
+ * lib_strcmp.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+#ifndef CONFIG_ARCH_STRCMP
+int strcmp(const char *cs, const char *ct)
+{
+ register signed char result;
+ for (;;)
+ {
+ if ((result = *cs - *ct++) != 0 || !*cs++)
+ break;
+ }
+ return result;
+}
+#endif
diff --git a/nuttx/lib/lib_strcpy.c b/nuttx/lib/lib_strcpy.c
new file mode 100644
index 0000000000..d5cc999933
--- /dev/null
+++ b/nuttx/lib/lib_strcpy.c
@@ -0,0 +1,59 @@
+/************************************************************
+ * lib_strcpy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+#ifndef CONFIG_ARCH_STRCPY
+char *strcpy(char *dest, const char *src)
+{
+ char *tmp = dest;
+ while ((*dest++ = *src++) != '\0');
+ return tmp;
+}
+#endif
diff --git a/nuttx/lib/lib_strdup.c b/nuttx/lib/lib_strdup.c
new file mode 100644
index 0000000000..285d76c2c6
--- /dev/null
+++ b/nuttx/lib/lib_strdup.c
@@ -0,0 +1,61 @@
+/************************************************************
+ * lib_strdup.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+char *strdup(const char *s)
+{
+ char *news = NULL;
+ if (s)
+ {
+ news = malloc(strlen(s) + 1);
+ if (news)
+ {
+ strcpy(news, s);
+ }
+ }
+ return news;
+}
diff --git a/nuttx/lib/lib_streamsem.c b/nuttx/lib/lib_streamsem.c
new file mode 100644
index 0000000000..d0dbad56dc
--- /dev/null
+++ b/nuttx/lib/lib_streamsem.c
@@ -0,0 +1,99 @@
+/************************************************************
+ * lib_streamsem.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_STREAMS > 0
+
+#include <sys/types.h>
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <semaphore.h>
+#include <errno.h>
+
+#include <nuttx/fs.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Private types
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+void stream_semtake(struct streamlist *list)
+{
+ /* Take the semaphore (perhaps waiting) */
+
+ while (sem_wait(&list->sl_sem) != 0)
+ {
+ /* The only case that an error should occr here is if
+ * the wait was awakened by a signal.
+ */
+
+ ASSERT(*get_errno_ptr() == EINTR);
+ }
+}
+
+void stream_semgive(struct streamlist *list)
+{
+ sem_post(&list->sl_sem);
+}
+
+#endif /* CONFIG_NFILE_STREAMS */
+
diff --git a/nuttx/lib/lib_strlen.c b/nuttx/lib/lib_strlen.c
new file mode 100644
index 0000000000..f2cf6f227f
--- /dev/null
+++ b/nuttx/lib/lib_strlen.c
@@ -0,0 +1,59 @@
+/************************************************************
+ * lib_strlen.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+#ifndef CONFIG_ARCH_STRLEN
+size_t strlen(const char *s)
+{
+ const char *sc;
+ for (sc = s; *sc != '\0'; ++sc);
+ return sc - s;
+}
+#endif
diff --git a/nuttx/lib/lib_strncpy.c b/nuttx/lib/lib_strncpy.c
new file mode 100644
index 0000000000..11d999fe89
--- /dev/null
+++ b/nuttx/lib/lib_strncpy.c
@@ -0,0 +1,57 @@
+/************************************************************
+ * lib_strncpy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+#ifndef CONFIG_ARCH_STRNCPY
+char *strncpy(char *dest, const char *src, size_t n)
+{
+ char *ret = dest; /* Value to be returned */
+ char *end = dest + n; /* End of dest buffer + 1 byte */
+
+ while ((*dest++ = *src++) != '\0' && dest != end);
+ return ret;
+}
+#endif
diff --git a/nuttx/lib/lib_strtol.c b/nuttx/lib/lib_strtol.c
new file mode 100644
index 0000000000..8e2d26f3f6
--- /dev/null
+++ b/nuttx/lib/lib_strtol.c
@@ -0,0 +1,176 @@
+/************************************************************
+ * lib_strtol.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <string.h>
+#include <ctype.h>
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/* Skip leading spaces */
+
+static void lib_skipspace(const char **nptr)
+{
+ register const char *tmp = *nptr;
+ while (isspace(*tmp)) tmp++;
+ *nptr = tmp;
+}
+
+static int lib_isbasedigit(int c, int base, int *value)
+{
+ int tmp = 0;
+ int ret = 0;
+
+ if (base <= 10)
+ {
+ if (c >= '0' && c <= base + '0' - 1)
+ {
+ tmp = c - '0';
+ ret = 1;
+ }
+ }
+ else if (base <= 36)
+ {
+ if (c >= '0' && c <= '9')
+ {
+ tmp = c - '0';
+ ret = 1;
+ }
+ else if (c >= 'a' && c <= 'a' + base - 11)
+ {
+ tmp = c - 'a' + 10;
+ ret = 1;
+ }
+ else if (c >= 'A' && c <= 'A' + base - 11)
+ {
+ tmp = c - 'A' + 10;
+ ret = 1;
+ }
+ }
+
+ if (value)
+ {
+ *value = tmp;
+ }
+ return ret;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/* Limited to base 1-36 */
+
+long strtol(const char *nptr, char **endptr, int base)
+{
+ unsigned long accum = 0;
+ int value;
+ int negate = 0;
+
+ if (nptr)
+ {
+ /* Skip leading spaces */
+
+ lib_skipspace(&nptr);
+
+ /* Check for leading + or - */
+
+ if (*nptr == '-')
+ {
+ negate = 1;
+ nptr++;
+ lib_skipspace(&nptr);
+ }
+ else if (*nptr == '+')
+ {
+ nptr++;
+ lib_skipspace(&nptr);
+ }
+
+ /* Check for unspecified base */
+
+ if (!base)
+ {
+ base = 10;
+ if (*nptr == '0')
+ {
+ base = 8;
+ nptr++;
+ if ((*nptr == 'X' || *nptr == 'x') &&
+ lib_isbasedigit(nptr[1], 16, NULL))
+ {
+ base = 16;
+ nptr++;
+ }
+ }
+ }
+ else if (base == 16)
+ {
+ if (nptr[0] == '0' && (nptr[1] == 'X' || nptr[1] == 'x'))
+ {
+ nptr += 2;
+ }
+ }
+
+ while (lib_isbasedigit(*nptr, base, &value))
+ {
+ accum = accum*base + value;
+ nptr++;
+ }
+
+ if (endptr)
+ {
+ *endptr = (char *)nptr;
+ }
+
+ if (negate)
+ {
+ return -(long)accum;
+ }
+ }
+ return (long)accum;
+}
+
+unsigned long strtoul(const char *nptr, char **endptr, int base)
+{
+ return (unsigned long)strtol(nptr, endptr, base);
+}
diff --git a/nuttx/lib/lib_ungetc.c b/nuttx/lib/lib_ungetc.c
new file mode 100644
index 0000000000..45cde1efc9
--- /dev/null
+++ b/nuttx/lib/lib_ungetc.c
@@ -0,0 +1,124 @@
+/************************************************************
+ * lib_ungetc.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_STREAMS > 0
+
+#include <stdio.h>
+#include <errno.h>
+#include <nuttx/fs.h>
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Global Functions
+ **********************************************************/
+
+/************************************************************
+ * fgetc
+ **********************************************************/
+
+int ungetc(int c, FILE *stream)
+{
+#if CONFIG_NUNGET_CHARS > 0
+ int nungotten;
+#endif
+
+ /* Stream must be open for read access */
+
+ if ((stream && stream->fs_filedes < 0) ||
+ ((stream->fs_oflags & O_RDOK) == 0))
+ {
+ *get_errno_ptr() = EBADF;
+ return EOF;
+ }
+
+#if CONFIG_NUNGET_CHARS > 0
+ nungotten = stream->fs_nungotten;
+ if (stream->fs_nungotten < CONFIG_NUNGET_CHARS)
+ {
+ stream->fs_ungotten[nungotten] = c;
+ stream->fs_nungotten = nungotten + 1;
+ return c;
+ }
+ else
+#endif
+ {
+ *get_errno_ptr() = ENOMEM;
+ return EOF;
+ }
+}
+
+#endif /* CONFIG_NFILE_STREAMS */
diff --git a/nuttx/lib/lib_vfprintf.c b/nuttx/lib/lib_vfprintf.c
new file mode 100644
index 0000000000..7d1b666a40
--- /dev/null
+++ b/nuttx/lib/lib_vfprintf.c
@@ -0,0 +1,93 @@
+/************************************************************
+ * lib_vfprintf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <semaphore.h>
+
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+int vfprintf(FILE *stream, const char *fmt, va_list ap)
+{
+ struct lib_stdstream_s stdstream;
+ int n = ERROR;
+
+ if (stream)
+ {
+ /* Wrap the stream in a stream object and let lib_vsprintf
+ * do the work.
+ */
+
+ lib_stdstream(&stdstream, stream);
+ n = lib_vsprintf(&stdstream.public, fmt, ap);
+ }
+ return n;
+}
diff --git a/nuttx/lib/lib_vprintf.c b/nuttx/lib/lib_vprintf.c
new file mode 100644
index 0000000000..6c731ca9aa
--- /dev/null
+++ b/nuttx/lib/lib_vprintf.c
@@ -0,0 +1,91 @@
+/************************************************************
+ * lib_vprintf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdio.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Global Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Global Functions
+ **********************************************************/
+
+/************************************************************
+ * vprintf
+ **********************************************************/
+
+int vprintf(const char *fmt, va_list ap)
+{
+ /* vfprintf into stdout */
+
+ return vfprintf(stdout, fmt, ap);
+}
diff --git a/nuttx/lib/lib_vsprintf.c b/nuttx/lib/lib_vsprintf.c
new file mode 100644
index 0000000000..ec3d34fdba
--- /dev/null
+++ b/nuttx/lib/lib_vsprintf.c
@@ -0,0 +1,95 @@
+/************************************************************
+ * lib_vsprintf.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdio.h>
+
+#include "lib_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Global Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Constant Data
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * vsprintf
+ ************************************************************/
+
+int vsprintf (char *dest, const char *src, va_list ap)
+{
+ struct lib_memstream_s memstream;
+
+ /* Wrap the destination buffer in a stream object and let
+ * lib_vsprintf do the work.
+ */
+
+ lib_memstream(&memstream, dest, LIB_BUFLEN_UNKNOWN);
+ return lib_vsprintf(&memstream.public, src, ap);
+}
diff --git a/nuttx/lib/sq_addafter.c b/nuttx/lib/sq_addafter.c
new file mode 100644
index 0000000000..896aed5cae
--- /dev/null
+++ b/nuttx/lib/sq_addafter.c
@@ -0,0 +1,71 @@
+/************************************************************
+ * sq_addafter.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sq_addafter.c
+ *
+ * Description:
+ * The sq_addafter function adds 'node' after 'prev' in the
+ * 'queue.'
+ *
+ ************************************************************/
+
+void sq_addafter(sq_entry_t *prev, sq_entry_t *node,
+ sq_queue_t *queue)
+{
+ if (!queue->head || prev == queue->tail)
+ {
+ sq_addlast(node, queue);
+ }
+ else
+ {
+ node->flink = prev->flink;
+ prev->flink = node;
+ }
+}
diff --git a/nuttx/lib/sq_addfirst.c b/nuttx/lib/sq_addfirst.c
new file mode 100644
index 0000000000..f59fbc5684
--- /dev/null
+++ b/nuttx/lib/sq_addfirst.c
@@ -0,0 +1,67 @@
+/************************************************************
+ * sq_addfirst.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sq_addfirst
+ *
+ * Description:
+ * The sq_addfirst function places the 'node' at the head
+ * of the 'queue'
+ *
+ ************************************************************/
+
+void sq_addfirst(sq_entry_t *node, sq_queue_t *queue)
+{
+ node->flink = queue->head;
+ if (!queue->head)
+ {
+ queue->tail = node;
+ }
+ queue->head = node;
+}
diff --git a/nuttx/lib/sq_addlast.c b/nuttx/lib/sq_addlast.c
new file mode 100644
index 0000000000..172808b33c
--- /dev/null
+++ b/nuttx/lib/sq_addlast.c
@@ -0,0 +1,72 @@
+/************************************************************
+ * sq_addlast.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sq_addlast
+ *
+ * Description:
+ * The sq_addlast function places the 'node' at the tail of
+ * the 'queue'
+ ************************************************************/
+
+void sq_addlast(sq_entry_t *node, sq_queue_t *queue)
+{
+ node->flink = NULL;
+ if (!queue->head)
+ {
+ queue->head = node;
+ queue->tail = node;
+ }
+ else
+ {
+ queue->tail->flink = node;
+ queue->tail = node;
+ }
+}
+
diff --git a/nuttx/lib/sq_rem.c b/nuttx/lib/sq_rem.c
new file mode 100644
index 0000000000..30a49b9b18
--- /dev/null
+++ b/nuttx/lib/sq_rem.c
@@ -0,0 +1,83 @@
+/************************************************************
+ * sq_rem.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sq_rem
+ *
+ * Description:
+ * sq_rem removes a 'node' for 'queue.'
+ *
+ ************************************************************/
+
+void sq_rem(sq_entry_t *node, sq_queue_t *queue)
+{
+ if (queue->head && node)
+ {
+ if (node == queue->head)
+ {
+ queue->head = node->flink;
+ if (node == queue->tail)
+ {
+ queue->tail = NULL;
+ }
+ }
+ else
+ {
+ sq_entry_t *prev;
+ for(prev = (sq_entry_t*)queue->head;
+ prev && prev->flink != node;
+ prev = prev->flink);
+
+ if (prev)
+ {
+ sq_remafter(prev, queue);
+ }
+ }
+ }
+}
diff --git a/nuttx/lib/sq_remafter.c b/nuttx/lib/sq_remafter.c
new file mode 100644
index 0000000000..353e8ec0a9
--- /dev/null
+++ b/nuttx/lib/sq_remafter.c
@@ -0,0 +1,78 @@
+/************************************************************
+ * sq_remafter.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name:
+ *
+ * Description:
+ * sq_remafter removes the entry following 'node; from the
+ * 'queue' Returns a reference to the removed entry.
+ *
+ ************************************************************/
+
+sq_entry_t *sq_remafter(sq_entry_t *node, sq_queue_t *queue)
+{
+ sq_entry_t *ret = node->flink;
+ if (queue->head && ret)
+ {
+ if (queue->tail == ret)
+ {
+ queue->tail = node;
+ node->flink = NULL;
+ }
+ else
+ {
+ node->flink = ret->flink;
+ }
+
+ ret->flink = NULL;
+ }
+
+ return ret;
+}
diff --git a/nuttx/lib/sq_remfirst.c b/nuttx/lib/sq_remfirst.c
new file mode 100644
index 0000000000..dbbd5f378f
--- /dev/null
+++ b/nuttx/lib/sq_remfirst.c
@@ -0,0 +1,75 @@
+/************************************************************
+ * sq_remfirst.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sq_remfirst
+ *
+ * Description:
+ * sq_remfirst function removes the first entry from
+ * 'queue'
+ *
+ ************************************************************/
+
+sq_entry_t *sq_remfirst(sq_queue_t *queue)
+{
+ sq_entry_t *ret = queue->head;
+
+ if (ret)
+ {
+ queue->head = ret->flink;
+ if (!queue->head)
+ {
+ queue->tail = NULL;
+ }
+
+ ret->flink = NULL;
+ }
+
+ return ret;
+}
diff --git a/nuttx/lib/sq_remlast.c b/nuttx/lib/sq_remlast.c
new file mode 100644
index 0000000000..35e5cda6a7
--- /dev/null
+++ b/nuttx/lib/sq_remlast.c
@@ -0,0 +1,87 @@
+/************************************************************
+ * sq_remlast.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sq_remlast
+ *
+ * Description:
+ * Removes the last entry in a singly-linked queue.
+ *
+ ************************************************************/
+
+sq_entry_t *sq_remlast(sq_queue_t *queue)
+{
+ sq_entry_t *ret = queue->tail;
+
+ if (ret)
+ {
+ if (queue->head == queue->tail)
+ {
+ queue->head = NULL;
+ queue->tail = NULL;
+ }
+ else
+ {
+ sq_entry_t *prev;
+ for(prev = queue->head;
+ prev && prev->flink != ret;
+ prev = prev->flink);
+
+ if (prev)
+ {
+ prev->flink = NULL;
+ queue->tail = prev;
+ }
+ }
+
+ ret->flink = NULL;
+ }
+
+ return ret;
+}
diff --git a/nuttx/mm/Makefile b/nuttx/mm/Makefile
new file mode 100644
index 0000000000..79460a34a7
--- /dev/null
+++ b/nuttx/mm/Makefile
@@ -0,0 +1,76 @@
+############################################################
+# Makefile
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+-include $(TOPDIR)/Make.defs
+
+MKDEP = $(TOPDIR)/tools/mkdeps.sh
+
+ASRCS =
+AOBJS = $(ASRCS:.S=.o)
+CSRCS = mm_initialize.c mm_sem.c mm_addfreechunk.c mm_size2ndx.c mm_shrinkchunk.c \
+ mm_malloc.c mm_zalloc.c mm_calloc.c mm_realloc.c \
+ mm_memalign.c mm_free.c mm_mallinfo.c
+COBJS = $(CSRCS:.c=.o)
+
+SRCS = $(ASRCS) $(CSRCS)
+OBJS = $(AOBJS) $(COBJS)
+
+BIN = libmm.a
+
+all: $(BIN)
+
+$(AOBJS): %.o: %.S
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(COBJS): %.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(BIN): $(OBJS)
+ $(AR) rcs $@ $(OBJS)
+
+.depend: Makefile $(SRCS)
+ $(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
+ touch $@
+
+depend: .depend
+
+clean:
+ rm -f $(BIN) *.o *~
+
+distclean: clean
+ rm -f Make.dep .depend
+
+-include Make.dep
+
diff --git a/nuttx/mm/Makefile.test b/nuttx/mm/Makefile.test
new file mode 100644
index 0000000000..1ff3f9913c
--- /dev/null
+++ b/nuttx/mm/Makefile.test
@@ -0,0 +1,64 @@
+############################################################
+# Makefile.test
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+SRCS = mm_test.c mm_initialize.c mm_sem.c mm_addfreechunk.c mm_size2ndx.c mm_shrinkchunk.c \
+ mm_malloc.c mm_zalloc.c mm_calloc.c mm_realloc.c \
+ mm_memalign.c mm_free.c mm_mallinfo.c
+OBJS = $(SRCS:.c=.o1)
+
+LIBS = -lpthread -lc
+
+CC = gcc
+LD = gcc
+
+DEFINES = -DMM_TEST=1
+WARNIGNS = -Wall -Wstrict-prototypes -Wshadow
+CFLAGS = -g $(DEFINES)
+LDFLAGS =
+
+BIN = ../mm_test
+
+all: $(BIN)
+
+$(OBJS): %.o1: %.c
+ @echo "Compiling $<"
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(BIN): $(OBJS)
+ @echo "Linking {$(OBJS)} to produce $@"
+ $(LD) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
+
+clean:
+ rm -f $(BIN) *.o1 *~
diff --git a/nuttx/mm/mm_addfreechunk.c b/nuttx/mm/mm_addfreechunk.c
new file mode 100644
index 0000000000..f59b9f59ef
--- /dev/null
+++ b/nuttx/mm/mm_addfreechunk.c
@@ -0,0 +1,91 @@
+/************************************************************
+ * mm_addfreechunk.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * mm_addfreechunk
+ *
+ * Description:
+ * Add a free chunk to the node next
+ * It is assumed that the caller holds the mm semaphore
+ *
+ ************************************************************/
+
+void mm_addfreechunk(struct mm_freenode_s *node)
+{
+ struct mm_freenode_s *next;
+ struct mm_freenode_s *prev;
+
+ /* Convert the size to a nodelist index */
+
+ int ndx = mm_size2ndx(node->size);
+
+ /* Now put the new node int the next */
+
+ for (prev = &g_nodelist[ndx], next = g_nodelist[ndx].flink;
+ next && next->size && next->size < node->size;
+ prev = next, next = next->flink);
+
+ /* Does it go in mid next or at the end? */
+
+ prev->flink = node;
+ node->blink = prev;
+ node->flink = next;
+
+ if (next)
+ {
+ /* The new node goes between prev and next */
+
+ next->blink = node;
+ }
+}
diff --git a/nuttx/mm/mm_calloc.c b/nuttx/mm/mm_calloc.c
new file mode 100644
index 0000000000..de69b881c1
--- /dev/null
+++ b/nuttx/mm/mm_calloc.c
@@ -0,0 +1,68 @@
+/************************************************************
+ * mm_calloc.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * calloc
+ *
+ * Descripton:
+ * calloc calculates the size and calls zalloc
+ ************************************************************/
+
+void *calloc(size_t n, size_t elem_size)
+{
+ void *ret = NULL;
+
+ if (n > 0 && elem_size > 0)
+ {
+ ret = zalloc(n * elem_size);
+ }
+
+ return ret;
+}
diff --git a/nuttx/mm/mm_environment.h b/nuttx/mm/mm_environment.h
new file mode 100644
index 0000000000..15f6924860
--- /dev/null
+++ b/nuttx/mm/mm_environment.h
@@ -0,0 +1,120 @@
+/************************************************************
+ * mm_environment.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __MM_ENVIRONMENT_H
+#define __MM_ENVIRONMENT_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/* The platform configuratioin file will not be included
+ * when the memory manager is built for the host-based
+ * test harness.
+ */
+
+#ifndef MM_TEST
+# include <nuttx/config.h>
+# include <sys/types.h>
+# include <stdlib.h>
+# include <string.h>
+# include <debug.h>
+# include <errno.h>
+# include <assert.h>
+#else
+# include <sys/types.h>
+# include <string.h>
+# include <assert.h>
+#endif
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* Special definitions used when the memory mnager is built
+ * for the host-based test harness.
+ */
+
+#ifdef MM_TEST
+
+/* Standard types */
+
+typedef unsigned int uint32;
+
+/* Use the real system errno */
+
+# define mm_errno errno
+
+/* When built for the test harness, we change the names of the
+ * exported functions so that they do not collide with the
+ * host libc names.
+ */
+
+# define malloc mm_malloc
+# define memalign mm_memalign
+# define realloc mm_realloc
+# define zalloc mm_zalloc
+# define calloc mm_calloc
+# define free mm_free
+
+/* Use normal libc assertion functions */
+
+# undef ASSERT
+# define ASSERT(e) assert(e)
+# undef DEBUGASSERT
+# define DEBUGASSERT(e) assert(e)
+
+/* Debug macros are always on */
+
+# define CONFIG_DEBUG
+
+# undef dbg
+# define dbg(format, arg...) printf(format, ##arg)
+# undef vdg
+# define vdbg(format, arg...) printf(format, ##arg)
+
+#else
+# define mm_errno (*get_errno_ptr())
+#endif
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/************************************************************
+ * Pulblic Function Prototypes
+ ************************************************************/
+
+#endif /* __MM_ENVIRONMENT_H */
diff --git a/nuttx/mm/mm_free.c b/nuttx/mm/mm_free.c
new file mode 100644
index 0000000000..3363a46b07
--- /dev/null
+++ b/nuttx/mm/mm_free.c
@@ -0,0 +1,151 @@
+/************************************************************
+ * mm_free.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <assert.h>
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * free
+ *
+ * Description:
+ * Returns a chunk of memory into the list of free nodes,
+ * merging with adjacent free chunks if possible.
+ *
+ ************************************************************/
+
+void free(void *mem)
+{
+ struct mm_freenode_s *node;
+ struct mm_freenode_s *prev;
+ struct mm_freenode_s *next;
+
+ /* Protect against attempts to free a NULL reference */
+
+ if (!mem)
+ {
+ return;
+ }
+
+ /* We need to hold the MM semaphore while we muck with the
+ * nodelist.
+ */
+
+ mm_takesemaphore();
+
+ /* Map the memory chunk into a free node */
+
+ node = (struct mm_freenode_s *)((char*)mem - SIZEOF_MM_ALLOCNODE);
+ node->preceding &= ~MM_ALLOC_BIT;
+
+ /* Check if the following node is free and, if so, merge it */
+
+ next = (struct mm_freenode_s *)((char*)node + node->size);
+ if ((next->preceding & MM_ALLOC_BIT) == 0)
+ {
+ struct mm_allocnode_s *andbeyond;
+
+ /* Get the node following the next node (which will
+ * become the new next node). We know that we can never
+ * index past the tail chunk because it is always allocated.
+ */
+
+ andbeyond = (struct mm_allocnode_s*)((char*)next + next->size);
+
+ /* Remove the next node. There must be a predecessor,
+ * but there may not be a successor node.
+ */
+
+ DEBUGASSERT(next->blink);
+ next->blink->flink = next->flink;
+ if (next->flink)
+ {
+ next->flink->blink = next->blink;
+ }
+
+ /* Then merge the two chunks */
+
+ node->size += next->size;
+ andbeyond->preceding = node->size | (andbeyond->preceding & MM_ALLOC_BIT);
+ next = andbeyond;
+ }
+
+ /* Check if the preceding node is also free and, if so, merge
+ * it with this node
+ */
+
+ prev = (struct mm_freenode_s *)((char*)node - node->preceding);
+ if ((prev->preceding & MM_ALLOC_BIT) == 0)
+ {
+ /* Remove the node. There must be a predecessor, but there may
+ * not be a successor node.
+ */
+
+ DEBUGASSERT(prev->blink);
+ prev->blink->flink = prev->flink;
+ if (prev->flink)
+ {
+ prev->flink->blink = prev->blink;
+ }
+
+ /* Then merge the two chunks */
+
+ prev->size += node->size;
+ next->preceding = prev->size | (next->preceding & MM_ALLOC_BIT);
+ node = prev;
+ }
+
+ /* Add the merged node to the nodelist */
+
+ mm_addfreechunk(node);
+
+ mm_givesemaphore();
+}
diff --git a/nuttx/mm/mm_initialize.c b/nuttx/mm/mm_initialize.c
new file mode 100644
index 0000000000..a16ac9b0e3
--- /dev/null
+++ b/nuttx/mm/mm_initialize.c
@@ -0,0 +1,145 @@
+/************************************************************
+ * mm_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/* This is the size of the heap provided to mm */
+
+uint32 g_heapsize;
+
+/* This is the first and last nodes of the heap */
+
+struct mm_allocnode_s *g_heapstart;
+struct mm_allocnode_s *g_heapend;
+
+/* All free nodes are maintained in a doubly linked list. This
+ * array provides some hooks into the list at various points to
+ * speed searches for free nodes.
+ */
+
+struct mm_freenode_s g_nodelist[MM_NNODES];
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mm_initialize
+ *
+ * Description:
+ * This is an internal OS function called only at power-up
+ * boot time.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+void mm_initialize(void *heapstart, size_t heapsize)
+{
+ struct mm_freenode_s *node;
+ int i;
+
+ CHECK_ALLOCNODE_SIZE;
+ CHECK_FREENODE_SIZE;
+
+ /* Adjust the provide heap start and size so that they are
+ * both aligned with the MM_MIN_CHUNK size.
+ */
+
+ uint32 heapbase = MM_ALIGN_UP((uint32)heapstart);
+ uint32 heapend = MM_ALIGN_DOWN((uint32)heapstart + (uint32)heapsize);
+
+ /* Save the size of the heap */
+
+ g_heapsize = heapend - heapbase;
+
+ /* Initialize the node array */
+
+ memset(g_nodelist, 0, sizeof(struct mm_freenode_s) * MM_NNODES);
+ for (i = 1; i < MM_NNODES; i++)
+ {
+ g_nodelist[i-1].flink = &g_nodelist[i];
+ g_nodelist[i].blink = &g_nodelist[i-1];
+ }
+
+ /* Create two "allocated" guard nodes at the beginning and end of
+ * the heap. These only serve to keep us from allocating outside
+ * of the heap.
+ *
+ * And create one free node between the guard nodes that contains
+ * all available memory.
+ */
+
+ g_heapstart = (struct mm_allocnode_s *)heapbase;
+ g_heapstart->size = SIZEOF_MM_ALLOCNODE;
+ g_heapstart->preceding = MM_ALLOC_BIT;
+
+ node = (struct mm_freenode_s *)(heapbase + SIZEOF_MM_ALLOCNODE);
+ node->size = g_heapsize - 2*SIZEOF_MM_ALLOCNODE;
+ node->preceding = SIZEOF_MM_ALLOCNODE;
+
+ g_heapend = (struct mm_allocnode_s *)(heapend - SIZEOF_MM_ALLOCNODE);
+ g_heapend->size = SIZEOF_MM_ALLOCNODE;
+ g_heapend->preceding = node->size | MM_ALLOC_BIT;
+
+ /* Add the single, large free node to the nodelist */
+
+ mm_addfreechunk(node);
+
+ /* Initialize the malloc semaphore to one (to support one-at-
+ * a-time access to private data sets.
+ */
+
+ mm_seminitialize();
+}
diff --git a/nuttx/mm/mm_internal.h b/nuttx/mm/mm_internal.h
new file mode 100644
index 0000000000..e171f2b605
--- /dev/null
+++ b/nuttx/mm/mm_internal.h
@@ -0,0 +1,185 @@
+/************************************************************
+ * mm_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __MM_INTERNAL_H
+#define __MM_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* These definitions define the characteristics of allocator
+ *
+ * MM_MIN_SHIFT is used to define MM_MIN_CHUNK.
+ * MM_MIN_CHUNK - is the smallest physical chunk that can
+ * be allocated. It must be at least a large as
+ * sizeof(struct mm_freenode_s). Larger values may
+ * improve performance slightly, but will waste memory
+ * due to quantization losses.
+ *
+ * MM_MAX_SHIFT is used to define MM_MAX_CHUNK
+ * MM_MAX_CHUNK is the largest, contiguous chunk of memory
+ * that can be allocated. It can range from 16-bytes to
+ * 4Gb. Larger values of MM_MAX_SHIFT can cause larger
+ * data structure sizes and, perhaps, minor performance
+ * losses.
+ */
+
+#define MM_MIN_SHIFT 4 /* 16 bytes */
+#define MM_MAX_SHIFT 22 /* 4 Mb */
+
+/* All other definitions derive from these two */
+
+#define MM_MIN_CHUNK (1 << MM_MIN_SHIFT)
+#define MM_MAX_CHUNK (1 << MM_MAX_SHIFT)
+#define MM_NNODES (MM_MAX_SHIFT - MM_MIN_SHIFT + 1)
+
+#define MM_GRAN_MASK (MM_MIN_CHUNK-1)
+#define MM_ALIGN_UP(a) (((a) + MM_GRAN_MASK) & ~MM_GRAN_MASK)
+#define MM_ALIGN_DOWN(a) ((a) & ~MM_GRAN_MASK)
+
+/* An allocated chunk is distinguished from a free chunk by
+ * bit 31 of the 'preceding' chunk size. If set, then this is
+ * an allocated chunk.
+ */
+
+#define MM_ALLOC_BIT 0x80000000
+#define MM_IS_ALLOCATED(n) \
+ ((int)((struct mm_allocnode_s*)(n)->preceding) < 0))
+
+/************************************************************
+ * Public Types
+ ************************************************************/
+
+/* This describes an allocated chunk. An allocated chunk is
+ * distinguished from a free chunk by bit 31 of the 'precding'
+ * chunk size. If set, then this is an allocated chunk.
+ */
+
+struct mm_allocnode_s
+{
+ uint32 size; /* Size of this chunk */
+ uint32 preceding; /* Size of the preceding chunk */
+};
+
+#define SIZEOF_MM_ALLOCNODE 8
+#define CHECK_ALLOCNODE_SIZE \
+ DEBUGASSERT(sizeof(struct mm_allocnode_s) == SIZEOF_MM_ALLOCNODE)
+
+/* This describes a free chunk */
+
+struct mm_freenode_s
+{
+ uint32 size; /* Size of this chunk */
+ uint32 preceding; /* Size of the preceding chunk */
+ struct mm_freenode_s *flink; /* Supports a doubly linked list */
+ struct mm_freenode_s *blink;
+};
+
+#define SIZEOF_MM_FREENODE 16
+#define CHECK_FREENODE_SIZE \
+ DEBUGASSERT(sizeof(struct mm_freenode_s) == SIZEOF_MM_FREENODE)
+
+/* Normally defined in stdlib.h */
+
+#ifdef MM_TEST
+struct mallinfo
+{
+ int arena; /* This is the total size of memory allocated
+ * for use by malloc in bytes. */
+ int ordblks; /* This is the number of free (not in use) chunks */
+ int mxordblk; /* Size of the largest free (not in use) chunk */
+ int uordblks; /* This is the total size of memory occupied by
+ * chunks handed out by malloc. */
+ int fordblks; /* This is the total size of memory occupied
+ * by free (not in use) chunks.*/
+};
+#endif
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* This is the size of the heap provided to mm */
+
+extern uint32 g_heapsize;
+
+/* This is the first and last nodes of the heap */
+
+extern struct mm_allocnode_s *g_heapstart;
+extern struct mm_allocnode_s *g_heapend;
+
+/* All free nodes are maintained in a doubly linked list. This
+ * array provides some hooks into the list at various points to
+ * speed searches for free nodes.
+ */
+
+extern struct mm_freenode_s g_nodelist[MM_NNODES];
+
+/************************************************************
+ * Inline Functions
+ ************************************************************/
+
+/************************************************************
+ * Pulblic Function Prototypes
+ ************************************************************/
+
+/* Normally defined in malloc.h */
+
+#ifdef MM_TEST
+ extern void *mm_malloc(size_t);
+ extern void mm_free(void*);
+ extern void *mm_realloc(void*, size_t);
+ extern void *mm_memalign(size_t, size_t);
+ extern void *mm_zalloc(size_t);
+ extern void *mm_calloc(size_t, size_t);
+ extern struct mallinfo mallinfo(void);
+#endif
+
+extern void mm_shrinkchunk(struct mm_allocnode_s *node, uint32 size);
+extern void mm_addfreechunk(struct mm_freenode_s *node);
+extern int mm_size2ndx(uint32 size);
+extern void mm_seminitialize(void);
+extern void mm_takesemaphore(void);
+extern void mm_givesemaphore(void);
+#ifdef MM_TEST
+ extern int mm_getsemaphore(void);
+#endif
+
+#endif /* __MM_INTERNAL_H */
diff --git a/nuttx/mm/mm_mallinfo.c b/nuttx/mm/mm_mallinfo.c
new file mode 100644
index 0000000000..7b242b4ea1
--- /dev/null
+++ b/nuttx/mm/mm_mallinfo.c
@@ -0,0 +1,108 @@
+/************************************************************
+ * mm_mallinfo.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <assert.h>
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * mallinfo
+ *
+ * Description:
+ * mallinfo returns a copy of updated current mallinfo.
+ *
+ ************************************************************/
+
+struct mallinfo mallinfo(void)
+{
+ static struct mallinfo stats;
+ struct mm_allocnode_s *node;
+ uint32 mxordblk = 0;
+ int ordblks = 0; /* Number of non-inuse chunks */
+ uint32 uordblks = 0; /* Total allocated space */
+ uint32 fordblks = 0; /* Total non-inuse space */
+
+ /* Visit each node in physical memory */
+
+ for (node = g_heapstart;
+ node < g_heapend;
+ node = (struct mm_allocnode_s *)((char*)node + node->size))
+ {
+ if (node->preceding & MM_ALLOC_BIT)
+ {
+ uordblks += node->size;
+ }
+ else
+ {
+ ordblks++;
+ fordblks += node->size;
+ if (node->size > mxordblk)
+ {
+ mxordblk = node->size;
+ }
+ }
+ }
+
+ DEBUGASSERT(node == g_heapend);
+ uordblks += SIZEOF_MM_ALLOCNODE; /* account for the tail node */
+ DEBUGASSERT(uordblks + fordblks == g_heapsize);
+
+ stats.arena = g_heapsize;
+ stats.ordblks = ordblks;
+ stats.mxordblk = mxordblk;
+ stats.uordblks = uordblks;
+ stats.fordblks = fordblks;
+ return stats;
+}
diff --git a/nuttx/mm/mm_malloc.c b/nuttx/mm/mm_malloc.c
new file mode 100644
index 0000000000..222aca3b2a
--- /dev/null
+++ b/nuttx/mm/mm_malloc.c
@@ -0,0 +1,203 @@
+/************************************************************
+ * mm_malloc.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+/* Special definitions when we operate in the normal vs. the
+ * host-pc test environement.
+ */
+
+#include <assert.h>
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#ifndef NULL
+# define NULL ((void*)0)
+#endif
+
+/************************************************************
+ * Type Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/************************************************************
+ * Public Data
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * malloc
+ *
+ * Description:
+ * Find the smallest chunk that satisfies the request.
+ * Take the memory from that chunk, save the remaining,
+ * smaller chunk (if any).
+ *
+ * 8-byte alignment of the allocated data is assured.
+ *
+ ************************************************************/
+
+void *malloc(size_t size)
+{
+ struct mm_freenode_s *node;
+ void *ret = NULL;
+ int ndx;
+
+ /* Handle bad sizes */
+
+ if (size <= 0)
+ {
+ return NULL;
+ }
+
+ /* Adjust the size to account for (1) the size of the allocated
+ * node and (2) to make sure that it is an even multiple of
+ * our granule size.
+ */
+
+ size = MM_ALIGN_UP(size + SIZEOF_MM_ALLOCNODE);
+
+ /* We need to hold the MM semaphore while we muck with the
+ * nodelist.
+ */
+
+ mm_takesemaphore();
+
+ /* Get the location in the node list to start the search.
+ * Special case really big alloctions
+ */
+
+ if (size >= MM_MAX_CHUNK)
+ {
+ ndx = MM_NNODES-1;
+ }
+ else
+ {
+ /* Convert the request size into a nodelist index */
+
+ ndx = mm_size2ndx(size);
+ }
+
+ /* Search for a large enough chunk in the list of nodes.
+ * This list is ordered by size, but will have occasional
+ * zero sized nodes as we visit other g_nodelist[] entries.
+ */
+
+ for (node = g_nodelist[ndx].flink;
+ node && node->size < size;
+ node = node->flink);
+
+ /* If we found a node with non-zero size, then this is one
+ * to use. Since the list is ordered, we know that is must be
+ * best fitting chunk available.
+ */
+
+ if (node)
+ {
+ struct mm_freenode_s *remainder;
+ struct mm_freenode_s *next;
+ uint32 remaining;
+
+ /* Remove the node. There must be a predecessor, but there may
+ * not be a successor node.
+ */
+
+ DEBUGASSERT(node->blink);
+ node->blink->flink = node->flink;
+ if (node->flink)
+ {
+ node->flink->blink = node->blink;
+ }
+
+ /* Check if we have to split the free node into one of the
+ * allocated size and another smaller freenode. In some
+ * cases, the remaining bytes can be smaller (they may be
+ * SIZEOF_MM_ALLOCNODE). In that case, we will just carry
+ * the few wasted bytes at the end of the allocation.
+ */
+
+ remaining = node->size - size;
+ if (remaining >= SIZEOF_MM_FREENODE)
+ {
+ /* Get a pointer to the next node in physical memory */
+
+ next = (struct mm_freenode_s*)(((char*)node) + node->size);
+
+ /* Create the remainder node */
+
+ remainder = (struct mm_freenode_s*)(((char*)node) + size);
+ remainder->size = remaining;
+ remainder->preceding = size;
+
+ /* Adjust the size of the node under consideration */
+
+ node->size = size;
+
+ /* Adjust the 'preceding' size of the (old) next node,
+ * preserving the allocated flag.
+ */
+
+ next->preceding = remaining | (next->preceding & MM_ALLOC_BIT);
+
+ /* Add the remainder back into the nodelist */
+
+ mm_addfreechunk(remainder);
+ }
+
+ /* Handle the case of an exact size match */
+
+ node->preceding |= MM_ALLOC_BIT;
+ ret = (void*)((char*)node + SIZEOF_MM_ALLOCNODE);
+ }
+
+ mm_givesemaphore();
+ return ret;
+}
diff --git a/nuttx/mm/mm_memalign.c b/nuttx/mm/mm_memalign.c
new file mode 100644
index 0000000000..b91625d0a2
--- /dev/null
+++ b/nuttx/mm/mm_memalign.c
@@ -0,0 +1,212 @@
+/************************************************************
+ * mm_memalign.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <assert.h>
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * memalign
+ *
+ * Description:
+ * memalign requests more than enough space from malloc,
+ * finds a region within that chunk that meets the alignment
+ * request and then frees any leading or trailing space.
+ *
+ * The alignment argument must be a power of two (not
+ * checked). 8-byte alignment is guaranteed by normal
+ * malloc calls.
+ *
+ ************************************************************/
+
+void *memalign(size_t alignment, size_t size)
+{
+ struct mm_allocnode_s *node;
+ uint32 rawchunk;
+ uint32 alignedchunk;
+ uint32 mask = (uint32)(alignment - 1);
+ uint32 allocsize;
+
+ /* If this requested alignement less than or equal to the
+ * natural alignment of malloc, then just let malloc do the
+ * work.
+ */
+
+ if (alignment <= MM_MIN_CHUNK)
+ {
+ return malloc(size);
+ }
+
+ /* Adjust the size to account for (1) the size of the allocated
+ * node, (2) to make sure that it is an even multiple of
+ * our granule size, and to include the alignment amount.
+ *
+ * Notice that we increase the allocation size by twice the
+ * the requested alignment. We do this so that there will
+ * be at least two valid alignment points within the allocated
+ * memory.
+ *
+ * NOTE: These are sizes given to malloc and not chunk sizes.
+ * The do not include SIZEOF_MM_ALLOCNODE.
+ */
+
+ size = MM_ALIGN_UP(size); /* Make mutliples of our granule size */
+ allocsize = size + 2*alignment; /* Add double full alignment size */
+
+ /* If the alignment is small
+
+ /* Then malloc that size */
+
+ rawchunk = (uint32)malloc(allocsize);
+ if (!rawchunk)
+ {
+ return NULL;
+ }
+
+ /* We need to hold the MM semaphore while we muck with the
+ * chunks and nodelist.
+ */
+
+ mm_takesemaphore();
+
+ /* Get the node associated with the allocation and the next
+ * node after the allocation.
+ */
+
+ node = (struct mm_allocnode_s*)(rawchunk - SIZEOF_MM_ALLOCNODE);
+
+ /* Find the aligned subregion */
+
+ alignedchunk = (rawchunk + mask) & ~mask;
+
+ /* Check if there is free space at the beginning of the aligned chunk */
+
+ if (alignedchunk != rawchunk)
+ {
+ struct mm_allocnode_s *newnode;
+ struct mm_allocnode_s *next;
+ uint32 precedingsize;
+
+ /* Get the node the next node after the allocation. */
+
+ next = (struct mm_allocnode_s*)((char*)node + node->size);
+
+ /* Make sure that there is space to convert the preceding mm_allocnode_s
+ * into an mm_freenode_s. I think that this should always be true
+ */
+
+ DEBUGASSERT(alignedchunk >= rawchunk + 8);
+
+ newnode = (struct mm_allocnode_s*)(alignedchunk - SIZEOF_MM_ALLOCNODE);
+
+ /* Preceding size is full size of the new 'node,' including
+ * SIZEOF_MM_ALLOCNODE
+ */
+
+ precedingsize = (uint32)newnode - (uint32)node;
+
+ /* If we were unlucky, then the alignedchunk can lie in such
+ * a position that precedingsize < SIZEOF_NODE_FREENODE. We
+ * can't let that happen because we are going to cast 'node' to
+ * struct mm_freenode_s below. This is why we allocated memory
+ * large enough to support two alignment points. In this case,
+ * we will simply use the second alignment point.
+ */
+
+ if (precedingsize < SIZEOF_MM_FREENODE)
+ {
+ alignedchunk += alignment;
+ newnode = (struct mm_allocnode_s*)(alignedchunk - SIZEOF_MM_ALLOCNODE);
+ precedingsize = (uint32)newnode - (uint32)node;
+ }
+
+ /* Set up the size of the new node */
+
+ newnode->size = (uint32)next - (uint32)newnode;
+ newnode->preceding = precedingsize | MM_ALLOC_BIT;
+
+ /* Reduce the size of the original chunk and mark it not allocated, */
+
+ node->size = precedingsize;
+ node->preceding &= ~MM_ALLOC_BIT;
+
+ /* Fix the preceding size of the next node */
+
+ next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT);
+
+ /* Convert the newnode chunk size back into malloc-compatible
+ * size by subtracting the header size SIZEOF_MM_ALLOCNODE.
+ */
+
+ allocsize = newnode->size - SIZEOF_MM_ALLOCNODE;
+
+ /* Add the original, newly freed node to the free nodelist */
+
+ mm_addfreechunk((struct mm_freenode_s *)node);
+
+ /* Replace the original node with the newlay realloaced,
+ * aligned node
+ */
+
+ node = newnode;
+ }
+
+ /* Check if there is free space at the end of the aligned chunk */
+
+ if (allocsize > size)
+ {
+ /* Shrink the chunk by that much -- remember, mm_shrinkchunk
+ * wants internal chunk sizes that include SIZEOF_MM_ALLOCNODE,
+ * and not the malloc-compatible sizes that we have.
+ */
+
+ mm_shrinkchunk(node, size + SIZEOF_MM_ALLOCNODE);
+ }
+
+ mm_givesemaphore();
+ return (void*)alignedchunk;
+}
diff --git a/nuttx/mm/mm_realloc.c b/nuttx/mm/mm_realloc.c
new file mode 100644
index 0000000000..01836de60d
--- /dev/null
+++ b/nuttx/mm/mm_realloc.c
@@ -0,0 +1,336 @@
+/************************************************************
+ * mm_realloc.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <string.h>
+#include "mm_environment.h"
+#include <stdio.h> /* For NULL */
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * realloc
+ *
+ * Description:
+ * If the reallocation is for less space, then:
+ * (1) the current allocation is reduced in size
+ * (2) the remainder at the end of the allocation is
+ * returned to the free list.
+ *
+ * If the request is for more space and the current
+ * allocation can be extended, it will be extended by:
+ * (1) Taking the additional space from the following
+ * free chunk, or
+ * (2) Taking the additional space from the preceding
+ * free chunk.
+ * (3) Or both
+ *
+ * If the request is for more space but the current chunk
+ * cannot be extended, then malloc a new buffer, copy the
+ * data into the new buffer, and free the old buffer.
+ *
+ ************************************************************/
+
+void *realloc(void *oldmem, size_t size)
+{
+ struct mm_allocnode_s *oldnode;
+ struct mm_freenode_s *prev;
+ struct mm_freenode_s *next;
+ uint32 oldsize;
+ uint32 prevsize = 0;
+ uint32 nextsize = 0;
+
+ /* If oldmem is NULL, then realloc is equivalent to malloc */
+
+ if (!oldmem)
+ {
+ return malloc(size);
+ }
+
+ /* If size is zero, then realloc is equivalent to free */
+
+ if (size <= 0)
+ {
+ free(oldmem);
+ return NULL;
+ }
+
+ /* Adjust the size to account for (1) the size of the allocated
+ * node and (2) to make sure that it is an even multiple of
+ * our granule size.
+ */
+
+ size = MM_ALIGN_UP(size + SIZEOF_MM_ALLOCNODE);
+
+ /* Map the memory chunk into an allocated node structure */
+
+ oldnode = (struct mm_allocnode_s *)((char*)oldmem - SIZEOF_MM_ALLOCNODE);
+
+ /* We need to hold the MM semaphore while we muck with the
+ * nodelist.
+ */
+
+ mm_takesemaphore();
+
+ /* Check if this is a request to reduce the size of the allocation. */
+
+ oldsize = oldnode->size;
+ if (size <= oldsize)
+ {
+ mm_shrinkchunk(oldnode, size);
+ mm_givesemaphore();
+ return oldmem;
+ }
+
+ /* This is a request to increase the size of the allocation, Get the
+ * available sizes before and after the oldnode so that we can make
+ * the best decision
+ */
+
+ next = (struct mm_freenode_s *)((char*)oldnode + oldnode->size);
+ if ((next->preceding & MM_ALLOC_BIT) == 0)
+ {
+ nextsize = next->size;
+ }
+
+ prev = (struct mm_freenode_s *)((char*)oldnode - (oldnode->preceding & ~MM_ALLOC_BIT));
+ if ((prev->preceding & MM_ALLOC_BIT) == 0)
+ {
+ prevsize = prev->size;
+ }
+
+ /* Now, check if we can extend the current allocation or not */
+
+ if (nextsize + prevsize + oldsize >= size)
+ {
+ uint32 needed = size - oldsize;
+ uint32 takeprev;
+ uint32 takenext;
+
+ /* Check if we can extend into the previous chunk and if the
+ * previous chunk is smaller than the next chunk.
+ */
+
+ if (prevsize > 0 && (nextsize >= prevsize || nextsize <= 0))
+ {
+ /* Can we get everything we need from the previous chunk? */
+
+ if (needed > prevsize)
+ {
+ /* No, take the whole previous chunk and get the
+ * rest that we need from the next chunk.
+ */
+
+ takeprev = prevsize;
+ takenext = needed - prevsize;
+ }
+ else
+ {
+ /* Yes, take what we need from the previous chunk */
+
+ takeprev = needed;
+ takenext = 0;
+ }
+
+ needed = 0;
+ }
+
+ /* Check if we can extend into the next chunk and if we still
+ * need more memory.
+ */
+
+ if (nextsize > 0 && needed)
+ {
+ /* Can we get everything we need from the next chunk? */
+
+ if (needed > nextsize)
+ {
+ /* No, take the whole next chunk and get the
+ * rest that we need from the previous chunk.
+ */
+
+ takeprev = needed - nextsize;
+ takenext = nextsize;
+ }
+ else
+ {
+ /* Yes, take what we need from the previous chunk */
+
+ takeprev = 0;
+ takenext = needed;
+ }
+ }
+
+ /* Extend into the previous free chunk */
+
+ if (takeprev)
+ {
+ struct mm_allocnode_s *newnode;
+
+ /* Remove the previous node. There must be a predecessor,
+ * but there may not be a successor node.
+ */
+
+ DEBUGASSERT(prev->blink);
+ prev->blink->flink = prev->flink;
+ if (prev->flink)
+ {
+ prev->flink->blink = prev->blink;
+ }
+
+ /* Extend the node into the previous free chunk */
+
+ newnode = (struct mm_allocnode_s *)((char*)oldnode - takeprev);
+
+ /* Did we consume the entire preceding chunk? */
+
+ if (takeprev < prevsize)
+ {
+ /* No, just take what we need from the previous chunk
+ * and put it back into the free list
+ */
+
+ prev->size -= takeprev;
+ newnode->size = oldsize + takeprev;
+ newnode->preceding = prev->size | MM_ALLOC_BIT;
+ next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT);
+
+ /* Return the previous free node to the nodelist (with the new size) */
+
+ mm_addfreechunk(prev);
+
+ /* Now we want to return newnode */
+
+ oldnode = newnode;
+ }
+ else
+ {
+ /* Yes update its size (newnode->preceding is already set) */
+
+ newnode->size += oldsize;
+ next->preceding = newnode->size;
+ }
+
+ oldnode = newnode;
+ oldsize = newnode->size;
+ }
+
+ /* Extend into the next free chunk */
+
+ if (takenext)
+ {
+ struct mm_freenode_s *newnode;
+ struct mm_allocnode_s *andbeyond;
+
+ /* Get the chunk following the next node (which could be the tail chunk) */
+
+ andbeyond = (struct mm_allocnode_s*)((char*)next + nextsize);
+
+ /* Remove the next node. There must be a predecessor,
+ * but there may not be a successor node.
+ */
+
+ DEBUGASSERT(next->blink);
+ next->blink->flink = next->flink;
+ if (next->flink)
+ {
+ next->flink->blink = next->blink;
+ }
+
+ /* Extend the node into the previous next chunk */
+
+ oldnode->size = oldsize + takenext;
+ newnode = (struct mm_freenode_s *)((char*)oldnode + oldnode->size);
+
+ /* Did we consume the entire preceding chunk? */
+
+ if (takenext < nextsize)
+ {
+ /* No, take what we need from the next chunk and return it
+ * to the free nodelist.
+ */
+
+ newnode->size = nextsize - takenext;
+ newnode->preceding = oldnode->size;
+ andbeyond->preceding = newnode->size | (andbeyond->preceding & MM_ALLOC_BIT);
+
+ /* Add the new free node to the nodelist (with the new size) */
+
+ mm_addfreechunk(newnode);
+ }
+ else
+ {
+ /* Yes, just update some pointers. */
+
+ andbeyond->preceding = oldnode->size | (andbeyond->preceding & MM_ALLOC_BIT);
+ }
+ }
+
+
+ mm_givesemaphore();
+ return (void*)((char*)oldnode + SIZEOF_MM_ALLOCNODE);
+ }
+
+ /* The current chunk cannot be extended. Just allocate a new chunk and copy */
+
+ else
+ {
+ /* Allocate a new block. On failure, realloc must return NULL but
+ * leave the original memory in place.
+ */
+
+ mm_givesemaphore();
+ char *newmem = (char*)malloc(size);
+ if (newmem)
+ {
+ memcpy(newmem, oldmem, oldsize);
+ free(oldmem);
+ }
+
+ return newmem;
+ }
+
+}
+
diff --git a/nuttx/mm/mm_sem.c b/nuttx/mm/mm_sem.c
new file mode 100644
index 0000000000..be3a2c1b0e
--- /dev/null
+++ b/nuttx/mm/mm_sem.c
@@ -0,0 +1,177 @@
+/************************************************************
+ * mm_sem.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include "mm_environment.h"
+#include <unistd.h>
+#include <semaphore.h>
+#include <errno.h>
+#include <assert.h>
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* Define the following to enable semaphore state monitoring */
+//#define MONITOR_MM_SEMAPHORE 1
+
+#ifdef MONITOR_MM_SEMAPHORE
+# ifdef CONFIG_DEBUG
+# include <debug.h>
+# define msemdbg dbg
+# else
+# define msemdbg printf
+# endif
+#else
+# define msemdbg(x...)
+#endif
+
+/************************************************************
+ * Private Data
+ ************************************************************/
+
+/* Mutually exclusive access to this data set is enforced with
+ * the following (un-named) semaphore. */
+
+static sem_t g_mm_semaphore;
+static pid_t g_holder;
+static int g_counts_held;
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * mm_seminitialize
+ ************************************************************/
+
+void mm_seminitialize(void)
+{
+ /* Initialize the MM semaphore to one (to support one-at-
+ * a-time access to private data sets.
+ */
+
+ (void)sem_init(&g_mm_semaphore, 0, 1);
+
+ g_holder = -1;
+ g_counts_held = 0;
+}
+
+/************************************************************
+ * mm_takesemaphore
+ ************************************************************/
+
+void mm_takesemaphore(void)
+{
+ pid_t my_pid = getpid();
+
+ /* Do I already have the semaphore? */
+
+ if (g_holder == my_pid)
+ {
+ /* Yes, just increment the number of references that I have */
+
+ g_counts_held++;
+ }
+ else
+ {
+ /* Take the semaphore (perhaps waiting) */
+
+ msemdbg("%s: PID=%d taking\n", __FUNCTION__, my_pid);
+ while (sem_wait(&g_mm_semaphore) != 0)
+ {
+ /* The only case that an error should occur here is if
+ * the wait was awakened by a signal.
+ */
+
+ ASSERT(mm_errno == EINTR);
+ }
+
+ /* We have it. Claim the stak and return */
+
+ g_holder = my_pid;
+ g_counts_held = 1;
+ }
+
+ msemdbg("%s: Holder=%d count=%d\n",
+ __FUNCTION__, g_holder, g_counts_held);
+}
+
+/************************************************************
+ * mm_givesemaphore
+ ************************************************************/
+
+void mm_givesemaphore(void)
+{
+ pid_t my_pid = getpid();
+
+ /* I better be holding at least one reference to the semaphore */
+
+ ASSERT(g_holder == my_pid);
+
+ /* Do I hold multiple references to the semphore */
+
+ if (g_counts_held > 1)
+ {
+ /* Yes, just release one count and return */
+
+ g_counts_held--;
+ msemdbg("%s: Holder=%d count=%d\n",
+ __FUNCTION__, g_holder, g_counts_held);
+ }
+ else
+ {
+ /* Nope, this is the last reference I have */
+
+ msemdbg("%s: PID=%d giving\n", __FUNCTION__, my_pid);
+ g_holder = -1;
+ g_counts_held = 0;
+ ASSERT(sem_post(&g_mm_semaphore) == 0);
+ }
+}
+
+#ifdef MM_TEST
+int mm_getsemaphore(void)
+{
+ int sval;
+ sem_getvalue(&g_mm_semaphore, &sval);
+ return sval;
+}
+#endif
+
diff --git a/nuttx/mm/mm_shrinkchunk.c b/nuttx/mm/mm_shrinkchunk.c
new file mode 100644
index 0000000000..daf57c7076
--- /dev/null
+++ b/nuttx/mm/mm_shrinkchunk.c
@@ -0,0 +1,140 @@
+/************************************************************
+ * mm_shrinkchunk.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * mm_shrinkchunk
+ *
+ * Description:
+ * Reduce the size of the chunk specified by the node
+ * structure to the specified size. this internal logic
+ * is used both from memalign to dispose of any trailing
+ * memory in the the aligned allocation and also by realloc
+ * when there is a request to reduce the size of an allocation.
+ *
+ * NOTES:
+ * (1) size is the whole chunk size (payload and header)
+ * (2) the caller must hold the MM semaphore.
+ *
+ ************************************************************/
+
+void mm_shrinkchunk(struct mm_allocnode_s *node, uint32 size)
+{
+ struct mm_freenode_s *next;
+
+ /* Get a reference to the next node */
+
+ next = (struct mm_freenode_s*)((char*)node + node->size);
+
+ /* Check if it is free */
+
+ if ((next->preceding & MM_ALLOC_BIT) == 0)
+ {
+ struct mm_allocnode_s *andbeyond;
+ struct mm_freenode_s *newnode;
+
+ /* Get the chunk next the next node (which could be the tail chunk) */
+
+ andbeyond = (struct mm_allocnode_s*)((char*)next + next->size);
+
+ /* Remove the next node. There must be a predecessor, but there may
+ * not be a successor node.
+ */
+
+ DEBUGASSERT(next->blink);
+ next->blink->flink = next->flink;
+ if (next->flink)
+ {
+ next->flink->blink = next->blink;
+ }
+
+ /* Create a new chunk that will hold both the next chunk
+ * and the tailing memory from the aligned chunk.
+ */
+
+ newnode = (struct mm_freenode_s*)((char*)node + size);
+
+ /* Set up the size of the new node */
+
+ newnode->size = next->size + node->size - size;
+ newnode->preceding = size;
+ node->size = size;
+ andbeyond->preceding = newnode->size | (andbeyond->preceding & MM_ALLOC_BIT);
+
+ /* Add the new node to the freenodelist */
+
+ mm_addfreechunk(newnode);
+ }
+
+ /* The next chunk is allocated. Try to free the end portion
+ * at the end chunk to be shrunk.
+ */
+
+ else if (node->size >= size + SIZEOF_MM_FREENODE)
+ {
+ struct mm_freenode_s *newnode;
+
+ /* Create a new chunk that will hold both the next chunk
+ * and the tailing memory from the aligned chunk.
+ */
+
+ newnode = (struct mm_freenode_s*)((char*)node + size);
+
+ /* Set up the size of the new node */
+
+ newnode->size = node->size - size;
+ newnode->preceding = size;
+ node->size = size;
+ next->preceding = newnode->size | MM_ALLOC_BIT;
+
+ /* Add the new node to the freenodelist */
+
+ mm_addfreechunk(newnode);
+ }
+}
diff --git a/nuttx/mm/mm_size2ndx.c b/nuttx/mm/mm_size2ndx.c
new file mode 100644
index 0000000000..271e3e2214
--- /dev/null
+++ b/nuttx/mm/mm_size2ndx.c
@@ -0,0 +1,70 @@
+/************************************************************
+ * mm_size2ndx.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/* Convert the size to a nodelist index */
+
+int mm_size2ndx(uint32 size)
+{
+ int ndx = 0;
+
+ if (size >= MM_MAX_CHUNK)
+ {
+ return MM_NNODES-1;
+ }
+
+ size >>= MM_MIN_SHIFT;
+ while (size > 1)
+ {
+ ndx++;
+ size >>= 1;
+ }
+
+ return ndx;
+}
diff --git a/nuttx/mm/mm_test.c b/nuttx/mm/mm_test.c
new file mode 100644
index 0000000000..86861121ac
--- /dev/null
+++ b/nuttx/mm/mm_test.c
@@ -0,0 +1,439 @@
+/************************************************************
+ * mm_test.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef unsigned int uint32;
+#include "mm_internal.h"
+
+/* Definitions */
+
+#define TEST_HEAP_SIZE 0x00100000
+#define NTEST_ALLOCS 32
+
+/* #define STOP_ON_ERRORS do{}while(0) */
+#define STOP_ON_ERRORS exit(1)
+
+/* Heap provided to memory manager */
+
+unsigned long heap_base;
+unsigned long heap_size = TEST_HEAP_SIZE;
+
+/* Test allocations */
+
+static const int alloc_sizes[NTEST_ALLOCS] =
+{
+ 1024, 12, 962, 5692, 10254, 111, 9932, 601,
+ 222, 2746, 3, 124321, 68, 776, 6750, 852,
+ 4732, 28, 901, 480, 5011, 1536, 2011, 81647,
+ 646, 1646, 69179, 194, 2590, 7, 969, 70
+};
+static const int realloc_sizes[NTEST_ALLOCS] =
+{
+ 18, 3088, 963, 123, 511, 11666, 3723, 42,
+ 9374, 1990, 1412, 6, 592, 4088, 11, 5040,
+ 8663, 91255, 28, 4346, 9172, 168, 229, 4734,
+ 59139, 221, 7830, 30421, 1666, 4, 812, 416
+};
+static const int random1[NTEST_ALLOCS] =
+{
+ 20, 11, 3, 31, 9, 29, 7, 17,
+ 21, 2, 26, 18, 14, 25, 0, 10,
+ 27, 19, 22, 28, 8, 30, 12, 15,
+ 4, 1, 24, 6, 16, 13, 5, 23
+};
+static const int random2[NTEST_ALLOCS] =
+{
+ 2, 19, 12, 23, 30, 11, 27, 4,
+ 20, 7, 0, 16, 28, 15, 5, 24,
+ 10, 17, 25, 31, 8, 29, 3, 26,
+ 9, 18, 22, 13, 1, 21, 14, 6
+};
+static const int random3[NTEST_ALLOCS] =
+{
+ 8, 17, 3, 18, 26, 23, 30, 11,
+ 12, 22, 4, 20, 25, 10, 27, 1,
+ 29, 14, 19, 21, 0, 31, 7, 24,
+ 9, 15, 2, 28, 16, 6, 13, 5
+};
+static const int alignment[NTEST_ALLOCS/2] =
+{
+ 128, 2048, 131072, 8192, 32, 32768, 16384 , 262144,
+ 512, 4096, 65536, 8, 64, 1024, 16, 4
+};
+static void *allocs[NTEST_ALLOCS];
+static struct mallinfo alloc_info;
+static unsigned int g_adjheapsize = 0;
+
+/************************************************************
+ * mm_showchunkinfo
+ ************************************************************/
+
+static int mm_findinfreelist(struct mm_freenode_s *node)
+{
+ struct mm_freenode_s *list;
+
+ for(list = &g_nodelist[0];
+ list;
+ list = list->flink)
+ {
+ if (list == node)
+ {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static void mm_showchunkinfo(void)
+{
+ struct mm_allocnode_s *node;
+ int found;
+
+ /* Visit each node in physical memory */
+
+ printf(" CHUNK LIST:\n");
+
+ for (node = g_heapstart;
+ node < g_heapend;
+ node = (struct mm_allocnode_s *)((char*)node + node->size))
+ {
+ printf(" %p 0x%08x 0x%08x %s",
+ node, node->size, node->preceding & ~MM_ALLOC_BIT,
+ node->preceding & MM_ALLOC_BIT ? "Allocated" : "Free ");
+ found = mm_findinfreelist((struct mm_freenode_s *)node);
+ if (found && (node->preceding & MM_ALLOC_BIT) != 0)
+ {
+ printf(" Should NOT have been in free list\n");
+ }
+ else if (!found && (node->preceding & MM_ALLOC_BIT) == 0)
+ {
+ printf(" SHOULD have been in free listT\n");
+ }
+ else
+ {
+ printf(" OK\n");
+ }
+ }
+}
+
+static void mm_showfreelist(void)
+{
+ struct mm_freenode_s *prev;
+ struct mm_freenode_s *node;
+ int i = 0;
+
+ printf(" FREE NODE LIST:\n");
+ for(prev = NULL, node = &g_nodelist[0];
+ node;
+ prev = node, node = node->flink)
+ {
+ /* Dump "fake" nodes in a different way */
+
+ if (node->size == 0)
+ {
+ printf(" [NODE %2d] %08x %08x %08x\n",
+ i, node->preceding, (int)node->flink, (int)node->blink);
+ i++;
+ }
+ else
+ {
+ printf(" %08x %08x %08x %08x %08x\n",
+ (int)node, node->size, node->preceding, (int)node->flink, (int)node->blink);
+ }
+
+ /* Verify all backward links */
+
+ if (node->blink != prev)
+ {
+ fprintf(stderr, "Backward link is wrong: Is %p, should be %p\n",
+ node->blink, prev);
+ STOP_ON_ERRORS;
+ }
+ }
+}
+
+static void mm_showmallinfo(void)
+{
+ int sval;
+
+ mm_showchunkinfo();
+ mm_showfreelist();
+ alloc_info = mallinfo();
+ printf(" mallinfo:\n");
+ printf(" Total space allocated from system = %ld\n",
+ alloc_info.arena);
+ printf(" Number of non-inuse chunks = %ld\n",
+ alloc_info.ordblks);
+ printf(" Largest non-inuse chunk = %ld\n",
+ alloc_info.mxordblk);
+ printf(" Total allocated space = %ld\n",
+ alloc_info.uordblks);
+ printf(" Total non-inuse space = %ld\n",
+ alloc_info.fordblks);
+
+ sval = mm_getsemaphore();
+ if (sval != 1)
+ {
+ fprintf(stderr, "After mallinfo, semaphore count=%d, should be 1\n", sval);
+ STOP_ON_ERRORS;
+ }
+
+ if (!g_adjheapsize)
+ {
+ g_adjheapsize = alloc_info.uordblks + alloc_info.fordblks;
+ if (g_adjheapsize > TEST_HEAP_SIZE + 16 ||
+ g_adjheapsize < TEST_HEAP_SIZE -16)
+ {
+ fprintf(stderr, "Total memory %d not close to uordlbks=%d + fordblks=%d = %d\n",
+ TEST_HEAP_SIZE, g_adjheapsize, alloc_info.uordblks, alloc_info.fordblks, g_adjheapsize);
+ STOP_ON_ERRORS;
+ }
+ }
+ else if (alloc_info.uordblks + alloc_info.fordblks != g_adjheapsize)
+ {
+ fprintf(stderr, "Total memory %d != uordlbks=%d + fordblks=%d\n",
+ g_adjheapsize, alloc_info.uordblks, alloc_info.fordblks);
+ STOP_ON_ERRORS;
+ }
+}
+
+static void do_mallocs(void **mem, const int *size, const int *rand, int n)
+{
+ int sval;
+ int i;
+ int j;
+
+ for (i = 0; i < n; i++)
+ {
+ j = rand[i];
+ if (!mem[j])
+ {
+ printf("(%d)Allocating %d bytes\n", i, size[j]);
+ mem[j] = mm_malloc(size[j]);
+ printf("(%d)Memory allocated at %p\n", i, mem[j]);
+ if (mem[j] == NULL)
+ {
+ int allocsize = MM_ALIGN_UP(size[j] + SIZEOF_MM_ALLOCNODE);
+ fprintf(stderr, "(%d)malloc failed for allocsize=%d\n", i, allocsize);
+ if (allocsize > alloc_info.mxordblk)
+ {
+ fprintf(stderr, " Normal, largest free block is only %ld\n", alloc_info.mxordblk);
+ }
+ else
+ {
+ fprintf(stderr, " ERROR largest free block is %ld\n", alloc_info.mxordblk);
+ exit(1);
+ }
+ }
+ else
+ {
+ memset(mem[j], 0xAA, size[j]);
+ }
+
+ sval = mm_getsemaphore();
+ if (sval != 1)
+ {
+ fprintf(stderr, " After malloc semaphore count=%d, should be 1\n", sval);
+ STOP_ON_ERRORS;
+ }
+
+ mm_showmallinfo();
+ }
+ }
+}
+
+static void do_reallocs(void **mem, const int *oldsize, const int *newsize, const int *rand, int n)
+{
+ int sval;
+ int i;
+ int j;
+
+ for (i = 0; i < n; i++)
+ {
+ j = rand[i];
+ printf("(%d)Re-allocating at %p from %d to %d bytes\n",
+ i, mem[j], oldsize[j], newsize[j]);
+ mem[j] = mm_realloc(mem[j], newsize[j]);
+ printf("(%d)Memory re-allocated at %p\n", i, mem[j]);
+ if (mem[j] == NULL)
+ {
+ int allocsize = MM_ALIGN_UP(newsize[j] + SIZEOF_MM_ALLOCNODE);
+ fprintf(stderr, "(%d)realloc failed for allocsize=%d\n", i, allocsize);
+ if (allocsize > alloc_info.mxordblk)
+ {
+ fprintf(stderr, " Normal, largest free block is only %ld\n", alloc_info.mxordblk);
+ }
+ else
+ {
+ fprintf(stderr, " ERROR largest free block is %ld\n", alloc_info.mxordblk);
+ exit(1);
+ }
+ }
+ else
+ {
+ memset(mem[j], 0x55, newsize[j]);
+ }
+
+ sval = mm_getsemaphore();
+ if (sval != 1)
+ {
+ fprintf(stderr, " After realloc semaphore count=%d, should be 1\n", sval);
+ STOP_ON_ERRORS;
+ }
+
+ mm_showmallinfo();
+ }
+}
+
+static void do_memaligns(void **mem, const int *size, const int *align, const int *rand, int n)
+{
+ int sval;
+ int i;
+ int j;
+
+ for (i = 0; i < n; i++)
+ {
+ j = rand[i];
+ printf("(%d)Allocating %d bytes aligned to 0x%08x\n",
+ i, size[j], align[i]);
+ mem[j] = mm_memalign(align[i], size[j]);
+ printf("(%d)Memory allocated at %p\n", i, mem[j]);
+ if (mem[j] == NULL)
+ {
+ int allocsize = MM_ALIGN_UP(size[j] + SIZEOF_MM_ALLOCNODE) + 2*align[i];
+ fprintf(stderr, "(%d)memalign failed for allocsize=%d\n", i, allocsize);
+ if (allocsize > alloc_info.mxordblk)
+ {
+ fprintf(stderr, " Normal, largest free block is only %ld\n", alloc_info.mxordblk);
+ }
+ else
+ {
+ fprintf(stderr, " ERROR largest free block is %ld\n", alloc_info.mxordblk);
+ exit(1);
+ }
+ }
+ else
+ {
+ memset(mem[j], 0x33, size[j]);
+ }
+
+ sval = mm_getsemaphore();
+ if (sval != 1)
+ {
+ fprintf(stderr, " After memalign semaphore count=%d, should be 1\n", sval);
+ STOP_ON_ERRORS;
+ }
+
+ mm_showmallinfo();
+ }
+}
+
+static do_frees(void **mem, const int *size, const int *rand, int n)
+{
+ int sval;
+ int i;
+ int j;
+
+ for (i = 0; i < n; i++)
+ {
+ j = random2[i];
+ printf("(%d)Releasing memory at %p (size=%d bytes)\n",
+ i, mem[j], size[j]);
+ mm_free(mem[j]);
+ mem[j] = NULL;
+
+ sval = mm_getsemaphore();
+ if (sval != 1)
+ {
+ fprintf(stderr, " After free semaphore count=%d, should be 1\n", sval);
+ STOP_ON_ERRORS;
+ }
+
+ mm_showmallinfo();
+ }
+}
+
+int main(int argc, char **argv, char **envp)
+{
+ void *heapbase;
+ int i, j;
+
+ /* Allocate a heap */
+
+ printf("Allocating test heap of %ldKb\n", TEST_HEAP_SIZE/1024);
+ heapbase = malloc(TEST_HEAP_SIZE);
+ printf("Allocated heap_base=%p\n", heap_base);
+ if (heapbase == 0)
+ {
+ fprintf(stderr, "Failed to allocate test heap\n");
+ exit(1);
+ }
+
+ /* Initialize the memory manager */
+
+ mm_initialize(heapbase, TEST_HEAP_SIZE);
+ mm_showmallinfo();
+
+ /* Allocate some memory */
+
+ do_mallocs(allocs, alloc_sizes, random1, NTEST_ALLOCS);
+
+ /* Re-allocate the memory */
+
+ do_reallocs(allocs, alloc_sizes, realloc_sizes, random2, NTEST_ALLOCS);
+
+ /* Release the memory */
+
+ do_frees(allocs, realloc_sizes, random3, NTEST_ALLOCS);
+
+ /* Allocate aligned memory */
+
+ do_memaligns(allocs, alloc_sizes, alignment, random2, NTEST_ALLOCS/2);
+ do_memaligns(allocs, alloc_sizes, alignment, &random2[NTEST_ALLOCS/2], NTEST_ALLOCS/2);
+
+ /* Release aligned memory */
+
+ do_frees(allocs, alloc_sizes, random1, NTEST_ALLOCS);
+
+ /* Clean up and exit */
+
+ free(heapbase);
+
+ printf("TEST COMPLETE\n");
+ return 0;
+}
diff --git a/nuttx/mm/mm_zalloc.c b/nuttx/mm/mm_zalloc.c
new file mode 100644
index 0000000000..a26992b001
--- /dev/null
+++ b/nuttx/mm/mm_zalloc.c
@@ -0,0 +1,68 @@
+/************************************************************
+ * mm_zalloc.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include "mm_environment.h"
+#include "mm_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public: Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: zalloc
+ *
+ * Description:
+ * zalloc calls malloc, then zeroes out the allocated chunk.
+ *
+ ************************************************************/
+
+void *zalloc(size_t size)
+{
+ void *alloc = malloc(size);
+ if (alloc)
+ {
+ memset(alloc, 0, size);
+ }
+
+ return alloc;
+}
diff --git a/nuttx/sched/Makefile b/nuttx/sched/Makefile
new file mode 100644
index 0000000000..ebc27785d5
--- /dev/null
+++ b/nuttx/sched/Makefile
@@ -0,0 +1,130 @@
+############################################################
+# Makefile
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+-include $(TOPDIR)/Make.defs
+
+MKDEP = $(TOPDIR)/tools/mkdeps.sh
+
+ASRCS =
+AOBJS = $(ASRCS:.S=.o)
+
+MISC_SRCS = os_start.c get_errno_ptr.c \
+ sched_setupstreams.c sched_getfiles.c sched_getstreams.c \
+ sched_setupidlefiles.c sched_setuptaskfiles.c sched_setuppthreadfiles.c \
+ sched_releasefiles.c
+TSK_SRCS = task_create.c task_delete.c task_restart.c \
+ exit.c abort.c atexit.c getpid.c \
+ sched_addreadytorun.c sched_removereadytorun.c sched_addprioritized.c \
+ sched_mergepending.c sched_addblocked.c sched_removeblocked.c \
+ sched_free.c sched_gettcb.c sched_releasetcb.c
+SCHED_SRCS = sched_setparam.c sched_getparam.c \
+ sched_setscheduler.c sched_getscheduler.c \
+ sched_yield.c sched_rrgetinterval.c \
+ sched_getprioritymax.c sched_getprioritymin.c \
+ sched_lock.c sched_unlock.c sched_lockcount.c
+WDOG_SRCS = wd_initialize.c wd_create.c wd_start.c wd_cancel.c wd_delete.c
+TIME_SRCS = sched_processtimer.c clock_initialize.c mktime.c gmtime_r.c \
+ clock_settime.c clock_gettime.c clock_getres.c sleep.c usleep.c
+SIGNAL_SRCS = sig_initialize.c \
+ sig_action.c sig_procmask.c sig_pending.c sig_suspend.c \
+ sig_queue.c sig_waitinfo.c sig_timedwait.c \
+ sig_emptyset.c sig_fillset.c sig_addset.c sig_delset.c \
+ sig_ismember.c sig_findaction.c \
+ sig_allocatependingsigaction.c sig_releasependingsigaction.c \
+ sig_unmaskpendingsignal.c sig_removependingsignal.c \
+ sig_releasependingsignal.c sig_lowest.c sig_mqnotempty.c \
+ sig_cleanup.c sig_received.c sig_deliver.c
+MQUEUE_SRCS = mq_open.c mq_close.c mq_unlink.c mq_send.c mq_receive.c \
+ mq_notify.c mq_setattr.c mq_getattr.c \
+ mq_initialize.c mq_descreate.c mq_findnamed.c \
+ mq_msgfree.c mq_msgqfree.c
+PTHREAD_SRCS = pthread_attrinit.c pthread_attrdestroy.c \
+ pthread_attrsetschedpolicy.c pthread_attrgetschedpolicy.c \
+ pthread_attrsetinheritsched.c pthread_attrgetinheritsched.c \
+ pthread_attrsetstacksize.c pthread_attrgetstacksize.c \
+ pthread_attrsetschedparam.c pthread_attrgetschedparam.c \
+ pthread_create.c pthread_exit.c pthread_join.c pthread_detach.c \
+ pthread_yield.c pthread_self.c \
+ pthread_getschedparam.c pthread_setschedparam.c \
+ pthread_mutexattrinit.c pthread_mutexattrdestroy.c \
+ pthread_mutexattrgetpshared.c pthread_mutexattrsetpshared.c \
+ pthread_mutexinit.c pthread_mutexdestroy.c \
+ pthread_mutexlock.c pthread_mutextrylock.c pthread_mutexunlock.c \
+ pthread_condinit.c pthread_conddestroy.c \
+ pthread_condattrinit.c pthread_condattrdestroy.c \
+ pthread_condwait.c pthread_condtimedwait.c \
+ pthread_condsignal.c pthread_condbroadcast.c \
+ pthread_cancel.c pthread_setcancelstate.c \
+ pthread_keycreate.c pthread_setspecific.c pthread_getspecific.c pthread_keydelete.c \
+ pthread_initialize.c pthread_completejoin.c pthread_findjoininfo.c \
+ pthread_removejoininfo.c
+SEM_SRCS = sem_initialize.c sem_init.c sem_destroy.c\
+ sem_open.c sem_close.c sem_unlink.c \
+ sem_wait.c sem_trywait.c sem_post.c sem_getvalue.c \
+ sem_waitirq.c sem_findnamed.c
+IRQ_SRCS = irq_initialize.c irq_attach.c irq_dispatch.c irq_unexpectedisr.c
+CSRCS = $(MISC_SRCS) $(TSK_SRCS) $(SCHED_SRCS) $(WDOG_SRCS) $(TIME_SRCS) \
+ $(SIGNAL_SRCS) $(MQUEUE_SRCS) $(PTHREAD_SRCS) $(SEM_SRCS) $(IRQ_SRCS)
+COBJS = $(CSRCS:.c=.o)
+
+SRCS = $(ASRCS) $(CSRCS)
+OBJS = $(AOBJS) $(COBJS)
+
+BIN = libsched.a
+
+all: $(BIN)
+
+$(AOBJS): %.o: %.S
+ $(CC) -c $(CFLAGS) -D__ASSEMBLY__ $< -o $@
+
+$(COBJS): %.o: %.c
+ $(CC) -c $(CFLAGS) $< -o $@
+
+$(BIN): $(OBJS)
+ $(AR) rcs $@ $(OBJS)
+
+.depend: Makefile $(SRCS)
+ $(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
+ touch $@
+
+depend: .depend
+
+clean:
+ rm -f $(BIN) *.o *~
+
+distclean: clean
+ rm -f Make.dep .depend
+
+-include Make.dep
diff --git a/nuttx/sched/abort.c b/nuttx/sched/abort.c
new file mode 100644
index 0000000000..7c077a9670
--- /dev/null
+++ b/nuttx/sched/abort.c
@@ -0,0 +1,81 @@
+/************************************************************
+ * abort.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdlib.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functionss
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: abort
+ *
+ * Description:
+ * The abort() function causes abnormal program termination.
+ * All open streams are closed and flushed.
+ ************************************************************/
+
+void abort(void)
+{
+ exit(EXIT_FAILURE);
+}
diff --git a/nuttx/sched/atexit.c b/nuttx/sched/atexit.c
new file mode 100644
index 0000000000..9feb4ce9a7
--- /dev/null
+++ b/nuttx/sched/atexit.c
@@ -0,0 +1,103 @@
+/************************************************************
+ * atexit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <unistd.h>
+#include <debug.h>
+#include <errno.h>
+#include <nuttx/fs.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functionss
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: atexit
+ *
+ * Description:
+ * Registers a function to be called at program exit.
+ *
+ * Parameters:
+ * func
+ *
+ * Return Value:
+ * zero on success.
+ *
+ ************************************************************/
+
+int atexit(void (*func)(void))
+{
+ _TCB *tcb = (_TCB*)g_readytorun.head;
+ int ret = ERROR;
+
+ if ((func) && (!tcb->exitfunc))
+ {
+ tcb->exitfunc = func;
+ ret = OK;
+ }
+ return ret;
+}
+
diff --git a/nuttx/sched/clock_getres.c b/nuttx/sched/clock_getres.c
new file mode 100644
index 0000000000..5cf07f2499
--- /dev/null
+++ b/nuttx/sched/clock_getres.c
@@ -0,0 +1,119 @@
+/************************************************************
+ * clock_getres.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <time.h>
+#include <errno.h>
+#include <debug.h>
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Public Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: clock_getres
+ *
+ * Description:
+ * Clock Functions based on POSIX APIs
+ *
+ ************************************************************/
+
+int clock_getres(clockid_t clock_id, struct timespec *res)
+{
+ uint32 time_res;
+ int ret = OK;
+
+ dbg("%s: clock_id=%d\n", __FUNCTION__, clock_id);
+
+ /* Only CLOCK_REALTIME is supported */
+
+ if (clock_id != CLOCK_REALTIME)
+ {
+ dbg("%s: Returning ERROR\n", __FUNCTION__);
+ *get_errno_ptr() = EINVAL;
+ ret = ERROR;
+ }
+ else
+ {
+ /* Get the clock resolution in nanoseconds */
+
+ time_res = MSEC_PER_TICK * NSEC_PER_MSEC;
+
+ /* And return this as a timespec. */
+
+ res->tv_sec = 0;
+ res->tv_nsec = time_res;
+
+ dbg("%s: Returning res=(%d,%d) time_res=%d\n",
+ __FUNCTION__,
+ (int)res->tv_sec, (int)res->tv_nsec,
+ (int)time_res);
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/clock_gettime.c b/nuttx/sched/clock_gettime.c
new file mode 100644
index 0000000000..ac9270975c
--- /dev/null
+++ b/nuttx/sched/clock_gettime.c
@@ -0,0 +1,148 @@
+/************************************************************
+ * clock_gettime.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <time.h>
+#include <errno.h>
+#include <debug.h>
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Public Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: clock_gettime
+ *
+ * Description:
+ * Clock Functions based on POSIX APIs
+ *
+ ************************************************************/
+
+int clock_gettime(clockid_t clock_id, struct timespec *tp)
+{
+ uint32 msecs;
+ uint32 secs;
+ uint32 nsecs;
+ int ret = OK;
+
+ dbg("%s: clock_id=%d\n", __FUNCTION__, clock_id);
+
+ /* Only CLOCK_REALTIME is supported */
+
+ if (clock_id != CLOCK_REALTIME)
+ {
+ dbg("%s: Returning ERROR\n", __FUNCTION__);
+
+ *get_errno_ptr() = EINVAL;
+ ret = ERROR;
+ }
+ else
+ {
+ /* Get the elapsed time since power up (in milliseconds) biased
+ * as appropriate.
+ */
+
+ msecs = MSEC_PER_TICK * (g_system_timer - g_tickbias);
+
+ dbg("%s: msecs = %d g_tickbias=%d\n", __FUNCTION__,
+ (int)msecs, (int)g_tickbias);
+
+ /* Get the elapsed time in seconds and nanoseconds. */
+
+ secs = msecs / MSEC_PER_SEC;
+ nsecs = (msecs - (secs * MSEC_PER_SEC)) * NSEC_PER_MSEC;
+
+ dbg("%s: secs = %d + %d nsecs = %d + %d\n", __FUNCTION__,
+ (int)msecs, (int)g_basetime.tv_sec,
+ (int)nsecs, (int)g_basetime.tv_nsec);
+
+ /* Add the base time to this. */
+
+ secs += (uint32)g_basetime.tv_sec;
+ nsecs += (uint32)g_basetime.tv_nsec;
+
+ /* Handle carry to seconds. */
+
+ if (nsecs > NSEC_PER_SEC)
+ {
+ uint32 dwCarrySecs = nsecs / NSEC_PER_SEC;
+ secs += dwCarrySecs;
+ nsecs -= (dwCarrySecs * NSEC_PER_SEC);
+ }
+
+ /* And return the result to the caller. */
+
+ tp->tv_sec = (time_t)secs;
+ tp->tv_nsec = (long)nsecs;
+
+ dbg("%s: Returning tp=(%d,%d)\n", __FUNCTION__,
+ (int)tp->tv_sec, (int)tp->tv_nsec);
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/clock_initialize.c b/nuttx/sched/clock_initialize.c
new file mode 100644
index 0000000000..7b63a65215
--- /dev/null
+++ b/nuttx/sched/clock_initialize.c
@@ -0,0 +1,128 @@
+/************************************************************
+ * clock_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <time.h>
+#include <errno.h>
+#include <debug.h>
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Public Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+volatile uint32 g_system_timer = 0;
+struct timespec g_basetime = {0,0};
+uint32 g_tickbias = 0;
+
+/**********************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: clock_initialize
+ *
+ * Description:
+ * Perform one-time initialization of the timing facilities.
+ *
+ ************************************************************/
+
+void clock_initialize(void)
+{
+ time_t jdn;
+
+ /* Initialize the real time close */
+
+ g_system_timer = 0;
+
+ /* Get the EPOCH-relative julian date from the calendar year,
+ * month, and date
+ */
+
+ jdn = clock_calendar2utc(CONFIG_START_YEAR, CONFIG_START_MONTH,
+ CONFIG_START_DAY);
+
+ /* Set the base time as seconds into this julian day. */
+
+ g_basetime.tv_sec = jdn * (24*60*60);
+ g_basetime.tv_nsec = 0;
+
+ /* These is no time bias from this time. */
+
+ g_tickbias = 0;
+}
+
+/************************************************************
+ * Function: clock_timer
+ *
+ * Description:
+ * This function must be called once every time the real
+ * time clock interrupt occurs. The interval of this
+ * clock interrupt must be MSEC_PER_TICK
+ *
+ ************************************************************/
+
+void clock_timer(void)
+{
+ g_system_timer++;
+}
diff --git a/nuttx/sched/clock_internal.h b/nuttx/sched/clock_internal.h
new file mode 100644
index 0000000000..18806d9521
--- /dev/null
+++ b/nuttx/sched/clock_internal.h
@@ -0,0 +1,101 @@
+/************************************************************
+ * clock_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __CLOCK_INTERNAL_H
+#define __CLOCK_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <nuttx/compiler.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* Timing constants */
+
+#define NSEC_PER_SEC 1000000000
+#define USEC_PER_SEC 1000000
+#define MSEC_PER_SEC 1000
+#define NSEC_PER_MSEC 1000000
+#define USEC_PER_MSEC 1000
+#define NSEC_PER_USEC 1000
+
+#define MSEC_PER_TICK 10
+#define USEC_PER_TICK (MSEC_PER_TICK * USEC_PER_MSEC)
+#define NSEC_PER_TICK (MSEC_PER_TICK * NSEC_PER_MSEC)
+#define TICK_PER_SEC (MSEC_PER_SEC / MSEC_PER_TICK)
+
+#define MSEC2TICK(msec) (((msec)+(MSEC_PER_TICK/2))/MSEC_PER_TICK)
+#define USEC2TICK(usec) (((usec)+(USEC_PER_TICK/2))/USEC_PER_TICK)
+
+#define JD_OF_EPOCH 2440588 /* Julian Date of noon, J1970 */
+
+#ifdef CONFIG_JULIAN_TIME
+# define GREG_DUTC -141427 /* Default is October 15, 1582 */
+# define GREG_YEAR 1582
+# define GREG_MONTH 10
+# define GREG_DAY 15
+#endif /* CONFIG_JULIAN_TIME */
+
+/************************************************************
+ * Public Type Definitions
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+extern volatile uint32 g_system_timer;
+extern struct timespec g_basetime;
+extern uint32 g_tickbias;
+
+/************************************************************
+ * Public Inline Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+extern void weak_function clock_initialize(void);
+extern void weak_function clock_timer(void);
+
+extern time_t clock_calendar2utc(int year, int month, int day);
+
+#endif /* __CLOCK_INTERNAL_H */
diff --git a/nuttx/sched/clock_settime.c b/nuttx/sched/clock_settime.c
new file mode 100644
index 0000000000..c57fb28088
--- /dev/null
+++ b/nuttx/sched/clock_settime.c
@@ -0,0 +1,119 @@
+/************************************************************
+ * clock_settime.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <time.h>
+#include <errno.h>
+#include <debug.h>
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Public Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: clock_settime
+ *
+ * Description:
+ * Clock Functions based on POSIX APIs
+ *
+ ************************************************************/
+
+int clock_settime(clockid_t clock_id, const struct timespec *tp)
+{
+ int ret = OK;
+
+ dbg("%s: clock_id=%d\n", __FUNCTION__, clock_id);
+
+ /* Only CLOCK_REALTIME is supported */
+
+ if (clock_id != CLOCK_REALTIME || !tp)
+ {
+ dbg("%s: Returning ERROR\n", __FUNCTION__);
+ *get_errno_ptr() = EINVAL;
+ ret = ERROR;
+ }
+ else
+ {
+ /* Save the new base time. */
+
+ g_basetime = *tp;
+
+ /* Get the elapsed time since power up (in milliseconds) biased
+ * as appropriate.
+ */
+
+ g_tickbias = g_system_timer;
+
+ dbg("%s: basetime=(%d,%d) tickbias=%d\n",
+ __FUNCTION__,
+ (int)g_basetime.tv_sec, (int)g_basetime.tv_nsec,
+ (int)g_tickbias);
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/exit.c b/nuttx/sched/exit.c
new file mode 100644
index 0000000000..e71ed68e89
--- /dev/null
+++ b/nuttx/sched/exit.c
@@ -0,0 +1,112 @@
+/************************************************************
+ * exit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <debug.h>
+#include <errno.h>
+#include <nuttx/fs.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functionss
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: exit
+ *
+ * Description:
+ * The exit() function causes normal process termination
+ * and the value of status & 0377 is returned to the parent.
+ *
+ * All functions registered with atexit() and on_exit() are
+ * called, in the reverse order of their registration.
+ *
+ * All open streams are flushed and closed. Files created
+ * by tmpfile() are removed.
+ *
+ ************************************************************/
+
+void exit(int status)
+{
+ _TCB *tcb = (_TCB*)g_readytorun.head;
+
+ /* Flush all streams (File descriptors will be closed when
+ * the TCB is deallocated.
+ */
+
+#if CONFIG_NFILE_STREAMS > 0
+ lib_flushall(tcb->streams);
+#endif
+
+ /* If an exit function was registered, call it now. */
+
+ if (tcb->exitfunc) {
+
+ (*tcb->exitfunc)();
+
+ } /* end if */
+
+ _exit(status & 0377);
+}
diff --git a/nuttx/sched/get_errno_ptr.c b/nuttx/sched/get_errno_ptr.c
new file mode 100644
index 0000000000..c9f0d52c84
--- /dev/null
+++ b/nuttx/sched/get_errno_ptr.c
@@ -0,0 +1,70 @@
+/************************************************************
+ * get_errno_ptr.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sched.h>
+#include <errno.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: get_errno_ptr
+ *
+ * Description:
+ * Return a pointer to the thread specific errno.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * A pointer to the per-thread errno variable.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int *get_errno_ptr(void)
+{
+ _TCB *ptcb = (_TCB*)g_readytorun.head;
+ return &ptcb->errno;
+}
+
+
diff --git a/nuttx/sched/getpid.c b/nuttx/sched/getpid.c
new file mode 100644
index 0000000000..a68ce896a8
--- /dev/null
+++ b/nuttx/sched/getpid.c
@@ -0,0 +1,84 @@
+/************************************************************
+ * getpid.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <sched.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: getpid
+ *
+ * Description:
+ * Get the task ID of the currently executing task.
+ *
+ ************************************************************/
+
+pid_t getpid(void)
+{
+ /* Return the task ID from the TCB at the head of the
+ * ready-to-run task list
+ */
+
+ return ((_TCB*)g_readytorun.head)->pid;
+}
diff --git a/nuttx/sched/gmtime_r.c b/nuttx/sched/gmtime_r.c
new file mode 100644
index 0000000000..8661d73b7b
--- /dev/null
+++ b/nuttx/sched/gmtime_r.c
@@ -0,0 +1,215 @@
+/************************************************************
+ * gmtime_r.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <time.h>
+#include <errno.h>
+#include <debug.h>
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/* Calendar/UTC conversion routines */
+
+static void clock_utc2calendar(time_t utc, int *year, int *month, int *day);
+static void clock_utc2gregorian (time_t jdn, int *year, int *month, int *day);
+
+#ifdef CONFIG_JULIAN_TIME
+static void clock_utc2julian(time_t jdn, int *year, int *month, int *day);
+#endif /* CONFIG_JULIAN_TIME */
+
+/**********************************************************
+ * Public Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: clock_calendar2utc, clock_gregorian2utc,
+ * and clock_julian2utc
+ *
+ * Description:
+ * Calendar to UTC conversion routines. These conversions
+ * are based on algorithms from p. 604 of Seidelman, P. K.
+ * 1992. Explanatory Supplement to the Astronomical
+ * Almanac. University Science Books, Mill Valley.
+ *
+ ************************************************************/
+
+static void clock_utc2calendar(time_t utc, int *year, int *month, int *day)
+{
+#ifdef CONFIG_JULIAN_TIME
+
+ if (utc >= GREG_DUTC)
+ {
+ clock_utc2gregorian(utc + JD_OF_EPOCH, year, month, day);
+ }
+ else
+ {
+ clock_utc2julian (utc + JD_OF_EPOCH, year, month, day);
+ }
+
+#else /* CONFIG_JULIAN_TIME */
+
+ clock_utc2gregorian(utc + JD_OF_EPOCH, year, month, day);
+
+#endif /* CONFIG_JULIAN_TIME */
+}
+
+static void clock_utc2gregorian(time_t jd, int *year, int *month, int *day)
+{
+ long l, n, i, j, d, m, y;
+
+ l = jd + 68569;
+ n = (4*l) / 146097;
+ l = l - (146097*n + 3)/4;
+ i = (4000*(l+1))/1461001;
+ l = l - (1461*i)/4 + 31;
+ j = (80*l)/2447;
+ d = l - (2447*j)/80;
+ l = j/11;
+ m = j + 2 - 12*l;
+ y = 100*(n-49) + i + l;
+
+ *year = y;
+ *month = m;
+ *day = d;
+}
+
+
+#ifdef CONFIG_JULIAN_TIME
+static void clock_utc2julian(time_t jd, int *year, int *month, int *day)
+{
+ long j, k, l, n, d, i, m, y;
+
+ j = jd + 1402;
+ k = (j-1)/1461;
+ l = j - 1461*k;
+ n = (l-1)/365 - l/1461;
+ i = l - 365*n + 30;
+ j = (80*i)/2447;
+ d = i - (2447*j)/80;
+ i = j/11;
+ m = j + 2 - 12*i;
+ y = 4*k + n + i - 4716;
+
+ *year = y;
+ *month = m;
+ *day = d;
+}
+#endif /* CONFIG_JULIAN_TIME */
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: gmtime_r
+ *
+ * Description:
+ * Time conversion (based on the POSIX API)
+ *
+ ************************************************************/
+
+struct tm *gmtime_r(const time_t *clock, struct tm *result)
+{
+ time_t time;
+ time_t jdn;
+ int year, month, day;
+ int hour, min, sec;
+
+ /* Get the seconds since the EPOCH */
+
+ time = *clock;
+ dbg("%s: clock=%d\n", __FUNCTION__, (int)time);
+
+ /* Convert to days, hours, minutes, and seconds since the EPOCH */
+
+ jdn = time / (24*60*60);
+ time -= (24*60*60) * jdn;
+
+ hour = time / (60*60);
+ time -= (60*60) * hour;
+
+ min = time / 60;
+ time -= 60 * min;
+
+ sec = time;
+
+ dbg("%s: hour=%d min=%d sec=%d\n", __FUNCTION__,
+ (int)hour, (int)min, (int)sec);
+
+ /* Convert the days since the EPOCH to calendar day */
+
+ clock_utc2calendar(jdn, &year, &month, &day);
+
+ dbg("%s: jdn=%d year=%d month=%d day=%d\n", __FUNCTION__,
+ (int)jdn, (int)year, (int)month, (int)day);
+
+ /* Then return the struct tm contents */
+
+ result->tm_year = (int)year - 1900;
+ result->tm_mon = (int)month - 1;
+ result->tm_mday = (int)day;
+ result->tm_hour = (int)hour;
+ result->tm_min = (int)min;
+ result->tm_sec = (int)sec;
+
+ return result;
+}
diff --git a/nuttx/sched/irq_attach.c b/nuttx/sched/irq_attach.c
new file mode 100644
index 0000000000..2c669071d8
--- /dev/null
+++ b/nuttx/sched/irq_attach.c
@@ -0,0 +1,107 @@
+/************************************************************
+ * irq_attach.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <nuttx/irq.h>
+#include "irq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: irq_attach
+ *
+ * Description:
+ * Configure the IRQ subsystem so that IRQ number 'irq'
+ * is dispatched to 'isr'
+ *
+ ************************************************************/
+
+int irq_attach(int irq, xcpt_t isr)
+{
+ int ret = ERROR;
+
+ if ((unsigned)irq < NR_IRQS)
+ {
+ int state;
+
+ /* If the new ISR is NULL, then the ISR is being detached.
+ * In this case, disable the ISR and direct any interrupts
+ * to the unexpected interrupt handler.
+ */
+
+ state = irqsave();
+ if (isr == NULL)
+ {
+ up_disable_irq(irq);
+ isr = irq_unexpected_isr;
+ }
+
+ /* Save the new ISR in the table. */
+
+ g_irqvector[irq] = isr;
+ irqrestore(state);
+ ret = OK;
+ }
+
+ return ret;
+}
+
+
diff --git a/nuttx/sched/irq_dispatch.c b/nuttx/sched/irq_dispatch.c
new file mode 100644
index 0000000000..f783b2f54d
--- /dev/null
+++ b/nuttx/sched/irq_dispatch.c
@@ -0,0 +1,98 @@
+/************************************************************
+ * irq_dispatch.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+#include "irq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: irq_dispatch
+ *
+ * Description:
+ * This function must be called from the achitecture-
+ * specific logic in order to dispaly an interrupt to
+ * the appropriate, registered handling logic.
+ *
+ ***********************************************************/
+
+void irq_dispatch(int irq, struct xcptcontext *xcp)
+{
+ xcpt_t vector;
+
+ /* Perform some sanity checks */
+
+ if ((unsigned)irq >= NR_IRQS || g_irqvector[irq] == NULL)
+ {
+ vector = irq_unexpected_isr;
+ }
+ else
+ {
+ vector = g_irqvector[irq];
+ }
+
+ /* Then dispatch to the interrupt handler */
+
+ vector(irq, xcp);
+}
+
diff --git a/nuttx/sched/irq_initialize.c b/nuttx/sched/irq_initialize.c
new file mode 100644
index 0000000000..b213a5b09f
--- /dev/null
+++ b/nuttx/sched/irq_initialize.c
@@ -0,0 +1,90 @@
+/************************************************************
+ * irq_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+#include "irq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+xcpt_t g_irqvector[NR_IRQS+1];
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: irq_initialize
+ *
+ * Description:
+ * Configure the IRQ subsystem
+ *
+ ************************************************************/
+
+void irq_initialize(void)
+{
+ int i;
+
+ /* Point all interrupt vectors to the unexpected interrupt */
+
+ for (i = 0; i < NR_IRQS; i++)
+ {
+ g_irqvector[i] = irq_unexpected_isr;
+ }
+}
+
diff --git a/nuttx/sched/irq_internal.h b/nuttx/sched/irq_internal.h
new file mode 100644
index 0000000000..a11120f55c
--- /dev/null
+++ b/nuttx/sched/irq_internal.h
@@ -0,0 +1,82 @@
+/************************************************************
+ * irq_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __IRQ_INTERNAL_H
+#define __IRQ_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+#include <nuttx/compiler.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public Type Declarations
+ ************************************************************/
+
+extern xcpt_t g_irqvector[NR_IRQS+1];
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN void weak_function irq_initialize(void);
+EXTERN int irq_unexpected_isr(int irq, struct xcptcontext *xcp);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __IRQ_INTERNAL_H */
+
diff --git a/nuttx/sched/irq_unexpectedisr.c b/nuttx/sched/irq_unexpectedisr.c
new file mode 100644
index 0000000000..c235c53a88
--- /dev/null
+++ b/nuttx/sched/irq_unexpectedisr.c
@@ -0,0 +1,83 @@
+/************************************************************
+ * irq_unexpectedisr.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <nuttx/irq.h>
+#include "os_internal.h"
+#include "irq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: irq_unexpected_isr
+ *
+ * Description:
+ * An interrupt has been received for an IRQ that was
+ * never registered with the system.
+ *
+ ************************************************************/
+
+int irq_unexpected_isr(int irq, struct xcptcontext *xcp)
+{
+ (void)irqsave();
+ PANIC(OSERR_UNEXPECTEDISR);
+ return 0;
+}
diff --git a/nuttx/sched/mktime.c b/nuttx/sched/mktime.c
new file mode 100644
index 0000000000..9c84964633
--- /dev/null
+++ b/nuttx/sched/mktime.c
@@ -0,0 +1,211 @@
+/************************************************************
+ * mktime.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <time.h>
+#include <debug.h>
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/**********************************************************
+ * Public Constant Data
+ **********************************************************/
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/**********************************************************
+ * Private Variables
+ **********************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: clock_gregorian2utc, clock_julian2utc
+ *
+ * Description:
+ * UTC conversion routines. These conversions are based
+ * on algorithms from p. 604 of Seidelman, P. K. 1992.
+ * Explanatory Supplement to the Astronomical Almanac.
+ * University Science Books, Mill Valley.
+ *
+ ************************************************************/
+
+static inline time_t clock_gregorian2utc(int year, int month, int day)
+{
+ int temp;
+
+ /* temp = (month - 14)/12; */
+
+ temp = (month <= 2 ? -1:0);
+
+ return (1461*(year + 4800 + temp))/4
+ + (367*(month - 2 - 12*temp))/12
+ - (3*((year + 4900 + temp)/100))/4 + day - 32075;
+}
+
+#ifdef CONFIG_JULIAN_TIME
+static inline time_t clock_julian2utc(int year, int month, int day)
+{
+ return 367*year
+ - (7*(year + 5001 + (month-9)/7))/4
+ + (275*month)/9
+ + day + 1729777;
+}
+#endif
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mktime
+ *
+ * Description:
+ * Time conversion (based on the POSIX API)
+ *
+ ************************************************************/
+
+time_t mktime(struct tm *tp)
+{
+ time_t ret;
+ time_t jdn;
+
+ /* Get the EPOCH-relative julian date from the calendar year,
+ * month, and date
+ */
+
+ jdn = clock_calendar2utc(tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday);
+ dbg("%s: jdn=%d tm_year=%d tm_mon=%d tm_mday=%d\n",
+ __FUNCTION__, (int)jdn, tp->tm_year, tp->tm_mon, tp->tm_mday);
+
+ /* Return the seconds into the julian day. */
+
+ ret = ((jdn*24 + tp->tm_hour)*60 + tp->tm_min)*60 + tp->tm_sec;
+ dbg("%s:\tret=%d tm_hour=%d tm_min=%d tm_sec=%d\n",
+ __FUNCTION__, (int)ret, tp->tm_hour, tp->tm_min, tp->tm_sec);
+
+ return ret;
+}
+
+/************************************************************
+ * Function: clock_calendar2utc
+ *
+ * Description:
+ * Calendar/UTC conversion based on algorithms from p. 604
+ * of Seidelman, P. K. 1992. Explanatory Supplement to
+ * the Astronomical Almanac. University Science Books,
+ * Mill Valley.
+ *
+ ************************************************************/
+
+time_t clock_calendar2utc(int year, int month, int day)
+{
+ int dyear;
+#ifdef CONFIG_JULIAN_TIME
+ int isgreg;
+#endif /* CONFIG_JULIAN_TIME */
+
+ /* Correct year & month ranges. Shift month into range 1-12 */
+
+ dyear = (month-1) / 12;
+ month -= 12 * dyear;
+ year += dyear;
+
+ if (month < 1)
+ {
+ month += 12;
+ year -= 1;
+ }
+
+#ifdef CONFIG_JULIAN_TIME
+ /* Determine which calendar to use */
+
+ if (year > GREG_YEAR)
+ {
+ isgreg = TRUE;
+ }
+ else if (year < GREG_YEAR)
+ {
+ isgreg = FALSE;
+ }
+ else if (month > GREG_MONTH)
+ {
+ isgreg = TRUE;
+ }
+ else if (month < GREG_MONTH)
+ {
+ isgreg = FALSE;
+ }
+ else
+ {
+ isgreg = (day >= GREG_DAY);
+ }
+
+ /* Calculate and return date */
+
+ if (isgreg)
+ {
+ return clock_gregorian2utc(year, month, day) - JD_OF_EPOCH;
+ }
+ else
+ {
+ return clock_julian2utc (year, month, day) - JD_OF_EPOCH;
+ }
+
+#else /* CONFIG_JULIAN_TIME */
+
+ return clock_gregorian2utc(year, month, day) - JD_OF_EPOCH;
+
+#endif /* CONFIG_JULIAN_TIME */
+}
diff --git a/nuttx/sched/mq_close.c b/nuttx/sched/mq_close.c
new file mode 100644
index 0000000000..b1625de736
--- /dev/null
+++ b/nuttx/sched/mq_close.c
@@ -0,0 +1,189 @@
+/************************************************************
+ * mq_close.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <mqueue.h>
+#include <sched.h>
+#include "os_internal.h"
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_desfree
+ *
+ * Description:
+ * Deallocate a message queue descriptor
+ *
+ * Inputs:
+ * mqdes - message queue descriptor to free
+ *
+ ************************************************************/
+
+static inline void mq_desfree(mqd_t mqdes)
+{
+ /* Just put it back on the free list */
+
+ sq_addlast((sq_entry_t*)mqdes, &g_desfree);
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_close
+ *
+ * Description:
+ * This function is used to indicate that the calling task
+ * is finished with the specified message queued mqdes.
+ * The mq_close() deallocates any system resources
+ * allocated by the system for use by this task for its
+ * message queue.
+ *
+ * If the calling task has attached a notification to the
+ * message queue via this mqdes, this attachment will be
+ * removed and the message queue is available for another
+ * process to attach a notification.
+ *
+ * Parameters:
+ * mqdes - Message queue descriptor.
+ *
+ * Return Value:
+ * 0 (OK) if the message queue is closed successfully,
+ * otherwise, -1 (ERROR).
+ *
+ * Assumptions:
+ * - The behavior of a task that is blocked on either a mq_send()
+ * or mq_receive is undefined when mq_close() is called.
+ * - The results of using this message queue descriptor after a
+ * a successful return from mq_close() is undefined.
+ *
+ ************************************************************/
+
+int mq_close(mqd_t mqdes)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ msgq_t *msgq;
+ uint32 saved_state;
+ int ret = ERROR;
+
+ /* Verify the inputs */
+
+ if (mqdes)
+ {
+ sched_lock();
+
+ /* Remove the message descriptor from the current task's
+ * list of message descriptors.
+ */
+
+ sq_rem((sq_entry_t*)mqdes, &rtcb->msgdesq);
+
+ /* Find the message queue associated with the message descriptor */
+
+ msgq = mqdes->msgq;
+
+ /* Check if the calling task has a notification attached to
+ * the message queue via this mqdes.
+ */
+
+ if (msgq->ntmqdes == mqdes)
+ {
+ msgq->ntpid = INVALID_PROCESS_ID;
+ msgq->ntsigno = 0;
+ msgq->ntvalue.sival_int = 0;
+ msgq->ntmqdes = NULL;
+ }
+
+ /* Decrement the connection count on the message queue. */
+
+ if (msgq->nconnect)
+ {
+ msgq->nconnect--;
+ }
+
+ /* If it is no longer connected to any message descriptor and if the
+ * message queue has already been unlinked, then we can discard the
+ * message queue.
+ */
+
+ if (!msgq->nconnect && msgq->unlinked)
+ {
+ /* Remove the message queue from the list of all
+ * message queues
+ */
+
+ saved_state = irqsave();
+ (void)sq_rem((sq_entry_t*)msgq, &g_msgqueues);
+ irqrestore(saved_state);
+
+ /* Then deallocate it (and any messages left in it) */
+
+ mq_msgqfree(msgq);
+ }
+
+ /* Deallocate the message descriptor */
+
+ mq_desfree(mqdes);
+
+ sched_unlock();
+ ret = OK;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/mq_descreate.c b/nuttx/sched/mq_descreate.c
new file mode 100644
index 0000000000..5f68268d30
--- /dev/null
+++ b/nuttx/sched/mq_descreate.c
@@ -0,0 +1,156 @@
+/************************************************************
+ * mq_descreate.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <stdarg.h> /* va_list */
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <mqueue.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+#include <sys/types.h> /* uint32, etc. */
+#include <queue.h>
+#include <nuttx/kmalloc.h>
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_desalloc
+ *
+ * Description:
+ * Allocate a message queue descriptor.
+ *
+ * Inputs:
+ * None
+ *
+ * Return Value:
+ * Reference to the allocated mq descriptor.
+ *
+ ************************************************************/
+
+static inline mqd_t mq_desalloc(void)
+{
+ mqd_t mqdes;
+
+ /* Try to get the message descriptorfrom the free list */
+
+ mqdes = (mqd_t)sq_remfirst(&g_desfree);
+
+ /* Check if we got one. */
+
+ if (!mqdes)
+ {
+ /* Add another block of message descriptors to the list */
+
+ mq_desblockalloc();
+
+ /* And try again */
+
+ mqdes = (mqd_t)sq_remfirst(&g_desfree);
+ }
+
+ return mqdes;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_descreate
+ *
+ * Description:
+ * Create a message queue descriptor for the specified TCB
+ *
+ * Inputs:
+ * TCB - task that needs the descriptor.
+ * msgq - Named message queue containing the message
+ * oflags - access rights for the descriptor
+ *
+ * Return Value:
+ *
+ *
+ ************************************************************/
+
+mqd_t mq_descreate(_TCB* mtcb, msgq_t* msgq, int oflags)
+{
+ mqd_t mqdes;
+
+ /* Create a message queue descriptor for the TCB */
+
+ mqdes = mq_desalloc();
+ if (mqdes)
+ {
+ /* Initialize the MQ descriptor */
+
+ memset(mqdes, 0, sizeof(struct mq_des));
+ mqdes->msgq = msgq;
+ mqdes->oflags = oflags;
+
+ /* And add it to the specified tasks's TCB */
+
+ sq_addlast((sq_entry_t*)mqdes, &mtcb->msgdesq);
+ }
+
+ return mqdes;
+}
diff --git a/nuttx/sched/mq_findnamed.c b/nuttx/sched/mq_findnamed.c
new file mode 100644
index 0000000000..f01e24d1f3
--- /dev/null
+++ b/nuttx/sched/mq_findnamed.c
@@ -0,0 +1,103 @@
+/************************************************************
+ * mq_findnamed.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <string.h>
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_findnamed
+ *
+ * Description:
+ * This function finds the named message queue with the
+ * specified name in the list of message queues.
+ *
+ * Inputs:
+ * mq_name - the name of the message queue to find
+ *
+ * Return Value:
+ * A reference to the matching named message queue
+ * structure (or NULL if none was found).
+ *
+ ************************************************************/
+
+msgq_t *mq_findnamed(const char *mq_name)
+{
+ msgq_t *msgq;
+
+ /* Search the list of named message queues */
+
+ for (msgq = (msgq_t*)g_msgqueues.head; (msgq); msgq = msgq->flink)
+ {
+ /* Break out of the lloop with a non-NULL msgq if the
+ * name matches.
+ */
+
+ if (!strcmp(mq_name, msgq->name))
+ {
+ break;
+ }
+ }
+
+ return msgq;
+}
diff --git a/nuttx/sched/mq_getattr.c b/nuttx/sched/mq_getattr.c
new file mode 100644
index 0000000000..52292410e4
--- /dev/null
+++ b/nuttx/sched/mq_getattr.c
@@ -0,0 +1,114 @@
+/************************************************************
+ * mq_getattr.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <stdarg.h> /* va_list */
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <mqueue.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_getattr
+ *
+ * Description:
+ * This functions gets status information and attributes
+ * associated with the specified message queue.
+ *
+ * Parameters:
+ * mqdes - Message queue descriptor
+ * mq_stat - Buffer in which to return attributes
+ *
+ * Return Value:
+ * 0 (OK) if attributes provided, -1 (ERROR) otherwise.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int mq_getattr(mqd_t mqdes, struct mq_attr *mq_stat)
+{
+ int ret = ERROR;
+
+ if (mqdes && mq_stat)
+ {
+ /* Return the attributes */
+
+ mq_stat->mq_maxmsg = mqdes->msgq->maxmsgs;
+ mq_stat->mq_msgsize = mqdes->msgq->maxmsgsize;
+ mq_stat->mq_flags = mqdes->oflags;
+ mq_stat->mq_curmsgs = mqdes->msgq->nmsgs;
+
+ ret = OK;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/mq_initialize.c b/nuttx/sched/mq_initialize.c
new file mode 100644
index 0000000000..deff6a2545
--- /dev/null
+++ b/nuttx/sched/mq_initialize.c
@@ -0,0 +1,246 @@
+/************************************************************
+ * mq_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <queue.h>
+#include <nuttx/kmalloc.h>
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/* This is a container for a list of message queue
+ * descriptors.
+ */
+
+struct mq_des_block_s
+{
+ sq_entry_t queue;
+ struct mq_des mqdes[NUM_MSG_DESCRIPTORS];
+};
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* This is a list of all opened message queues */
+
+sq_queue_t g_msgqueues;
+
+/* The g_msgfree is a list of messages that are available
+ * for general use. The number of messages in this list is a
+ * system configuration item.
+ */
+
+sq_queue_t g_msgfree;
+
+/* The g_msgfreeInt is a list of messages that are reserved
+ * for use by interrupt handlers.
+ */
+
+sq_queue_t g_msgfreeirq;
+
+/* The g_desfree data structure is a list of message
+ * descriptors available to the operating system for general use.
+ * The number of messages in the pool is a constant.
+ */
+
+sq_queue_t g_desfree;
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/* g_msgalloc is a pointer to the start of the allocated
+ * block of messages.
+ */
+
+static mqmsg_t *g_msgalloc;
+
+/* g_msgfreeirqalloc is a pointer to the start of the
+ * allocated block of messages.
+ */
+
+static mqmsg_t *g_msgfreeirqalloc;
+
+/* g_desalloc is a list of allocated block of message queue
+ * descriptors.
+ */
+
+static sq_queue_t g_desalloc;
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_msgblockalloc
+ *
+ * Description:
+ * Allocate a block of messages and place them on the free
+ * list.
+ *
+ *
+ * Inputs:
+ * queue
+ ************************************************************/
+
+static mqmsg_t *mq_msgblockalloc(sq_queue_t *queue, uint16 nmsgs,
+ ubyte alloc_type)
+{
+ mqmsg_t *mqmsgblock;
+
+ /* The g_msgfree must be loaded at initialization time to hold the
+ * configured number of messages.
+ */
+
+ mqmsgblock = (mqmsg_t*)kmalloc(sizeof(mqmsg_t) * nmsgs);
+ if (mqmsgblock)
+ {
+ mqmsg_t *mqmsg = mqmsgblock;
+ int i;
+
+ for (i = 0; i < nmsgs; i++)
+ {
+ mqmsg->type = alloc_type;
+ sq_addlast((sq_entry_t*)mqmsg++, queue);
+ }
+ }
+
+ return mqmsgblock;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_initialize
+ *
+ * Description:
+ * This function initializes the messasge system. This
+ * function must be called early in the initialization
+ * sequence before any of the other message interfaces
+ * execute.
+ *
+ * Inputs:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ ************************************************************/
+
+void mq_initialize(void)
+{
+ /* Initialize the list of message queues */
+
+ sq_init(&g_msgqueues);
+
+ /* Initialize the message free lists */
+
+ sq_init(&g_msgfree);
+ sq_init(&g_msgfreeirq);
+ sq_init(&g_desalloc);
+
+ /* Allocate a block of messages for general use */
+
+ g_msgalloc =
+ mq_msgblockalloc(&g_msgfree, CONFIG_PREALLOC_MQ_MSGS,
+ MQ_ALLOC_FIXED);
+
+ /* Allocate a block of messages for use exclusively by
+ * interrupt handlers
+ */
+
+ g_msgfreeirqalloc =
+ mq_msgblockalloc(&g_msgfreeirq, NUM_INTERRUPT_MSGS,
+ MQ_ALLOC_IRQ);
+
+ /* Allocate a block of message queue descriptors */
+
+ mq_desblockalloc();
+}
+
+/************************************************************
+ * Function: mq_desblockalloc
+ *
+ * Description:
+ * Allocate a block of message descriptors and place them
+ * on the free list.
+ *
+ * Inputs:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ ************************************************************/
+
+void mq_desblockalloc(void)
+{
+ struct mq_des_block_s *mqdesblock;
+
+ /* Allocate a block of message descriptors */
+
+ mqdesblock = (struct mq_des_block_s *)kmalloc(sizeof(struct mq_des_block_s));
+ if (mqdesblock)
+ {
+ int i;
+
+ /* Add the block to the list of allocated blocks (in case
+ * we ever need to reclaim the memory.
+ */
+
+ sq_addlast(&mqdesblock->queue, &g_desalloc);
+
+ /* Then add each message queue descriptor to the free list */
+
+ for (i = 0; i < NUM_MSG_DESCRIPTORS; i++)
+ {
+ sq_addlast((sq_entry_t*)&mqdesblock->mqdes[i], &g_desfree);
+ }
+ }
+}
+
diff --git a/nuttx/sched/mq_internal.h b/nuttx/sched/mq_internal.h
new file mode 100644
index 0000000000..5768bbd706
--- /dev/null
+++ b/nuttx/sched/mq_internal.h
@@ -0,0 +1,200 @@
+/************************************************************
+ * mq_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __MQ_INTERNAL_H
+#define __MQ_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <mqueue.h>
+#include <sched.h>
+#include <nuttx/compiler.h>
+
+/************************************************************
+ * Compilations Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define MQ_MAX_BYTES CONFIG_MQ_MAXMSGSIZE
+#define MQ_MAX_HWORDS ((MQ_MAX_BYTES + sizeof(uint16) - 1) / sizeof(uint16))
+#define MQ_MAX_MSGS 16
+#define MQ_PRIO_MAX 255
+
+/* This defines the number of messages descriptors to allocate
+ * at each "gulp."
+ */
+
+#define NUM_MSG_DESCRIPTORS 24
+
+/* This defines the number of messages to set aside for
+ * exclusive use by interrupt handlers
+ */
+
+#define NUM_INTERRUPT_MSGS 8
+
+/************************************************************
+ * Global Type Declarations
+ ************************************************************/
+
+enum mqalloc_e
+{
+ MQ_ALLOC_FIXED = 0, /* pre-allocated; never freed */
+ MQ_ALLOC_DYN, /* dynamically allocated; free when unused */
+ MQ_ALLOC_IRQ /* Preallocated, reserved for interrupt handling */
+};
+typedef enum mqalloc_e mqalloc_t;
+
+/* This structure describes one buffered POSIX message. */
+/* NOTE: This structure is allocated from the same pool as MQ_type.
+ * Therefore, (1) it must have a fixed "mail" size, and (2) must
+ * exactly match MQ_type in size.
+ */
+
+struct mqmsg
+{
+ /* The position of the following two field must exactly match
+ * MQ_type.
+ */
+
+ struct mqmsg *next; /* Forward link to next message */
+ ubyte type; /* (Used to manage allocations) */
+
+ ubyte priority; /* priority of message */
+ ubyte msglen; /* Message data length */
+ ubyte pad; /* Not used */
+ uint16 mail[MQ_MAX_HWORDS]; /* Message data */
+};
+typedef struct mqmsg mqmsg_t;
+
+/* This structure defines a message queue */
+
+struct msgq_s
+{
+ struct msgq_s *flink; /* Forward link to next message queue */
+ sq_queue_t msglist; /* Prioritized message list */
+ sint16 maxmsgs; /* Maximum number of messages in the queue */
+ sint16 nmsgs; /* Number of message in the queue */
+ sint16 nconnect; /* Number of connections to message queue */
+ sint16 nwaitnotfull; /* Number tasks waiting for not full */
+ sint16 nwaitnotempty; /* Number tasks waiting for not empty */
+ ubyte maxmsgsize; /* Max size of message in message queue */
+ boolean unlinked; /* TRUE if the msg queue has been unlinked */
+ struct mq_des *ntmqdes; /* Notification: Owning mqdes (NULL if none) */
+ pid_t ntpid; /* Notification: Receiving Task's PID */
+ int ntsigno; /* Notification: Signal number */
+ union sigval ntvalue; /* Notification: Signal value */
+ char name[1]; /* Start of the queue name */
+};
+typedef struct msgq_s msgq_t;
+
+#define SIZEOF_MQ_HEADER ((int)(((msgq_t*)NULL)->name))
+
+/* This describes the message queue descriptor that is held in the
+ * task's TCB
+ */
+
+struct mq_des
+{
+ struct mq_des *flink; /* Forward link to next message descriptor */
+ msgq_t *msgq; /* Pointer to associated message queue */
+ int oflags; /* Flags set when message queue was opened */
+};
+
+/* This is the handle used to reference a message queue */
+
+typedef struct mq_des *mqd_t;
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* This is a list of all opened message queues */
+
+extern sq_queue_t g_msgqueues;
+
+/* The g_msgfree is a list of messages that are available
+ * for general use. The number of messages in this list is a
+ * system configuration item.
+ */
+
+extern sq_queue_t g_msgfree;
+
+/* The g_msgfreeInt is a list of messages that are reserved
+ * for use by interrupt handlers.
+ */
+
+extern sq_queue_t g_msgfreeirq;
+
+/* The g_desfree data structure is a list of message
+ * descriptors available to the operating system for general use.
+ * The number of messages in the pool is a constant.
+ */
+
+extern sq_queue_t g_desfree;
+
+/************************************************************
+ * Global Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+/* Functions defined in mq_initialized.c *******************/
+
+EXTERN void weak_function mq_initialize(void);
+EXTERN void mq_desblockalloc(void);
+
+EXTERN mqd_t mq_descreate(_TCB* mtcb, msgq_t* msgq, int oflags);
+EXTERN msgq_t *mq_findnamed(const char *mq_name);
+EXTERN void mq_msgfree(mqmsg_t *mqmsg);
+EXTERN void mq_msgqfree(msgq_t *msgq);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MQ_INTERNAL_H */
+
diff --git a/nuttx/sched/mq_msgfree.c b/nuttx/sched/mq_msgfree.c
new file mode 100644
index 0000000000..9c5f12af5c
--- /dev/null
+++ b/nuttx/sched/mq_msgfree.c
@@ -0,0 +1,135 @@
+/************************************************************
+ * mq_msgfree.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <queue.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_msgfree
+ *
+ * Description:
+ * The mq_msgfree function will return a message to the
+ * free pool of messages if it was a pre-allocated message.
+ * If the message was allocated dynamically it will be
+ * deallocated.
+ *
+ * Inputs:
+ * mqmsg - message to free
+ *
+ * Return Value:
+ * None
+ *
+ ************************************************************/
+
+void mq_msgfree(mqmsg_t * mqmsg)
+{
+ uint32 saved_state;
+
+ /* If this is a generally available pre-allocated message,
+ * then just put it back in the free list.
+ */
+
+ if (mqmsg->type == MQ_ALLOC_FIXED)
+ {
+ /* Make sure we avoid concurrent access to the free
+ * list from interrupt handlers.
+ */
+
+ saved_state = irqsave();
+ sq_addlast((sq_entry_t*)mqmsg, &g_msgfree);
+ irqrestore(saved_state);
+ }
+
+ /* If this is a message pre-allocated for interrupts,
+ * then put it back in the correct free list.
+ */
+
+ else if (mqmsg->type == MQ_ALLOC_IRQ)
+ {
+ /* Make sure we avoid concurrent access to the free
+ * list from interrupt handlers.
+ */
+
+ saved_state = irqsave();
+ sq_addlast((sq_entry_t*)mqmsg, &g_msgfreeirq);
+ irqrestore(saved_state);
+ }
+
+ /* Otherwise, deallocate it. Note: interrupt handlers
+ * will never deallocate messages because they will not
+ * received them.
+ */
+
+ else if (mqmsg->type == MQ_ALLOC_DYN)
+ {
+ sched_free(mqmsg);
+ }
+
+ else
+ {
+ PANIC(OSERR_BADMSGTYPE);
+ }
+}
diff --git a/nuttx/sched/mq_msgqfree.c b/nuttx/sched/mq_msgqfree.c
new file mode 100644
index 0000000000..59a27603e1
--- /dev/null
+++ b/nuttx/sched/mq_msgqfree.c
@@ -0,0 +1,107 @@
+/************************************************************
+ * mq_msgqfree.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_msgqfree
+ *
+ * Description:
+ * This function deallocates an initialized message queue
+ * structure. First, it deallocates all of the queued
+ * messages in the message Q. It is assumed that this
+ * message is fully unlinked and closed so that not thread
+ * will attempt access it while it is being deleted.
+ *
+ * Inputs:
+ * msgq - Named essage queue to be freed
+ *
+ * Return Value:
+ * None
+ *
+ ************************************************************/
+
+void mq_msgqfree(msgq_t *msgq)
+{
+ mqmsg_t *curr;
+ mqmsg_t *next;
+
+ /* Deallocate any stranded messages in the message queue. */
+
+ curr = (mqmsg_t*)msgq->msglist.head;
+ while (curr)
+ {
+ /* Deallocate the message structure. */
+
+ next = curr->next;
+ mq_msgfree(curr);
+ curr = next;
+ }
+
+ /* Then deallocate the message queue itself */
+
+ sched_free(msgq);
+}
diff --git a/nuttx/sched/mq_notify.c b/nuttx/sched/mq_notify.c
new file mode 100644
index 0000000000..aaed2e45b1
--- /dev/null
+++ b/nuttx/sched/mq_notify.c
@@ -0,0 +1,170 @@
+/************************************************************
+ * mq_notify.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <mqueue.h>
+#include <sched.h>
+#include "os_internal.h"
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_notify
+ *
+ * Description:
+ * If "notification" is not NULL, this function connects
+ * the task with the message queue such that the specified
+ * signal will be sent to the task whenever the message
+ * changes from empty to non-empty. One one notification
+ * can be attached to a message queue.
+ *
+ * If "notification" is NULL, the attached notification is
+ * detached (if it was held by the calling task) and the
+ * queue is available to attach another notification.
+ *
+ * When the notification is sent to the registered process,
+ * its registration will be removed. The message queue
+ * will then be available for registration.
+ *
+ * Parameters:
+ * mqdes - Message queue descriptor
+ * notification - Real-time signal structure containing:
+ * sigev_notify - Should be SIGEV_SIGNAL (but actually ignored)
+ * sigev_signo - The signo to use for the notification
+ * sigev_value - Value associated with the signal
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ * POSIX Compatibility:
+ * int mq_notify(mqd_t mqdes, const struct sigevent *notification);
+ *
+ * The notification will be sent to the registered task even if another
+ * task is waiting for the message queue to become non-empty. This is
+ * inconsistent with the POSIX specification which says, "If a process
+ * has registered for notification of message a arrival at a message
+ * queue and some process is blocked in mq_receive() waiting to receive
+ * a message when a message arrives at the queue, the arriving message
+ * message shall satisfy mq_receive()... The resulting behavior is as if
+ * the message queue remains empty, and no notification shall be sent."
+ *
+ ************************************************************/
+
+int mq_notify(mqd_t mqdes, const struct sigevent *notification)
+{
+ _TCB *rtcb;
+ msgq_t *msgq;
+ int ret = ERROR;
+
+ if (mqdes)
+ {
+ sched_lock();
+
+ /* Get a pointer to the message queue */
+
+ msgq = mqdes->msgq;
+
+ /* Get the current process ID */
+
+ rtcb = (_TCB*)g_readytorun.head;
+
+ /* Is there already a notification attached */
+
+ if (!msgq->ntmqdes)
+ {
+ /* No... Have we been asked to establish one? Make
+ * sure a good signal number has been provided
+ */
+
+ if (notification && GOOD_SIGNO(notification->sigev_signo))
+ {
+ /* Yes... Assign it to the current task. */
+
+ msgq->ntvalue = notification->sigev_value;
+ msgq->ntsigno = notification->sigev_signo;
+ msgq->ntpid = rtcb->pid;
+ msgq->ntmqdes = mqdes;
+ ret = OK;
+ }
+ }
+
+ /* Yes... a notification is attached. Does this task own it?
+ * Is it trying to remove it?
+ */
+
+ else if ((msgq->ntpid == rtcb->pid) && (!notification))
+ {
+ /* Yes... Detach the notification */
+
+ msgq->ntpid = INVALID_PROCESS_ID;
+ msgq->ntsigno = 0;
+ msgq->ntvalue.sival_int = 0;
+ msgq->ntmqdes = NULL;
+ ret = OK;
+ }
+ sched_unlock();
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/mq_open.c b/nuttx/sched/mq_open.c
new file mode 100644
index 0000000000..97b2af121b
--- /dev/null
+++ b/nuttx/sched/mq_open.c
@@ -0,0 +1,226 @@
+/************************************************************
+ * mq_open.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <stdarg.h> /* va_list */
+#include <mqueue.h>
+#include <string.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include "os_internal.h"
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_open
+ *
+ * Description:
+ * This function establish a connection between a named
+ * message queue and the calling task. After a successful
+ * call of mq_open(), the task can reference the message
+ * queue using the address returned by the call. The
+ * message queue remains usable until it is closed by a
+ * successful call to mq_close().
+ *
+ * Parameters:
+ * mq_name - Name of the queue to open
+ * oflags - open flags
+ * Optional parameters. When the O_CREAT flag is
+ * specified, two optional parameters are expected:
+ * 1. mode_t mode (ignored), and
+ * 2. struct mq_attr *attr. The mq_maxmsg attribute
+ * is used at the time that the message queue is
+ * created to determine the maximum number of
+ * messages that may be placed in the message queue.
+ *
+ * Return Value:
+ * A message queue descriptor or -1 (ERROR)
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+mqd_t mq_open(const char *mq_name, int oflags, ...)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ msgq_t *msgq;
+ mqd_t mqdes = NULL;
+ va_list arg; /* Points to each un-named argument */
+ mode_t mode; /* MQ creation mode parameter (ignored) */
+ struct mq_attr *attr; /* MQ creation attributes */
+ int namelen; /* Length of MQ name */
+
+ /* Make sure that a non-NULL name is supplied */
+
+ if (mq_name)
+ {
+ sched_lock();
+ namelen = strlen(mq_name);
+ if (namelen > 0)
+ {
+ /* See if the message queue already exists */
+
+ msgq = mq_findnamed(mq_name);
+ if (msgq)
+ {
+ /* It does. Check if the caller wanted to create
+ * a new message queue with this name.
+ */
+
+ if (!(oflags & O_CREAT) || !(oflags & O_EXCL))
+ {
+ /* Create a message queue descriptor for the TCB */
+
+ mqdes = mq_descreate(rtcb, msgq, oflags);
+ if (mqdes)
+ {
+ /* Allow a new connection to the message queue */
+
+ msgq->nconnect++;
+ }
+ }
+ }
+
+ /* It doesn't exist. Should we create one? */
+
+ else if ((oflags & O_CREAT) != 0)
+ {
+ /* Allocate memory for the new message queue. The size to
+ * allocate is the size of the msgq_t header plus the size
+ * of the message queue name+1.
+ */
+
+ msgq = (msgq_t*)kzmalloc(SIZEOF_MQ_HEADER + namelen + 1);
+ if (msgq)
+ {
+ /* Create a message queue descriptor for the TCB */
+
+ mqdes = mq_descreate(rtcb, msgq, oflags);
+ if (mqdes)
+ {
+ /* Set up to get the optional arguments needed to create
+ * a message queue.
+ */
+
+ va_start(arg, oflags);
+ mode = va_arg(arg, mode_t);
+ attr = va_arg(arg, struct mq_attr*);
+
+ /* Initialize the new named message queue */
+
+ sq_init(&msgq->msglist);
+ if (attr)
+ {
+ msgq->maxmsgs = (sint16)attr->mq_maxmsg;
+ if (attr->mq_msgsize <= MQ_MAX_BYTES)
+ {
+ msgq->maxmsgsize = (sint16)attr->mq_msgsize;
+ }
+ else
+ {
+ msgq->maxmsgsize = MQ_MAX_BYTES;
+ }
+ }
+ else
+ {
+ msgq->maxmsgs = MQ_MAX_MSGS;
+ msgq->maxmsgsize = MQ_MAX_BYTES;
+ }
+
+ msgq->nconnect = 1;
+ msgq->ntpid = INVALID_PROCESS_ID;
+ strcpy(msgq->name, mq_name);
+
+ /* Add the new message queue to the list of
+ * message queues
+ */
+
+ sq_addlast((sq_entry_t*)msgq, &g_msgqueues);
+
+ /* Clean-up variable argument stuff */
+
+ va_end(arg);
+ }
+ else
+ {
+ /* Deallocate the msgq structure. Since it is
+ * uninitialized, mq_deallocate() is not used.
+ */
+
+ sched_free(msgq);
+ }
+ }
+ }
+ }
+ sched_unlock();
+ }
+
+ if (mqdes == NULL)
+ {
+ return (mqd_t)ERROR;
+ }
+ else
+ {
+ return mqdes;
+ }
+}
diff --git a/nuttx/sched/mq_receive.c b/nuttx/sched/mq_receive.c
new file mode 100644
index 0000000000..3dbe662c2f
--- /dev/null
+++ b/nuttx/sched/mq_receive.c
@@ -0,0 +1,236 @@
+/************************************************************
+ * mq_receive.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <stdarg.h> /* va_list */
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <mqueue.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_receive
+ *
+ * Description:
+ * This function receives the oldest of the highest
+ * priority messages from the message queue specified by
+ * "mqdes." If the size of the buffer in bytes (msglen) is
+ * less than the "mq_msgsize" attribute of the message
+ * queue, mq_receive will return an error. Otherwise, the
+ * select message is removed from the queue and copied to
+ * "msg."
+ *
+ * If the message queue is empty and O_NONBLOCK was not
+ * set, mq_receive() will block until a message is added
+ * to the message queue. If more than one task is waiting
+ * to receive a message, only the task with the highest
+ * priority that has waited the longest will be unblocked.
+ *
+ * If the queue is empty and O_NONBLOCK is set, ERROR will
+ * be returned.
+ *
+ * Parameters:
+ * mqdes - Message Queue Descriptor
+ * msg - Buffer to receive the message
+ * msglen - Size of the buffer in bytes
+ * prio - If not NULL, the location to store message
+ * priority.
+ *
+ * Return Value:
+ * Length of the selected message in bytes, otherwise -1
+ * (ERROR).
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int mq_receive(mqd_t mqdes, void *msg, size_t msglen, int *prio)
+{
+ _TCB *rtcb;
+ _TCB *btcb;
+ msgq_t *msgq;
+ mqmsg_t *curr;
+ uint32 saved_state;
+ ubyte rcvmsglen;
+ int ret = ERROR;
+
+ /* Verify the input parameters */
+
+ sched_lock();
+ if (msg && mqdes && (mqdes->oflags & O_RDOK) != 0 &&
+ msglen >= (size_t)mqdes->msgq->maxmsgsize)
+ {
+ /* Get a pointer to the message queue */
+
+ msgq = mqdes->msgq;
+
+ /* Several operations must be performed below: We must determine if
+ * a message is pending and, if not, wait for the message. Since
+ * messages can be sent from the interrupt level, there is a race
+ * condition that can only be eliminated by disabling interrupts!
+ */
+
+ saved_state = irqsave();
+
+ /* Get the message from the head of the queue */
+
+ while ((curr = (mqmsg_t*)sq_remfirst(&msgq->msglist)) == NULL)
+ {
+ /* Should we block until there the above condition has been
+ * satisfied?
+ */
+
+ if (!(mqdes->oflags & O_NONBLOCK))
+ {
+ /* Block and try again */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ rtcb->msgwaitq = msgq;
+ msgq->nwaitnotempty++;
+ up_block_task(rtcb, TSTATE_WAIT_MQNOTEMPTY);
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ /* If we got message, then decrement the number of messages in
+ * the queue while we are still in the critical section
+ */
+
+ if (curr)
+ {
+ msgq->nmsgs--;
+ }
+ irqrestore(saved_state);
+
+ /* Check (again) if we got a message from the message queue*/
+
+ if (curr)
+ {
+ /* Get the length of the message (also the return value) */
+
+ ret = rcvmsglen = curr->msglen;
+
+ /* Copy the message into the caller's buffer */
+
+ memcpy((void*)curr->mail, msg, rcvmsglen);
+
+ /* Copy the message priority as well (if a buffer is provided) */
+
+ if (prio)
+ {
+ *prio = curr->priority;
+ }
+
+ /* We are done with the message. Deallocate it now. */
+
+ mq_msgfree(curr);
+
+ /* Check if any tasks are waiting for the MQ not full event. */
+
+ if (msgq->nwaitnotfull > 0)
+ {
+ /* Find the highest priority task that is waiting for
+ * this queue to be not-full in g_waitingformqnotfull list.
+ * This must be performed in a critical section because
+ * messages can be sent from interrupt handlers.
+ */
+
+ saved_state = irqsave();
+ for (btcb = (_TCB*)g_waitingformqnotfull.head;
+ btcb && btcb->msgwaitq != msgq;
+ btcb = btcb->flink);
+
+ /* If one was found, unblock it. NOTE: There is a race
+ * condition here: the queue might be full again by the
+ * time the task is unblocked
+ */
+
+ if (!btcb)
+ {
+ PANIC(OSERR_MQNOTFULLCOUNT);
+ }
+ else
+ {
+ btcb->msgwaitq = NULL;
+ msgq->nwaitnotfull--;
+ up_unblock_task(btcb);
+ }
+ irqrestore(saved_state);
+ }
+ }
+ }
+
+ sched_unlock();
+ return ret;
+}
diff --git a/nuttx/sched/mq_send.c b/nuttx/sched/mq_send.c
new file mode 100644
index 0000000000..b5dda82786
--- /dev/null
+++ b/nuttx/sched/mq_send.c
@@ -0,0 +1,398 @@
+/************************************************************
+ * mq_send.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/kmalloc.h>
+#include <sys/types.h> /* uint32, etc. */
+#include <mqueue.h>
+#include <string.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_msgalloc
+ *
+ * Description:
+ * The mq_msgalloc function will get a free message for use
+ * by the operating system. The message will be allocated
+ * from the g_msgfree list.
+ *
+ * If the list is empty AND the message is NOT being
+ * allocated from the interrupt level, then the message
+ * will be allocated. If a message cannot be obtained,
+ * the operating system is dead and therefore cannot
+ * continue.
+ *
+ * If the list is empty AND the message IS being allocated
+ * from the interrupt level. This function will attempt to
+ * get a message from the g_msgfreeirq list. If this is
+ * unsuccessful, the calling interrupt handler will be
+ * notified.
+ *
+ * Inputs:
+ * None
+ *
+ * Return Value:
+ * A reference to the allocated msg structure
+ *
+ ************************************************************/
+
+mqmsg_t *mq_msgalloc(void)
+{
+ mqmsg_t *mqmsg;
+ uint32 saved_state;
+
+ /* If we were called from an interrupt handler, then try to
+ * get the message from generally available list of messages.
+ * If this fails, then try the list of messages reserved for
+ * interrupt handlers
+ */
+
+ if (up_interrupt_context())
+ {
+ /* Try the general free list */
+
+ mqmsg = (mqmsg_t*)sq_remfirst(&g_msgfree);
+ if (!mqmsg)
+ {
+ /* Try the free list reserved for interrupt handlers */
+
+ mqmsg = (mqmsg_t*)sq_remfirst(&g_msgfreeirq);
+ }
+ }
+
+ /* We were not called from an interrupt handler. */
+
+ else
+ {
+ /* Try to get the message from the generally available free list.
+ * Disable interrupts -- we might be called from an interrupt handler.
+ */
+
+ saved_state = irqsave();
+ mqmsg = (mqmsg_t*)sq_remfirst(&g_msgfree);
+ irqrestore(saved_state);
+
+ /* If we cannot a message from the free list, then we will have to allocate one. */
+
+ if (!mqmsg)
+ {
+ mqmsg = (mqmsg_t *)kmalloc((sizeof (mqmsg_t)));
+
+ /* Check if we got an allocated message */
+
+ if (mqmsg)
+ {
+ mqmsg->type = MQ_ALLOC_DYN;
+ }
+
+ /* No? We are dead */
+
+ else
+ {
+ dbg("%s: Out of messages\n", __FUNCTION__);
+ PANIC((uint32)OSERR_OUTOFMESSAGES);
+ }
+ }
+ }
+
+ return(mqmsg);
+
+}
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_send
+ *
+ * Description:
+ * This function adds the specificied message (msg) to the
+ * message queue (mqdes). The "msglen" parameter specifies
+ * the length of the message in bytes pointed to by "msg."
+ * This length must not exceed the maximum message length
+ * from the mq_getattr().
+ *
+ * If the message queue is not full, mq_send() will in the
+ * message in the message queue at the position indicated
+ * by the "prio" argrument. Messages with higher priority
+ * will be inserted before lower priority messages. The
+ * value of "prio" must not exceed MQ_PRIO_MAX.
+ *
+ * If the specified message queue is full and O_NONBLOCK
+ * is not set in the message queue, then mq_send() will
+ * block until space becomes available to the queue the
+ * message.
+ *
+ * If the message queue is full and O_NONBLOCK is set,
+ * the message is not queued and ERROR is returned.
+ *
+ * Parameters:
+ * mqdes - Message queue descriptor
+ * msg - Message to send
+ * msglen - The length of the message in bytes
+ * prio - The priority of the message
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions/restrictions:
+ *
+ ************************************************************/
+
+int mq_send(mqd_t mqdes, const void *msg, size_t msglen, int prio)
+{
+ _TCB *rtcb;
+ _TCB *btcb;
+ msgq_t *msgq;
+ mqmsg_t *curr;
+ mqmsg_t *next;
+ mqmsg_t *prev;
+ uint32 saved_state;
+ int ret = ERROR;
+
+ /* Verify the input parameters */
+
+ sched_lock();
+ if (msg && mqdes && (mqdes->oflags & O_WROK) != 0 &&
+ msglen > 0 && msglen <= (size_t)mqdes->msgq->maxmsgsize &&
+ prio >= 0 && prio <= MQ_PRIO_MAX)
+ {
+ /* Get a pointer to the message queue */
+
+ msgq = mqdes->msgq;
+
+ /* If we are sending a message from an interrupt handler, then
+ * try to get message structure unconditionally.
+ */
+
+ saved_state = irqsave();
+ if (up_interrupt_context())
+ {
+ curr = mq_msgalloc();
+ }
+
+ /* Otherwise, arbitrarily limit the number of messages in the
+ * queue to the value determined when the message queue was opened.
+ * This makes us more POSIX-like as well as prohibits one slow
+ * responding task from consuming all available memory.
+ */
+
+ else if (msgq->nmsgs >= msgq->maxmsgs)
+ {
+ /* Should we block until there is sufficient space in the
+ * message queue?
+ */
+
+ if ((mqdes->oflags & O_NONBLOCK) != 0)
+ {
+ /* No... We will return an error to the caller. */
+
+ curr = NULL;
+ }
+
+ /* Yes... We will not return control until the message queue is
+ * available.
+ */
+
+ else
+ {
+ /* Loop until there are fewer than max allowable messages in the
+ * receiving message queue
+ */
+
+ while (msgq->nmsgs >= msgq->maxmsgs)
+ {
+ /* Block until the message queue is no longer full.
+ * When we are unblocked, we will try again
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ rtcb->msgwaitq = msgq;
+ (msgq->nwaitnotfull)++;
+ up_block_task(rtcb, TSTATE_WAIT_MQNOTFULL);
+ }
+
+ /* It should be okay to get add a message to the receiving
+ * message queue now.
+ */
+
+ curr = mq_msgalloc();
+ }
+ }
+
+ /* We are not in an interrupt handler and the receiving message queue
+ * is not full
+ */
+
+ else
+ {
+ /* Just allocate a message */
+
+ curr = mq_msgalloc();
+ }
+ irqrestore(saved_state);
+
+ /* Check if we were able to get a message structure */
+
+ if (curr)
+ {
+ /* Construct the current message header info */
+
+ curr->priority = (ubyte)prio;
+ curr->msglen = (ubyte)msglen;
+
+ /* Copy the message data into the message */
+
+ memcpy((void*)msg, (const void*)curr->mail, msglen);
+
+ /* Insert the new message in the message queue */
+
+ saved_state = irqsave();
+
+ /* Search the message list to find the location to insert the new
+ * message. Each is list is maintained in ascending priority order.
+ */
+
+ for (prev = NULL, next = (mqmsg_t*)msgq->msglist.head;
+ next && prio <= next->priority;
+ prev = next, next = next->next);
+
+ /* Add the message at the right place */
+
+ if (prev)
+ {
+ sq_addafter((sq_entry_t*)prev, (sq_entry_t*)curr,
+ &msgq->msglist);
+ }
+ else
+ {
+ sq_addfirst((sq_entry_t*)curr, &msgq->msglist);
+ }
+
+ /* Increment the count of message in the queue */
+
+ msgq->nmsgs++;
+ irqrestore(saved_state);
+
+ /* Check if we need to notify any tasks that are attached to the
+ * message queue
+ */
+
+ if (msgq->ntmqdes)
+ {
+ /* Remove the message notification data from the message queue. */
+
+ union sigval value = msgq->ntvalue;
+ int signo = msgq->ntsigno;
+ int pid = msgq->ntpid;
+
+ /* Detach the notification */
+
+ msgq->ntpid = INVALID_PROCESS_ID;
+ msgq->ntsigno = 0;
+ msgq->ntvalue.sival_int = 0;
+ msgq->ntmqdes = NULL;
+
+ /* Queue the signal -- What if this returns an error? */
+
+ sig_mqnotempty(pid, signo, value);
+ }
+
+ /* Check if any tasks are waiting for the MQ not empty event. */
+
+ saved_state = irqsave();
+ if (msgq->nwaitnotempty > 0)
+ {
+ /* Find the highest priority task that is waiting for
+ * this queue to be non-empty in g_waitingformqnotempty
+ * list. sched_lock() should give us sufficent protection since
+ * interrupts should never cause a change in this list
+ */
+
+ for (btcb = (_TCB*)g_waitingformqnotempty.head;
+ btcb && btcb->msgwaitq != msgq;
+ btcb = btcb->flink);
+
+ /* If one was found, unblock it */
+
+ if (!btcb)
+ {
+ PANIC(OSERR_MQNONEMPTYCOUNT);
+ }
+ else
+ {
+ btcb->msgwaitq = NULL;
+ msgq->nwaitnotempty--;
+ up_unblock_task(btcb);
+ }
+ }
+ irqrestore(saved_state);
+ ret = OK;
+ }
+ }
+
+ sched_unlock();
+ return(ret);
+}
+
diff --git a/nuttx/sched/mq_setattr.c b/nuttx/sched/mq_setattr.c
new file mode 100644
index 0000000000..6976886de5
--- /dev/null
+++ b/nuttx/sched/mq_setattr.c
@@ -0,0 +1,115 @@
+/************************************************************
+ * mq_setattr.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <mqueue.h>
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_setattr
+ *
+ * Description:
+ * This function sets the attributes associated with the
+ * specified message queue "mqdes." Only the "O_NONBLOCK"
+ * bit of the "mq_flags" can be changed.
+ *
+ * If "oldstat" is non-null, mq_setattr() will store the
+ * previous message queue attributes at that location (just
+ * as would have been returned by mq_getattr()).
+ *
+ * Parameters:
+ * mqdes - Message queue descriptor
+ * mq_stat - New attributes
+ * oldstate - Old attributes
+ *
+ * Return Value:
+ * 0 (OK) if attributes are set successfully, otherwise
+ * -1 (ERROR).
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int mq_setattr(mqd_t mqdes, const struct mq_attr *mq_stat,
+ struct mq_attr *oldstat)
+{
+ int ret = ERROR;
+
+ if (mqdes && mq_stat)
+ {
+ /* Return the attributes if so requested */
+
+ if (oldstat)
+ {
+ (void)mq_getattr(mqdes, oldstat);
+ }
+
+ /* Set the new value of the O_NONBLOCK flag. */
+
+ mqdes->oflags = ((mq_stat->mq_flags & O_NONBLOCK) |
+ (mqdes->oflags & (~O_NONBLOCK)));
+ ret = OK;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/mq_unlink.c b/nuttx/sched/mq_unlink.c
new file mode 100644
index 0000000000..dc1f951405
--- /dev/null
+++ b/nuttx/sched/mq_unlink.c
@@ -0,0 +1,142 @@
+/************************************************************
+ * mq_unlink.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h> /* uint32, etc. */
+#include <mqueue.h>
+#include <sched.h>
+#include "os_internal.h"
+#include "mq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: mq_unlink
+ *
+ * Description:
+ * This function removes the message queue named by
+ * "mq_name." If one or more tasks have the message queue
+ * open when mq_unlink() is called, removal of the message
+ * queue is postponed until all references to the message
+ * queue have been closed.
+ *
+ * Parameters:
+ * mq_name - Name of the message queue
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int mq_unlink(const char *mq_name)
+{
+ msgq_t *msgq;
+ uint32 saved_state;
+ int ret = ERROR;
+
+ /* Verify the input values */
+
+ if (mq_name)
+ {
+ sched_lock();
+
+ /* Find the named message queue */
+
+ msgq = mq_findnamed(mq_name);
+ if (msgq)
+ {
+ /* If it is no longer connected, then we can just
+ * discard the message queue now.
+ */
+
+ if (!msgq->nconnect)
+ {
+ /* Remove the message queue from the list of all
+ * message queues
+ */
+
+ saved_state = irqsave();
+ (void)sq_rem((sq_entry_t*)msgq, &g_msgqueues);
+ irqrestore(saved_state);
+
+ /* Then deallocate it (and any messages left in it) */
+
+ mq_msgqfree(msgq);
+ }
+
+ /* If the message queue is still connected to a message descriptor,
+ * then mark it for deletion when the last message descriptor is
+ * closed
+ */
+
+ else
+ {
+ msgq->unlinked = TRUE;
+ }
+
+ ret = OK;
+ }
+ sched_unlock();
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/os_internal.h b/nuttx/sched/os_internal.h
new file mode 100644
index 0000000000..0be0bb5ac9
--- /dev/null
+++ b/nuttx/sched/os_internal.h
@@ -0,0 +1,267 @@
+/************************************************************
+ * os_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __OS_INTERNAL_H
+#define __OS_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+#include <sched.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/os_external.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* OS CRASH CODES */
+
+enum os_crash_codes_e
+{
+ OSERR_NOERROR = 0, /* No error */
+ OSERR_NOTIMPLEMENTED, /* Feature is not implemented */
+ OSERR_INTERNAL, /* Internal logic error */
+ OSERR_UNEXPECTEDISR, /* Received unexpected interrupt */
+ OSERR_UNDEFINEDINSN, /* Undefined instruction */
+ OSERR_ERREXCEPTION, /* Other CPU-detected errors */
+ OSERR_OUTOFMEMORY, /* Insufficient memory */
+ OSERR_OUTOFMESSAGES, /* Out of messages */
+ OSERR_NOIDLETASK, /* There is no idle task */
+ OSERR_MQNONEMPTYCOUNT, /* Expected waiter for non-empty queue */
+ OSERR_MQNOTFULLCOUNT, /* Expected waiter for non-full queue */
+ OSERR_BADWAITSEM, /* Already waiting for a semaphore */
+ OSERR_BADMSGTYPE, /* Tried to free a bad message type */
+ OSERR_FAILEDTOADDSIGNAL, /* Failed to add pending signal */
+ OSERR_FAILEDTOREMOVESIGNAL, /* Failed to remove pending signal */
+ OSERR_TIMEOUTNOTCB, /* Timed out, but not TCB registered */
+ OSERR_NOPENDINGSIGNAL, /* Expected a signal to be pending */
+ OSERR_BADDELETESTATE, /* Bad state in task deletion */
+ OSERR_WDOGNOTFOUND, /* Active watchdog not found */
+ OSERR_EXITFROMINTERRUPT, /* Interrupt code attempted to exit */
+ OSERR_BADUNBLOCKSTATE, /* Attempt to unblock from bad state */
+ OSERR_BADBLOCKSTATE, /* Attempt to block from bad state */
+ OSERR_BADREPRIORITIZESTATE, /* Attempt to reprioritize in bad state or priority */
+};
+
+/* Special task IDS */
+
+#define NULL_TASK_PROCESS_ID 0
+#define INVALID_PROCESS_ID 0
+
+/* Although task IDs can take the (positive, non-zero)
+ * range of pid_t, the number of tasks that will be supported
+ * at any one time is (artificially) limited by the following
+ * definition. Limiting the number of tasks speeds certain
+ * OS functions (this is the only limitation in the number of
+ * tasks built into the design).
+ */
+
+#define MAX_TASKS_ALLOWED 64
+#define MAX_TASKS_MASK 0x3f
+#define PIDHASH(pid) ((pid) & MAX_TASKS_MASK)
+
+/* Stubs used when there are no file descriptors */
+
+#if CONFIG_NFILE_DESCRIPTORS <= 0
+# define sched_setupidlefiles(t) (OK)
+# define sched_setuptaskfiles(t) (OK)
+# define sched_setuppthreadfiles(t) (OK)
+# define sched_releasefiles(t) (OK)
+#endif
+
+/* A more efficient ways to access the errno */
+
+#define SET_ERRNO(e) \
+ { _TCB *rtcb = _TCB*)g_readytorun.head; rtcb->errno = (e); }
+
+#define _SET_TCB_ERRNO(t,e) \
+ { (t)->errno = (e); }
+
+/************************************************************
+ * Public Type Definitions
+ ************************************************************/
+
+/* This structure defines the format of the hash table that
+ * is used to (1) determine if a task ID is unique, and (2)
+ * to map a process ID to its corresponding TCB.
+ */
+
+struct pidhash_s
+{
+ _TCB *tcb;
+ pid_t pid;
+};
+typedef struct pidhash_s pidhash_t;
+
+/* This structure defines an element of the g_tasklisttable[].
+ * This table is used to map a task_state enumeration to the
+ * corresponding task list.
+ */
+
+struct tasklist_s
+{
+ dq_queue_t *list; /* Pointer to the task list */
+ boolean prioritized; /* TRUE if the list is prioritized */
+};
+typedef struct tasklist_s tasklist_t;
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* Declared in os_start.c ***********************************/
+
+/* The state of a task is indicated both by the task_state field
+ * of the TCB and by a series of task lists. All of these
+ * tasks lists are declared below. Although it is not always
+ * necessary, most of these lists are prioritized so that common
+ * list handling logic can be used (only the g_readytorun,
+ * the g_pendingtasks, and the g_waitingforsemaphore lists need
+ * to be prioritized).
+ */
+
+/* This is the list of all tasks that are ready to run. The head
+ * of this list is the currently active task; the tail of this
+ * list is always the idle task.
+ */
+
+extern dq_queue_t g_readytorun;
+
+/* This is the list of all tasks that are ready-to-run, but
+ * cannot be placed in the g_readytorun list because: (1) They
+ * are higher priority than the currently active task at the head
+ * of the g_readytorun list, and (2) the currenly active task has
+ * disabled pre-emption.
+ */
+
+extern dq_queue_t g_pendingtasks;
+
+/* This is the list of all tasks that are blocked waiting for a semaphore */
+
+extern dq_queue_t g_waitingforsemaphore;
+
+/* This is the list of all tasks that are blocked waiting for a signal */
+
+extern dq_queue_t g_waitingforsignal;
+
+/* This is the list of all tasks that are blocked waiting for a message
+ * queue to become non-empty.
+ */
+
+extern dq_queue_t g_waitingformqnotempty;
+
+/* This is the list of all tasks that are blocked waiting for a message
+ * queue to become non-full.
+ */
+
+extern dq_queue_t g_waitingformqnotfull;
+
+/* This the list of all tasks that have been initialized, but not yet
+ * activated. NOTE: This is the only list that is not prioritized.
+ */
+
+extern dq_queue_t g_inactivetasks;
+
+/* This is the list of dayed memory deallocations that need to be handled
+ * within the IDLE loop. These deallocations get queued by sched_free()
+ * if the OS attempts to deallocate memory while it is within an interrupt
+ * handler.
+ */
+
+extern sq_queue_t g_delayeddeallocations;
+
+/* This is the value of the last process ID assigned to a task */
+
+extern pid_t g_lastpid;
+
+/* The following hash table is used for two things:
+ * 1. This hash table greatly speeds the determination of
+ * a new unique process ID for a task, and
+ * 2. Is used to quickly map a process ID into a TCB.
+ * It has the side effects of using more memory and limiting
+ * the number of tasks to MAX_TASKS_ALLOWED.
+ */
+
+extern pidhash_t g_pidhash[MAX_TASKS_ALLOWED];
+
+/* This is a table of task lists. This table is indexed by
+ * the task state enumeration type (tstate_t) and provides
+ * a pointer to the associated static task list (if there
+ * is one) as well as a boolean indication as to if the list
+ * is an ordered list or not.
+ */
+
+extern const tasklist_t g_tasklisttable[NUM_TASK_STATES];
+
+/************************************************************
+ * Public Inline Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+extern STATUS _task_init(_TCB *tcb, char *name, int priority,
+ start_t start, main_t main,
+ boolean pthread,
+ char *arg1, char *arg2,
+ char *arg3, char *arg4);
+
+extern boolean sched_addreadytorun(_TCB *rtrtcb);
+extern boolean sched_removereadytorun(_TCB *rtrtcb);
+extern boolean sched_addprioritized(_TCB *newTcb,
+ dq_queue_t *list);
+extern boolean sched_mergepending(void);
+extern void sched_addblocked(_TCB *btcb, tstate_t task_state);
+extern void sched_removeblocked(_TCB *btcb);
+extern _TCB *sched_gettcb(pid_t pid);
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+extern int sched_setupidlefiles(_TCB *tcb);
+extern int sched_setuptaskfiles(_TCB *tcb);
+extern int sched_setuppthreadfiles(_TCB *tcb);
+#if CONFIG_NFILE_STREAMS > 0
+extern int sched_setupstreams(_TCB *tcb);
+extern int sched_flushfiles(_TCB *tcb);
+#endif
+extern int sched_releasefiles(_TCB *tcb);
+#endif
+
+extern int sched_releasetcb(_TCB *tcb);
+
+#endif /* __OS_INTERNAL_H */
diff --git a/nuttx/sched/os_start.c b/nuttx/sched/os_start.c
new file mode 100644
index 0000000000..b4bae75c55
--- /dev/null
+++ b/nuttx/sched/os_start.c
@@ -0,0 +1,382 @@
+/************************************************************
+ * os_start.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <debug.h>
+#include <string.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/fs.h>
+#include <nuttx/lib.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+#include "wd_internal.h"
+#include "sem_internal.h"
+#include "mq_internal.h"
+#include "pthread_internal.h"
+#include "clock_internal.h"
+#include "irq_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* Task Lists ***********************************************/
+/* The state of a task is indicated both by the task_state field
+ * of the TCB and by a series of task lists. All of these
+ * tasks lists are declared below. Although it is not always
+ * necessary, most of these lists are prioritized so that common
+ * list handling logic can be used (only the g_readytorun,
+ * the g_pendingtasks, and the g_waitingforsemaphore lists need
+ * to be prioritized).
+ */
+
+/* This is the list of all tasks that are ready to run. The head
+ * of this list is the currently active task; the tail of this
+ * list is always the idle task.
+ */
+
+dq_queue_t g_readytorun;
+
+/* This is the list of all tasks that are ready-to-run, but
+ * cannot be placed in the g_readytorun list because: (1) They
+ * are higher priority than the currently active task at the head
+ * of the g_readytorun list, and (2) the currenly active task has
+ * disabled pre-emption.
+ */
+
+dq_queue_t g_pendingtasks;
+
+/* This is the list of all tasks that are blocked waiting for a semaphore */
+
+dq_queue_t g_waitingforsemaphore;
+
+/* This is the list of all tasks that are blocked waiting for a signal */
+
+dq_queue_t g_waitingforsignal;
+
+/* This is the list of all tasks that are blocked waiting for a message
+ * queue to become non-empty.
+ */
+
+dq_queue_t g_waitingformqnotempty;
+
+/* This is the list of all tasks that are blocked waiting for a message
+ * queue to become non-full.
+ */
+
+dq_queue_t g_waitingformqnotfull;
+
+/* This the list of all tasks that have been initialized, but not yet
+ * activated. NOTE: This is the only list that is not prioritized.
+ */
+
+dq_queue_t g_inactivetasks;
+
+/* This is the list of dayed memory deallocations that need to be handled
+ * within the IDLE loop. These deallocations get queued by sched_free()
+ * if the OS attempts to deallocate memory while it is within an interrupt
+ * handler.
+ */
+
+sq_queue_t g_delayeddeallocations;
+
+/* This is the value of the last process ID assigned to a task */
+
+pid_t g_lastpid;
+
+/* The following hash table is used for two things:
+ * 1. This hash table greatly speeds the determination of
+ * a new unique process ID for a task, and
+ * 2. Is used to quickly map a process ID into a TCB.
+ * It has the side effects of using more memory and limiting
+ * the number of tasks to MAX_TASKS_ALLOWED.
+ */
+
+pidhash_t g_pidhash[MAX_TASKS_ALLOWED];
+
+/* This is a table of task lists. This table is indexed by
+ * the task state enumeration type (tstate_t) and provides
+ * a pointer to the associated static task list (if there
+ * is one) as well as a boolean indication as to if the list
+ * is an ordered list or not.
+ */
+
+const tasklist_t g_tasklisttable[NUM_TASK_STATES] =
+{
+ { NULL, FALSE }, /* TSTATE_TASK_INVALID */
+ { &g_pendingtasks, TRUE }, /* TSTATE_TASK_PENDING */
+ { &g_readytorun, TRUE }, /* TSTATE_TASK_READYTORUN */
+ { &g_readytorun, TRUE }, /* TSTATE_TASK_RUNNING */
+ { &g_inactivetasks, FALSE }, /* TSTATE_TASK_INACTIVE */
+ { &g_waitingforsemaphore, TRUE }, /* TSTATE_WAIT_SEM */
+ { &g_waitingforsignal, FALSE }, /* TSTATE_WAIT_SIG */
+ { &g_waitingformqnotempty, TRUE }, /* TSTATE_WAIT_MQNOTEMPTY */
+ { &g_waitingformqnotfull, TRUE } /* TSTATE_WAIT_MQNOTFULL */
+};
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+/* This is the task control block for this thread of execution.
+ * This thread of execution is the idle task. NOTE: the
+ * system boots into the idle task. The idle task spawns
+ * the user init task and the user init task is responsible
+ * for bringing up the rest of the system
+ */
+
+static _TCB g_idletcb;
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: os_start
+ * Description: This function is called to initialize the
+ * operating system and to spawn the user init thread of
+ * execution
+ ************************************************************/
+
+void os_start(void)
+{
+ int init_taskid;
+ int i;
+
+ lldbg("os_start:\n");
+
+ /* Initialize all task lists */
+
+ dq_init(&g_readytorun);
+ dq_init(&g_pendingtasks);
+ dq_init(&g_waitingforsemaphore);
+ dq_init(&g_waitingforsignal);
+ dq_init(&g_waitingformqnotfull);
+ dq_init(&g_waitingformqnotempty);
+ dq_init(&g_inactivetasks);
+ sq_init(&g_delayeddeallocations);
+
+ /* Initialize the logic that determine unique process IDs. */
+
+ g_lastpid = 0;
+ for (i = 0; i < MAX_TASKS_ALLOWED; i++)
+ {
+ g_pidhash[i].tcb = NULL;
+ g_pidhash[i].pid = INVALID_PROCESS_ID;
+ }
+
+ /* Assign the process ID of ZERO to the idle task */
+
+ g_pidhash[ PIDHASH(0)].tcb = &g_idletcb;
+ g_pidhash[ PIDHASH(0)].pid = 0;
+
+ /* Initialize a TCB for this thread of execution. NOTE: The default
+ * value for most components of the g_idletcb are zero. The entire
+ * structure is set to zero. Then only the (potentially) non-zero
+ * elements are initialized. NOTE: The idle task is the only task in
+ * that has pid == 0 and sched_priority == 0.
+ */
+
+ bzero((void*)&g_idletcb, sizeof(_TCB));
+ g_idletcb.task_state = TSTATE_TASK_RUNNING;
+ g_idletcb.entry.main = (main_t)os_start;
+
+#if CONFIG_TASK_NAME_SIZE > 0
+ strncpy(g_idletcb.name, "Idle Task", CONFIG_TASK_NAME_SIZE-1);
+ g_idletcb.argv[0] = g_idletcb.name;
+#else
+ g_idletcb.argv[0] = "Idle Task";
+#endif /* CONFIG_TASK_NAME_SIZE */
+
+ /* Then add the idle task's TCB to the head of the ready to run list */
+
+ dq_addfirst((dq_entry_t*)&g_idletcb, &g_readytorun);
+
+ /* Initialize the processor-specific portion of the TCB */
+
+ up_initial_state(&g_idletcb);
+
+ /* Initialize the memory manager */
+
+#ifndef CONFIG_HEAP_BASE
+ {
+ void *heap_start;
+ size_t heap_size;
+ up_allocate_heap(&heap_start, &heap_size);
+ mm_initialize(heap_start, heap_size);
+ }
+#else
+ mm_initialize((void*)CONFIG_HEAP_BASE, CONFIG_HEAP_SIZE);
+#endif
+
+ /* Initialize the interrupt handling subsystem (if included) */
+
+ if (irq_initialize != NULL)
+ {
+ irq_initialize();
+ }
+
+ /* Provide an access point to initialize any user-specific logic very
+ * early in the initialization sequence. Note that user_ininitialize()
+ * is called only if it is provided in the link.
+ */
+
+ if (user_initialize != NULL)
+ {
+ user_initialize();
+ }
+
+ /* Initialize the POSIX timer facility (if included in the link) */
+
+ if (clock_initialize != NULL)
+ {
+ clock_initialize();
+ }
+
+ /* Initialize the watchdog facility (if included in the link) */
+
+ if (wd_initialize != NULL)
+ {
+ wd_initialize();
+ }
+
+ /* Initialize the signal facility (if in link) */
+
+ if (sig_initialize != NULL)
+ {
+ sig_initialize();
+ }
+
+ /* Initialize the semaphore facility. (if in link) */
+
+ if (sem_initialize != NULL)
+ {
+ sem_initialize();
+ }
+
+ /* Initialize the named message queue facility (if in link) */
+
+ if (mq_initialize != NULL)
+ {
+ mq_initialize();
+ }
+
+ /* Initialize the thread-specific data facility (if in link) */
+
+ if (pthread_initialize != NULL)
+ {
+ pthread_initialize();
+ }
+
+ /* Initialize the file system (needed to support device drivers) */
+
+ if (fs_initialize != NULL)
+ {
+ fs_initialize();
+ }
+
+ /* The processor specific details of running the operating system
+ * will be handled here. Such things as setting up interrupt
+ * service routines and starting the clock are some of the things
+ * that are different for each processor and hardware platform.
+ */
+
+ up_initialize();
+
+ /* Initialize the C libraries (if included in the link). This
+ * is done last because the libraries may depend on the above.
+ */
+
+ if (lib_initialize != NULL)
+ {
+ lib_initialize();
+ }
+
+ /* Create stdout, stderr, stdin */
+
+ (void)sched_setupidlefiles(&g_idletcb);
+
+ /* Once the operating system has been initialized, the system must be
+ * started by spawning the user init thread of execution.
+ */
+
+ dbg("os_start: Starting init thread\n");
+ init_taskid = task_create("init", SCHED_PRIORITY_DEFAULT,
+ CONFIG_PROC_STACK_SIZE,
+ (main_t)user_start, 0, 0, 0, 0);
+ ASSERT(init_taskid != ERROR);
+
+ /* When control is return to this point, the system is idle. */
+
+ dbg("os_start: Beginning Idle Loop\n");
+ for (;;)
+ {
+ /* Check if there is anything in the delayed deallocation list. */
+
+ while (g_delayeddeallocations.head)
+ {
+ /* Remove the first delayed deallocation. */
+
+ uint32 savedState = irqsave();
+ void *address = (void*)sq_remfirst(&g_delayeddeallocations);
+ irqrestore(savedState);
+
+ /* Then deallocate it */
+
+ if (address) sched_free(address);
+ }
+
+ /* Perform idle state operations */
+
+ up_idle();
+ }
+}
diff --git a/nuttx/sched/pthread_attrdestroy.c b/nuttx/sched/pthread_attrdestroy.c
new file mode 100644
index 0000000000..9bb86268c6
--- /dev/null
+++ b/nuttx/sched/pthread_attrdestroy.c
@@ -0,0 +1,108 @@
+/************************************************************
+ * pthread_attrdestroy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_destroy
+ *
+ * Description:
+ * An attributes object can be deleted when it is no longer
+ * needed.
+ *
+ * Parameters:
+ * attr
+ *
+ * Return Value:
+ * 0 meaning success
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_destroy(pthread_attr_t *attr)
+{
+ int ret;
+
+ dbg("%s: attr=0x%p\n", __FUNCTION__, attr);
+
+ if (!attr)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ memset(attr, 0, sizeof(pthread_attr_t));
+ ret = OK;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
diff --git a/nuttx/sched/pthread_attrgetinheritsched.c b/nuttx/sched/pthread_attrgetinheritsched.c
new file mode 100644
index 0000000000..d0d3143e75
--- /dev/null
+++ b/nuttx/sched/pthread_attrgetinheritsched.c
@@ -0,0 +1,112 @@
+/************************************************************
+ * pthread_attrgetinheritsched.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_getinheritsched
+ *
+ * Description:
+ * Report whether the scheduling info in the pthread
+ * attributes will be used or if the thread will
+ * inherit the properties of the parent.
+ *
+ * Parameters:
+ * attr
+ * inheritsched
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_getinheritsched(const pthread_attr_t *attr,
+ int *inheritsched)
+{
+ int ret;
+
+ dbg("%s: attr=0x%p inheritsched=0x%p\n",
+ __FUNCTION__, attr, inheritsched);
+
+ if (!attr || !inheritsched)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ *inheritsched = (int)attr->inheritsched;
+ ret = OK;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
diff --git a/nuttx/sched/pthread_attrgetschedparam.c b/nuttx/sched/pthread_attrgetschedparam.c
new file mode 100644
index 0000000000..2285895e7f
--- /dev/null
+++ b/nuttx/sched/pthread_attrgetschedparam.c
@@ -0,0 +1,109 @@
+/************************************************************
+ * pthread_attrgetschedparam.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_getschedparam
+ *
+ * Description:
+ *
+ * Parameters:
+ * attr
+ * param
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_getschedparam(pthread_attr_t *attr,
+ struct sched_param *param)
+{
+ int ret;
+
+ dbg("%s: attr=0x%p param=0x%p\n", __FUNCTION__, attr, param);
+
+ if (!attr || !param)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ param->sched_priority = attr->priority;
+ ret = OK;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
+
diff --git a/nuttx/sched/pthread_attrgetschedpolicy.c b/nuttx/sched/pthread_attrgetschedpolicy.c
new file mode 100644
index 0000000000..801226c982
--- /dev/null
+++ b/nuttx/sched/pthread_attrgetschedpolicy.c
@@ -0,0 +1,106 @@
+/************************************************************
+ * pthread_attrgetschedpolicy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_getschedpolicy
+ *
+ * Description:
+ * Obtain the scheduling algorithm attribute.
+ *
+ * Parameters:
+ * attr
+ * policy
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy)
+{
+ int ret;
+
+ dbg("%s: attr=0x%p policy=0x%p\n", __FUNCTION__, attr, policy);
+
+ if (!attr || !policy)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ *policy = attr->policy;
+ ret = OK;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_attrgetstacksize.c b/nuttx/sched/pthread_attrgetstacksize.c
new file mode 100644
index 0000000000..d36ae172e8
--- /dev/null
+++ b/nuttx/sched/pthread_attrgetstacksize.c
@@ -0,0 +1,107 @@
+/************************************************************
+ * pthread_attrgetstacksize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_getstacksize
+ *
+ * Description:
+ *
+ * Parameters:
+ * attr
+ * stacksize
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_getstacksize(pthread_attr_t *attr, long *stacksize)
+{
+ int ret;
+
+ dbg("%s: attr=0x%p stacksize=0x%p\n", __FUNCTION__, attr, stacksize);
+
+ if (!stacksize)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ *stacksize = attr->stacksize;
+ ret = OK;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
diff --git a/nuttx/sched/pthread_attrinit.c b/nuttx/sched/pthread_attrinit.c
new file mode 100644
index 0000000000..51d2ea5250
--- /dev/null
+++ b/nuttx/sched/pthread_attrinit.c
@@ -0,0 +1,111 @@
+/************************************************************
+ * pthread_attrinit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_init
+ *
+ * Description:
+ * Initializes a thread attributes object (attr) with
+ * default values for all of the individual attributes
+ * used by a given implementation.
+ *
+ * Parameters:
+ * attr
+ *
+ * Return Value:
+ * 0 on success, otherwise an error number
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_init(pthread_attr_t *attr)
+{
+ int ret = OK;
+
+ dbg("%s: attr=0x%p\n", __FUNCTION__, attr);
+ if (!attr)
+ {
+ ret = ENOMEM;
+ }
+ else
+ {
+ /* Set the child thread priority to be the default
+ * priority. Set the child stack size to some arbitrary
+ * default value.
+ */
+
+ memcpy(attr, &g_default_pthread_attr, sizeof(pthread_attr_t));
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
diff --git a/nuttx/sched/pthread_attrsetinheritsched.c b/nuttx/sched/pthread_attrsetinheritsched.c
new file mode 100644
index 0000000000..e83d13d73d
--- /dev/null
+++ b/nuttx/sched/pthread_attrsetinheritsched.c
@@ -0,0 +1,112 @@
+/************************************************************
+ * pthread_attrsetinheritsched.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_setinheritsched
+ *
+ * Description:
+ * Indicate whether the scheduling info in the pthread
+ * attributes will be used or if the thread will
+ * inherit the properties of the parent.
+ *
+ * Parameters:
+ * attr
+ * policy
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_setinheritsched(pthread_attr_t *attr,
+ int inheritsched)
+{
+ int ret;
+
+ dbg("%s: inheritsched=%d\n", __FUNCTION__, inheritsched);
+
+ if (!attr ||
+ (inheritsched != PTHREAD_INHERIT_SCHED &&
+ inheritsched != PTHREAD_EXPLICIT_SCHED))
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ attr->inheritsched = (ubyte)inheritsched;
+ ret = OK;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
diff --git a/nuttx/sched/pthread_attrsetschedparam.c b/nuttx/sched/pthread_attrsetschedparam.c
new file mode 100644
index 0000000000..4a0d983483
--- /dev/null
+++ b/nuttx/sched/pthread_attrsetschedparam.c
@@ -0,0 +1,107 @@
+/************************************************************
+ * pthread_attrsetschedparam.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_setschedparam
+ *
+ * Description:
+ *
+ * Parameters:
+ * attr
+ * param
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_setschedparam(pthread_attr_t *attr,
+ const struct sched_param *param)
+{
+ int ret;
+
+ dbg("%s: attr=0x%p param=0x%p\n", __FUNCTION__, attr, param);
+
+ if (!attr || !param)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ attr->priority = (short)param->sched_priority;
+ ret = OK;
+ }
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
diff --git a/nuttx/sched/pthread_attrsetschedpolicy.c b/nuttx/sched/pthread_attrsetschedpolicy.c
new file mode 100644
index 0000000000..d750cf6fd4
--- /dev/null
+++ b/nuttx/sched/pthread_attrsetschedpolicy.c
@@ -0,0 +1,110 @@
+/************************************************************
+ * pthread_attrsetschedpolicy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_setschedpolicy
+ *
+ * Description:
+ * Set the scheduling algorithm attribute.
+ *
+ * Parameters:
+ * attr
+ * policy
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
+{
+ int ret;
+
+ dbg("%s: attr=0x%p policy=%d\n", __FUNCTION__, attr, policy);
+
+#if CONFIG_RR_INTERVAL > 0
+ if (!attr || (policy != SCHED_FIFO && policy != SCHED_RR))
+#else
+ if (!attr || policy != SCHED_FIFO )
+#endif
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ attr->policy = policy;
+ ret = OK;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_attrsetstacksize.c b/nuttx/sched/pthread_attrsetstacksize.c
new file mode 100644
index 0000000000..b33b75e635
--- /dev/null
+++ b/nuttx/sched/pthread_attrsetstacksize.c
@@ -0,0 +1,107 @@
+/************************************************************
+ * pthread_attrsetstacksize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_attr_setstacksize
+ *
+ * Description:
+ *
+ * Parameters:
+ * attr
+ * stacksize
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_attr_setstacksize(pthread_attr_t *attr, long stacksize)
+{
+ int ret;
+
+ dbg("%s: attr=0x%p stacksize=%ld\n",
+ __FUNCTION__, attr, stacksize);
+
+ if (!attr || stacksize < PTHREAD_STACK_MIN)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ attr->stacksize = stacksize;
+ ret = OK;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
diff --git a/nuttx/sched/pthread_cancel.c b/nuttx/sched/pthread_cancel.c
new file mode 100644
index 0000000000..d34c29ae3d
--- /dev/null
+++ b/nuttx/sched/pthread_cancel.c
@@ -0,0 +1,143 @@
+/**************************************************************************
+ * pthread_cancel.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **************************************************************************/
+
+/**************************************************************************
+ * Included Files
+ **************************************************************************/
+
+#include <unistd.h>
+#include <pthread.h>
+#include <errno.h>
+#include "os_internal.h"
+#include "pthread_internal.h"
+
+/**************************************************************************
+ * Private Definitions
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Types
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Function Prototypes
+ **************************************************************************/
+
+/**************************************************************************
+ * Global Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Functions
+ **************************************************************************/
+
+/**************************************************************************
+ * Public Functions
+ **************************************************************************/
+
+int pthread_cancel(pthread_t thread)
+{
+ _TCB *tcb;
+
+ /* First, make sure that the handle references a valid thread */
+
+ if (!thread.pid)
+ {
+ /* pid == 0 is the IDLE task. Callers cannot cancel the
+ * IDLE task.
+ */
+
+ return ESRCH;
+ }
+
+ tcb = sched_gettcb(thread.pid);
+ if (!tcb)
+ {
+ /* The pid does not correspond to any known thread */
+
+ return ESRCH;
+ }
+
+ /* Check to see if this thread has the non-cancelable bit set in its
+ * flags. Suppress context changes for a bit so that the flags are stable.
+ * (the flags should not change in interrupt handling.
+ */
+
+ sched_lock();
+ if ((tcb->flags & TCB_FLAG_NONCANCELABLE) != 0)
+ {
+ /* Then we cannot cancel the thread now. Here is how this is
+ * supposed to work:
+ *
+ * "When cancelability is disabled, all cancels are held pending
+ * in the target thread until the thread changes the cancelability.
+ * When cancelability is deferred, all cancels are held pending in
+ * the target thread until the thread changes the cancelability, calls
+ * a function which is a cancellation point or calls pthread_testcancel(),
+ * thus creating a cancellation point. When cancelability is asynchronous,
+ * all cancels are acted upon immediately, interrupting the thread with its
+ * processing."
+ */
+
+ tcb->flags |= TCB_FLAG_CANCEL_PENDING;
+ sched_unlock();
+ return OK;
+ }
+ sched_unlock();
+
+ /* Check to see if the ID refers to ourselves.. this would be the
+ * same as pthread_exit(PTHREAD_CANCELED).
+ */
+
+ if (tcb == (_TCB*)g_readytorun.head)
+ {
+ pthread_exit(PTHREAD_CANCELED);
+ }
+
+ /* Complete pending join operations */
+
+ (void)pthread_completejoin(thread.pid, PTHREAD_CANCELED);
+
+ /* Then let pthread_delete do the real work */
+
+ task_delete(thread.pid);
+ return OK;
+}
+
+
diff --git a/nuttx/sched/pthread_completejoin.c b/nuttx/sched/pthread_completejoin.c
new file mode 100644
index 0000000000..a5e7651e01
--- /dev/null
+++ b/nuttx/sched/pthread_completejoin.c
@@ -0,0 +1,211 @@
+/************************************************************
+ * pthread_completejoin.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include "os_internal.h"
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_destroyjoininfo
+ *
+ * Description:
+ * Destroy a join_t structure. This must
+ * be done by the child thread at child thread destruction
+ * time.
+ *
+ ************************************************************/
+
+static void pthread_destroyjoininfo(join_t *pjoin)
+{
+ int ntasks_waiting;
+ int status;
+
+ dbg("%s: pjoin=0x%p\n", __FUNCTION__, pjoin);
+
+ /* Are any tasks waiting for our exit value? */
+
+ status = sem_getvalue(&pjoin->exit_sem, &ntasks_waiting);
+ if (status == OK && ntasks_waiting < 0)
+ {
+ /* Set the data semaphore so that this thread will be
+ * awakened when all waiting tasks receive the data
+ */
+
+ (void)sem_init(&pjoin->data_sem, 0, (ntasks_waiting+1));
+
+ /* Post the semaphore to restart each thread that is waiting
+ * on the semaphore
+ */
+
+ do
+ {
+ status = pthread_givesemaphore(&pjoin->exit_sem);
+ if (status == OK)
+ {
+ status = sem_getvalue(&pjoin->exit_sem, &ntasks_waiting);
+ }
+ }
+ while (ntasks_waiting < 0 && status == OK);
+
+ /* Now wait for all these restarted tasks to obtain the return
+ * value.
+ */
+
+ (void)pthread_takesemaphore(&pjoin->data_sem);
+ (void)sem_destroy(&pjoin->data_sem);
+ }
+
+ /* All of the joined threads have had received the exit value.
+ * Now we can destroy this thread's exit semaphore
+ */
+
+ (void)sem_destroy(&pjoin->exit_sem);
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_completejoin
+ *
+ * Description:
+ * A thread has been terminated -- either by returning,
+ * calling pthread_exit(), or through pthread_cancel().
+ * In any event, we must complete any pending join events.
+ *
+ * Parameters:
+ * exit_value
+ *
+ * Returned Value:
+ * OK unless there is no join information associated with
+ * the pid. This could happen, for example, if a task
+ * started with task_create() calls pthread_exit().
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_completejoin(pid_t pid, void *exit_value)
+{
+ join_t *pjoin;
+ boolean detached = FALSE;
+
+ dbg("%s: process_id=%d exit_value=%p\n", __FUNCTION__, pid, exit_value);
+
+ /* First, find thread's structure in the private data set. */
+
+ (void)pthread_takesemaphore(&g_join_semaphore);
+ pjoin = pthread_findjoininfo(pid);
+ if (!pjoin)
+ {
+
+ (void)pthread_givesemaphore(&g_join_semaphore);
+ return ERROR;
+ }
+ else
+ {
+ /* Has the thread been marked as detached? */
+
+ pjoin->terminated = TRUE;
+ detached = pjoin->detached;
+ if (detached)
+ {
+ dbg("%s: Detaching\n", __FUNCTION__);
+
+ /* If so, then remove the thread's structure from the private
+ * data set. After this point, no other thread can perform a join
+ * operation.
+ */
+
+ (void)pthread_removejoininfo(pid);
+ (void)pthread_givesemaphore(&g_join_semaphore);
+
+ /* Destroy this thread data structure. */
+
+ pthread_destroyjoininfo(pjoin);
+
+ /* Deallocate the join entry if it was detached. */
+
+ sched_free((void*)pjoin);
+ }
+
+ /* No, then we can assume that some other thread is waiting for the join info */
+
+ else
+ {
+ /* Save the return exit value in the thread structure. */
+
+ pjoin->exit_value = exit_value;
+
+ /* Destroy this thread data structure. */
+
+ pthread_destroyjoininfo(pjoin);
+
+ /* pthread_join may now access the thread entry structure. */
+
+ (void)pthread_givesemaphore(&g_join_semaphore);
+ }
+ }
+
+ return OK;
+}
diff --git a/nuttx/sched/pthread_condattrdestroy.c b/nuttx/sched/pthread_condattrdestroy.c
new file mode 100644
index 0000000000..a12c3f57c0
--- /dev/null
+++ b/nuttx/sched/pthread_condattrdestroy.c
@@ -0,0 +1,82 @@
+/************************************************************
+ * pthread_condattrdestroy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_condattr_destroy
+ *
+ * Description:
+ * Operations on condition variable attributes
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_condattr_destroy(pthread_condattr_t *attr)
+{
+ int ret = OK;
+
+ dbg("%s: attr=0x%p\n", __FUNCTION__, attr);
+
+ if (!attr)
+ {
+ ret = EINVAL;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
+
diff --git a/nuttx/sched/pthread_condattrinit.c b/nuttx/sched/pthread_condattrinit.c
new file mode 100644
index 0000000000..739b703ff0
--- /dev/null
+++ b/nuttx/sched/pthread_condattrinit.c
@@ -0,0 +1,85 @@
+/************************************************************
+ * pthread_condattrinit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_condattr_init
+ *
+ * Description:
+ * Operations on condition variable attributes
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_condattr_init(pthread_condattr_t *attr)
+{
+ int ret = OK;
+
+ dbg("%s: attr=0x%p\n", __FUNCTION__, attr);
+
+ if (!attr)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ *attr = 0;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
diff --git a/nuttx/sched/pthread_condbroadcast.c b/nuttx/sched/pthread_condbroadcast.c
new file mode 100644
index 0000000000..322a4d288b
--- /dev/null
+++ b/nuttx/sched/pthread_condbroadcast.c
@@ -0,0 +1,142 @@
+/************************************************************
+ * pthread_condbroadcast.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_cond_broadcast
+ *
+ * Description:
+ * A thread broadcast on a condition variable.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_cond_broadcast(pthread_cond_t *cond)
+{
+ int ret = OK;
+ int sval;
+
+ dbg("%s: cond=0x%p\n", __FUNCTION__, cond);
+
+ if (!cond)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ /* Disable pre-emption until all of the waiting threads have
+ * been restarted. This is necessary to assure that the sval
+ * behaves as expected in the following while loop
+ */
+
+ sched_lock();
+
+ /* Get the current value of the semaphore */
+
+ if (sem_getvalue((sem_t*)&cond->sem, &sval) != OK)
+ ret = EINVAL;
+
+ else
+ {
+ /* Loop until all of the waiting threads have been restarted. */
+
+ while (sval < 0)
+ {
+ /* If the value is less than zero (meaning that one or more
+ * thread is waiting), then post the condition semaphore.
+ * Only the highest priority waiting thread will get to execute
+ */
+
+ ret = pthread_givesemaphore((sem_t*)&cond->sem);
+
+ /* Increment the semaphore count (as was done by the
+ * above post).
+ */
+
+ sval++;
+ }
+ }
+
+ /* Now we can let the restarted threads run */
+
+ sched_unlock();
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
diff --git a/nuttx/sched/pthread_conddestroy.c b/nuttx/sched/pthread_conddestroy.c
new file mode 100644
index 0000000000..5272008570
--- /dev/null
+++ b/nuttx/sched/pthread_conddestroy.c
@@ -0,0 +1,87 @@
+/************************************************************
+ * pthread_conddestroy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_cond_destroy
+ *
+ * Description:
+ * A thread can delete condition variables.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_cond_destroy(pthread_cond_t *cond)
+{
+ int ret = OK;
+
+ dbg("%s: cond=0x%p\n", __FUNCTION__, cond);
+
+ if (!cond)
+ {
+ ret = EINVAL;
+ }
+
+ /* Destroy the semaphore contained in the structure */
+
+ else if (sem_destroy((sem_t*)&cond->sem) != OK)
+ {
+ ret = EINVAL;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
diff --git a/nuttx/sched/pthread_condinit.c b/nuttx/sched/pthread_condinit.c
new file mode 100644
index 0000000000..9dac4692fe
--- /dev/null
+++ b/nuttx/sched/pthread_condinit.c
@@ -0,0 +1,91 @@
+/************************************************************
+ * pthread_condinit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <debug.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Global Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_cond_init
+ *
+ * Description:
+ * A thread can create condition variables.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr)
+{
+ int ret = OK;
+
+ dbg("%s: cond=0x%p attr=0x%p\n", __FUNCTION__, cond, attr);
+
+ if (!cond)
+ {
+ ret = EINVAL;
+ }
+
+ /* Initialize the semaphore contained in the condition structure
+ * with initial count = 0
+ */
+
+ else if (sem_init((sem_t*)&cond->sem, 0, 0) != OK)
+ {
+ ret = EINVAL;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
+
diff --git a/nuttx/sched/pthread_condsignal.c b/nuttx/sched/pthread_condsignal.c
new file mode 100644
index 0000000000..f9ee3781d2
--- /dev/null
+++ b/nuttx/sched/pthread_condsignal.c
@@ -0,0 +1,126 @@
+/************************************************************
+ * pthread_condsignal.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_cond_signal
+ *
+ * Description:
+ * A thread can signal on a condition variable.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_cond_signal(pthread_cond_t *cond)
+{
+ int ret = OK;
+ int sval;
+
+ dbg("%s: cond=0x%p\n", __FUNCTION__, cond);
+
+ if (!cond)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ /* Get the current value of the semaphore */
+
+ if (sem_getvalue((sem_t*)&cond->sem, &sval) != OK)
+ {
+ ret = EINVAL;
+ }
+
+ /* If the value is less than zero (meaning that one or more
+ * thread is waiting), then post the condition semaphore.
+ * Only the highest priority waiting thread will get to execute
+ */
+
+ else
+ {
+ dbg("%s: sval=%d\n", __FUNCTION__, sval);
+ if (sval < 0)
+ {
+ dbg("%s: Signalling...\n", __FUNCTION__);
+ ret = pthread_givesemaphore((sem_t*)&cond->sem);
+ }
+ }
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
diff --git a/nuttx/sched/pthread_condtimedwait.c b/nuttx/sched/pthread_condtimedwait.c
new file mode 100644
index 0000000000..f651226998
--- /dev/null
+++ b/nuttx/sched/pthread_condtimedwait.c
@@ -0,0 +1,306 @@
+/************************************************************
+ * pthread_condtimedwait.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <signal.h>
+#include <time.h>
+#include <errno.h>
+#include <wdog.h>
+#include <debug.h>
+#include "os_internal.h"
+#include "pthread_internal.h"
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+#define ECHO_COND_WAIT_SIGNO 3
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static void pthread_condtimedout(int pid, int signo, int arg3, int arg4)
+{
+ union sigval value;
+
+ /* Send the specified signal to the specified task. */
+
+ value.sival_ptr = 0;
+ (void)sigqueue(pid, signo, value);
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_cond_timedwait
+ *
+ * Description:
+ * A thread can perform a timed wait on a condition variable.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ * Timing is of resolution 1 msec, with +/-1 millisecond
+ * accuracy.
+ *
+ ************************************************************/
+
+int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
+ const struct timespec *abstime)
+{
+ struct timespec currtime;
+ struct timespec reltime;
+ WDOG_ID wdog;
+ sint32 relusec;
+ sint32 ticks;
+ int mypid = (int)getpid();
+ int ret = OK;
+ int int_state;
+ int status;
+
+ dbg("%s: cond=0x%p mutex=0x%p abstime=0x%p\n",
+ __FUNCTION__, cond, mutex, abstime);
+
+ /* Make sure that non-NULL references were provided. */
+
+ if (!cond || !mutex)
+ {
+ ret = EINVAL;
+ }
+
+ /* Make sure that the caller holds the mutex */
+
+ else if (mutex->pid != mypid)
+ {
+ ret = EPERM;
+ }
+
+ /* If no wait time is provided, this function degenerates to
+ * the same behavior as pthread_cond_wait().
+ */
+
+ else if (!abstime)
+ {
+ ret = pthread_cond_wait(cond, mutex);
+ }
+
+ else
+ {
+ /* Create a watchdog */
+
+ wdog = wd_create();
+ if (!wdog)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ dbg("%s: Give up mutex...\n", __FUNCTION__);
+
+ /* We must disable pre-emption and interrupts here so that
+ * the time stays valid until the wait begins. This adds
+ * complexity because we assure that interrupts and
+ * pre-emption are re-enabled correctly.
+ */
+
+ sched_lock();
+ int_state = irqsave();
+
+ /* Convert the timespec to clock ticks. We must disable pre-emption
+ * here so that this time stays valid until the wait begins.
+ * NOTE: Here we use internal knowledge that CLOCK_REALTIME is
+ * defined to be zero!
+ */
+
+ ret = clock_gettime(0, &currtime);
+ if (ret)
+ {
+ /* Restore interrupts (pre-emption will be enabled when
+ * we fall through the if/then/else
+ */
+
+ irqrestore(int_state);
+ }
+ else
+ {
+ /* The relative time to wait is the absolute time minus the
+ * the current time.
+ */
+
+ reltime.tv_nsec = (abstime->tv_nsec - currtime.tv_nsec);
+ reltime.tv_sec = (abstime->tv_sec - currtime.tv_sec);
+
+ /* Check if we were supposed to borrow from the seconds
+ * to borrow from the seconds
+ */
+
+ if (reltime.tv_nsec < 0)
+ {
+ reltime.tv_nsec += NSEC_PER_SEC;
+ reltime.tv_sec -= 1;
+ }
+
+ /* Convert this relative time into microseconds.*/
+
+ relusec =
+ reltime.tv_sec * USEC_PER_SEC +
+ reltime.tv_nsec / NSEC_PER_USEC;
+
+ /* Convert microseconds to clock ticks */
+
+ ticks = relusec / USEC_PER_TICK;
+
+ /* Check the absolute time to wait. If it is now or in the past, then
+ * just return with the timedout condition.
+ */
+
+ if (ticks <= 0)
+ {
+ /* Restore interrupts and indicate that we have already timed out.
+ * (pre-emption will be enabled when we fall through the
+ * if/then/else
+ */
+
+ irqrestore(int_state);
+ ret = ETIMEDOUT;
+ }
+ else
+ {
+ /* Give up the mutex */
+
+ mutex->pid = 0;
+ ret = pthread_givesemaphore((sem_t*)&mutex->sem);
+ if (ret)
+ {
+ /* Restore interrupts (pre-emption will be enabled when
+ * we fall through the if/then/else)
+ */
+
+ irqrestore(int_state);
+ }
+ else
+ {
+ /* Start the watchdog */
+
+ wd_start(wdog, ticks, (wdentry_t)pthread_condtimedout,
+ mypid, ECHO_COND_WAIT_SIGNO, 0, 0);
+
+ /* Take the condition semaphore. Do not restore interrupts
+ * until we return from the wait. This is necessary to
+ * make sure that the watchdog timer and the condition wait
+ * are started atomically.
+ */
+
+ status = sem_wait((sem_t*)&cond->sem);
+ irqrestore(int_state);
+
+ /* Did we get the condition semaphore. */
+
+ if (status != OK)
+ {
+ /* NO.. Handle the special case where the semaphore wait was
+ * awakened by the receipt of a signal -- presumably the
+ * signal posted by pthread_condtimedout().
+ */
+
+ if (*get_errno_ptr() == EINTR)
+ {
+ dbg("%s: Timedout!\n", __FUNCTION__);
+ ret = ETIMEDOUT;
+ }
+ else
+ {
+ ret = EINVAL;
+ }
+ }
+ }
+
+ /* Reacquire the mutex (retaining the ret). */
+
+ dbg("%s: Re-locking...\n", __FUNCTION__);
+ status = pthread_takesemaphore((sem_t*)&mutex->sem);
+ if (!status)
+ {
+ mutex->pid = mypid;
+ }
+ else if (!ret)
+ {
+ ret = status;
+ }
+ }
+
+ /* Re-enable pre-emption (It is expected that interrupts
+ * have already been re-enabled in the above logic)
+ */
+
+ sched_unlock();
+ }
+
+ /* We no longer need the watchdog */
+
+ wd_delete(wdog);
+ }
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
diff --git a/nuttx/sched/pthread_condwait.c b/nuttx/sched/pthread_condwait.c
new file mode 100644
index 0000000000..555d3d06ff
--- /dev/null
+++ b/nuttx/sched/pthread_condwait.c
@@ -0,0 +1,137 @@
+/************************************************************
+ * pthread_condwait.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: int pthread_cond_wait
+ *
+ * Description:
+ * A thread can wait for a condition variable to be
+ * signalled or broadcast.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
+{
+ int ret;
+
+ dbg("%s: cond=0x%p mutex=0x%p\n", __FUNCTION__, cond, mutex);
+
+ /* Make sure that non-NULL references were provided. */
+
+ if (!cond || !mutex)
+ {
+ ret = EINVAL;
+ }
+
+ /* Make sure that the caller holds the mutex */
+
+ else if (mutex->pid != (int)getpid())
+ {
+ ret = EPERM;
+ }
+
+ else
+ {
+ /* Give up the mutex */
+
+ dbg("%s: Give up mutex / take cond\n", __FUNCTION__);
+
+ sched_lock();
+ mutex->pid = 0;
+ ret = pthread_givesemaphore((sem_t*)&mutex->sem);
+
+ /* Take the semaphore */
+
+ ret |= pthread_takesemaphore((sem_t*)&cond->sem);
+ sched_unlock();
+
+ /* Reacquire the mutex */
+
+ dbg("%s: Reacquire mutex...\n", __FUNCTION__);
+ ret |= pthread_takesemaphore((sem_t*)&mutex->sem);
+ if (!ret)
+ {
+ mutex->pid = getpid();;
+ }
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
diff --git a/nuttx/sched/pthread_create.c b/nuttx/sched/pthread_create.c
new file mode 100644
index 0000000000..c8ec604d1a
--- /dev/null
+++ b/nuttx/sched/pthread_create.c
@@ -0,0 +1,342 @@
+/************************************************************
+ * pthread_create.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <pthread.h>
+#include <sched.h>
+#include <debug.h>
+#include <errno.h>
+#include <queue.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* Default pthread attributes */
+
+pthread_attr_t g_default_pthread_attr =
+{
+ .stacksize = PTHREAD_STACK_DEFAULT,
+ .priority = PTHREAD_DEFAULT_PRIORITY,
+ .policy = SCHED_RR,
+ .inheritsched = PTHREAD_EXPLICIT_SCHED,
+};
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_addjoininfo
+ *
+ * Description:
+ * Add a join_t to the local data set.
+ *
+ * Parameters:
+ * pjoin
+ *
+ * Return Value:
+ * None or pointer to the found entry.
+ *
+ * Assumptions:
+ * The caller has provided protection from re-entrancy.
+ *
+ ************************************************************/
+
+static void pthread_addjoininfo(join_t *pjoin)
+{
+ pjoin->next = NULL;
+ if (!g_pthread_tail)
+ {
+ g_pthread_head = pjoin;
+ }
+ else
+ {
+ g_pthread_tail->next = pjoin;
+ }
+ g_pthread_tail = pjoin;
+}
+
+/************************************************************
+ * Name: pthread_start
+ *
+ * Description:
+ * This function is the low level entry point into the
+ * pthread
+ *
+ * Parameters:
+ * None
+ *
+ ************************************************************/
+
+static void pthread_start(void)
+{
+ _TCB *ptcb = (_TCB*)g_readytorun.head;
+ join_t *pjoin = (join_t*)ptcb->joininfo;
+ pthread_addr_t exit_status;
+
+ /* Sucessfully spawned, add the pjoin to our data set.
+ * Don't re-enable pre-emption until this is done.
+ */
+
+ (void)pthread_takesemaphore(&g_join_semaphore);
+ pthread_addjoininfo(pjoin);
+ (void)pthread_givesemaphore(&g_join_semaphore);
+
+ /* Report to the spawner that we successfully started. */
+
+ pjoin->started = TRUE;
+ (void)pthread_givesemaphore(&pjoin->data_sem);
+
+ /* Pass control to the thread entry point. The argument is
+ * argv[1]. argv[0] (the thread name) and argv[2-4] are not made
+ * available to the pthread.
+ */
+
+ exit_status = (*ptcb->entry.pthread)((pthread_addr_t)ptcb->argv[1]);
+
+ /* The thread has returned */
+
+ pthread_exit(exit_status);
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: pthread_create
+ *
+ * Description:
+ * This function creates and activates a new thread with a
+ * specified attributes.
+ *
+ * Input Parameters:
+ * thread
+ * attr
+ * startRoutine
+ * arg
+ ************************************************************/
+
+int pthread_create(pthread_t *thread, pthread_attr_t *attr,
+ pthread_startroutine_t startRoutine,
+ pthread_addr_t arg)
+{
+ _TCB *ptcb;
+ join_t *pjoin;
+ STATUS status;
+ int priority;
+#if CONFIG_RR_INTERVAL > 0
+ int policy;
+#endif
+ pid_t pid;
+
+ /* If attributes were not supplied, use the default attributes */
+
+ if (!attr)
+ {
+ attr = &g_default_pthread_attr;
+ }
+
+ /* Allocate a TCB for the new task. */
+
+ ptcb = (_TCB*)kzmalloc(sizeof(_TCB));
+ if (!ptcb)
+ {
+ *get_errno_ptr() = ENOMEM;
+ return ERROR;
+ }
+
+ /* Associate file descriptors with the new task */
+
+ if (sched_setuppthreadfiles(ptcb) != OK)
+ {
+ sched_releasetcb(ptcb);
+ return ERROR;
+ }
+
+ /* Allocate a detachable structure to support pthread_join logic */
+
+ pjoin = (join_t*)kzmalloc(sizeof(join_t));
+ if (!pjoin)
+ {
+ sched_releasetcb(ptcb);
+ return ERROR;
+ }
+
+ /* Allocate the stack for the TCB */
+
+ status = up_create_stack(ptcb, attr->stacksize);
+ if (status != OK)
+ {
+ sched_releasetcb(ptcb);
+ sched_free(pjoin);
+ return ERROR;
+ }
+
+ /* Should we use the priority and scheduler specified in the
+ * pthread attributes? Or should we use the current thread's
+ * priority and scheduler?
+ */
+
+ if (attr->inheritsched == PTHREAD_INHERIT_SCHED)
+ {
+ /* Get the priority of this thread. */
+
+ struct sched_param param;
+ status = sched_getparam(0, &param);
+ if (status == OK)
+ {
+ priority = param.sched_priority;
+ }
+
+ /* Get the scheduler policy for this thread */
+
+#if CONFIG_RR_INTERVAL > 0
+ policy = sched_getscheduler(0);
+ if (policy == ERROR)
+ {
+ policy = SCHED_FIFO;
+ }
+#endif
+ }
+ else
+ {
+ /* Use the priority and scheduler from the attributes */
+
+ priority = attr->priority;
+#if CONFIG_RR_INTERVAL > 0
+ policy = attr->policy;
+#endif
+ }
+
+ /* Initialize the task */
+
+ status = _task_init(ptcb, NULL, priority, pthread_start, (main_t)startRoutine,
+ TRUE, (char*)arg, NULL, NULL, NULL);
+ if (status != OK)
+ {
+
+ sched_releasetcb(ptcb);
+ sched_free(pjoin);
+ return ERROR;
+ }
+
+ /* Attach the join info to the TCB. */
+
+ ptcb->joininfo = (void*)pjoin;
+
+ /* If round robin scheduling is selected, set the appropriate flag
+ * in the TCB.
+ */
+
+#if CONFIG_RR_INTERVAL > 0
+ if (policy == SCHED_RR)
+ {
+ ptcb->flags |= TCB_FLAG_ROUND_ROBIN;
+ ptcb->timeslice = CONFIG_RR_INTERVAL;
+ }
+#endif
+
+ /* Get the assigned pid before we start the task (who knows what
+ * could happen to ptcb after this!). Copy this ID into the join structure
+ * as well.
+ */
+
+ pid = (int)ptcb->pid;
+ pjoin->thread.pid = pid;
+
+ /* Initialize the semaphores in the join structure to zero. */
+
+ status = sem_init(&pjoin->data_sem, 0, 0);
+ if (status == OK) status = sem_init(&pjoin->exit_sem, 0, 0);
+
+ /* Activate the task */
+
+ sched_lock();
+ if (status == OK)
+ {
+ status = task_activate(ptcb);
+ }
+
+ if (status == OK)
+ {
+ /* Wait for the task to actually get running and to register
+ * its join_t
+ */
+
+ (void)pthread_takesemaphore(&pjoin->data_sem);
+
+ /* Return the thread information to the caller */
+
+ if (thread) thread->pid = pid;
+ if (!pjoin->started) status = ERROR;
+
+ sched_unlock();
+ (void)sem_destroy(&pjoin->data_sem);
+ }
+ else
+ {
+ sched_unlock();
+ dq_rem((dq_entry_t*)ptcb, &g_inactivetasks);
+ (void)sem_destroy(&pjoin->data_sem);
+ (void)sem_destroy(&pjoin->exit_sem);
+ sched_releasetcb(ptcb);
+ sched_free(pjoin);
+ return ERROR;
+ }
+ return OK;
+}
diff --git a/nuttx/sched/pthread_detach.c b/nuttx/sched/pthread_detach.c
new file mode 100644
index 0000000000..967ed237f9
--- /dev/null
+++ b/nuttx/sched/pthread_detach.c
@@ -0,0 +1,134 @@
+/************************************************************
+ * pthread_detach.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include "os_internal.h"
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_detach
+ *
+ * Description:
+ * A thread object may be "detached" to specify that the return
+ * value and completion status will not be requested.
+ *
+ * Parameters:
+ * thread
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_detach(pthread_t thread)
+{
+ join_t *pjoin;
+ int ret;
+
+ dbg("%s: Thread=%d\n", __FUNCTION__, thread.pid);
+
+ /* Find the entry associated with this pthread. */
+
+ (void)pthread_takesemaphore(&g_join_semaphore);
+ pjoin = pthread_findjoininfo(thread.pid);
+ if (!pjoin)
+ {
+ dbg("%s: Could not find thread entry\n", __FUNCTION__);
+ ret = EINVAL;
+ }
+ else
+ {
+ /* Has the thread already terminated? */
+
+ if (pjoin->terminated)
+ {
+ /* YES.. just remove the thread entry. */
+
+ (void)pthread_removejoininfo(thread.pid);
+ sched_free(pjoin);
+ pjoin = NULL;
+ }
+ else
+ {
+ /* NO.. Just mark the thread as detached. It
+ * will be removed and deallocated when the
+ * thread exits
+ */
+
+ pjoin->detached = TRUE;
+ }
+
+ /* Either case is successful */
+
+ ret = OK;
+ }
+ (void)pthread_givesemaphore(&g_join_semaphore);
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_exit.c b/nuttx/sched/pthread_exit.c
new file mode 100644
index 0000000000..5330ee3f07
--- /dev/null
+++ b/nuttx/sched/pthread_exit.c
@@ -0,0 +1,119 @@
+/************************************************************
+ * pthread_exit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_exit
+ *
+ * Description:
+ * Terminate execution of a thread started with pthread_create.
+ *
+ * Parameters:
+ * exit_valie
+ *
+ * Returned Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+void pthread_exit(void *exit_value)
+{
+ int error_code = (int)exit_value;
+ int status;
+
+ dbg("%s: exit_value=%p\n", __FUNCTION__, exit_value);
+
+ /* Complete pending join operations */
+
+ status = pthread_completejoin(getpid(), exit_value);
+ if (status != OK)
+ {
+ /* Assume that the join completion failured becuase this
+ * not really a pthread. Exit by calling exit() to flush
+ * and close all file descriptors and calling atexit()
+ * functions.
+ */
+
+ if (error_code == EXIT_SUCCESS)
+ {
+ error_code = EXIT_FAILURE;
+ }
+ exit(error_code);
+ }
+
+ /* Then just exit, retaining all file descriptors and without
+ * calling atexit() funcitons.
+ */
+
+ _exit(error_code);
+}
diff --git a/nuttx/sched/pthread_findjoininfo.c b/nuttx/sched/pthread_findjoininfo.c
new file mode 100644
index 0000000000..48871cf80c
--- /dev/null
+++ b/nuttx/sched/pthread_findjoininfo.c
@@ -0,0 +1,98 @@
+/************************************************************
+ * pthread_findjoininfo.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: thread_findjoininfo
+ *
+ * Description:
+ * Find a join_t to the local data set.
+ *
+ * Parameters:
+ * pid
+ *
+ * Return Value:
+ * None or pointer to the found entry.
+ *
+ * Assumptions:
+ * The caller has provided protection from re-entrancy.
+ *
+ ************************************************************/
+
+join_t *pthread_findjoininfo(int pid)
+{
+ join_t *pjoin;
+
+ /* Find the entry with the matching pid */
+
+ for (pjoin = g_pthread_head;
+ (pjoin && pjoin->thread.pid != pid);
+ pjoin = pjoin->next);
+
+ /* and return it */
+
+ return pjoin;
+}
+
diff --git a/nuttx/sched/pthread_getschedparam.c b/nuttx/sched/pthread_getschedparam.c
new file mode 100644
index 0000000000..0a30fba8f6
--- /dev/null
+++ b/nuttx/sched/pthread_getschedparam.c
@@ -0,0 +1,123 @@
+/************************************************************
+ * pthread_getschedparam.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_getschedparam
+ *
+ * Description:
+ * Obtain the thread scheduling parameters.
+ *
+ * Parameters:
+ * thread
+ * policy
+ * param
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_getschedparam(pthread_t thread, int *policy,
+ struct sched_param *param)
+{
+ int ret;
+
+ dbg("%s: thread ID=%d policy=0x%p param=0x%p\n",
+ __FUNCTION__, thread.pid, policy, param);
+
+ if (!policy || !param)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ /* Get the schedparams of the thread. */
+
+ ret = sched_getparam(thread.pid, param);
+ if (ret != OK)
+ {
+ ret = EINVAL;
+ }
+
+ /* Return the policy. */
+
+ *policy = sched_getscheduler(thread.pid);
+ if (*policy == ERROR)
+ {
+ ret = *get_errno_ptr();
+ }
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
diff --git a/nuttx/sched/pthread_getspecific.c b/nuttx/sched/pthread_getspecific.c
new file mode 100644
index 0000000000..886557f387
--- /dev/null
+++ b/nuttx/sched/pthread_getspecific.c
@@ -0,0 +1,119 @@
+/************************************************************
+ * pthread_getspecific.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "os_internal.h"
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_getspecific
+ *
+ * Description:
+ * The pthread_getspecific() function returns the value
+ * currently bound to the specified key on behalf of the
+ * calling thread.
+ *
+ * The effect of calling pthread_getspecific() with
+ * with a key value not obtained from pthread_create() or
+ * after a key has been deleted with pthread_key_delete()
+ * is undefined.
+ *
+ * Parameters:
+ * key = The data key to get or set
+ *
+ * Return Value:
+ * The function pthread_getspecific() returns the thread-
+ * specific data associated with the given key. If no
+ * thread specific data is associated with the key, then
+ * the value NULL is returned.
+ *
+ * EINVAL - The key value is invalid.
+ *
+ * Assumptions:
+ *
+ * POSIX Compatibility:
+ * - Both calling pthread_setspecific() and pthread_getspecific()
+ * may be called from a thread-specific data destructor
+ * function.
+ *
+ ************************************************************/
+
+void *pthread_getspecific(pthread_key_t key)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ void *ret = NULL;
+
+ /* Check if the key is valid. */
+
+ if (key < g_pthread_num_keys)
+ {
+ /* Return the stored value. */
+
+ ret = rtcb->pthread_data[key];
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/pthread_initialize.c b/nuttx/sched/pthread_initialize.c
new file mode 100644
index 0000000000..e1b24f432f
--- /dev/null
+++ b/nuttx/sched/pthread_initialize.c
@@ -0,0 +1,193 @@
+/************************************************************
+ * pthread_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include <errno.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* This is the head of a private singly linked list. It
+ * is used to retain information about the spawned threads.
+ */
+
+join_t *g_pthread_head = NULL;
+join_t *g_pthread_tail = NULL;
+
+/* Mutually exclusive access to this data set is enforced with
+ * the following (un-named) semaphore.
+ */
+
+sem_t g_join_semaphore;
+
+/* This keys track of the number of global keys that have been
+ * allocated.
+ */
+
+ubyte g_pthread_num_keys;
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_initialize
+ *
+ * Description:
+ * This is an internal OS function called only at power-up
+ * boot time.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+void pthread_initialize(void)
+{
+ /* Initialize some global variables */
+
+ g_pthread_head = NULL;
+ g_pthread_tail = NULL;
+ g_pthread_num_keys = 0;
+
+ /* Initialize the join semaphore to one (to support one-at-
+ * a-time access to private data sets).
+ */
+
+ (void)sem_init(&g_join_semaphore, 0, 1);
+}
+
+/************************************************************
+ * Function: pthread_takesemaphore and pthread_givesemaphore
+ *
+ * Description:
+ * Support managed access to the private data sets.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_takesemaphore(sem_t *sem)
+{
+ /* Verify input parameters */
+
+ if (sem)
+ {
+ /* Take the semaphore */
+
+ while (sem_wait(sem) != OK)
+ {
+ /* Handle the special case where the semaphore wait was
+ * awakened by the receipt of a signal.
+ */
+
+ if (*get_errno_ptr() != EINTR)
+ {
+ *get_errno_ptr() = EINVAL;
+ return ERROR;
+ }
+ }
+ return OK;
+ }
+ else
+ {
+ /* NULL semaphore pointer! */
+
+ *get_errno_ptr() = EINVAL;
+ return ERROR;
+ }
+}
+
+int pthread_givesemaphore(sem_t *sem)
+{
+ /* Verify input parameters */
+
+ if (sem)
+ {
+ /* Give the semaphore */
+
+ if (sem_post(sem) == OK)
+ return OK;
+
+ else
+ {
+ /* sem_post() reported an error */
+
+ *get_errno_ptr() = EINVAL;
+ return ERROR;
+ }
+ }
+ else
+ {
+ /* NULL semaphore pointer! */
+
+ *get_errno_ptr() = EINVAL;
+ return ERROR;
+ }
+}
diff --git a/nuttx/sched/pthread_internal.h b/nuttx/sched/pthread_internal.h
new file mode 100644
index 0000000000..d0b1cdeb9e
--- /dev/null
+++ b/nuttx/sched/pthread_internal.h
@@ -0,0 +1,129 @@
+/************************************************************
+ * pthread_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __PTHREAD_INTERNAL_H
+#define __PTHREAD_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <nuttx/compiler.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public Type Declarations
+ ************************************************************/
+
+/* The following defines an entry in the pthread logic's
+ * local data set. Note that this structure is used to
+ * implemented a singly linked list. This structure
+ * is used (instead of, say, a binary search tree) because
+ * the data set will be searched using the pid as
+ * a key -- a process IDs will always be created in a
+ * montonically increasing fashion.
+ */
+
+struct join_s
+{
+ struct join_s *next; /* Implements link list */
+ boolean started; /* TRUE: pthread started. */
+ boolean detached; /* TRUE: pthread_detached'ed */
+ boolean terminated; /* TRUE: detach'ed+exit'ed */
+ pthread_t thread; /* Includes pid */
+ sem_t exit_sem; /* Implements join */
+ sem_t data_sem; /* Implements join */
+ pthread_addr_t exit_value; /* Returned data */
+
+};
+typedef struct join_s join_t;
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/* This is the head of a private singly linked list. It
+ * is used to retain information about the spawned threads.
+ */
+
+extern join_t *g_pthread_head;
+extern join_t *g_pthread_tail;
+
+/* Mutually exclusive access to this data set is enforced with
+ * the following (un-named) semaphore.
+ */
+
+extern sem_t g_join_semaphore;
+
+/* This keys track of the number of global keys that have been
+ * allocated.
+ */
+
+extern ubyte g_pthread_num_keys;
+
+/* Default pthread attributes */
+
+extern pthread_attr_t g_default_pthread_attr;
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN void weak_function pthread_initialize(void);
+EXTERN int pthread_completejoin(pid_t pid, void *exit_value);
+EXTERN join_t *pthread_findjoininfo(int pid);
+EXTERN int pthread_givesemaphore(sem_t *sem);
+EXTERN join_t *pthread_removejoininfo(int pid);
+EXTERN int pthread_takesemaphore(sem_t *sem);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __PTHREAD_INTERNAL_H */
+
diff --git a/nuttx/sched/pthread_join.c b/nuttx/sched/pthread_join.c
new file mode 100644
index 0000000000..4e956777c9
--- /dev/null
+++ b/nuttx/sched/pthread_join.c
@@ -0,0 +1,213 @@
+/************************************************************
+ * pthread_join.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include "os_internal.h"
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_join
+ *
+ * Description:
+ * A thread can await termination of another thread and
+ * retrieve the return value of the thread.
+ *
+ * Parameters:
+ * thread
+ * pexit_value
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, one of the following error codes:
+ * EINVAL The value specified by thread does not refer to a
+ * joinable thread.
+ * ESRCH No thread could be found corresponding to that
+ * specified by the given thread ID.
+ * EDEADLK A deadlock was detected or the value of thread
+ * specifies the calling thread.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_join(pthread_t thread, pthread_addr_t *pexit_value)
+{
+ join_t *pjoin;
+ int ret;
+
+ dbg("%s: thread=%d\n", __FUNCTION__, thread.pid);
+
+ /* First make sure that this is not an attempt to join to
+ * ourself.
+ */
+
+ if (thread.pid == getpid())
+ {
+ return EDEADLK;
+ }
+
+ /* Make sure no other task is mucking with the data structures
+ * while we are performing the following operations. NOTE:
+ * we can be also sure that pthread_exit() will not execute
+ * because it will also attempt to get this semaphore.
+ */
+
+ (void)pthread_takesemaphore(&g_join_semaphore);
+
+ /* Find the join information associated with this thread.
+ * This can fail for one of three reasons: (1) There is no
+ * thread associated with 'thread,' (2) the thread is a task
+ * and does not have join information, or (3) the thread
+ * was detached and has exitted.
+ */
+
+ pjoin = pthread_findjoininfo(thread.pid);
+ if (!pjoin)
+ {
+ /* Determine what kind of error to return */
+
+ _TCB *tcb = sched_gettcb(thread.pid);
+
+ dbg("%s: Could not find thread data\n", __FUNCTION__);
+
+ /* Case (1) or (3) -- we can't tell which. Assume (3) */
+
+ if (!tcb)
+ {
+ ret = ESRCH;
+ }
+
+ /* The thread is still active but has no join info. In that
+ * case, it must be a task and not a pthread.
+ */
+
+ else /* if ((tcb->flags & EDEADLK) == 0) */
+ {
+ ret = EINVAL;
+ }
+
+ (void)pthread_givesemaphore(&g_join_semaphore);
+ }
+ else if (pjoin->terminated)
+ {
+ dbg("%s: Thread has terminated\n", __FUNCTION__);
+
+ /* Get the thread exit value from the terminated thread. */
+
+ if (pexit_value)
+ {
+ dbg("%s: exit_value=0x%p\n", __FUNCTION__, pjoin->exit_value);
+ *pexit_value = pjoin->exit_value;
+ }
+
+ /* Then remove and deallocate the thread entry. */
+
+ (void)pthread_removejoininfo(thread.pid);
+ (void)pthread_givesemaphore(&g_join_semaphore);
+ sched_free(pjoin);
+ ret = OK;
+ }
+ else
+ {
+ dbg("%s: Thread is still running\n", __FUNCTION__);
+
+ /* Relinquish the data set semaphore, making certain that
+ * no task has the opportunity to run between the time
+ * we relinquish the data set semaphore and the time that
+ * we wait on the join semaphore.
+ */
+
+ sched_lock();
+ (void)pthread_givesemaphore(&g_join_semaphore);
+
+ /* Take the thread's join semaphore */
+
+ (void)pthread_takesemaphore(&pjoin->exit_sem);
+
+ /* Get the thread exit value */
+
+ if (pexit_value)
+ {
+ *pexit_value = pjoin->exit_value;
+ dbg("%s: exit_value=0x%p\n", __FUNCTION__, pjoin->exit_value);
+ }
+
+ /* Post the thread's join semaphore so that exitting thread
+ * will know that we have received the data.
+ */
+
+ (void)pthread_givesemaphore(&pjoin->data_sem);
+
+ /* Pre-emption is okay now. */
+
+ sched_unlock();
+
+ ret = OK;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_keycreate.c b/nuttx/sched/pthread_keycreate.c
new file mode 100644
index 0000000000..bda51e53a2
--- /dev/null
+++ b/nuttx/sched/pthread_keycreate.c
@@ -0,0 +1,135 @@
+/************************************************************
+ * pthread_keycreate.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_key_create
+ *
+ * Description:
+ * This function creates a thread-specific data key visible
+ * to all threads in the system. Although the same key value
+ * may be used by different threads, the values bound to
+ * the key by pthread_setspecific() are maintained on a
+ * per-thread basis and persist for the life of the calling
+ * thread.
+ *
+ * Upon key creation, the value NULL will be associated with
+ * the the new key in all active threads. Upon thread
+ * creation, the value NULL will be associated with all
+ * defined keys in the new thread.
+ *
+ * Parameters:
+ * key = A pointer to the key to create.
+ * destructor = An optional destructor() function that may
+ * be associated with each key that is invoked when a
+ * thread exits. However, this argument is ignored in
+ * the current implementation.
+ *
+ * Return Value:
+ * If successful, the pthread_key_create() function will
+ * store the newly created key value at *key and return
+ * zero (OK). Otherwise, an error number will be
+ * returned to indicate the error:
+ *
+ * EAGAIN - The system lacked sufficient resources
+ * to create another thread-specific data key, or the
+ * system-imposed limit on the total number of keys
+ * pers process {PTHREAD_KEYS_MAX} has been exceeded
+ * ENONMEM - Insufficient memory exists to create the key.
+ *
+ * Assumptions:
+ *
+ * POSIX Compatibility:
+ * - The present implementation ignores the destructor
+ * argument.
+ *
+ ************************************************************/
+
+int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
+{
+ int ret = EAGAIN;
+
+ /* Check if we have exceeded the system-defined number of keys. */
+
+ if (g_pthread_num_keys < PTHREAD_KEYS_MAX)
+ {
+ /* Return the key value */
+
+ *key = g_pthread_num_keys;
+
+ /* Increment the count of global keys. */
+
+ g_pthread_num_keys++;
+
+ /* Return success. */
+
+ ret = OK;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/pthread_keydelete.c b/nuttx/sched/pthread_keydelete.c
new file mode 100644
index 0000000000..486de00d83
--- /dev/null
+++ b/nuttx/sched/pthread_keydelete.c
@@ -0,0 +1,93 @@
+/************************************************************
+ * pthread_keydelete.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_key_delete
+ *
+ * Description:
+ * This POSIX function should delete a thread-specific data
+ * key previously returned by pthread_key_create(). However,
+ * this function does nothing in the present implementation.
+ *
+ * Parameters:
+ * key = the key to delete
+ *
+ * Return Value:
+ * Always returns ENOSYSL.
+ *
+ * Assumptions:
+ *
+ * POSIX Compatibility:
+ *
+ ************************************************************/
+
+int pthread_key_delete(pthread_key_t key)
+{
+ return ENOSYS;
+}
diff --git a/nuttx/sched/pthread_mutexattrdestroy.c b/nuttx/sched/pthread_mutexattrdestroy.c
new file mode 100644
index 0000000000..8e3abdb47b
--- /dev/null
+++ b/nuttx/sched/pthread_mutexattrdestroy.c
@@ -0,0 +1,104 @@
+/************************************************************
+ * pthread_mutexattrdestroy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_mutexattr_destroy
+ *
+ * Description:
+ * Destroy mutex attributes.
+ *
+ * Parameters:
+ * attr
+ * pshared
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
+{
+ int ret = OK;
+
+ dbg("%s: attr=0x%p\n", __FUNCTION__, attr);
+
+ if (!attr)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ attr->pshared = 0;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_mutexattrgetpshared.c b/nuttx/sched/pthread_mutexattrgetpshared.c
new file mode 100644
index 0000000000..1d54b90127
--- /dev/null
+++ b/nuttx/sched/pthread_mutexattrgetpshared.c
@@ -0,0 +1,104 @@
+/************************************************************
+ * pthread_mutexattrgetpshared.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_mutexattr_getpshared
+ *
+ * Description:
+ * Get pshared mutex attribute.
+ *
+ * Parameters:
+ * attr
+ * pshared
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
+{
+ int ret = OK;
+
+ dbg("%s: attr=0x%p pshared=0x%p\n", __FUNCTION__, attr, pshared);
+
+ if (!attr || !pshared)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ *pshared = attr->pshared;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_mutexattrinit.c b/nuttx/sched/pthread_mutexattrinit.c
new file mode 100644
index 0000000000..920ccf0ee4
--- /dev/null
+++ b/nuttx/sched/pthread_mutexattrinit.c
@@ -0,0 +1,103 @@
+/************************************************************
+ * pthread_mutexattrinit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_mutexattr_init
+ *
+ * Description:
+ * Create mutex attributes.
+ *
+ * Parameters:
+ * attr
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_mutexattr_init(pthread_mutexattr_t *attr)
+{
+ int ret = OK;
+
+ dbg("%s: attr=0x%p\n", __FUNCTION__, attr);
+
+ if (!attr)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ attr->pshared = 0;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_mutexattrsetpshared.c b/nuttx/sched/pthread_mutexattrsetpshared.c
new file mode 100644
index 0000000000..a9ddd0c3c9
--- /dev/null
+++ b/nuttx/sched/pthread_mutexattrsetpshared.c
@@ -0,0 +1,104 @@
+/************************************************************
+ * pthread_mutexattrsetpshared.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_mutexattr_setpshared
+ *
+ * Description:
+ * Set pshared mutex attribute.
+ *
+ * Parameters:
+ * attr
+ * pshared
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
+{
+ int ret = OK;
+
+ dbg("%s: attr=0x%p pshared=%d\n", __FUNCTION__, attr, pshared);
+
+ if (!attr || (pshared != 0 && pshared != 1))
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ attr->pshared = pshared;
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_mutexdestroy.c b/nuttx/sched/pthread_mutexdestroy.c
new file mode 100644
index 0000000000..bf82d4f436
--- /dev/null
+++ b/nuttx/sched/pthread_mutexdestroy.c
@@ -0,0 +1,128 @@
+/************************************************************
+ * pthread_mutexdestroy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_mutex_destroy
+ *
+ * Description:
+ * Destroy a mutex.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_mutex_destroy(pthread_mutex_t *mutex)
+{
+ int ret = OK;
+ int status;
+
+ dbg("%s: mutex=0x%p\n", __FUNCTION__, mutex);
+
+ if (!mutex)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ /* Make sure the semaphore is stable while we make the following
+ * checks
+ */
+
+ sched_lock();
+
+ /* Is the semaphore available? */
+
+ if (mutex->pid != 0)
+ {
+ ret = EBUSY;
+ }
+ else
+ {
+ /* Destroy the semaphore */
+
+ status = sem_destroy((sem_t*)&mutex->sem);
+ if (status != OK)
+ {
+ ret = EINVAL;
+ }
+ }
+ sched_unlock();
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_mutexinit.c b/nuttx/sched/pthread_mutexinit.c
new file mode 100644
index 0000000000..faa06f227d
--- /dev/null
+++ b/nuttx/sched/pthread_mutexinit.c
@@ -0,0 +1,122 @@
+/************************************************************
+ * pthread_mutexinit.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_mutex_init
+ *
+ * Description:
+ * Create a mutex
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
+{
+ int ret = OK;
+ int pshared = 0;
+ int status;
+
+ dbg("%s: mutex=0x%p attr=0x%p\n", __FUNCTION__, mutex, attr);
+
+ if (!mutex)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ /* Were attributes specified? If so, use them */
+
+ if (attr)
+ {
+ pshared = attr->pshared;
+ }
+
+ /* Indicate that the semaphore is not held by any thread. */
+
+ mutex->pid = 0;
+
+ /* Initialize the mutex like a semaphore with initial count = 1 */
+
+ status = sem_init((sem_t*)&mutex->sem, pshared, 1);
+ if (status != OK)
+ {
+ ret = EINVAL;
+ }
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_mutexlock.c b/nuttx/sched/pthread_mutexlock.c
new file mode 100644
index 0000000000..43e7c83562
--- /dev/null
+++ b/nuttx/sched/pthread_mutexlock.c
@@ -0,0 +1,138 @@
+/************************************************************
+ * pthread_mutexlock.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_mutex_lock
+ *
+ * Description:
+ * Lock a mutex.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_mutex_lock(pthread_mutex_t *mutex)
+{
+ int mypid = (int)getpid();
+ int ret = OK;
+
+ dbg("%s: mutex=0x%p\n", __FUNCTION__, mutex);
+
+ if (!mutex)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ /* Make sure the semaphore is stable while we make the following
+ * checks. This all needs to be one atomic action.
+ */
+
+ sched_lock();
+
+ /* Does this task already hold the semaphore? */
+
+ if (mutex->pid == mypid)
+ {
+ dbg("%s: Returning EDEADLK\n", __FUNCTION__);
+ ret = EDEADLK;
+ }
+ else
+ {
+ /* Take the semaphore */
+
+ ret = pthread_takesemaphore((sem_t*)&mutex->sem);
+
+ /* If we succussfully obtained the semaphore, then indicate
+ * that we own it.
+ */
+
+ if (!ret)
+ {
+ mutex->pid = mypid;
+ }
+ }
+ sched_unlock();
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
+
+
diff --git a/nuttx/sched/pthread_mutextrylock.c b/nuttx/sched/pthread_mutextrylock.c
new file mode 100644
index 0000000000..010c85f805
--- /dev/null
+++ b/nuttx/sched/pthread_mutextrylock.c
@@ -0,0 +1,137 @@
+/************************************************************
+ * pthread_mutextrylock.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_mutex_trylock
+ *
+ * Description:
+ * Attempt to lock a mutex
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_mutex_trylock(pthread_mutex_t *mutex)
+{
+ int ret = OK;
+
+ dbg("%s: mutex=0x%p\n", __FUNCTION__, mutex);
+
+ if (!mutex)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ /* Make sure the semaphore is stable while we make the following
+ * checks. This all needs to be one atomic action.
+ */
+
+ sched_lock();
+
+ /* Try to get the semaphore. */
+
+ if (sem_trywait((sem_t*)&mutex->sem) == OK)
+ {
+ /* If we succussfully obtained the semaphore, then indicate
+ * that we own it.
+ */
+
+ mutex->pid = (int)getpid();
+ }
+
+ /* Was it not available? */
+
+ else if (*get_errno_ptr() == EAGAIN)
+ {
+ ret = EBUSY;
+ }
+ else
+ {
+ ret = EINVAL;
+ }
+
+ sched_unlock();
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
+
diff --git a/nuttx/sched/pthread_mutexunlock.c b/nuttx/sched/pthread_mutexunlock.c
new file mode 100644
index 0000000000..133dc1a942
--- /dev/null
+++ b/nuttx/sched/pthread_mutexunlock.c
@@ -0,0 +1,127 @@
+/************************************************************
+ * pthread_mutexunlock.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_mutex_unlock
+ *
+ * Description:
+ * Unlock a mutex.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_mutex_unlock(pthread_mutex_t *mutex)
+{
+ int ret = OK;
+
+ dbg("%s: mutex=0x%p\n", __FUNCTION__, mutex);
+
+ if (!mutex)
+ {
+ ret = EINVAL;
+ }
+ else
+ {
+ /* Make sure the semaphore is stable while we make the following
+ * checks. This all needs to be one atomic action.
+ */
+
+ sched_lock();
+
+ /* Does the calling thread own the semaphore? */
+
+ if (mutex->pid != (int)getpid())
+ {
+ dbg("%s: Holder=%d Returning EPERM\n", __FUNCTION__, mutex->pid);
+ ret = EPERM;
+ }
+ else
+ {
+ /* Nulllify the pid and post the semaphore */
+
+ mutex->pid = 0;
+ ret = pthread_givesemaphore((sem_t*)&mutex->sem);
+ }
+ sched_unlock();
+ }
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
+
+
diff --git a/nuttx/sched/pthread_removejoininfo.c b/nuttx/sched/pthread_removejoininfo.c
new file mode 100644
index 0000000000..ecdc6d99e1
--- /dev/null
+++ b/nuttx/sched/pthread_removejoininfo.c
@@ -0,0 +1,136 @@
+/************************************************************
+ * pthread_removejoininfo.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_removejoininfo
+ *
+ * Description:
+ * Remove a join_t from the local data set.
+ *
+ * Parameters:
+ * pid
+ *
+ * Return Value:
+ * None or pointer to the found entry.
+ *
+ * Assumptions:
+ * The caller has provided protection from re-entrancy.
+ *
+ ************************************************************/
+
+join_t *pthread_removejoininfo(int pid)
+{
+ join_t *prev;
+ join_t *join;
+
+ /* Find the entry with the matching pid */
+
+ for (prev = NULL, join = g_pthread_head;
+ (join && join->thread.pid != pid);
+ prev = join, join = join->next);
+
+ /* Remove it from the data set. */
+
+ /* First check if this is the entry at the head of the list. */
+
+ if (join)
+ {
+ if (!prev)
+ {
+ /* Check if this is the only entry in the list */
+
+ if (!join->next)
+ {
+ g_pthread_head = NULL;
+ g_pthread_tail = NULL;
+ }
+
+ /* Otherwise, remove it from the head of the list */
+
+ else
+ {
+ g_pthread_head = join->next;
+ }
+ }
+
+ /* It is not at the head of the list, check if it is at the tail. */
+
+ else if (!join->next)
+ {
+ g_pthread_tail = prev;
+ prev->next = NULL;
+ }
+
+ /* No, remove it from the middle of the list. */
+
+ else
+ {
+ prev->next = join->next;
+ }
+ }
+
+ return join;
+}
diff --git a/nuttx/sched/pthread_self.c b/nuttx/sched/pthread_self.c
new file mode 100644
index 0000000000..55ceddbddd
--- /dev/null
+++ b/nuttx/sched/pthread_self.c
@@ -0,0 +1,91 @@
+/************************************************************
+ * pthread_self.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_self
+ *
+ * Description:
+ * A thread may obtain a copy of its own thread handle.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * A copy of this threads handle
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+pthread_t pthread_self(void)
+{
+ pthread_t thread;
+ thread.pid = (int)getpid();
+ return thread;
+}
+
diff --git a/nuttx/sched/pthread_setcancelstate.c b/nuttx/sched/pthread_setcancelstate.c
new file mode 100644
index 0000000000..4a23b10680
--- /dev/null
+++ b/nuttx/sched/pthread_setcancelstate.c
@@ -0,0 +1,127 @@
+/**************************************************************************
+ * pthread_setcancelstate.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **************************************************************************/
+
+/**************************************************************************
+ * Included Files
+ **************************************************************************/
+
+#include <pthread.h>
+#include <errno.h>
+#include "os_internal.h"
+
+/**************************************************************************
+ * Private Definitions
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Types
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Function Prototypes
+ **************************************************************************/
+
+/**************************************************************************
+ * Global Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Variables
+ **************************************************************************/
+
+/**************************************************************************
+ * Private Functions
+ **************************************************************************/
+
+/**************************************************************************
+ * Public Functions
+ **************************************************************************/
+
+int pthread_setcancelstate(int state, int *oldstate)
+{
+ _TCB *tcb = (_TCB*)g_readytorun.head;
+ int ret = OK;
+
+ /* Suppress context changes for a bit so that the flags are stable. (the
+ * flags should not change in interrupt handling).
+ */
+
+ sched_lock();
+
+ /* Return the current state if so requrested */
+
+ if (oldstate)
+ {
+ if ((tcb->flags & TCB_FLAG_NONCANCELABLE) != 0)
+ {
+ *oldstate = PTHREAD_CANCEL_DISABLE;
+ }
+ else
+ {
+ *oldstate = PTHREAD_CANCEL_ENABLE;
+ }
+ }
+
+ /* Set the new cancellation state */
+
+ if (state == PTHREAD_CANCEL_ENABLE)
+ {
+ unsigned flags = tcb->flags;
+
+ /* Clear the non-cancelable and cancel pending flags */
+
+ tcb->flags &= ~(TCB_FLAG_NONCANCELABLE|TCB_FLAG_CANCEL_PENDING);
+
+ /* If the cancel was pending, then just exit as requested */
+
+ if (flags & TCB_FLAG_CANCEL_PENDING)
+ {
+ pthread_exit(PTHREAD_CANCELED);
+ }
+ }
+ else if (state == PTHREAD_CANCEL_DISABLE)
+ {
+ /* Set the non-cancelable state */
+
+ tcb->flags |= TCB_FLAG_NONCANCELABLE;
+ }
+ else
+ {
+ ret = EINVAL;
+ }
+
+ sched_unlock();
+ return ret;
+}
diff --git a/nuttx/sched/pthread_setschedparam.c b/nuttx/sched/pthread_setschedparam.c
new file mode 100644
index 0000000000..5d0f913c25
--- /dev/null
+++ b/nuttx/sched/pthread_setschedparam.c
@@ -0,0 +1,103 @@
+/************************************************************
+ * pthread_setschedparam.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_setschedparam
+ *
+ * Description:
+ * Set thread scheduling parameters.
+ *
+ * Parameters:
+ * thread
+ * policy
+ * param
+ *
+ * Return Value:
+ * 0 if successful. Otherwise, an error code.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int pthread_setschedparam(pthread_t thread, int policy,
+ const struct sched_param *param)
+{
+ int ret;
+
+ dbg("%s: thread ID=%d policy=%d param=0x%p\n",
+ __FUNCTION__, thread.pid, policy, param);
+
+ /* Let sched_setscheduler do all of the work */
+
+ ret = sched_setscheduler(thread.pid, policy, param);
+
+ dbg("%s: Returning %d\n", __FUNCTION__, ret);
+ return ret;
+}
diff --git a/nuttx/sched/pthread_setspecific.c b/nuttx/sched/pthread_setspecific.c
new file mode 100644
index 0000000000..c08a181907
--- /dev/null
+++ b/nuttx/sched/pthread_setspecific.c
@@ -0,0 +1,131 @@
+/************************************************************
+ * pthread_setspecific.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+#include "os_internal.h"
+#include "pthread_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_setspecific
+ *
+ * Description:
+ * The pthread_setspecific() function associates a thread-
+ * specific value with a key obtained via a previous call
+ * to pthread_key_create(). Different threads may bind
+ * different values to the same key. These values are
+ * typically pointers to blocks of dynamically allocated
+ * memory that have been reserved for use by the calling
+ * thread.
+ *
+ * The effect of calling pthread_setspecific() with
+ * with a key value not obtained from pthread_create() or
+ * after a key has been deleted with pthread_key_delete()
+ * is undefined.
+ *
+ * Parameters:
+ * key = The data key to get or set
+ * value = The value to bind to the key.
+ *
+ * Return Value:
+ * If successful, pthread_setspecific() will return zero (OK).
+ * Otherwise, an error number will be returned:
+ *
+ * ENOMEM - Insufficient memory exists to associate
+ * the value with the key.
+ * EINVAL - The key value is invalid.
+ *
+ * Assumptions:
+ *
+ * POSIX Compatibility:
+ * int pthread_setspecific(pthread_key_t key, void *value)
+ * void *pthread_getspecific(pthread_key_t key)
+ *
+ * - Both calling pthread_setspecific() and pthread_getspecific()
+ * may be called from a thread-specific data destructor
+ * function.
+ *
+ ************************************************************/
+
+int pthread_setspecific(pthread_key_t key, void *value)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ int ret = EINVAL;
+
+ /* Check if the key is valid. */
+
+ if (key < g_pthread_num_keys)
+ {
+ /* Store the data in the TCB. */
+
+ rtcb->pthread_data[key] = value;
+
+ /* Return success. */
+
+ ret = OK;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/pthread_yield.c b/nuttx/sched/pthread_yield.c
new file mode 100644
index 0000000000..06d68e5a1c
--- /dev/null
+++ b/nuttx/sched/pthread_yield.c
@@ -0,0 +1,87 @@
+/************************************************************
+ * pthread_yield.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <pthread.h>
+#include <sched.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: pthread_yield
+ *
+ * Description:
+ * A thread may tell the scheduler that its processor can be
+ * made available.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+void pthread_yield(void)
+{
+ (void)sched_yield();
+}
diff --git a/nuttx/sched/sched_addblocked.c b/nuttx/sched/sched_addblocked.c
new file mode 100644
index 0000000000..195937832e
--- /dev/null
+++ b/nuttx/sched/sched_addblocked.c
@@ -0,0 +1,117 @@
+/************************************************************
+ * sched_addblocked.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <queue.h>
+#include <assert.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_addblocked
+ *
+ * Description:
+ * This function adds a TCB to one of the blocked state
+ * task lists as inferreded from task_state.
+ *
+ * Inputs:
+ * btcb - Points to the TCB that is blocked
+ * task_state - identifies the state of the blocked task
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ * - The caller has established a critical section before
+ * calling this function.
+ *
+ ************************************************************/
+
+void sched_addblocked(_TCB *btcb, tstate_t task_state)
+{
+ /* Make sure that we received a valid blocked state */
+
+ ASSERT(task_state >= FIRST_BLOCKED_STATE &&
+ task_state <= LAST_BLOCKED_STATE);
+
+ /* Add the TCB to the blocked task list associated with
+ * this state. First, determine if the task is to be added
+ * to a prioritized task list
+ */
+
+ if (g_tasklisttable[task_state].prioritized)
+ {
+ /* Add the task to a prioritized list */
+
+ sched_addprioritized(btcb, g_tasklisttable[task_state].list);
+ }
+ else
+ {
+ /* Add the task to a non-prioritized list */
+
+ dq_addlast((dq_entry_t*)btcb, g_tasklisttable[task_state].list);
+ }
+
+ /* Make sure the TCB's state corresponds to the list */
+
+ btcb->task_state = task_state;
+}
diff --git a/nuttx/sched/sched_addprioritized.c b/nuttx/sched/sched_addprioritized.c
new file mode 100644
index 0000000000..54e7920484
--- /dev/null
+++ b/nuttx/sched/sched_addprioritized.c
@@ -0,0 +1,170 @@
+/************************************************************
+ * sched_addprioritized.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <queue.h>
+#include <assert.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sched_addprioritized
+ *
+ * Description:
+ * This function adds a TCB to a prioritized TCB list.
+ *
+ * Inputs:
+ * tcb - Points to the TCB to add to the prioritized list
+ * list - Points to the prioritized list to add tcb to
+ *
+ * Return Value:
+ * TRUE if the head of the list has changed.
+ *
+ * Assumptions:
+ * - The caller has established a critical section before
+ * calling this function (calling sched_lock() first is NOT
+ * a good idea -- use irqsave()).
+ * - The caller has already removed the input tcb from
+ * whatever list it was in.
+ * - The caller handles the condition that occurs if the
+ * the head of the task list is changed.
+ * - The caller must set the task_state field of the TCB to
+ * match the state associated with the list.
+ ************************************************************/
+
+boolean sched_addprioritized(_TCB *tcb, dq_queue_t *list)
+{
+ _TCB *next;
+ _TCB *prev;
+ ubyte sched_priority = tcb->sched_priority;
+ boolean ret = FALSE;
+
+ /* Lets do a sanity check before we get started. */
+
+ ASSERT(sched_priority >= SCHED_PRIORITY_MIN);
+
+ /* Search the list to find the location to insert the new Tcb.
+ * Each is list is maintained in ascending sched_priority order.
+ */
+
+ for (next = (_TCB*)list->head;
+ (next && sched_priority <= next->sched_priority);
+ next = next->flink);
+
+ /* Add the tcb to the spot found in the list. Check if the tcb
+ * goes at the end of the list. NOTE: This could only happen if list
+ * is the g_pendingtasks list!
+ */
+
+ if (!next)
+ {
+ /* The tcb goes at the end of the list. */
+
+ prev = (_TCB*)list->tail;
+ if (!prev)
+ {
+ /* Special case: The list is empty */
+
+ tcb->flink = NULL;
+ tcb->blink = NULL;
+ list->head = (dq_entry_t*)tcb;
+ list->tail = (dq_entry_t*)tcb;
+ ret = TRUE;
+ }
+ else
+ {
+ /* The tcb goes at the end of a non-empty list */
+
+ tcb->flink = NULL;
+ tcb->blink = prev;
+ prev->flink = tcb;
+ list->tail = (dq_entry_t*)tcb;
+ }
+ }
+ else
+ {
+ /* The tcb goes just before next */
+
+ prev = (_TCB*)next->blink;
+ if (!prev)
+ {
+ /* Special case: Insert at the head of the list */
+
+ tcb->flink = next;
+ tcb->blink = NULL;
+ next->blink = tcb;
+ list->head = (dq_entry_t*)tcb;
+ ret = TRUE;
+ }
+ else
+ {
+ /* Insert in the middle of the list */
+
+ tcb->flink = next;
+ tcb->blink = prev;
+ prev->flink = tcb;
+ next->blink = tcb;
+ }
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sched_addreadytorun.c b/nuttx/sched/sched_addreadytorun.c
new file mode 100644
index 0000000000..c52da36702
--- /dev/null
+++ b/nuttx/sched/sched_addreadytorun.c
@@ -0,0 +1,144 @@
+/************************************************************
+ * sched_addreadytorun.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <queue.h>
+#include <assert.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_addreadytorun
+ *
+ * Description:
+ * This function adds a TCB to the ready to run
+ * list. If the currently active task has preemption disabled
+ * and the new TCB would cause this task to be preempted, the
+ * new task is added to the g_pendingtasks list instead. The
+ * pending tasks will be made ready-to-run when preemption
+ * is unlocked.
+ *
+ * Inputs:
+ * btcb - Points to the blocked TCB that is ready-to-run
+ *
+ * Return Value:
+ * TRUE if the currently active task (the head of the
+ * g_readytorun list) has changed.
+ *
+ * Assumptions:
+ * - The caller has established a critical section before
+ * calling this function (calling sched_lock() first is NOT
+ * a good idea -- use irqsave()).
+ * - The caller has already removed the input rtcb from
+ * whatever list it was in.
+ * - The caller handles the condition that occurs if the
+ * the head of the g_readytorun list is changed.
+ ************************************************************/
+
+boolean sched_addreadytorun(_TCB *btcb)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ boolean ret;
+
+ /* Check if pre-emption is disabled for the current running task and
+ * if the rtrTask would cause the current running task to be preempted.
+ */
+
+ if (rtcb->lockcount && rtcb->sched_priority < btcb->sched_priority)
+ {
+ /* Yes. Preemption would occur! Add the btcb to the g_pendingtasks
+ * task list for now.
+ */
+
+ sched_addprioritized(btcb, &g_pendingtasks);
+ btcb->task_state = TSTATE_TASK_PENDING;
+ ret = FALSE;
+ }
+
+ /* Otherwise, add the new task to the g_readytorun task list */
+
+ else if (sched_addprioritized(btcb, &g_readytorun))
+ {
+ /* Information the instrumentation logic that we are switching tasks */
+
+ sched_note_switch(rtcb, btcb);
+
+ /* The new btcb was added at the head of the g_readytorun list. It
+ * is now to new active task!
+ */
+
+ ASSERT(!rtcb->lockcount && btcb->flink != NULL);
+
+ btcb->task_state = TSTATE_TASK_RUNNING;
+ btcb->flink->task_state = TSTATE_TASK_READYTORUN;
+ ret = TRUE;
+ }
+ else
+ {
+ /* The new btcb was added in the middle of the g_readytorun list */
+
+ btcb->task_state = TSTATE_TASK_READYTORUN;
+ ret = FALSE;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sched_free.c b/nuttx/sched/sched_free.c
new file mode 100644
index 0000000000..205da0a465
--- /dev/null
+++ b/nuttx/sched/sched_free.c
@@ -0,0 +1,103 @@
+/************************************************************
+ * sched_free.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <nuttx/kmalloc.h>
+#include <queue.h>
+#include <assert.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_free
+ *
+ * Description:
+ * This function performs deallocations that
+ * the operating system may need to make. This special
+ * interface to free is used to handling corner cases
+ * where the operating system may have to perform deallocations
+ * from within an interrupt handler.
+ *
+ ************************************************************/
+
+void sched_free(void *address)
+{
+ /* Check if this is an attempt to deallocate memory from
+ * an exception handler.
+ */
+
+ if (up_interrupt_context())
+ {
+ /* Yes.. Delay the deallocation until a more appropriate time. */
+
+ uint32 savedState = irqsave();
+ sq_addlast((sq_entry_t*)address, &g_delayeddeallocations);
+ irqrestore(savedState);
+ }
+ else
+ {
+ /* No.. just deallocate the memory now. */
+
+ kfree(address);
+ }
+}
diff --git a/nuttx/sched/sched_getfiles.c b/nuttx/sched/sched_getfiles.c
new file mode 100644
index 0000000000..d6986a5bcd
--- /dev/null
+++ b/nuttx/sched/sched_getfiles.c
@@ -0,0 +1,77 @@
+/************************************************************
+ * sched_getfiles.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+
+#include <sched.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sched_getfiles
+ *
+ * Description:
+ * Return a pointer to the file list for this thread
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * A pointer to the errno.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+struct filelist *sched_getfiles(void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ return rtcb->filelist;
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/sched/sched_getparam.c b/nuttx/sched/sched_getparam.c
new file mode 100644
index 0000000000..85d8c55c77
--- /dev/null
+++ b/nuttx/sched/sched_getparam.c
@@ -0,0 +1,142 @@
+/************************************************************
+ * sched_getparam.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_getparam
+ *
+ * Description:
+ * This function gets the scheduling priority of the task
+ * specified by pid.
+ *
+ * Inputs:
+ * pid - the task ID of the task. If pid is zero, the priority
+ * of the calling task is returned.
+ * param - A structure whose member sched_priority is the integer
+ * priority. The task's priority is copied to the sched_priority
+ * element of this structure.
+ *
+ * Return Value:
+ * 0 (OK) if successful, otherwise -1 (ERROR).
+ *
+ * This function can fail if param is null or if pid does
+ * not correspond to any task.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_getparam (pid_t pid, struct sched_param * param)
+{
+ _TCB *rtcb;
+ _TCB *tcb;
+ int ret = OK;
+
+ if (!param)
+ {
+ return ERROR;
+ }
+
+ /* Check if the task to restart is the calling task */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ if ((pid == 0) || (pid == rtcb->pid))
+ {
+ /* Return the priority if the calling task. */
+
+ param->sched_priority = (int)rtcb->sched_priority;
+ }
+
+ /* Ths pid is not for the calling task, we will have to look it up */
+
+ else
+ {
+ /* Get the TCB associated with this pid */
+
+ sched_lock();
+ tcb = sched_gettcb(pid);
+ if (!tcb)
+ {
+ /* This pid does not correspond to any known task */
+
+ ret = ERROR;
+ }
+ else
+ {
+ /* Return the priority of the task */
+
+ param->sched_priority = (int)tcb->sched_priority;
+ }
+ sched_unlock();
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sched_getprioritymax.c b/nuttx/sched/sched_getprioritymax.c
new file mode 100644
index 0000000000..f137b72c6e
--- /dev/null
+++ b/nuttx/sched/sched_getprioritymax.c
@@ -0,0 +1,102 @@
+/************************************************************
+ * sched_getprioritymax.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: ched_get_priority_max
+ *
+ * Description:
+ * This function returns the value of the highest possible
+ * task priority for a specified scheduling policy.
+ *
+ * Inputs:
+ * policy - Scheduling policy requested.
+ *
+ * Return Value:
+ * The maximum priority value or -1 (ERROR)
+ * (errno is not set)
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_get_priority_max(int policy)
+{
+ if (policy != SCHED_FIFO)
+ {
+ return ERROR;
+ }
+ else
+ {
+ return SCHED_PRIORITY_MAX;
+ }
+}
diff --git a/nuttx/sched/sched_getprioritymin.c b/nuttx/sched/sched_getprioritymin.c
new file mode 100644
index 0000000000..154432c668
--- /dev/null
+++ b/nuttx/sched/sched_getprioritymin.c
@@ -0,0 +1,102 @@
+/************************************************************
+ * sched_getprioritymin.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_get_priority_min
+ *
+ * Description:
+ * This function returns the value of the lowest possible
+ * task priority for a specified scheduling policy.
+ *
+ * Inputs:
+ * policy - Scheduling policy requested.
+ *
+ * Return Value:
+ * The minimum priority value or -1 (ERROR)
+ * (errno is not set)
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_get_priority_min(int policy)
+{
+ if (policy != SCHED_FIFO)
+ {
+ return ERROR;
+ }
+ else
+ {
+ return SCHED_PRIORITY_MIN;
+ }
+}
diff --git a/nuttx/sched/sched_getscheduler.c b/nuttx/sched/sched_getscheduler.c
new file mode 100644
index 0000000000..9406d9774c
--- /dev/null
+++ b/nuttx/sched/sched_getscheduler.c
@@ -0,0 +1,130 @@
+/************************************************************
+ * sched_getscheduler.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <errno.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_getscheduler
+ *
+ * Description:
+ * sched_getscheduler() returns the scheduling policy
+ * currently applied to the process identified by pid. If
+ * pid equals zero, the policy of the calling process will
+ * be retrieved.
+ *
+ * Inputs:
+ * pid - the task ID of the task to query. If pid is
+ * zero, the calling task is queried.
+ *
+ * Return Value:
+ * On success, sched_getscheduler() returns the policy for
+ * the task (either SCHED_FIFO or SCHED_RR). On error,
+ * ERROR (-1) is returned, and errno is set appropriately:
+ *
+ * ESRCH The task whose ID is pid could not be found.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_getscheduler(pid_t pid)
+{
+ _TCB *tcb;
+
+ /* Verify that the pid corresponds to a real task */
+
+ if (pid = 0)
+ {
+ tcb = (_TCB*)g_readytorun.head;
+ }
+ else
+ {
+ tcb = sched_gettcb(pid);
+ }
+
+ if (!tcb)
+ {
+ *get_errno_ptr() = ESRCH;
+ return ERROR;
+ }
+#if CONFIG_RR_INTERVAL > 0
+ else if ((tcb->flags & TCB_FLAG_ROUND_ROBIN) != 0)
+ {
+ return SCHED_RR;
+ }
+#endif
+ else
+ {
+ return SCHED_FIFO;
+ }
+}
diff --git a/nuttx/sched/sched_getstreams.c b/nuttx/sched/sched_getstreams.c
new file mode 100644
index 0000000000..fef913bc4a
--- /dev/null
+++ b/nuttx/sched/sched_getstreams.c
@@ -0,0 +1,77 @@
+/************************************************************
+ * sched_getstreams.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0
+
+#include <sched.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sched_getstreams
+ *
+ * Description:
+ * Return a pointer to the streams list for this thread
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * A pointer to the errno.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+struct streamlist *sched_getstreams(void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ return rtcb->streams;
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS && CONFIG_NFILE_STREAMS */
diff --git a/nuttx/sched/sched_gettcb.c b/nuttx/sched/sched_gettcb.c
new file mode 100644
index 0000000000..7073b796a5
--- /dev/null
+++ b/nuttx/sched/sched_gettcb.c
@@ -0,0 +1,104 @@
+/************************************************************
+ * sched_gettcb.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <sched.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_gettcb
+ *
+ * Description:
+ * Given a task ID, this function will return
+ * the a pointer to the corresponding TCB (or NULL if there
+ * is no such task ID).
+ *
+ ************************************************************/
+
+_TCB *sched_gettcb(pid_t pid)
+{
+ _TCB *ret = NULL;
+ int hash_ndx;
+
+ /* Verify that the PID is within range */
+
+ if (pid >= 0 )
+ {
+ /* Get the hash_ndx associated with the pid */
+
+ hash_ndx = PIDHASH(pid);
+
+ /* Verify that the correct TCB was found. */
+
+ if (pid == g_pidhash[hash_ndx].pid)
+ {
+ /* Return the TCB associated with this pid (if any) */
+
+ ret = g_pidhash[hash_ndx].tcb;
+ }
+ }
+
+ /* Return the TCB. */
+
+ return ret;
+}
diff --git a/nuttx/sched/sched_lock.c b/nuttx/sched/sched_lock.c
new file mode 100644
index 0000000000..344d3d75b1
--- /dev/null
+++ b/nuttx/sched/sched_lock.c
@@ -0,0 +1,109 @@
+/************************************************************
+ * sched_lock.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <assert.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functionss
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_lock
+ *
+ * Description:
+ * This function disables context switching by disabling
+ * addition of new tasks to the g_readytorun task list.
+ * The task that calls this function will be the only task
+ * that is allowed to run until it either calls
+ * sched_unlock() (the appropriate number of times) or
+ * until it blocks itself.
+ *
+ * Inputs
+ * None
+ *
+ * Return Value:
+ * OK on success; ERROR on failure
+ *
+ ************************************************************/
+
+STATUS sched_lock(void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+
+ /* Check for some special cases: (1) rtcb may be NULL only during
+ * early boot-up phases, and (2) sched_lock() should have no
+ * effect if called from the interrupt level.
+ */
+
+ if (rtcb && !up_interrupt_context())
+ {
+ ASSERT(rtcb->lockcount < MAX_LOCK_COUNT);
+ rtcb->lockcount++;
+ }
+ return OK;
+}
diff --git a/nuttx/sched/sched_lockcount.c b/nuttx/sched/sched_lockcount.c
new file mode 100644
index 0000000000..8db9b116b1
--- /dev/null
+++ b/nuttx/sched/sched_lockcount.c
@@ -0,0 +1,93 @@
+/************************************************************
+ * sched_lockcount.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functionss
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_lockcount
+ *
+ * Description:
+ * This function returns the current value of the lockcount.
+ * If zero, preemption is enabled; if non-zero, this value
+ * indicates the number of times that osTask() has been
+ * called on this thread of execution.
+ *
+ * Inputs:
+ * None
+ *
+ * Return Value:
+ * lockcount
+ *
+ ************************************************************/
+
+sint32 sched_lockcount(void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ return (sint32)rtcb->lockcount;
+}
diff --git a/nuttx/sched/sched_mergepending.c b/nuttx/sched/sched_mergepending.c
new file mode 100644
index 0000000000..db03bccc33
--- /dev/null
+++ b/nuttx/sched/sched_mergepending.c
@@ -0,0 +1,168 @@
+/************************************************************
+ * sched_mergepending.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <queue.h>
+#include <assert.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sched_mergepending
+ *
+ * Description:
+ * This function merges the prioritized g_pendingtasks list
+ * into the prioritized g_readytorun task list.
+ *
+ * Inputs:
+ * None
+ *
+ * Return Value:
+ * TRUE if the head of the g_readytorun task list has changed.
+ *
+ * Assumptions:
+ * - The caller has established a critical section before
+ * calling this function (calling sched_lock() first is NOT
+ * a good idea -- use irqsave()).
+ * - The caller handles the condition that occurs if the
+ * the head of the sched_mergTSTATE_TASK_PENDINGs is changed.
+ *
+ ************************************************************/
+
+boolean sched_mergepending(void)
+{
+ _TCB *pndtcb;
+ _TCB *pndnext;
+ _TCB *rtrtcb;
+ _TCB *rtrprev;
+ boolean ret = FALSE;
+
+ /* Initialize the inner search loop */
+
+ rtrtcb = (_TCB*)g_readytorun.head;
+
+ /* Process every TCB in the g_pendingtasks list */
+
+ for (pndtcb = (_TCB*)g_pendingtasks.head; pndtcb; pndtcb = pndnext)
+ {
+ pndnext = pndtcb->flink;
+
+ /* Search the g_readytorun list to find the location to insert the
+ * new pndtcb. Each is list is maintained in ascending sched_priority
+ * order.
+ */
+
+ for (;
+ (rtrtcb && pndtcb->sched_priority <= rtrtcb->sched_priority);
+ rtrtcb = rtrtcb->flink);
+
+ /* Add the pndtcb to the spot found in the list. Check if the
+ * pndtcb goes at the ends of the g_readytorun list. This would be
+ * error condition since the idle test must always be at the end of
+ * the g_readytorun list!
+ */
+
+ if (!rtrtcb)
+ {
+ PANIC(OSERR_NOIDLETASK);
+ }
+ else
+ {
+ /* The pndtcb goes just before rtrtcb */
+
+ rtrprev = rtrtcb->blink;
+ if (!rtrprev)
+ {
+ /* Special case: Inserting pndtcb at the head of the list */
+
+ pndtcb->flink = rtrtcb;
+ pndtcb->blink = NULL;
+ rtrtcb->blink = pndtcb;
+ g_readytorun.head = (dq_entry_t*)pndtcb;
+ rtrtcb->task_state = TSTATE_TASK_READYTORUN;
+ pndtcb->task_state = TSTATE_TASK_RUNNING;
+ ret = TRUE;
+ }
+ else
+ {
+ /* Insert in the middle of the list */
+
+ pndtcb->flink = rtrtcb;
+ pndtcb->blink = rtrprev;
+ rtrprev->flink = pndtcb;
+ rtrtcb->blink = pndtcb;
+ pndtcb->task_state = TSTATE_TASK_READYTORUN;
+ }
+ }
+
+ /* Set up for the next time through */
+
+ rtrtcb = pndtcb;
+ }
+
+ /* Mark the input list empty */
+
+ g_pendingtasks.head = NULL;
+ g_pendingtasks.tail = NULL;
+
+ return ret;
+}
diff --git a/nuttx/sched/sched_processtimer.c b/nuttx/sched/sched_processtimer.c
new file mode 100644
index 0000000000..2ab4c4bfaf
--- /dev/null
+++ b/nuttx/sched/sched_processtimer.c
@@ -0,0 +1,181 @@
+/************************************************************
+ * sched_processtimer.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+
+#if CONFIG_RR_INTERVAL > 0
+# include <sched.h>
+#endif
+
+#include "os_internal.h"
+#include "wd_internal.h"
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+static inline void sched_process_timeslice(void)
+{
+#if CONFIG_RR_INTERVAL > 0
+ _TCB *rtcb;
+
+ /* Check if the currently executing task uses round robin
+ * scheduling.
+ */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ if ((rtcb->flags & TCB_FLAG_ROUND_ROBIN) != 0)
+ {
+ /* Yes, check if decrementing the timeslice counter
+ * would cause the timeslice to expire
+ */
+
+ if (rtcb->timeslice <= 1)
+ {
+ /* We know we are at the head of the ready to run
+ * prioritized list. We must be the highest priority
+ * task eligible for execution. Check the next task
+ * in the ready to run list. If it is the same
+ * priority, then we need to relinquish the CPU and
+ * give that task a shot.
+ */
+
+ if (rtcb->flink &&
+ rtcb->flink->sched_priority >= rtcb->sched_priority)
+ {
+ struct sched_param param;
+
+ /* Reset the timeslice */
+
+ rtcb->timeslice = CONFIG_RR_INTERVAL;
+
+ /* Just resetting the task priority to its current
+ * value. This this will cause the task to be
+ * rescheduled behind any other tasks at the same
+ * priority.
+ */
+
+ param.sched_priority = rtcb->sched_priority;
+ (void)sched_setparam(0, &param);
+ }
+ }
+ else
+ {
+ /* Decrement the timeslice counter */
+
+ rtcb->timeslice--;
+ }
+ }
+#endif
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * System Timer Hooks
+ *
+ * These are standard interfaces that are exported by the OS
+ * for use by the architecture specific logic
+ *
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_process_timer
+ *
+ * Description:
+ * This function handles system timer events.
+ * The timer interrupt logic itself is implemented in the
+ * architecture specific code, but must call the following OS
+ * function periodically -- the calling interval must be
+ * MSEC_PER_TICK
+ *
+ * Inputs:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ ************************************************************/
+
+void sched_process_timer(void)
+{
+ /* Increment the system time (if in the link) */
+
+ if (clock_timer != NULL)
+ {
+ clock_timer();
+ }
+
+ /* Process watchdogs (if in the link) */
+
+ if (wd_timer != NULL)
+ {
+ wd_timer();
+ }
+
+ /* Check if the currently executing task has exceeded its
+ * timeslice.
+ */
+
+ sched_process_timeslice();
+}
diff --git a/nuttx/sched/sched_releasefiles.c b/nuttx/sched/sched_releasefiles.c
new file mode 100644
index 0000000000..979aca22d1
--- /dev/null
+++ b/nuttx/sched/sched_releasefiles.c
@@ -0,0 +1,90 @@
+/************************************************************
+ * sched_releasefiles.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+
+#include <sched.h>
+#include <nuttx/fs.h>
+#include <nuttx/lib.h>
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sched_releasefiles
+ *
+ * Description:
+ * Release file resources attached to a TCB.
+ *
+ * Parameters:
+ * tcb - tcb of the new task.
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_releasefiles(_TCB *tcb)
+{
+ if (tcb)
+ {
+ /* Free the file descriptor list */
+
+ files_releaselist(tcb->filelist);
+ tcb->filelist = NULL;
+
+#if CONFIG_NFILE_STREAMS > 0
+ /* Free the stream list */
+
+ lib_releaselist(tcb->streams);
+ tcb->streams = NULL;
+#endif /* CONFIG_NFILE_STREAMS */
+ }
+ return OK;
+}
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/sched/sched_releasetcb.c b/nuttx/sched/sched_releasetcb.c
new file mode 100644
index 0000000000..ffbfc6c583
--- /dev/null
+++ b/nuttx/sched/sched_releasetcb.c
@@ -0,0 +1,142 @@
+/************************************************************
+ * sched_releasetcb.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sched.h>
+#include <errno.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_releasepid
+ *
+ * Description: When a task is destroyed, this function must
+ * be called to make its process ID available for re-use.
+ ************************************************************/
+
+static void sched_releasepid(pid_t pid)
+{
+ int hash_ndx = PIDHASH(pid);
+
+ /* Make any pid associated with this hash available. Note:
+ * no special precautions need be taken here because the
+ * following action is atomic
+ */
+
+ g_pidhash[hash_ndx].tcb = NULL;
+ g_pidhash[hash_ndx].pid = INVALID_PROCESS_ID;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sched_releasetcb
+ *
+ * Description:
+ * Free all resources contained in a TCB
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * OK on success; ERROR on failure
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_releasetcb(_TCB *tcb)
+{
+ int ret = OK;
+ int i;
+
+ if (tcb)
+ {
+ /* Release the task's process ID if one was assigned. PID
+ * zero is reserved for the IDLE task. The TCB of the IDLE
+ * task is never release so a value of zero simply means that
+ * the process ID was never allocated to this TCB.
+ */
+
+ if (tcb->pid)
+ {
+ sched_releasepid(tcb->pid);
+ }
+
+ /* Delete the thread's stack if one has been allocated */
+
+ if (tcb->stack_alloc_ptr)
+ {
+ up_release_stack(tcb);
+ }
+
+ /* Release command line arguments that were allocated
+ * for task start/re-start.
+ */
+
+ if ((tcb->flags & TCB_FLAG_PTHREAD) == 0)
+ {
+ for (i = 1; i < NUM_TASK_ARGS+1 && tcb->argv[i]; i++)
+ {
+ sched_free(tcb->argv[i]);
+ }
+ }
+
+ /* Release any allocated file structures */
+
+ ret = sched_releasefiles(tcb);
+
+ /* And, finally, release the TCB itself */
+
+ sched_free(tcb);
+ }
+ return ret;
+}
+
+
+
diff --git a/nuttx/sched/sched_removeblocked.c b/nuttx/sched/sched_removeblocked.c
new file mode 100644
index 0000000000..f3ed363ae0
--- /dev/null
+++ b/nuttx/sched/sched_removeblocked.c
@@ -0,0 +1,113 @@
+/************************************************************
+ * sched_removeblocked.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <queue.h>
+#include <assert.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_removeblocked
+ *
+ * Description:
+ * This function removes a TCB from one of the blocked
+ * state task lists as inferreded from the task_state
+ * inside the TCB.
+ *
+ * Inputs:
+ * btcb - Points to the TCB that is blocked
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ * - The caller has established a critical section before
+ * calling this function.
+ *
+ ************************************************************/
+
+void sched_removeblocked(_TCB *btcb)
+{
+ tstate_t task_state = btcb->task_state;
+
+ /* Make sure the TCB is in a valid blocked state */
+
+ ASSERT(task_state >= FIRST_BLOCKED_STATE &&
+ task_state <= LAST_BLOCKED_STATE);
+
+ /* Remove the TCB from the blocked task list associated
+ * with this state
+ */
+
+ dq_rem((dq_entry_t*)btcb, g_tasklisttable[task_state].list);
+
+ /* Make sure the TCB's state corresponds to not being in
+ * any list
+ */
+
+ btcb->task_state = TSTATE_TASK_INVALID;
+}
diff --git a/nuttx/sched/sched_removereadytorun.c b/nuttx/sched/sched_removereadytorun.c
new file mode 100644
index 0000000000..24057f3b72
--- /dev/null
+++ b/nuttx/sched/sched_removereadytorun.c
@@ -0,0 +1,117 @@
+/************************************************************
+ * sched_removereadytorun.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <queue.h>
+#include <assert.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_removereadytorun
+ *
+ * Description:
+ * This function removes a TCB from the ready to run list.
+ *
+ * Inputs:
+ * rtcb - Points to the TCB that is ready-to-run
+ *
+ * Return Value:
+ * TRUE if the currently active task (the head of the
+ * g_readytorun list) has changed.
+ *
+ * Assumptions:
+ * - The caller has established a critical section before
+ * calling this function (calling sched_lock() first is NOT
+ * a good idea -- use irqsave()).
+ * - The caller handles the condition that occurs if the
+ * the head of the g_readytorun list is changed.
+ ************************************************************/
+
+boolean sched_removereadytorun(_TCB *rtcb)
+{
+ boolean ret = FALSE;
+
+ /* Check if the TCB to be removed is at the head of the ready
+ * to run list. In this case, we are removing the currently
+ * active task.
+ */
+
+ if (!rtcb->blink)
+ {
+ ASSERT(rtcb->flink != NULL);
+
+ /* Inform the instrumentation layer that we are switching tasks */
+
+ sched_note_switch(rtcb, rtcb->flink);
+
+ rtcb->flink->task_state = TSTATE_TASK_RUNNING;
+ ret = TRUE;
+ }
+
+ /* Remove the TCB from the ready-to-run list */
+
+ dq_rem((dq_entry_t*)rtcb, &g_readytorun);
+
+ rtcb->task_state = TSTATE_TASK_INVALID;
+ return ret;
+}
diff --git a/nuttx/sched/sched_rrgetinterval.c b/nuttx/sched/sched_rrgetinterval.c
new file mode 100644
index 0000000000..8dfe99ff35
--- /dev/null
+++ b/nuttx/sched/sched_rrgetinterval.c
@@ -0,0 +1,156 @@
+/************************************************************
+ * sched_rrgetinterval.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <errno.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_rr_get_interval
+ *
+ * Description:
+ * sched_rr_get_interval() writes the timeslice interval
+ * for task identified by 'pid' into the timespec structure
+ * pointed to by 'interval.' If pid is zero, the timeslice
+ * for the calling process is written into 'interval. The
+ * identified process should be running under the SCHED_RR
+ * scheduling policy.'
+ *
+ * Inputs:
+ * pid - the task ID of the task. If pid is zero, the
+ * priority of the calling task is returned.
+ * interval - a structure used to return the time slice
+ *
+ * Return Value:
+ * On success, sched_rr_get_interval() returns OK (0). On
+ * error, ERROR (-1) is returned, and errno is set to:
+ *
+ * EFAULT -- Cannot copy to interval
+ * EINVAL Invalid pid.
+ * ENOSYS The system call is not yet implemented.
+ * ESRCH The process whose ID is pid could not be found.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_rr_get_interval(pid_t pid, struct timespec *interval)
+{
+#if CONFIG_RR_INTERVAL > 0
+ _TCB *rrtcb;
+
+ /* If pid is zero, the timeslice for the calling process is
+ * written into 'interval.'
+ */
+
+ if (!pid)
+ {
+ rrtcb = (_TCB*)g_readytorun.head;
+ }
+
+ /* Return a special error code on invalid PID */
+
+ else if (pid < 0)
+ {
+ *get_errno_ptr() = EINVAL;
+ return ERROR;
+ }
+
+ /* Otherwise, lookup the TCB associated with this pid */
+
+ else
+ {
+ rrtcb = sched_gettcb(pid);
+ if (!rrtcb)
+ {
+ *get_errno_ptr() = ESRCH;
+ return ERROR;
+ }
+ }
+
+ if (!interval)
+ {
+ *get_errno_ptr() = EFAULT;
+ return ERROR;
+ }
+
+ /* Convert the timeslice value from ticks to timespec */
+
+ interval->tv_sec = CONFIG_RR_INTERVAL / MSEC_PER_SEC;
+ interval->tv_nsec = (CONFIG_RR_INTERVAL % MSEC_PER_SEC) * NSEC_PER_MSEC;
+
+ return OK;
+#else
+ *get_errnor_ptr() = ENOSYS;
+ return ERROR;
+#endif
+}
diff --git a/nuttx/sched/sched_setparam.c b/nuttx/sched/sched_setparam.c
new file mode 100644
index 0000000000..482b62ddbf
--- /dev/null
+++ b/nuttx/sched/sched_setparam.c
@@ -0,0 +1,266 @@
+/************************************************************
+ * sched_setparam.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_setparam
+ *
+ * Description:
+ * This function sets the priority of a specified task.
+ *
+ * NOTE: Setting a task's priority to the same value has the
+ * similar effect to sched_yield() -- The task will be moved to
+ * after all other tasks with the same priority.
+ *
+ * Inputs:
+ * pid - the task ID of the task to reprioritize. If pid is
+ * zero, the priority of the calling task is changed.
+ * param - A structure whose member sched_priority is the integer
+ * priority. The range of valid priority numbers is from
+ * SCHED_PRIORITY_MIN through SCHED_PRIORITY_MAX.
+ *
+ * Return Value:
+ * OK if successful, otherwise ERROR. This function can
+ * fail for the following reasons:
+ *
+ * (1) parm is NULL or parm->sched_priority is out of
+ * range.
+ * (2) pid does not correspond to any task.
+ *
+ * (errno is not set).
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_setparam(pid_t pid, const struct sched_param *param)
+{
+ _TCB *rtcb;
+ _TCB *tcb;
+ tstate_t task_state;
+ uint32 saved_state;
+ int sched_priority = param->sched_priority;
+ int ret = 0;
+
+ /* Verify that the requested priority is in the valid range */
+
+ if (!param ||
+ param->sched_priority < SCHED_PRIORITY_MIN ||
+ param->sched_priority > SCHED_PRIORITY_MAX)
+ {
+ return ERROR;
+ }
+
+ /* Prohibit modifications to the head of the ready-to-run task
+ * list while adjusting the priority
+ */
+
+ sched_lock();
+
+ /* Check if the task to reprioritize is the calling task */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ if (pid == 0 || pid == rtcb->pid)
+ {
+ tcb = rtcb;
+ }
+
+ /* The pid is not the calling task, we will have to search for it */
+
+ else
+ {
+ tcb = sched_gettcb(pid);
+ if (!tcb)
+ {
+ /* No task with this pid was found */
+
+ sched_unlock();
+ return ERROR;
+ }
+ }
+
+ /* We need to assure that there there is no interrupt activity while
+ * performing the following.
+ */
+
+ saved_state = irqsave();
+
+ /* There are four cases that must be considered: */
+
+ task_state = tcb->task_state;
+ switch (task_state)
+ {
+ /* CASE 1. The task is running or ready-to-run and a context switch
+ * may be caused by the re-prioritization
+ */
+
+ case TSTATE_TASK_RUNNING:
+
+ /* A context switch will occur if the new priority of the running
+ * task becomes less than OR EQUAL TO the next highest priority
+ * ready to run task.
+ */
+
+ if (sched_priority <= tcb->flink->sched_priority)
+ {
+ /* A context switch will occur. */
+
+ up_reprioritize_rtr(tcb, (ubyte)sched_priority);
+ }
+
+ /* Otherwise, we can just change priority since it has no effect */
+
+ else
+ {
+ /* Change the task priority */
+
+ tcb->sched_priority = (ubyte)sched_priority;
+ }
+ break;
+
+ /* CASE 2. The task is running or ready-to-run and a context switch
+ * may be caused by the re-prioritization
+ */
+
+ case TSTATE_TASK_READYTORUN:
+
+ /* A context switch will occur if the new priority of the ready-to
+ * run task is (strictly) greater than the current running task
+ */
+
+ if (sched_priority > rtcb->sched_priority)
+ {
+ /* A context switch will occur. */
+
+ up_reprioritize_rtr(tcb, (ubyte)sched_priority);
+ }
+
+ /* Otherwise, we can just change priority and re-schedule (since it
+ * have no other effect).
+ */
+
+ else
+ {
+ /* Remove the TCB from the ready-to-run task list */
+
+ ASSERT(!sched_removereadytorun(tcb));
+
+ /* Change the task priority */
+
+ tcb->sched_priority = (ubyte)sched_priority;
+
+ /* Put it back into the ready-to-run task list */
+
+ ASSERT(!sched_addreadytorun(tcb));
+ }
+ break;
+
+ /* CASE 3. The task is not in the ready to run list. Changing its
+ * Priority cannot effect the currently executing task.
+ */
+
+ default:
+ /* CASE 3a. The task resides in a prioritized list. */
+
+ if (g_tasklisttable[task_state].prioritized)
+ {
+ /* Remove the TCB from the prioritized task list */
+
+ dq_rem((dq_entry_t*)tcb, g_tasklisttable[task_state].list);
+
+ /* Change the task priority */
+
+ tcb->sched_priority = (ubyte)sched_priority;
+
+ /* Put it back into the prioritized list at the correct
+ * position
+ */
+
+ sched_addprioritized(tcb, g_tasklisttable[task_state].list);
+ }
+
+ /* CASE 3b. The task resides in a non-prioritized list. */
+
+ else
+ {
+ /* Just change the task's priority */
+
+ tcb->sched_priority = (ubyte)sched_priority;
+ }
+ break;
+ }
+
+ irqrestore(saved_state);
+ sched_unlock();
+ return ret;
+}
diff --git a/nuttx/sched/sched_setscheduler.c b/nuttx/sched/sched_setscheduler.c
new file mode 100644
index 0000000000..29c0d0c98e
--- /dev/null
+++ b/nuttx/sched/sched_setscheduler.c
@@ -0,0 +1,188 @@
+/************************************************************
+ * sched_setscheduler.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sched.h>
+#include <errno.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name:sched_setscheduler
+ *
+ * Description:
+ * sched_setscheduler() sets both the scheduling policy
+ * and the priority for the task identified by pid. If
+ * pid equals zero, the scheduler of the calling task
+ * will be set. The parameter 'param' holds the priority
+ * of the thread under the new policy.
+ *
+ * Inputs:
+ * pid - the task ID of the task to modify. If pid is zero,
+ * the calling task is modified.
+ * policy - Scheduling policy requested (either SCHED_FIFO
+ * or SCHED_RR)
+ * param - A structure whose member sched_priority is the
+ * new priority. The range of valid priority numbers is
+ * from SCHED_PRIORITY_MIN through SCHED_PRIORITY_MAX.
+ *
+ * Return Value:
+ * On success, sched_setscheduler() returns OK (zero). On
+ * error, ERROR (-1) is returned, and errno is set
+ * appropriately:
+ *
+ * EINVAL The scheduling policy is not one of the
+ * recognized policies.
+ * ESRCH The task whose ID is pid could not be found.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_setscheduler(pid_t pid, int policy,
+ const struct sched_param *param)
+{
+ _TCB *tcb;
+#if CONFIG_RR_INTERVAL > 0
+ uint32 saved_state;
+#endif
+ int ret;
+
+ /* Check for supported scheduling policy */
+
+#if CONFIG_RR_INTERVAL > 0
+ if (policy != SCHED_FIFO && policy != SCHED_RR)
+#else
+ if (policy != SCHED_FIFO)
+#endif
+ {
+ *get_errno_ptr() = EINVAL;
+ return ERROR;
+ }
+
+ /* Check if the task to modify the calling task */
+
+ if (pid == 0 )
+ {
+ pid = getpid();
+ }
+
+ /* Verify that the pid corresponds to a real task */
+
+ tcb = sched_gettcb(pid);
+ if (!tcb)
+ {
+ *get_errno_ptr() = ESRCH;
+ return ERROR;
+ }
+
+ /* Prohibit any context switches while we muck with
+ * priority and scheduler settings.
+ */
+
+ sched_lock();
+
+#if CONFIG_RR_INTERVAL > 0
+ /* Further, disable timer interrupts while we set up
+ * scheduling policy.
+ */
+
+ saved_state = irqsave();
+ if (policy == SCHED_RR)
+ {
+ /* Set round robin scheduling */
+
+ tcb->flags |= TCB_FLAG_ROUND_ROBIN;
+ tcb->timeslice = CONFIG_RR_INTERVAL / MSEC_PER_TICK;
+ }
+ else
+ {
+ /* Set FIFO scheduling */
+ tcb->flags &= ~TCB_FLAG_ROUND_ROBIN;
+ tcb->timeslice = 0;
+ }
+ irqrestore(saved_state);
+#endif
+
+ /* Set the new priority */
+
+ ret = sched_setparam(pid, param);
+ sched_unlock();
+
+ if (ret != OK)
+ {
+ return ERROR;
+ }
+ else
+ {
+ return SCHED_FIFO;
+ }
+}
diff --git a/nuttx/sched/sched_setupidlefiles.c b/nuttx/sched/sched_setupidlefiles.c
new file mode 100644
index 0000000000..c8be5478c8
--- /dev/null
+++ b/nuttx/sched/sched_setupidlefiles.c
@@ -0,0 +1,113 @@
+/************************************************************
+ * sched_setupidlefiles.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sched.h>
+#include <errno.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sched_setupidlefiles
+ *
+ * Description:
+ * Configure the idle thread's TCB.
+ *
+ * Parameters:
+ * tcb - tcb of the idle task.
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_setupidlefiles(_TCB *tcb)
+{
+ int fd;
+
+ /* Allocate file descriptors for the TCB */
+
+ tcb->filelist = files_alloclist();
+ if (!tcb->filelist)
+ {
+ *get_errno_ptr() = ENOMEM;
+ return ERROR;
+ }
+
+#ifdef CONFIG_DEV_CONSOLE
+ /* Open stdin, dup to get stdout and stderr. */
+
+ fd = open("/dev/console", O_RDWR);
+ if (fd == 0)
+ {
+ (void)dup2(0, 1);
+ (void)dup2(0, 2);
+ }
+ else
+ {
+ (void)close(fd);
+ *get_errno_ptr() = ENFILE;
+ return ERROR;
+ }
+
+#if CONFIG_NFILE_STREAMS > 0
+ /* Allocate file strems for the TCB */
+
+ return sched_setupstreams(tcb);
+#else
+ return OK;
+#endif /* CONFIG_NFILE_STREAMS */
+#endif /* CONFIG_DEV_CONSOLE */
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/sched/sched_setuppthreadfiles.c b/nuttx/sched/sched_setuppthreadfiles.c
new file mode 100644
index 0000000000..0935b487d8
--- /dev/null
+++ b/nuttx/sched/sched_setuppthreadfiles.c
@@ -0,0 +1,94 @@
+/************************************************************
+ * sched_setuppthreadfiles.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+
+#include <sched.h>
+#include <nuttx/fs.h>
+#include <nuttx/lib.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sched_setuppthreadfiles
+ *
+ * Description:
+ * Configure a newly allocated TCB so that it will inherit
+ * file descriptors and streams from the parent pthread.
+ *
+ * Parameters:
+ * tcb - tcb of the new task.
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_setuppthreadfiles(_TCB *tcb)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+
+ /* The child thread inherits the parent file descriptors */
+
+ tcb->filelist = rtcb->filelist;
+ files_addreflist(tcb->filelist);
+
+#if CONFIG_NFILE_STREAMS > 0
+ /* The child thread inherits the parent streams */
+
+ tcb->streams = rtcb->streams;
+ lib_addreflist(tcb->streams);
+
+#endif /* CONFIG_NFILE_STREAMS */
+
+ return OK;
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/sched/sched_setupstreams.c b/nuttx/sched/sched_setupstreams.c
new file mode 100644
index 0000000000..325ba92e98
--- /dev/null
+++ b/nuttx/sched/sched_setupstreams.c
@@ -0,0 +1,80 @@
+/************************************************************
+ * sched_setupstreams.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0
+
+#include <sched.h>
+#include <nuttx/fs.h>
+#include <nuttx/lib.h>
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+int sched_setupstreams(_TCB *tcb)
+{
+ /* Allocate file strems for the TCB */
+
+ tcb->streams = lib_alloclist();
+ if (tcb->streams)
+ {
+ /* fdopen to get the stdin, stdout and stderr streams.
+ * The following logic depends on the fact that the library
+ * layer will allocate FILEs in order.
+ *
+ * fd = 0 is stdin
+ * fd = 1 is stdout
+ * fd = 2 is stderr
+ */
+
+ (void)lib_fdopen(0, "r", tcb->filelist, tcb->streams);
+ (void)lib_fdopen(1, "w", tcb->filelist, tcb->streams);
+ (void)lib_fdopen(2, "w", tcb->filelist, tcb->streams);
+ }
+
+ return OK;
+}
+
+#endif /* CONFIG_NFILE_STREAMS && CONFIG_NFILE_STREAMS*/
diff --git a/nuttx/sched/sched_setuptaskfiles.c b/nuttx/sched/sched_setuptaskfiles.c
new file mode 100644
index 0000000000..269e98e51f
--- /dev/null
+++ b/nuttx/sched/sched_setuptaskfiles.c
@@ -0,0 +1,112 @@
+/************************************************************
+ * sched_setuptaskfiles.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+
+#include <sched.h>
+#include <errno.h>
+#include <nuttx/fs.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sched_setuptaskfiles
+ *
+ * Description:
+ * Configure a newly allocated TCB so that it will inherit
+ * file descriptors and streams from the parent task.
+ *
+ * Parameters:
+ * tcb - tcb of the new task.
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_setuptaskfiles(_TCB *tcb)
+{
+#ifdef CONFIG_DEV_CONSOLE
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ int i;
+#endif /* CONFIG_DEV_CONSOLE */
+
+ /* Allocate file descriptors for the TCB */
+
+ tcb->filelist = files_alloclist();
+ if (!tcb->filelist)
+ {
+ *get_errno_ptr() = ENOMEM;
+ return ERROR;
+ }
+
+#ifdef CONFIG_DEV_CONSOLE
+ /* Duplicate the first three file descriptors */
+
+ if (rtcb->filelist)
+ {
+ for (i = 0; i < 3; i++)
+ {
+ (void)files_dup(&rtcb->filelist->fl_files[i],
+ &tcb->filelist->fl_files[i]);
+ }
+ }
+
+#if CONFIG_NFILE_STREAMS > 0
+ /* Allocate file strems for the TCB */
+
+ return sched_setupstreams(tcb);
+#else
+ return OK;
+#endif /* CONFIG_NFILE_STREAMS */
+#endif /* CONFIG_DEV_CONSOLE */
+}
+
+#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/sched/sched_unlock.c b/nuttx/sched/sched_unlock.c
new file mode 100644
index 0000000000..773b3dd716
--- /dev/null
+++ b/nuttx/sched/sched_unlock.c
@@ -0,0 +1,122 @@
+/************************************************************
+ * sched_unlock.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functionss
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_unlock
+ *
+ * Description: This function decrements the preemption lock
+ * count. Typically this is paired with sched_lock() and
+ * concludes a critical section of code. Preemption will not
+ * be unlocked until sched_unlock() has been called as many
+ * times as sched_lock(). When the lockcount is decremented
+ * to zero, any tasks that were eligible to preempt the
+ * current task will execute.
+ ************************************************************/
+
+STATUS sched_unlock(void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+
+ /* Check for some special cases: (1) rtcb may be NULL only during
+ * early boot-up phases, and (2) sched_unlock() should have no
+ * effect if called from the interrupt level.
+ */
+
+ if (rtcb && !up_interrupt_context())
+ {
+ /* Decrement the preemption lock counter */
+
+ if (rtcb->lockcount)
+ {
+ rtcb->lockcount--;
+ }
+
+ /* Check if the lock counter has decremented to zero. If so,
+ * then pre-emption has been re-enabled.
+ */
+
+ if (rtcb->lockcount <= 0)
+ {
+ rtcb->lockcount = 0;
+
+ /* Release any ready-to-run tasks that have collected in
+ * g_pendingtasks.
+ */
+
+ if (g_pendingtasks.head)
+ {
+ up_release_pending();
+ }
+ }
+ }
+ return OK;
+}
diff --git a/nuttx/sched/sched_yield.c b/nuttx/sched/sched_yield.c
new file mode 100644
index 0000000000..e91f8ecf6b
--- /dev/null
+++ b/nuttx/sched/sched_yield.c
@@ -0,0 +1,103 @@
+/************************************************************
+ * sched_yield.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: sched_yield
+ *
+ * Description:
+ * This function forces the calling task to give up the CPU
+ * (only to other tasks at the same priority).
+ *
+ * Inputs:
+ * None
+ *
+ * Return Value:
+ * 0 (OK) or -1 (ERROR) (errno is not set)
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sched_yield (void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ struct sched_param param;
+
+ /* This equivalent to just resetting the task priority to
+ * its current value since this will cause the task to
+ * be rescheduled behind any other tasks at the same priority.
+ */
+
+ param.sched_priority = rtcb->sched_priority;
+ return sched_setparam(0, &param);
+}
diff --git a/nuttx/sched/sem_close.c b/nuttx/sched/sem_close.c
new file mode 100644
index 0000000000..3113b5156b
--- /dev/null
+++ b/nuttx/sched/sem_close.c
@@ -0,0 +1,144 @@
+/************************************************************
+ * sem_close.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <errno.h>
+#include <semaphore.h>
+#include <sched.h>
+#include "os_internal.h"
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_close
+ *
+ * Description:
+ * This function is called to indicate that the calling\
+ * task is finished with the specified named semaphore,
+ * sem. The sem_close() deallocates any system resources
+ * allocated by the system for this named semaphore.
+ *
+ * If the semaphore has not been removed with a call to
+ * sem_unlink(), then sem_close() has no effect on the
+ * named semaphore. However, when the named semaphore has
+ * been fully unlinked, the semaphore will vanish when the
+ * last task closes it.
+ *
+ * Parameters:
+ * sem - semaphore descriptor
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if unsuccessful.
+ *
+ * Assumptions:
+ * - Care must be taken to avoid risking the deletion of
+ * a semaphore that another calling task has already
+ * locked.
+ * - sem_close must not be called for an un-named semaphore
+ *
+ ************************************************************/
+
+int sem_close (sem_t *sem)
+{
+ nsem_t *psem;
+ int ret = ERROR;
+
+ /* Verify the inputs */
+
+ if (sem)
+ {
+ sched_lock();
+
+ /* Search the list of named semaphores */
+
+ for (psem = (nsem_t*)g_nsems.head;
+ ((psem) && (sem != &psem->sem));
+ psem = psem->flink);
+
+ /* Check if we found it */
+
+ if (psem)
+ {
+ /* Decrement the count of sem_open connections to this semaphore */
+
+ if (psem->nconnect) psem->nconnect--;
+
+ /* If the semaphore is no long connected to any processes AND the
+ * semaphore was previously unlinked, then deallocate it.
+ */
+
+ if (!psem->nconnect && psem->unlinked)
+ {
+ dq_rem((dq_entry_t*)psem, &g_nsems);
+ sched_free(psem);
+ }
+ ret = OK;
+ }
+ sched_unlock();
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sem_destroy.c b/nuttx/sched/sem_destroy.c
new file mode 100644
index 0000000000..1d88563ffa
--- /dev/null
+++ b/nuttx/sched/sem_destroy.c
@@ -0,0 +1,122 @@
+/************************************************************
+ * sem_destroy.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_destroy
+ *
+ * Description:
+ * This function is used to destroy the un-named semaphore
+ * indicated by sem. Only a semaphore that was created
+ * using sem_init() may be destroyed using sem_destroy();
+ * the effect of calling sem_destroy() with a name semaphore
+ * is undefined. The effect of subsequent use of the
+ * semaphore sem is undefined until sem is re-initialized
+ * by another call to sem_init().
+ *
+ * The effect of destroying a semaphore upon which other
+ * processes are currently blocked is undefined.
+ *
+ * Parameters:
+ * sem - Semaphore to be destroyed.
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if unsuccessful.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sem_destroy (sem_t *sem)
+{
+ int ret = ERROR;
+
+ /* Assure a valid semaphore is specified */
+
+ if (sem)
+ {
+ /* There is really no particular action that we need
+ * take to destroy a semaphore. We will just reset
+ * the count to some reasonable value (1).
+ *
+ * Check if other threads are waiting on the semaphore.
+ * In this case, the behavior is undefined. We will:
+ * leave the count unchanged but still return OK.
+ */
+
+ if (sem->semcount >= 0)
+ {
+ sem->semcount = 1;
+ }
+ ret = OK;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sem_findnamed.c b/nuttx/sched/sem_findnamed.c
new file mode 100644
index 0000000000..d513f7a31f
--- /dev/null
+++ b/nuttx/sched/sem_findnamed.c
@@ -0,0 +1,103 @@
+/************************************************************
+ * sem_findnamed.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <string.h>
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_findnamed
+ *
+ * Description:
+ * Search the g_nsems list to find the semaphore with
+ * the matching name.
+ *
+ * Parameters:
+ * name - semaphore name
+ *
+ * Return Value:
+ * Pointer to the semaphore or NULL if not found
+ *
+ ************************************************************/
+
+nsem_t *sem_findnamed(const char *name)
+{
+ nsem_t *psem;
+
+ /* Search the list of named semaphores */
+
+ for (psem = (nsem_t*)g_nsems.head; (psem); psem = psem->flink)
+ {
+ if (!strcmp(name, psem->name))
+ {
+ break;
+ }
+ }
+
+ return(psem);
+}
+
diff --git a/nuttx/sched/sem_getvalue.c b/nuttx/sched/sem_getvalue.c
new file mode 100644
index 0000000000..b7dd201c70
--- /dev/null
+++ b/nuttx/sched/sem_getvalue.c
@@ -0,0 +1,112 @@
+/************************************************************
+ * sem_getvalue.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_getvalue
+ *
+ * Description:
+ * This function updates the location referenced by sval
+ * argument to have the value of the semaphore referenced
+ * by sem without effecting the state of the semaphore.
+ * The updated value represents the actual semaphore value
+ * that occurred at some unspecified time during the call,
+ * but may not reflect the actual value of the semaphore
+ * when it is returned to the calling task.
+ *
+ * If sem is locked, the value return by sem_getvalue()
+ * will either be zero or a negative number whose absolute
+ * value represents the number of tasks waiting for the
+ * semaphore.
+ *
+ * Parameters:
+ * sem - Semaphore descriptor
+ * sval - Buffer by which the value is returned
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if unsuccessful
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sem_getvalue(sem_t *sem, int *sval)
+{
+ int ret = ERROR;
+
+ if (sem && sval)
+ {
+ *sval = sem->semcount;
+ ret = OK;
+ }
+
+ return ret;
+}
+
diff --git a/nuttx/sched/sem_init.c b/nuttx/sched/sem_init.c
new file mode 100644
index 0000000000..36f2b99427
--- /dev/null
+++ b/nuttx/sched/sem_init.c
@@ -0,0 +1,110 @@
+/************************************************************
+ * sem_init.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_init
+ *
+ * Description:
+ * This function initializes the UNAMED semaphore sem.
+ * Following a successful call to sem_init(), the
+ * semaophore may be used in subsequent calls to
+ * sem_wait(), sem_post(), and sem_trywait(). The
+ * semaphore remains usable until it is destroyed.
+ *
+ * Only sem itself may be used for performing
+ * synchronization. The result of referring to copies of
+ * sem in calls to sem_wait(), sem_trywait(), sem_post(),
+ * and sem_destroy() is undefined.
+ *
+ * Parameters:
+ * sem - Semaphore to be initialized
+ * pshared - Process sharing (not used)
+ * value - Semaphore initialization value
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if unsuccessful.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sem_init (sem_t *sem, int pshared, unsigned int value)
+{
+ int ret = ERROR;
+
+ if (sem && value <= SEM_MAX_VALUE)
+ {
+ sem->semcount = (sint16)value;
+ ret = OK;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sem_initialize.c b/nuttx/sched/sem_initialize.c
new file mode 100644
index 0000000000..ed5e755990
--- /dev/null
+++ b/nuttx/sched/sem_initialize.c
@@ -0,0 +1,99 @@
+/************************************************************
+ * sem_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <queue.h>
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* This is a list of dyanamically allocated named semaphores */
+
+dq_queue_t g_nsems;
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_initialize
+ *
+ * Description:
+ * The control structures for all semaphores may be
+ * initialized by calling sem_initialize. This should be
+ * done once at poweron.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+void sem_initialize(void)
+{
+ /* Initialize the queue of named semaphores */
+
+ dq_init(&g_nsems);
+}
diff --git a/nuttx/sched/sem_internal.h b/nuttx/sched/sem_internal.h
new file mode 100644
index 0000000000..460b358fc6
--- /dev/null
+++ b/nuttx/sched/sem_internal.h
@@ -0,0 +1,99 @@
+/************************************************************
+ * sem_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __SEM_INTERNAL_H
+#define __SEM_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include <sched.h>
+#include <queue.h>
+#include <nuttx/compiler.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public Type Declarations
+ ************************************************************/
+
+/* This is the named semaphore structure */
+
+struct nsem_s
+{
+ struct nsem_s *flink; /* Forward link */
+ struct nsem_s *blink; /* Backward link */
+ uint16 nconnect; /* Number of connections to semaphore */
+ char *name; /* Semaphore name (NULL if un-named) */
+ boolean unlinked; /* TRUE if the semaphore has been unlinked */
+ sem_t sem; /* The semaphore itself */
+};
+typedef struct nsem_s nsem_t;
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/* This is a list of dyanamically allocated named semaphores */
+
+extern dq_queue_t g_nsems;
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN void weak_function sem_initialize(void);
+EXTERN void sem_waitirq(_TCB *wtcb);
+EXTERN nsem_t *sem_findnamed(const char *name);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SEM_INTERNAL_H */
+
diff --git a/nuttx/sched/sem_open.c b/nuttx/sched/sem_open.c
new file mode 100644
index 0000000000..f6139ec5e4
--- /dev/null
+++ b/nuttx/sched/sem_open.c
@@ -0,0 +1,206 @@
+/************************************************************
+ * sem_open.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <stdarg.h>
+#include <string.h>
+#include <semaphore.h>
+#include <nuttx/kmalloc.h>
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_open
+ *
+ * Description:
+ * This function establishes a connection between named
+ * semaphores and a task. Following a call to sem_open()
+ * with the semaphore name, the task may reference the
+ * semaphore associated with name using the address
+ * returned by this call. The semaphore may be used in
+ * subsequent calls to sem_wait(), sem_trywait(), and
+ * sem_post(). The semaphore remains usable until the
+ * semaphore is closed by a successful call to sem_close().
+ *
+ * If a task makes multiple calls to sem_open() with the
+ * same name, then the same semaphore address is returned
+ * (provided there have been no calls to sem_unlink()).
+ *
+ * Parameters:
+ * name - Semaphore name
+ * oflag - Semaphore creation options. This may either
+ * or both of the following bit settings.
+ * oflag = 0: Connect to the semaphore only if it
+ * already exists.
+ * oflag = O_CREAT: Connect to the semaphore if it
+ * exists, otherwise create the semaphore.
+ * oflag = O_CREAT|O_EXCL: Create a new semaphore
+ * unless one of this name already exists.
+ * Optional parameters. When the O_CREAT flag is specified,
+ * two optional parameters are expected:
+ * 1. mode_t mode (ignored), and
+ * 2. unsigned int value. This initial value of the semaphore.
+ * valid initial values of the semaphore must be less than
+ * or equal to SEM_MAX_VALUE.
+ *
+ * Return Value:
+ * A pointer to sem_t or -1 (ERROR) if unsuccessful.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+sem_t *sem_open (const char *name, int oflag, ...)
+{
+ int namelen;
+ nsem_t *psem;
+ sem_t *sem = (sem_t*)ERROR;
+ va_list arg; /* Points to each un-named argument */
+ mode_t mode; /* Creation mode parameter (ignored) */
+ unsigned int value; /* Semaphore value parameter */
+
+ /* Make sure that a non-NULL name is supplied */
+
+ if (name)
+ {
+ /* The POSIX specification requires that the "check for the
+ * existence of a semaphore and the creation of the semaphore
+ * if it does not exist shall be atomic with respect to other
+ * processes executing sem_open()..." A simple sched_lock()
+ * should be sufficient to meet this requirement.
+ */
+
+ sched_lock();
+ namelen = strlen(name);
+ if (namelen > 0)
+ {
+ /* See if the semaphore already exists */
+
+ psem = sem_findnamed(name);
+ if (psem)
+ {
+ /* It does. Check if the caller wanted to created
+ * a new semahore with this name.
+ */
+
+ if (!(oflag & O_CREAT) || !(oflag & O_EXCL))
+ {
+ /* Allow a new connection to the semaphore */
+
+ psem->nconnect++;
+ sem = &psem->sem;
+ }
+ }
+
+ /* It doesn't exist. Should we create one? */
+
+ else if ((oflag & O_CREAT) != 0)
+ {
+ /* Set up to get the optional arguments needed to create
+ * a message queue.
+ */
+
+ va_start(arg, oflag);
+ mode = va_arg(arg, mode_t);
+ value = va_arg(arg, unsigned int);
+
+ /* Verify that a legal initial value was selected. */
+
+ if (value <= SEM_MAX_VALUE)
+ {
+ /* Allocate memory for the new semaphore */
+
+ psem = (nsem_t*)kmalloc((sizeof(nsem_t) + namelen + 1));
+ if (psem)
+ {
+ /* Initialize the named semaphore */
+
+ sem = &psem->sem;
+ sem_init(sem, 0, value);
+
+ psem->nconnect = 1;
+ psem->unlinked = FALSE;
+ psem->name = (char*)psem + sizeof(nsem_t);
+ strcpy(psem->name, name);
+
+ /* Add the new semaphore to the list of named
+ * semaphores
+ */
+
+ dq_addfirst((dq_entry_t*)psem, &g_nsems);
+ }
+
+ /* Clean-up variable argument stuff */
+
+ va_end(arg);
+ }
+ }
+ }
+ sched_unlock();
+ }
+
+ return sem;
+}
diff --git a/nuttx/sched/sem_post.c b/nuttx/sched/sem_post.c
new file mode 100644
index 0000000000..c07b1d5343
--- /dev/null
+++ b/nuttx/sched/sem_post.c
@@ -0,0 +1,165 @@
+/************************************************************
+ * sem_post.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include <sched.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_post
+ *
+ * Description:
+ * When a task has finished with a semaphore, it will call
+ * sem_post(). This function unlocks the semaphore
+ * referenced by sem by performing the semaphore unlock
+ * operation on that semaphore.
+ *
+ * If the semaphore value resulting from this operation
+ * is positive, then no tasks were blocked waiting for the
+ * semaphore to become unlocked; the semaphore is simply
+ * incremented.
+ *
+ * If the value of the semaphore resulting from this
+ * operation is zero, then one of the tasks blocked
+ * waiting for the semaphore shall be allowed to return
+ * successfully from its call to sem_wait().
+ *
+ * Parameters:
+ * sem - Semaphore descriptor
+ *
+ * Return Value:
+ * 0 (OK) or -1 (ERROR) if unsuccessful
+ *
+ * Assumptions:
+ * This function cannot be called from an interrupt handler.
+ * It assumes the currently executing task is the one that
+ * is performing the unlock.
+ *
+ ************************************************************/
+
+int sem_post (sem_t *sem)
+{
+ _TCB *stcb;
+ STATUS ret = ERROR;
+ uint32 saved_state;
+
+ /* Make sure we were supplied with a valid semaphore. */
+
+ if (sem)
+ {
+ /* The following operations must be performed with interrupts
+ * disabled because sem_post() may be called from an interrupt
+ * handler.
+ */
+
+ saved_state = irqsave();
+
+ /* Perform the semaphore unlock operation. */
+
+ ASSERT(sem->semcount < SEM_MAX_VALUE);
+ sem->semcount++;
+
+ /* If the result of of semaphore unlock is non-positive, then
+ * there must be some task waiting for the semaphore.
+ */
+
+ if (sem->semcount <= 0)
+ {
+ /* Check if there are any tasks in the waiting for semaphore
+ * task list that are waiting for this semaphore. This is a
+ * prioritized list so the first one we encounter is the one
+ * that we want.
+ */
+
+ for (stcb = (_TCB*)g_waitingforsemaphore.head;
+ ((stcb) && (stcb->waitsem != sem));
+ stcb = stcb->flink);
+
+ if (stcb)
+ {
+ /* It is, let the task take the semaphore */
+
+ stcb->waitsem = NULL;
+
+ /* Restart the waiting task. */
+
+ up_unblock_task(stcb);
+ }
+ }
+ ret = OK;
+
+ /* Interrupts may now be enabled. */
+
+ irqrestore(saved_state);
+ }
+
+ return ret;
+}
+
diff --git a/nuttx/sched/sem_trywait.c b/nuttx/sched/sem_trywait.c
new file mode 100644
index 0000000000..561ba0b444
--- /dev/null
+++ b/nuttx/sched/sem_trywait.c
@@ -0,0 +1,141 @@
+/************************************************************
+ * sem_trywait.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include <sched.h>
+#include <errno.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_trywait
+ *
+ * Description:
+ * This function locks the specified semaphore only if the
+ * semaphore is currently not locked. Otherwise, it locks
+ * the semaphore. In either case, the call returns without
+ * blocking.
+ *
+ * Parameters:
+ * sem - the semaphore descriptor
+ *
+ * Return Value:
+ * 0 (OK) or -1 (ERROR) if unsuccessful
+ * If this function returns -1 (ERROR), then the cause
+ * of the failure will be reported in "errno" as:
+ * - EINVAL: Invalid attempt to get the semaphore
+ * - EAGAIN: The semaphore is not available.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sem_trywait(sem_t *sem)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ uint32 saved_state;
+ int ret = ERROR;
+
+ /* Assume any errors reported are due to invalid arguments. */
+
+ *get_errno_ptr() = EINVAL;
+
+ if (sem)
+ {
+ /* The following operations must be performed with interrupts
+ * disabled because sem_post() may be called from an interrupt
+ * handler.
+ */
+
+ saved_state = irqsave();
+
+ /* Any further errors could only be occurred because the semaphore
+ * is not available.
+ */
+
+ *get_errno_ptr() = EAGAIN;
+
+ /* If the semaphore is available, give it to the requesting task */
+
+ if (sem->semcount > 0)
+ {
+ /* It is, let the task take the semaphore */
+
+ sem->semcount--;
+ rtcb->waitsem = NULL;
+ ret = OK;
+ }
+
+ /* Interrupts may now be enabled. */
+
+ irqrestore(saved_state);
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sem_unlink.c b/nuttx/sched/sem_unlink.c
new file mode 100644
index 0000000000..467db622e9
--- /dev/null
+++ b/nuttx/sched/sem_unlink.c
@@ -0,0 +1,141 @@
+/************************************************************
+ * sem_unlink.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include <sched.h>
+#include <queue.h>
+#include "os_internal.h"
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_unlink
+ *
+ * Description:
+ * This function removes the semaphore named by the input
+ * parameter "name." If the semaphore named by "name" is
+ * currently referenced by other processes, the sem_unlink()
+ * will have no effect on the state of the semaphore. If
+ * one or more processes have the semaphore open when
+ * sem_unlink() is called, destruction of the semaphore
+ * will be postponed until all references to the semaphore
+ * have been destroyed by calls of sem_close().
+ *
+ * Parameters:
+ * name - Semaphore name
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if unsuccessful.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sem_unlink (const char *name)
+{
+ nsem_t *psem;
+ int ret = ERROR;
+
+ /* Verify the input values */
+
+ if (name)
+ {
+ sched_lock();
+
+ /* Find the named semaphore */
+
+ psem = sem_findnamed(name);
+
+ /* Check if the semaphore was found */
+
+ if (psem)
+ {
+ /* If the named semaphore was found and if there are no
+ * connects to it, then deallocate it
+ */
+ if (!psem->nconnect)
+ {
+ dq_rem((dq_entry_t*)psem, &g_nsems);
+ sched_free(psem);
+ }
+
+ /* If one or more process still has the semaphore open,
+ * then just mark it as unlinked. The unlinked semaphore will
+ * be deleted when the final process closes the semaphore.
+ */
+
+ else
+ {
+ psem->unlinked = TRUE;
+ }
+ ret = OK;
+ }
+ sched_unlock();
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sem_wait.c b/nuttx/sched/sem_wait.c
new file mode 100644
index 0000000000..006c559600
--- /dev/null
+++ b/nuttx/sched/sem_wait.c
@@ -0,0 +1,174 @@
+/************************************************************
+ * sem_wait.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <semaphore.h>
+#include <errno.h>
+#include <assert.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_wait
+ *
+ * Description:
+ * This function attempts to lock the semaphore referenced
+ * by sem. If the semaphore value is (<=) zero, then the
+ * calling task will not return until it successfully
+ * acquires the lock.
+ *
+ * Parameters:
+ * sem - Semaphore descriptor.
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) is unsuccessful
+ * If this function returns -1 (ERROR), then the cause
+ * of the failure will be reported in "errno" as:
+ * - EINVAL: Invalid attempt to get the semaphore
+ * - EINTR: The wait was interrupted by the receipt of
+ * a signal.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sem_wait (sem_t *sem)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ int ret = ERROR;
+ uint32 saved_state;
+
+ /* Assume any errors reported are due to invalid arguments. */
+
+ *get_errno_ptr() = EINVAL;
+
+ if (sem)
+ {
+ /* The following operations must be performed with interrupts
+ * disabled because sem_post() may be called from an interrupt
+ * handler.
+ */
+
+ saved_state = irqsave();
+
+ /* Check if the lock is available */
+
+ if (sem->semcount > 0)
+ {
+ /* It is, let the task take the semaphore. */
+
+ sem->semcount--;
+ rtcb->waitsem = NULL;
+ ret = OK;
+ }
+
+ /* The semaphore is NOT available, We will have to block the
+ * current thread of execution.
+ */
+
+ else
+ {
+ /* First, verify that the task is not already waiting on a
+ * semaphore
+ */
+
+ if (rtcb->waitsem != NULL)
+ {
+ PANIC(OSERR_BADWAITSEM);
+ }
+
+ /* Handle the POSIX semaphore */
+
+ sem->semcount--;
+
+ /* Save the waited on semaphore in the TCB */
+
+ rtcb->waitsem = sem;
+
+ /* Add the TCB to the prioritized semaphore wait queue */
+
+ up_block_task(rtcb, TSTATE_WAIT_SEM);
+
+ /* When we resume at this point, either (1) the semaphore has been
+ * assigned to this thread of execution, or (2) the semaphore wait
+ * has been interrupted by a signal.
+ */
+
+ if (*get_errno_ptr() != EINTR)
+ ret = OK;
+ else
+ sem->semcount++;
+ }
+
+ /* Interrupts may now be enabled. */
+
+ irqrestore(saved_state);
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sem_waitirq.c b/nuttx/sched/sem_waitirq.c
new file mode 100644
index 0000000000..96ab61f445
--- /dev/null
+++ b/nuttx/sched/sem_waitirq.c
@@ -0,0 +1,128 @@
+/************************************************************
+ * sem_waitirq.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <sched.h>
+#include <errno.h>
+#include <nuttx/arch.h>
+#include "sem_internal.h"
+
+/************************************************************
+ * Compilation Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sem_waitirq
+ *
+ * Description:
+ * This function is called when a signal is received by a
+ * task that is waiting on a semaphore. According to the
+ * POSIX spec, "...the calling thread shall not return
+ * from the call to [sem_wait] until it either locks the
+ * semaphore or the call is interrupted by a signal."
+ *
+ * Parameters:
+ * wtcb - A pointer to the TCB of the task that is waiting
+ * on a semphare, but has received a signal instead.
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+void sem_waitirq (_TCB *wtcb)
+{
+ uint32 saved_state;
+
+ /* Disable interrupts. This is necessary (unfortunately) because an
+ * interrupt handler may attempt to post the semaphore while we are
+ * doing this.
+ */
+
+ saved_state = irqsave();
+
+ /* It is possible that an interrupt/context switch beat us to the
+ * punch and already changed the task's state.
+ */
+
+ if (wtcb->task_state == TSTATE_WAIT_SEM)
+ {
+ /* Indicate that the semaphore wait is over. */
+
+ wtcb->waitsem = NULL;
+
+ /* Mark the errno value for the thread. */
+
+ wtcb->errno = EINTR;
+
+ /* Restart the the task. */
+
+ up_unblock_task(wtcb);
+ }
+
+ /* Interrupts may now be enabled. */
+
+ irqrestore(saved_state);
+}
diff --git a/nuttx/sched/sig_action.c b/nuttx/sched/sig_action.c
new file mode 100644
index 0000000000..14388c3d5b
--- /dev/null
+++ b/nuttx/sched/sig_action.c
@@ -0,0 +1,273 @@
+/************************************************************
+ * sig_action.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include <queue.h>
+#include <sched.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_allocateaction
+ *
+ * Description:
+ * Allocate a new element for a sigaction queue
+ *
+ ************************************************************/
+
+static sigactq_t *sig_allocateaction(void)
+{
+ sigactq_t *sigact;
+
+ /* Try to get the signal action structure from the free list */
+
+ sigact = (sigactq_t*)sq_remfirst(&g_sigfreeaction);
+
+ /* Check if we got one. */
+
+ if (!sigact)
+ {
+ /* Add another block of signal actions to the list */
+
+ sig_allocateactionblock();
+
+ /* And try again */
+
+ sigact = (sigactq_t*)sq_remfirst(&g_sigfreeaction);
+ if (!sigact)
+ {
+ PANIC(OSERR_OUTOFMEMORY);
+ }
+ }
+
+ return sigact;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigaction
+ *
+ * Description:
+ * This function allows the calling process to examine
+ * and/or specify the action to be associated with a
+ * specific signal.
+ *
+ * The structure sigaction, used to describe an action to
+ * be taken, is defined to include the following members:
+ *
+ * - sa_u.sa_handler: Pointer to a signal-catching
+ * function
+ * - sa_u.sa_sigaction: Alternative form of the
+ * signal-catching function
+ * - sa_mask: An additional set of signals to be blocked
+ * during execution of a signal catching function
+ * - sa_flags. Special flags to affect the behavior of a
+ * signal.
+ *
+ * If the argument 'act' is not NULL, it points to a
+ * structure specifying the action to be associated with
+ * the specified signal. If the argument 'oact' is not
+ * NULL, the action previously associated with the signal
+ * is stored in the location pointed to by the argument
+ * 'oact.'
+ *
+ * When a signal is caught by a signal-catching function
+ * installed by sigaction() function, a new signal mask is
+ * calculated and installed for the duration of the
+ * signal-catching function. This mask is formed by taking
+ * the union of the current signal mask and the value of the
+ * sa_mask for the signal being delivered and then including
+ * the signal being delivered. If and when the user's signal
+ * handler returns, the original signal mask is restored.
+ *
+ * Once an action is installed for a specific signal, it
+ * remains installed until another action is explicitly
+ * requested by another call to sigaction().
+ *
+ * Parameters:
+ * sig - Signal of interest
+ * act - Location of new handler
+ * oact - Location to store only handler
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if the signal number is invalid.
+ * (errno is not set)
+ *
+ * Assumptions:
+ *
+ * POSIX Compatibility:
+ * - Special values of sa_handler in the struct sigaction
+ * act input not handled (SIG_DFL, SIG_IGN).
+ * - All sa_flags in struct sigaction of act input are
+ * ignored (all treated like SA_SIGINFO).
+ *
+ ************************************************************/
+
+int sigaction(int signo, const struct sigaction *act,
+ struct sigaction *oact)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ sigactq_t *sigact;
+ int ret = ERROR; /* Assume failure */
+
+ /* Since sigactions can only be installed from the running
+ * thread of execution, no special precautions should be
+ * necessary. */
+
+ /* Verify the signal */
+
+ if (GOOD_SIGNO(signo))
+ {
+ ret = OK; /* Assume success */
+
+ /* Find the signal in the sigactionq */
+
+ sigact = sig_findaction(rtcb, signo);
+
+ /* Return the old sigaction value if so requested */
+
+ if (oact)
+ {
+ if (sigact)
+ {
+ *oact = sigact->act;
+ }
+ else
+ {
+ /* There isn't an old value */
+
+ oact->sa_u._sa_handler = NULL;
+ oact->sa_mask = NULL_SIGNAL_SET;
+ oact->sa_flags = 0;
+ }
+ }
+
+ /* If no sigaction was found, but one is needed, then
+ * allocate one.
+ */
+
+ if (!sigact && act && act->sa_u._sa_handler)
+ {
+ sigact = sig_allocateaction();
+
+ /* An error has occurred if we could not allocate the sigaction */
+
+ if (!sigact)
+ {
+ ret = ERROR;
+ }
+ else
+ {
+ /* Put the signal number in the queue entry */
+
+ sigact->signo = (ubyte)signo;
+
+ /* Add the new sigaction to sigactionq */
+
+ sq_addlast((sq_entry_t*)sigact, &rtcb->sigactionq);
+ }
+ }
+
+ /* Set the new sigaction if so requested */
+
+ if ((sigact) && (act))
+ {
+ /* Check if it is a request to install a new handler */
+
+ if (act->sa_u._sa_handler)
+ {
+ sigact->act = *act;
+ }
+
+ /* No.. It is a request to remove the old handler */
+
+ else
+ {
+ /* Remove the old sigaction from sigactionq */
+
+ sq_rem((sq_entry_t*)sigact, &rtcb->sigactionq);
+
+ /* And deallocate it */
+
+ sig_releaseaction(sigact);
+ }
+ }
+ }
+
+ return ret;
+}
+
+/************************************************************
+ * Function: sig_releaseaction
+ *
+ * Description:
+ * Deallocate a sigaction Q entry
+ *
+ ************************************************************/
+
+void sig_releaseaction(sigactq_t *sigact)
+{
+ /* Just put it back on the free list */
+
+ sq_addlast((sq_entry_t*)sigact, &g_sigfreeaction);
+}
diff --git a/nuttx/sched/sig_addset.c b/nuttx/sched/sig_addset.c
new file mode 100644
index 0000000000..b212a37a74
--- /dev/null
+++ b/nuttx/sched/sig_addset.c
@@ -0,0 +1,99 @@
+/************************************************************
+ * sig_addset.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <signal.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigaddset
+ *
+ * Description:
+ * This function adds the signal specified by signo to the
+ * signal set specified by set.
+ *
+ * Parameters:
+ * set - Signal set to add signal to
+ * signo - Signal to add
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if the signal number is invalid.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigaddset(sigset_t *set, int signo)
+{
+ int ret = ERROR;
+
+ /* Verify the signal */
+
+ if (GOOD_SIGNO(signo))
+ {
+ /* Add the signal to the set */
+
+ *set |= SIGNO2SET(signo);
+ ret = OK;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sig_allocatependingsigaction.c b/nuttx/sched/sig_allocatependingsigaction.c
new file mode 100644
index 0000000000..a6304e2059
--- /dev/null
+++ b/nuttx/sched/sig_allocatependingsigaction.c
@@ -0,0 +1,134 @@
+/************************************************************
+ * sig_allocatependingsigaction.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include <assert.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_allocatependingsigaction
+ *
+ * Description:
+ * Allocate a new element for the pending signal action queue
+ ************************************************************/
+
+sigq_t *sig_allocatependingsigaction(void)
+{
+ sigq_t *sigq;
+ uint32 saved_state;
+
+ /* Check if we were called from an interrupt handler. */
+
+ if (up_interrupt_context())
+ {
+ /* Try to get the pending signal action structure from the free list */
+
+ sigq = (sigq_t*)sq_remfirst(&g_sigpendingaction);
+
+ /* If so, then try the special list of structures reserved for
+ * interrupt handlers
+ */
+
+ if (!sigq)
+ {
+ sigq = (sigq_t*)sq_remfirst(&g_sigpendingirqaction);
+ }
+ }
+
+ /* If we were not called from an interrupt handler, then we are
+ * free to allocate pending signal action structures if necessary. */
+
+ else
+ {
+ /* Try to get the pending signal action structure from the free list */
+
+ saved_state = irqsave();
+ sigq = (sigq_t*)sq_remfirst(&g_sigpendingaction);
+ irqrestore(saved_state);
+
+ /* Check if we got one. */
+
+ if (!sigq)
+ {
+ /* No...Try the resource pool */
+
+ if (!sigq)
+ {
+ sigq = (sigq_t *)kmalloc((sizeof (sigq_t)));
+ }
+
+ /* Check if we got an allocated message */
+
+ if (sigq)
+ {
+ sigq->type = SIG_ALLOC_DYN;
+ }
+ }
+ }
+
+ return sigq;
+}
+
diff --git a/nuttx/sched/sig_cleanup.c b/nuttx/sched/sig_cleanup.c
new file mode 100644
index 0000000000..d9e1073dff
--- /dev/null
+++ b/nuttx/sched/sig_cleanup.c
@@ -0,0 +1,117 @@
+/************************************************************
+ * sig_cleanup.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_cleanup
+ *
+ * Description:
+ * Deallocate all signal-related lists in a TCB. This function
+ * is called only at task deletion time. The caller is
+ * expected to have assured the critical section necessary
+ * to perform this action.
+ ************************************************************/
+
+void sig_cleanup(_TCB *stcb)
+{
+ sigactq_t *sigact;
+ sigq_t *sigq;
+ sigpendq_t *sigpend;
+
+ /* Deallocate all entries in the list of signal actions */
+
+ while ((sigact = (sigactq_t*)sq_remfirst(&stcb->sigactionq)) != NULL)
+ {
+ sig_releaseaction(sigact);
+ }
+
+ /* Deallocate all entries in the list of pending signals */
+
+ while ((sigpend = (sigpendq_t*)sq_remfirst(&stcb->sigpendingq)) != NULL)
+ {
+ sig_releasependingsignal(sigpend);
+ }
+
+ /* Deallocate all entries in the list of pending signal actions */
+
+ while ((sigq = (sigq_t*)sq_remfirst(&stcb->sigpendactionq)) != NULL)
+ {
+ sig_releasependingsigaction(sigq);
+ }
+
+ /* Deallocate all entries in the list of posted signal actions */
+
+ while ((sigq = (sigq_t*)sq_remfirst(&stcb->sigpostedq)) != NULL)
+ {
+ sig_releasependingsigaction(sigq);
+ }
+
+ /* Misc. signal-related clean-up */
+
+ stcb->sigprocmask = ALL_SIGNAL_SET;
+ stcb->sigwaitmask = NULL_SIGNAL_SET;
+}
diff --git a/nuttx/sched/sig_deliver.c b/nuttx/sched/sig_deliver.c
new file mode 100644
index 0000000000..dfbbc1092d
--- /dev/null
+++ b/nuttx/sched/sig_deliver.c
@@ -0,0 +1,164 @@
+/************************************************************
+ * sig_deliver.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sem_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_deliver
+ *
+ * Description:
+ * This function is called on the thread of execution of
+ * the signal receiving task. It processes all queued
+ * signals then returns.
+ *
+ ************************************************************/
+
+void sig_deliver(_TCB *stcb)
+{
+ pid_t rpid;
+ sigq_t *sigq;
+ sigq_t *next;
+ sigset_t savesigprocmask;
+ uint32 saved_state;
+ int saved_errno;
+
+ sched_lock();
+
+ /* Save the thread errno. When we finished dispatching the
+ * signal actions and resume the task, the errno value must
+ * be unchanged by the operation of the signal handling. In
+ * particular, the EINTR indication that says that the task
+ * was reawakened by a signal must be retained.
+ */
+
+ saved_errno = stcb->errno;
+ for (sigq = (sigq_t*)stcb->sigpendactionq.head; (sigq); sigq = next)
+ {
+ next = sigq->flink;
+ dbg("sig_deliver: Sending signal sigq=0x%x\n", sigq);
+
+ /* Remove the signal structure from the sigpendactionq and place it
+ * in the sigpostedq. NOTE: Since signals are processed one at a
+ * time, there should never be more than one signal in the sigpostedq
+ */
+
+ saved_state = irqsave();
+ sq_rem((sq_entry_t*)sigq, &(stcb->sigpendactionq));
+ sq_addlast((sq_entry_t*)sigq, &(stcb->sigpostedq));
+ irqrestore(saved_state);
+
+ /* Call the signal handler (unless the signal was cancelled)
+ *
+ * Save a copy of the old sigprocmask and install the new
+ * (temporary) sigprocmask. The new sigprocmask is the union
+ * of the current sigprocmask and the sa_mask for the signal being
+ * delivered plus the signal being delivered.
+ */
+
+ savesigprocmask = stcb->sigprocmask;
+ stcb->sigprocmask = savesigprocmask | sigq->mask | SIGNO2SET(sigq->info.si_signo);
+
+ /* The current tasks process.ID. We'll need this later to see if
+ * the signal handler caused a context switch.
+ */
+
+ rpid = getpid();
+
+ /* Deliver the signal using its address environment */
+
+ (*sigq->action.sighandler)(sigq->info.si_signo, &sigq->info, NULL);
+
+ /* Restore the original sigprocmask */
+
+ stcb->sigprocmask = savesigprocmask;
+
+ /* Now, handle the (rare?) case where (a) a blocked signal was
+ * received while the signal handling executed but (b) restoring the
+ * original sigprocmask will unblock the signal.
+ */
+
+ sig_unmaskpendingsignal();
+
+ /* Remove the signal from the sigpostedq */
+
+ saved_state = irqsave();
+ sq_rem((sq_entry_t*)sigq, &(stcb->sigpostedq));
+ irqrestore(saved_state);
+
+ /* Then deallocate it */
+
+ sig_releasependingsigaction(sigq);
+ }
+
+ stcb->errno = saved_errno;
+ sched_unlock();
+}
+
diff --git a/nuttx/sched/sig_delset.c b/nuttx/sched/sig_delset.c
new file mode 100644
index 0000000000..b18454562c
--- /dev/null
+++ b/nuttx/sched/sig_delset.c
@@ -0,0 +1,99 @@
+/************************************************************
+ * sig_delset.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <signal.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigdelset
+ *
+ * Description:
+ * This function deletes the signal specified by signo from
+ * the signal set specified by set.
+ *
+ * Parameters:
+ * set - Signal set to delete the signal from
+ * signo - Signal to delete
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if the signal number is invalid.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigdelset(sigset_t *set, int signo)
+{
+ int ret = ERROR;
+
+ /* Verify the signal */
+
+ if (GOOD_SIGNO(signo))
+ {
+ /* Delete the signal to the set */
+
+ *set &= ~SIGNO2SET(signo);
+ ret = OK;
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sig_emptyset.c b/nuttx/sched/sig_emptyset.c
new file mode 100644
index 0000000000..7c5c9d58a0
--- /dev/null
+++ b/nuttx/sched/sig_emptyset.c
@@ -0,0 +1,88 @@
+/************************************************************
+ * sig_emptyset.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <signal.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigemptyset
+ *
+ * Description:
+ * This function initializes the signal set specified by
+ * set such that all signals are excluded.
+ *
+ * Parameters:
+ * set - Signal set to initalize
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if the signal set cannot be
+ * initialized.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigemptyset(sigset_t *set)
+{
+ *set = NULL_SIGNAL_SET;
+ return OK;
+}
diff --git a/nuttx/sched/sig_fillset.c b/nuttx/sched/sig_fillset.c
new file mode 100644
index 0000000000..2159c380cf
--- /dev/null
+++ b/nuttx/sched/sig_fillset.c
@@ -0,0 +1,88 @@
+/************************************************************
+ * sig_fillset.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <signal.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Publics Functioins
+ ************************************************************/
+
+/************************************************************
+ * Function: sigfillset
+ *
+ * Description:
+ * This function initializes the signal set specified by
+ * set such that all signals are included.
+ *
+ * Parameters:
+ * set - Signal set to initalize
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if the signal set cannot be
+ * initialized.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigfillset(sigset_t *set)
+{
+ *set = ALL_SIGNAL_SET;
+ return OK;
+}
diff --git a/nuttx/sched/sig_findaction.c b/nuttx/sched/sig_findaction.c
new file mode 100644
index 0000000000..98df313ec2
--- /dev/null
+++ b/nuttx/sched/sig_findaction.c
@@ -0,0 +1,100 @@
+/************************************************************
+ * sig_findaction.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_findaction
+ *
+ * Description:
+ * Allocate a new element for a signal queue
+ *
+ ************************************************************/
+
+sigactq_t *sig_findaction(_TCB *stcb, int signo)
+{
+ sigactq_t *sigact = NULL;
+
+ /* Verify the caller's sanity */
+
+ if (stcb)
+ {
+ /* Sigactions can only be assigned to the currently executing
+ * thread. So, a simple lock ought to give us sufficient
+ * protection.
+ */
+
+ sched_lock();
+
+ /* Seach the list for a sigaction on this signal */
+
+ for(sigact = (sigactq_t*)stcb->sigactionq.head;
+ ((sigact) && (sigact->signo != signo));
+ sigact = sigact->flink);
+
+ sched_unlock();
+ }
+
+ return sigact;
+}
diff --git a/nuttx/sched/sig_initialize.c b/nuttx/sched/sig_initialize.c
new file mode 100644
index 0000000000..1c1e8c23cb
--- /dev/null
+++ b/nuttx/sched/sig_initialize.c
@@ -0,0 +1,262 @@
+/************************************************************
+ * sig_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <queue.h>
+#include <nuttx/kmalloc.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* The g_sigfreeaction data structure is a list of available
+ * signal action structures. */
+
+sq_queue_t g_sigfreeaction;
+
+/* The g_sigpendingaction data structure is a list of available
+ * pending signal action structures.
+ */
+
+sq_queue_t g_sigpendingaction;
+
+/* The g_sigpendingirqaction is a list of available
+ * pending signal actions that are reserved for use by
+ * interrupt handlers.
+ */
+
+sq_queue_t g_sigpendingirqaction;
+
+/* The g_sigpendingsignal data structure is a list of
+ * available pending signal structures.
+ */
+
+sq_queue_t g_sigpendingsignal;
+
+/* The g_sigpendingirqsignal data structure is a list
+ * of available pending signal structures that are reserved
+ * for use by interrupt handlers.
+ */
+
+sq_queue_t g_sigpendingirqsignal;
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/* g_sigactionalloc is a pointer to the start of the
+ * allocated blocks of signal actions.
+ */
+
+static sigactq_t *g_sigactionalloc;
+
+/* g_sigpendingactionalloc is a pointer to the start of the
+ * allocated blocks of pending signal actions.
+ */
+
+static sigq_t *g_sigpendingactionalloc;
+
+/* g_sigpendingirqactionalloc is a pointer to the start of
+ * the allocated block of pending signal actions.
+ */
+
+static sigq_t *g_sigpendingirqactionalloc;
+
+/* g_sigpendingsignalalloc is a pointer to the start of the
+ * allocated blocks of pending signals.
+ */
+
+static sigpendq_t *g_sigpendingsignalalloc;
+
+/* g_sigpendingirqsignalalloc is a pointer to the start of
+ * the allocated blocks of pending signals.
+ */
+
+static sigpendq_t *g_sigpendingirqsignalalloc;
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+static sigq_t *sig_allocateblock(sq_queue_t *sigList, uint16 nSigs,
+ ubyte sigType);
+static sigpendq_t *sig_allocatependingsignalblock(sq_queue_t *sigList,
+ uint16 nSigs, ubyte sigType);
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_allocateblock
+ *
+ * Description:
+ * Allocate a block of pending signal actions and place them
+ * on the free list.
+ *
+ ************************************************************/
+
+static sigq_t *sig_allocateblock(sq_queue_t *sigList, uint16 nSigs,
+ ubyte sigType)
+{
+ sigq_t *sigqalloc;
+ sigq_t *sigq;
+ int i;
+
+ /* Allocate a block of pending signal actions */
+
+ sigqalloc = (sigq_t*)kmalloc((sizeof(sigq_t)) * nSigs);
+
+ sigq = sigqalloc;
+ for (i = 0; i < nSigs; i++)
+ {
+ sigq->type = sigType;
+ sq_addlast((sq_entry_t*)sigq++, sigList);
+ }
+
+ return sigqalloc;
+}
+
+/************************************************************
+ * Function: sig_allocatependingsignalblock
+ *
+ * Description:
+ * Allocate a block of pending signal structures and place them
+ * on the free list.
+ ************************************************************/
+
+static sigpendq_t *sig_allocatependingsignalblock(sq_queue_t *sigList,
+ uint16 nSigs, ubyte sigType)
+{
+ sigpendq_t *sigpendalloc;
+ sigpendq_t *sigpend;
+ int i;
+
+ /* Allocate a block of pending signal structures */
+
+ sigpendalloc =
+ (sigpendq_t*)kmalloc((sizeof(sigpendq_t)) * nSigs);
+
+ sigpend = sigpendalloc;
+ for (i = 0; i < nSigs; i++)
+ {
+ sigpend->type = sigType;
+ sq_addlast((sq_entry_t*)sigpend++, sigList);
+ }
+
+ return sigpendalloc;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_initialize
+ *
+ * Description:
+ * Perform one-time power-up initialization
+ ************************************************************/
+
+void sig_initialize(void)
+{
+ /* Initialize free lists */
+
+ sq_init(&g_sigfreeaction);
+ sq_init(&g_sigpendingaction);
+ sq_init(&g_sigpendingirqaction);
+ sq_init(&g_sigpendingsignal);
+ sq_init(&g_sigpendingirqsignal);
+
+ /* Add a block of signal structures to each list */
+ g_sigpendingactionalloc =
+ sig_allocateblock(&g_sigpendingaction,
+ NUM_PENDING_ACTIONS,
+ SIG_ALLOC_FIXED);
+ g_sigpendingirqactionalloc =
+ sig_allocateblock(&g_sigpendingaction,
+ NUM_PENDING_INT_ACTIONS,
+ SIG_ALLOC_IRQ);
+ sig_allocateactionblock();
+ g_sigpendingsignalalloc =
+ sig_allocatependingsignalblock(&g_sigpendingsignal,
+ NUM_SIGNALS_PENDING,
+ SIG_ALLOC_FIXED);
+ g_sigpendingirqsignalalloc =
+ sig_allocatependingsignalblock(&g_sigpendingirqsignal,
+ NUM_INT_SIGNALS_PENDING,
+ SIG_ALLOC_IRQ);
+}
+
+/************************************************************
+ * Function: sig_allocateactionblock
+ *
+ * Description:
+ * Allocate a block of signal actions and place them
+ * on the free list.
+ ************************************************************/
+
+void sig_allocateactionblock(void)
+{
+ sigactq_t *sigact;
+ int i;
+
+ /* Allocate a block of signal actions */
+
+ g_sigactionalloc =
+ (sigactq_t*)kmalloc((sizeof(sigactq_t)) * NUM_SIGNAL_ACTIONS);
+
+ sigact = g_sigactionalloc;
+ for (i = 0; i < NUM_SIGNAL_ACTIONS; i++)
+ {
+ sq_addlast((sq_entry_t*)sigact++, &g_sigfreeaction);
+ }
+}
+
+
diff --git a/nuttx/sched/sig_internal.h b/nuttx/sched/sig_internal.h
new file mode 100644
index 0000000000..8f3bebe1ce
--- /dev/null
+++ b/nuttx/sched/sig_internal.h
@@ -0,0 +1,190 @@
+/************************************************************
+ * sig_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __SIG_INTERNAL_H
+#define __SIG_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <queue.h>
+#include <sched.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/os_external.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/* The following definition determines the number of signal
+ * structures to allocate in a block
+ */
+
+#define NUM_SIGNAL_ACTIONS 16
+#define NUM_PENDING_ACTIONS 16
+#define NUM_PENDING_INT_ACTIONS 8
+#define NUM_SIGNALS_PENDING 16
+#define NUM_INT_SIGNALS_PENDING 8
+
+/************************************************************
+ * Public Type Definitions
+ ************************************************************/
+
+enum sigalloc_e
+{
+ SIG_ALLOC_FIXED = 0, /* pre-allocated; never freed */
+ SIG_ALLOC_DYN, /* dynamically allocated; free when unused */
+ SIG_ALLOC_IRQ /* Preallocated, reserved for interrupt handling */
+};
+typedef enum sigalloc_e sigalloc_t;
+
+/* The following defines the sigaction queue entry */
+
+struct sigactq
+{
+ struct sigactq *flink; /* Forward link */
+ struct sigaction act; /* Sigaction data */
+ ubyte signo; /* Signal associated with action */
+};
+typedef struct sigactq sigactq_t;
+
+/* The following defines the queue structure within each TCB
+ * to hold pending signals received by the task. These are signals that
+ * cannot be processed because: (1) the task is not waiting for them, or
+ * (2) the task has no action associated with the signal.
+ */
+
+struct sigpendq
+{
+ struct sigpendq *flink; /* Forward link */
+ siginfo_t info; /* Signal information */
+ ubyte type; /* (Used to manage allocations) */
+};
+typedef struct sigpendq sigpendq_t;
+
+/* The following defines the queue structure within each TCB
+ * to hold queued signal actions that need action by the task
+ */
+
+struct sigq_s
+{
+ struct sigq_s *flink; /* Forward link */
+ union
+ {
+ saVxHandType *sighandler;
+ } action; /* Signal action */
+ sigset_t mask; /* Additional signals to mask while the
+ * the signal-catching functin executes */
+ siginfo_t info; /* Signal information */
+ ubyte type; /* (Used to manage allocations) */
+};
+typedef struct sigq_s sigq_t;
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* The g_sigfreeaction data structure is a list of available
+ * signal action structures.
+ */
+
+extern sq_queue_t g_sigfreeaction;
+
+/* The g_sigpendingaction data structure is a list of available
+ * pending signal action structures.
+ */
+
+extern sq_queue_t g_sigpendingaction;
+
+/* The g_sigpendingirqaction is a list of available
+ * pending signal actions that are reserved for use by
+ * interrupt handlers.
+ */
+
+extern sq_queue_t g_sigpendingirqaction;
+
+/* The g_sigpendingsignal data structure is a list of
+ * available pending signal structures.
+ */
+
+extern sq_queue_t g_sigpendingsignal;
+
+/* The g_sigpendingirqsignal data structure is a list
+ * of available pending signal structures that are reserved
+ * for use by interrupt handlers.
+ */
+
+extern sq_queue_t g_sigpendingirqsignal;
+
+/************************************************************
+ * Public Inline Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+/* Internal signal-related interfaces ***********************/
+
+/* sig_intialize.c */
+
+extern void weak_function sig_initialize(void);
+extern void sig_allocateactionblock(void);
+
+/* sig_action.c */
+
+extern void sig_releaseaction(sigactq_t *sigact);
+
+/* sig_pending.c */
+
+extern sigset_t sig_pendingset(_TCB *stcb);
+
+/* In files of the same name */
+
+extern sigq_t *sig_allocatependingsigaction(void);
+extern void sig_cleanup(_TCB *stcb);
+extern void sig_deliver(_TCB *stcb);
+extern sigactq_t *sig_findaction(_TCB *stcb, int signo);
+extern int sig_lowest(sigset_t *set);
+extern int sig_mqnotempty (int tid, int signo,
+ const union sigval value);
+extern int sig_received(_TCB *stcb, siginfo_t *info);
+extern void sig_releasependingsigaction(sigq_t *sigq);
+extern void sig_releasependingsignal(sigpendq_t *sigpend);
+extern sigpendq_t *sig_removependingsignal(_TCB *stcb, int signo);
+extern void sig_unmaskpendingsignal(void);
+
+#endif /* __SIG_INTERNAL_H */
diff --git a/nuttx/sched/sig_ismember.c b/nuttx/sched/sig_ismember.c
new file mode 100644
index 0000000000..3c363f710a
--- /dev/null
+++ b/nuttx/sched/sig_ismember.c
@@ -0,0 +1,99 @@
+/************************************************************
+ * sig_ismember.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <signal.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigismember
+ *
+ * Description:
+ * This function tests whether the signal specified by signo
+ * is a member of the set specified by set.
+ *
+ * Parameters:
+ * set - Signal set to test
+ * signo - Signal to test for
+ *
+ * Return Value:
+ * 1 (TRUE), if the specified signal is a member of the set,
+ * 0 (OK or FALSE), if it is not, or
+ * -1 (ERROR) if the signal number is invalid.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigismember(const sigset_t *set, int signo)
+{
+ int ret = ERROR;
+
+ /* Verify the signal */
+
+ if (GOOD_SIGNO(signo))
+ {
+ /* Check if the signal is in the set */
+ ret = ((*set & SIGNO2SET(signo)) != 0);
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sig_lowest.c b/nuttx/sched/sig_lowest.c
new file mode 100644
index 0000000000..7cbf9c1fe7
--- /dev/null
+++ b/nuttx/sched/sig_lowest.c
@@ -0,0 +1,88 @@
+/************************************************************
+ * sig_lowest.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_lowest
+ *
+ * Description:
+ *
+ ************************************************************/
+
+int sig_lowest(sigset_t *set)
+{
+ int signo;
+
+ for (signo = MIN_SIGNO; signo <= MAX_SIGNO; signo++)
+ {
+ if (sigismember(set, signo))
+ {
+ return signo;
+ }
+ }
+
+ return ERROR;
+}
diff --git a/nuttx/sched/sig_mqnotempty.c b/nuttx/sched/sig_mqnotempty.c
new file mode 100644
index 0000000000..cc5a064f68
--- /dev/null
+++ b/nuttx/sched/sig_mqnotempty.c
@@ -0,0 +1,113 @@
+/************************************************************
+ * sig_mqnotempty.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include <sched.h>
+#include <debug.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functionss
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_mqnotempty
+ *
+ * Description:
+ * This function is equivalent to sigqueue(), but supports
+ * the messaging system's requirement to signal a task when
+ * a message queue becomes non-empty. It is identical to
+ * sigqueue(), except that it sets the si_code field in
+ * the siginfo structure to SI_MESGQ rather than SI_QUEUE.
+ *
+ ************************************************************/
+
+int sig_mqnotempty (int pid, int signo, const union sigval value)
+{
+ _TCB *stcb;
+ siginfo_t info;
+ int ret = ERROR;
+
+ sched_lock();
+
+ /* Get the TCB of the receiving task */
+
+ stcb = sched_gettcb(pid);
+ dbg("sig_mqnotempty: TCB=0x%08x signo=%d value=%d\n",
+ stcb, signo, value.sival_int);
+
+ /* Create the siginfo structure */
+
+ info.si_signo = signo;
+ info.si_code = SI_MESGQ;
+ info.si_value = value;
+
+ /* Verify that we can perform the signalling operation */
+
+ if ((stcb) && (GOOD_SIGNO(signo)))
+ {
+ /* Process the receipt of the signal */
+ ret = sig_received(stcb, &info);
+ }
+
+ sched_unlock();
+ return ret;
+}
diff --git a/nuttx/sched/sig_pending.c b/nuttx/sched/sig_pending.c
new file mode 100644
index 0000000000..3600c6d94a
--- /dev/null
+++ b/nuttx/sched/sig_pending.c
@@ -0,0 +1,128 @@
+/************************************************************
+ * sig_pending.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include <sched.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigpending
+ *
+ * Description:
+ * This function stores the returns the set of signals that
+ * are blocked for delivery and that are pending for the
+ * calling process in the space pointed to by set.
+ *
+ * Parameters:
+ * set - The location to return the pending signal set.
+ *
+ * Return Value:
+ * 0 (OK) or -1 (ERROR)
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigpending(sigset_t *set)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ int ret = ERROR;
+
+ if (set)
+ {
+ *set = sig_pendingset(rtcb);
+ ret = OK;
+ }
+
+ return ret;
+}
+
+/************************************************************
+ * Function: sig_pendingset
+ *
+ * Description:
+ * Convert the list of pending signals into a signal set
+ ************************************************************/
+
+sigset_t sig_pendingset(_TCB *stcb)
+{
+ sigset_t sigpendset;
+ sigpendq_t *sigpend;
+ uint32 saved_state;
+
+ sigpendset = NULL_SIGNAL_SET;
+
+ saved_state = irqsave();
+ for (sigpend = (sigpendq_t*)stcb->sigpendingq.head;
+ (sigpend); sigpend = sigpend->flink)
+ {
+ sigaddset(&sigpendset, sigpend->info.si_signo);
+ }
+ irqrestore(saved_state);
+
+ return sigpendset;
+}
+
+
diff --git a/nuttx/sched/sig_procmask.c b/nuttx/sched/sig_procmask.c
new file mode 100644
index 0000000000..b733bac0ec
--- /dev/null
+++ b/nuttx/sched/sig_procmask.c
@@ -0,0 +1,172 @@
+/************************************************************
+ * sig_procmask.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <signal.h>
+#include <time.h>
+#include <wdog.h>
+#include <assert.h>
+#include <debug.h>
+#include <sched.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigprocmask
+ *
+ * Description:
+ * This function allows the calling process to examine
+ * and/or change its signal mask. If the set is not NULL,
+ * then it points to a set of signals to be used to change
+ * the currently blocked set. The value of how indicates
+ * the manner in which the set is changed.
+ *
+ * If there any pending unblocked signals after the call
+ * to sigprocmask(), those signals will be delivered
+ * before sigprocmask() returns.
+ *
+ * If sigprocmask() fails, the signal mask of the process
+ * is not changed by this function call.
+ *
+ * Parameters:
+ * how - How the signal mast will be changed:
+ * SIG_BLOCK - The resulting set is the union of
+ * the current set and the signal set
+ * pointed to by set.
+ * SIG_UNBLOCK - The resulting set is the intersection
+ * of the current set and the complement
+ * of the signal set pointed to by _set.
+ * SIG_SETMASK - The resulting set is the signal set
+ * pointed to by set.
+ * set - Location of the new signal mask
+ * oset - Location to store the old signal mask
+ *
+ * Return Value:
+ * 0 (OK), or -1 (ERROR) if how is invalid.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigprocmask(int how, const sigset_t *set, sigset_t *oset)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ sigset_t oldsigprocmask;
+ uint32 saved_state;
+ int ret = OK;
+
+ sched_lock();
+
+ /* Return the old signal mask if requested */
+
+ oldsigprocmask = rtcb->sigprocmask;
+ if (oset) *oset = oldsigprocmask;
+
+ /* Modify the current signal mask if so requested */
+ if (set) {
+ /* Some of these operations are non-atomic. We need to protect
+ * ourselves from attempts to process signals from interrupts */
+
+ saved_state = irqsave();
+
+ /* Okay, determine what we are supposed to do */
+
+ switch (how) {
+ /* The resulting set is the union of the current set and the
+ * signal set pointed to by set. */
+
+ case SIG_BLOCK:
+ rtcb->sigprocmask |= *set;
+ break;
+
+ /* The resulting set is the intersection of the current set and
+ * the complement of the signal set pointed to by _set. */
+
+ case SIG_UNBLOCK:
+ rtcb->sigprocmask &= ~(*set);
+ break;
+
+ /* The resulting set is the signal set pointed to by set. */
+
+ case SIG_SETMASK:
+ rtcb->sigprocmask = *set;
+ break;
+
+ default:
+ ret = ERROR;
+ break;
+ } /* end switch */
+ irqrestore(saved_state);
+
+ /* Now, process any pending signals that were just unmasked */
+
+ sig_unmaskpendingsignal();
+ } /* end if */
+
+ sched_unlock();
+ return ret;
+}
diff --git a/nuttx/sched/sig_queue.c b/nuttx/sched/sig_queue.c
new file mode 100644
index 0000000000..b9abf7f392
--- /dev/null
+++ b/nuttx/sched/sig_queue.c
@@ -0,0 +1,131 @@
+/************************************************************
+ * sig_queue.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include <debug.h>
+#include <sched.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigqueue
+ *
+ * Description:
+ * This function sends the signal specified by signo with
+ * the signal parameter value to the process specified by
+ * pid.
+ *
+ * If the receiving process has the signal blocked via the
+ * sigprocmask, the signal will pend until it is unmasked.
+ * Only one pending signal (per signo) is retained. This
+ * is consistent with POSIX which states, "If a subsequent
+ * occurrence of a pending signal is generated, it is
+ * implementation defined as to whether the signal is
+ * delivered more than once."
+ *
+ * Parameters:
+ * pid - Process ID of task to receive signal
+ * signo - Signal number
+ * value - Value to pass to task with signal
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigqueue (int pid, int signo, const union sigval value)
+{
+ _TCB *stcb;
+ siginfo_t info;
+ int ret = ERROR;
+
+ sched_lock();
+
+ /* Get the TCB of the receiving task */
+
+ stcb = sched_gettcb(pid);
+ dbg("sigqueue: TCB=0x%08x signo=%d value=%d\n",
+ stcb, signo, value.sival_int);
+
+ /* Create the siginfo structure */
+
+ info.si_signo = signo;
+ info.si_code = SI_QUEUE;
+ info.si_value = value;
+
+ /* Verify that we can perform the signalling operation */
+
+ if (stcb && GOOD_SIGNO(signo))
+ {
+ /* Process the receipt of the signal */
+
+ ret = sig_received(stcb, &info);
+ }
+
+ sched_unlock();
+ return ret;
+}
+
diff --git a/nuttx/sched/sig_received.c b/nuttx/sched/sig_received.c
new file mode 100644
index 0000000000..8a2c3928d5
--- /dev/null
+++ b/nuttx/sched/sig_received.c
@@ -0,0 +1,384 @@
+/************************************************************
+ * sig_received.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sched.h>
+#include <debug.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sem_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_queueaction
+ *
+ * Description:
+ * Queue a signal action for delivery to a task.
+ *
+ ************************************************************/
+
+static int sig_queueaction(_TCB *stcb, siginfo_t *info)
+{
+ sigactq_t *sigact;
+ sigq_t *sigq;
+ uint32 saved_state;
+ int ret = OK;
+
+ sched_lock();
+
+ /* Find the sigaction associated with this signal */
+
+ sigact = sig_findaction(stcb, info->si_signo);
+
+ /* Check if a valid signal handler is available and if the signal is
+ * unblocked. NOTE: There is no default action.
+ */
+
+ if ((sigact) && (sigact->act.sa_u._sa_sigaction))
+ {
+ /* Allocate a new element for the signal queue. NOTE: sig_allocatependingsigaction
+ * will force a system crash if it is unable to allocate memory for the
+ * signal data */
+
+ sigq = sig_allocatependingsigaction();
+ if (!sigq) ret = ERROR;
+ else
+ {
+ /* Populate the new signal queue element */
+
+ sigq->action.sighandler = sigact->act.sa_u._sa_sigaction;
+ sigq->mask = sigact->act.sa_mask;
+ sigq->info = *info;
+
+ /* Put it at the end of the pending signals list */
+
+ saved_state = irqsave();
+ sq_addlast((sq_entry_t*)sigq, &(stcb->sigpendactionq));
+ irqrestore(saved_state);
+ }
+ }
+
+ sched_unlock();
+ return ret;
+}
+
+/************************************************************
+ * Function: sig_findpendingsignal
+ *
+ * Description:
+ * Find a specified element in the pending signal list
+ *
+ ************************************************************/
+
+static sigpendq_t *sig_findpendingsignal(_TCB *stcb, int signo)
+{
+ sigpendq_t *sigpend = NULL;
+ uint32 saved_state;
+
+ /* Verify the caller's sanity */
+
+ if (stcb)
+ {
+ /* Pending sigals can be added from interrupt level. */
+
+ saved_state = irqsave();
+
+ /* Seach the list for a sigpendion on this signal */
+
+ for(sigpend = (sigpendq_t*)stcb->sigpendingq.head;
+ (sigpend && sigpend->info.si_signo != signo);
+ sigpend = sigpend->flink);
+ irqrestore(saved_state);
+ }
+
+ return sigpend;
+}
+
+/************************************************************
+ * Function: sig_allocatependingsignal
+ *
+ * Description:
+ * Allocate a pending signal list entry
+ *
+ ************************************************************/
+
+static sigpendq_t *sig_allocatependingsignal(void)
+{
+ sigpendq_t *sigpend;
+ uint32 saved_state;
+
+ /* Check if we were called from an interrupt handler. */
+
+ if (up_interrupt_context())
+ {
+ /* Try to get the pending signal structure from the free list */
+
+ sigpend = (sigpendq_t*)sq_remfirst(&g_sigpendingsignal);
+
+ /* If so, then try the special list of structures reserved for
+ * interrupt handlers
+ */
+
+ sigpend = (sigpendq_t*)sq_remfirst(&g_sigpendingirqsignal);
+ }
+
+ /* If we were not called from an interrupt handler, then we are
+ * free to allocate pending action structures if necessary. */
+
+ else
+ {
+ /* Try to get the pending signal structure from the free list */
+
+ saved_state = irqsave();
+ sigpend = (sigpendq_t*)sq_remfirst(&g_sigpendingsignal);
+ irqrestore(saved_state);
+
+ /* Check if we got one. */
+
+ if (!sigpend)
+ {
+ /* No...Try the resource pool */
+
+ if (!sigpend)
+ {
+ sigpend = (sigpendq_t *)kmalloc((sizeof (sigpendq_t)));
+ }
+
+ /* Check if we got an allocated message */
+
+ if (sigpend)
+ {
+ sigpend->type = SIG_ALLOC_DYN;
+ }
+ }
+ }
+
+ return sigpend;
+}
+
+/************************************************************
+ * Function: sig_addpendingsignal
+ *
+ * Description:
+ * Add the specified signal to the signal pending list.
+ * NOTE: This function will queue only one entry for each
+ * pending signal. This was done intentionally so that a
+ * run-away sender cannot consume all of memory.
+ ************************************************************/
+
+static sigpendq_t *sig_addpendingsignal(_TCB *stcb, siginfo_t *info)
+{
+ sigpendq_t *sigpend;
+ uint32 saved_state;
+
+ /* Check if the signal is already pending */
+
+ sigpend = sig_findpendingsignal(stcb, info->si_signo);
+ if (sigpend)
+ {
+ /* The signal is already pending... retain only one copy */
+
+ sigpend->info = *info;
+ }
+
+ /* No... There is nothing pending for this signo */
+
+ else
+ {
+ /* Allocate a new pending signal entry */
+
+ sigpend = sig_allocatependingsignal();
+ if (sigpend)
+ {
+ /* Put the signal information into the allocated structure */
+
+ sigpend->info = *info;
+
+ /* Add the structure to the pending signal list */
+
+ saved_state = irqsave();
+ sq_addlast((sq_entry_t*)sigpend, &stcb->sigpendingq);
+ irqrestore(saved_state);
+ }
+ }
+
+ return sigpend;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_received
+ *
+ * Description:
+ * All signals received the task (whatever the source) go
+ * through this function to be processed. This function
+ * is responsible for:
+ *
+ * - Determining if the signal is blocked.
+ * - Queuing and dispatching signal actions
+ * - Unblocking tasks that are waiting for signals
+ * - Queuing pending signals.
+ *
+ ************************************************************/
+
+int sig_received(_TCB *stcb, siginfo_t *info)
+{
+ int ret = ERROR;
+ uint32 saved_state;
+
+ dbg("sig_received: TCB=0x%08x signo=%d code=%d value=%d\n",
+ stcb, info->si_signo, info->si_code, info->si_value.sival_int);
+
+ if (stcb && info)
+ {
+ ret = OK;
+
+ /****** MASKED SIGNAL HANDLING ******/
+ /* Check if the signal is masked -- if it is, it will be added to the
+ * list of pending signals.
+ */
+
+ if (sigismember(&stcb->sigprocmask, info->si_signo))
+ {
+ /* Check if the task is waiting for this pending signal. If so,
+ * then unblock it. This must be performed in a critical section
+ * because signals can be queued from the interrupt level.
+ */
+
+ saved_state = irqsave();
+ if (stcb->task_state == TSTATE_WAIT_SIG &&
+ sigismember(&stcb->sigwaitmask, info->si_signo))
+ {
+ stcb->sigunbinfo = *info;
+ stcb->sigwaitmask = NULL_SIGNAL_SET;
+ up_unblock_task(stcb);
+ irqrestore(saved_state);
+ }
+
+ /* Its not one we are waiting for... Add it to the list of pending
+ * signals.
+ */
+
+ else
+ {
+ irqrestore(saved_state);
+ if (!sig_addpendingsignal(stcb, info))
+ {
+ PANIC(OSERR_FAILEDTOADDSIGNAL);
+ }
+ }
+ }
+
+ /****** UNMASKED SIGNAL HANDLING ******/
+
+ else
+ {
+ /* Queue any sigaction's requested by this task. */
+
+ ret = sig_queueaction(stcb, info);
+
+ /* Then schedule execution of the signal handling action on
+ * the recipients thread.
+ */
+
+ up_schedule_sigaction(stcb, sig_deliver);
+
+ /* Check if the task is waiting for an unmasked signal. If so,
+ * then unblock it. This must be performed in a critical section
+ * because signals can be queued from the interrupt level.
+ */
+
+ saved_state = irqsave();
+ if (stcb->task_state == TSTATE_WAIT_SIG)
+ {
+ stcb->sigunbinfo = *info;
+ stcb->sigwaitmask = NULL_SIGNAL_SET;
+ up_unblock_task(stcb);
+ }
+ irqrestore(saved_state);
+
+ /* If the task neither was waiting for the signal nor had a signal
+ * handler attached to the signal, then the default action is
+ * simply to ignore the signal */
+ }
+
+ /****** OTHER SIGNAL HANDLING ******/
+ /* If the task is blocked waiting for a semaphore, then that
+ * task must be unblocked when a signal is received.
+ */
+
+ if (stcb->task_state == TSTATE_WAIT_SEM)
+ {
+ sem_waitirq(stcb);
+ }
+
+ /* If the task is blocked waiting on a message queue, then that
+ * task must be unblocked when a signal is received.
+ */
+
+ /* NOT YET IMPLEMENTED. */
+ }
+
+ return ret;
+}
diff --git a/nuttx/sched/sig_releasependingsigaction.c b/nuttx/sched/sig_releasependingsigaction.c
new file mode 100644
index 0000000000..7187c03c7f
--- /dev/null
+++ b/nuttx/sched/sig_releasependingsigaction.c
@@ -0,0 +1,118 @@
+/************************************************************
+ * sig_releasependingsigaction.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <sched.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_releasependingsigaction
+ *
+ * Description:
+ * Deallocate a pending signal action Q entry
+ *
+ ************************************************************/
+
+void sig_releasependingsigaction(sigq_t *sigq)
+{
+ uint32 saved_state;
+
+ /* If this is a generally available pre-allocated structyre,
+ * then just put it back in the free list.
+ */
+
+ if (sigq->type == SIG_ALLOC_FIXED)
+ {
+ /* Make sure we avoid concurrent access to the free
+ * list from interrupt handlers. */
+
+ saved_state = irqsave();
+ sq_addlast((sq_entry_t*)sigq, &g_sigpendingaction);
+ irqrestore(saved_state);
+ }
+
+ /* If this is a message pre-allocated for interrupts,
+ * then put it back in the correct free list.
+ */
+
+ else if (sigq->type == SIG_ALLOC_IRQ)
+ {
+ /* Make sure we avoid concurrent access to the free
+ * list from interrupt handlers. */
+
+ saved_state = irqsave();
+ sq_addlast((sq_entry_t*)sigq, &g_sigpendingirqaction);
+ irqrestore(saved_state);
+ }
+
+ /* Otherwise, deallocate it. Note: interrupt handlers
+ * will never deallocate signals because they will not
+ * receive them.
+ */
+
+ else if (sigq->type == SIG_ALLOC_DYN)
+ {
+ sched_free(sigq);
+ }
+}
diff --git a/nuttx/sched/sig_releasependingsignal.c b/nuttx/sched/sig_releasependingsignal.c
new file mode 100644
index 0000000000..453118fb4a
--- /dev/null
+++ b/nuttx/sched/sig_releasependingsignal.c
@@ -0,0 +1,126 @@
+/************************************************************
+ * sig_releasependingsignal.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <signal.h>
+#include <time.h>
+#include <wdog.h>
+#include <assert.h>
+#include <debug.h>
+#include <sched.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_releasependingsignal
+ *
+ * Description:
+ * Deallocate a pending signal list entry
+ ************************************************************/
+
+void sig_releasependingsignal(sigpendq_t *sigpend)
+{
+ uint32 saved_state;
+
+ /* If this is a generally available pre-allocated structyre,
+ * then just put it back in the free list.
+ */
+
+ if (sigpend->type == SIG_ALLOC_FIXED)
+ {
+ /* Make sure we avoid concurrent access to the free
+ * list from interrupt handlers. */
+
+ saved_state = irqsave();
+ sq_addlast((sq_entry_t*)sigpend, &g_sigpendingsignal);
+ irqrestore(saved_state);
+ }
+
+ /* If this is a message pre-allocated for interrupts,
+ * then put it back in the correct free list.
+ */
+
+ else if (sigpend->type == SIG_ALLOC_IRQ)
+ {
+ /* Make sure we avoid concurrent access to the free
+ * list from interrupt handlers.
+ */
+
+ saved_state = irqsave();
+ sq_addlast((sq_entry_t*)sigpend, &g_sigpendingirqsignal);
+ irqrestore(saved_state);
+ }
+
+ /* Otherwise, deallocate it. Note: interrupt handlers
+ * will never deallocate signals because they will not
+ * receive them. */
+
+ else if (sigpend->type == SIG_ALLOC_DYN)
+ {
+ sched_free(sigpend);
+ }
+}
diff --git a/nuttx/sched/sig_removependingsignal.c b/nuttx/sched/sig_removependingsignal.c
new file mode 100644
index 0000000000..4163f13f0c
--- /dev/null
+++ b/nuttx/sched/sig_removependingsignal.c
@@ -0,0 +1,108 @@
+/************************************************************
+ * sig_removependingsignal.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <signal.h>
+#include <time.h>
+#include <wdog.h>
+#include <assert.h>
+#include <debug.h>
+#include <sched.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_removependingsignal
+ *
+ * Description:
+ * Remove the specified signal from the signal pending list
+ ************************************************************/
+
+sigpendq_t *sig_removependingsignal(_TCB *stcb, int signo)
+{
+ sigpendq_t *currsig, *prevsig;
+ uint32 saved_state;
+
+ saved_state = irqsave();
+ for (prevsig = NULL, currsig = (sigpendq_t*)stcb->sigpendingq.head;
+ (currsig && currsig->info.si_signo != signo);
+ prevsig = currsig, currsig = currsig->flink);
+ if (currsig)
+ {
+ if (prevsig)
+ {
+ sq_remafter((sq_entry_t*)prevsig, &stcb->sigpendingq);
+ }
+ else
+ {
+ sq_remfirst(&stcb->sigpendingq);
+ }
+ }
+ irqrestore(saved_state);
+
+ return currsig;
+}
diff --git a/nuttx/sched/sig_suspend.c b/nuttx/sched/sig_suspend.c
new file mode 100644
index 0000000000..d0167b226e
--- /dev/null
+++ b/nuttx/sched/sig_suspend.c
@@ -0,0 +1,180 @@
+/************************************************************
+ * sig_suspend.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include <assert.h>
+#include <debug.h>
+#include <sched.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigsuspend
+ *
+ * Description:
+ *
+ * The sigsuspend() function replaces the signal mask of the
+ * process with the set of signals pointed to by the argument
+ * set and then suspends the process until delivery of a
+ * signal to the process.
+ *
+ * If the effect of the set argument is to unblock a
+ * pending signal, then no wait is performed.
+ *
+ * The original signal mask is restored when this function
+ * returns.
+ *
+ * Waiting for an empty signal set stops a task without
+ * freeing any resources.
+ *
+ * Parameters:
+ * set - signal mask to use while suspended.
+ *
+ * Return Value:
+ * -1 (ERROR) always
+ *
+ * Assumptions:
+ *
+ * POSIX Compatibility:
+ * int sigsuspend(const sigset_t *set);
+ *
+ * POSIX states that sigsuspend() "suspends the process
+ * until delivery of a signal whose action is either to
+ * execute a signal-catching function or to terminate the
+ * process." Only the deliver of a signal is required in
+ * the present implementation (even if the signal is
+ * ignored).
+ *
+ ************************************************************/
+
+int sigsuspend(const sigset_t *set)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ sigset_t intersection;
+ sigset_t saved_sigprocmask;
+ sigpendq_t *sigpend;
+ uint32 saved_state;
+ int unblocksigno;
+
+ /* Several operations must be performed below: We must determine if any
+ * signal is pending and, if not, wait for the signal. Since signals can
+ * be posted from the interrupt level, there is a race condition that
+ * can only be eliminated by disabling interrupts!
+ */
+
+ sched_lock(); /* Not necessary */
+ saved_state = irqsave();
+
+ /* Check if there is a pending signal corresponding to one of the
+ * signals that will be unblocked by the new sigprocmask.
+ */
+
+ intersection = ~(*set) & sig_pendingset(rtcb);
+ if (intersection != NULL_SIGNAL_SET)
+ {
+ /* One or more of the signals in intersections is sufficient to cause
+ * us to not wait. Pick the lowest numbered signal and mark it not
+ * pending.
+ */
+
+ unblocksigno = sig_lowest(&intersection);
+ sigpend = sig_removependingsignal(rtcb, unblocksigno);
+ if (!sigpend)
+ {
+ PANIC(OSERR_FAILEDTOREMOVESIGNAL);
+ }
+ sig_releasependingsignal(sigpend);
+ irqrestore(saved_state);
+ }
+ else
+ {
+ /* Its time to wait. Save a copy of the old sigprocmask and install
+ * the new (temporary) sigprocmask
+ */
+
+ saved_sigprocmask = rtcb->sigprocmask;
+ rtcb->sigprocmask = *set;
+ rtcb->sigwaitmask = NULL_SIGNAL_SET;
+
+ /* And wait until one of the unblocked signals is posted */
+
+ up_block_task(rtcb, TSTATE_WAIT_SIG);
+
+ /* We are running again, restore the original sigprocmask */
+
+ rtcb->sigprocmask = saved_sigprocmask;
+ irqrestore(saved_state);
+
+ /* Now, handle the (rare?) case where (a) a blocked signal was received
+ * while the task was suspended but (b) restoring the original
+ * sigprocmask will unblock the signal.
+ */
+
+ sig_unmaskpendingsignal();
+ }
+ sched_unlock();
+
+ return ERROR;
+}
diff --git a/nuttx/sched/sig_timedwait.c b/nuttx/sched/sig_timedwait.c
new file mode 100644
index 0000000000..842f68c2fb
--- /dev/null
+++ b/nuttx/sched/sig_timedwait.c
@@ -0,0 +1,267 @@
+/************************************************************
+ * sig_timedwait.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+#include <time.h>
+#include <wdog.h>
+#include <assert.h>
+#include <debug.h>
+#include <sched.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+#include "clock_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functionss
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_timeout
+ *
+ * Description:
+ * A timeout elapsed while waiting for signals to be queued.
+ ************************************************************/
+
+static void sig_timeout(int itcb, int parm2, int parm3, int parm4)
+{
+ _TCB *wtcb = (_TCB*)itcb;
+
+ if (!wtcb)
+ {
+ PANIC(OSERR_TIMEOUTNOTCB);
+ }
+
+ /* There may be a race condition -- make sure the task is
+ * still waiting for a signal
+ */
+
+ if (wtcb->task_state == TSTATE_WAIT_SIG)
+ {
+ wtcb->sigunbinfo.si_signo = ERROR;
+ wtcb->sigunbinfo.si_code = SI_TIMEOUT;
+ wtcb->sigunbinfo.si_value.sival_int = 0;
+ up_unblock_task(wtcb);
+ }
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigtimedwait
+ *
+ * Description:
+ * This function selects the pending signal set specified
+ * by the argument set. If multiple signals are pending
+ * in set, it will remove and return the lowest numbered
+ * one. If no signals in set are pending at the time of
+ * the call, the calling process will be suspended until
+ * one of the signals in set becomes pending, OR until
+ * the process is interrupted by an unblocked signal, OR
+ * until the time interval specified by timeout (if any),
+ * has expired. If timeout is NULL, then the timeout
+ * interval is forever.
+ *
+ * If the info argument is non-NULL, the selected signal
+ * number is stored in the si_signo member and the cause
+ * of the signal is store in the si_code emember. The
+ * content of si_value is only meaningful if the signal was
+ * generated by sigqueue().
+ *
+ * The following values for si_code are defined in signal.h:
+ * SI_QUEUE - Signal sent from sigqueue
+ * SI_MESGQ - Signal generated by arrival of a message on an
+ * empty message queue
+ * SI_NOWAIT - Signal already pending -- don't know how sent
+ * SI_TIMEOUT - No Signal, restarted by timeout
+ *
+ * Parameters:
+ * set - The pending signal set.
+ * info - The returned value
+ * timeout - The amount of time to wait
+ *
+ * Return Value:
+ * Signal number that cause the wait to be terminated, otherwise
+ * -1 (ERROR) is returned.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigtimedwait(const sigset_t *set, struct siginfo *info,
+ const struct timespec *timeout)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ sigset_t intersection;
+ sigpendq_t *sigpend;
+ WDOG_ID wdog;
+ uint32 saved_state;
+ sint32 waitticks;
+ int ret = ERROR;
+
+ sched_lock(); /* Not necessary */
+
+ /* Several operations must be performed below: We must determine if any
+ * signal is pending and, if not, wait for the signal. Since signals can
+ * be posted from the interrupt level, there is a race condition that
+ * can only be eliminated by disabling interrupts!
+ */
+
+ saved_state = irqsave();
+
+ /* Check if there is a pending signal corresponding to one of the
+ * signals in the pending signal set argument.
+ */
+
+ intersection = *set & sig_pendingset(rtcb);
+ if (intersection != NULL_SIGNAL_SET)
+ {
+ /* One or more of the signals in intersections is sufficient to cause
+ * us to not wait. Pick the lowest numbered signal and mark it not
+ * pending.
+ */
+
+ sigpend = sig_removependingsignal(rtcb, sig_lowest(&intersection));
+ if (!sigpend)
+ {
+ PANIC(OSERR_NOPENDINGSIGNAL);
+ }
+
+ /* Return the signal info to the caller if so requested */
+
+ if (info) *info = sigpend->info;
+
+ /* Then dispose of the pending signal structure properly */
+
+ sig_releasependingsignal(sigpend);
+ irqrestore(saved_state);
+
+ /* The return value is the number of the signal that awakened us */
+
+ ret = info->si_signo;
+ }
+
+ /* We will have to wait for a signal to be posted to this task. */
+
+ else
+ {
+ /* Save the set of pending signals to wait for */
+
+ rtcb->sigwaitmask = *set;
+
+ /* Check if we should wait for the timeout */
+
+ if (timeout)
+ {
+ /* Convert the timespec to milliseconds */
+
+ waitticks = MSEC2TICK(timeout->tv_sec * MSEC_PER_SEC
+ + timeout->tv_nsec / NSEC_PER_MSEC);
+
+ /* Create a watchdog */
+
+ wdog = wd_create();
+ if (wdog)
+ {
+ /* Start the watchdog */
+
+ wd_start(wdog, waitticks, (wdentry_t)sig_timeout,
+ (int)rtcb, 0, 0, 0);
+
+ /* Now wait for either the signal or the watchdog */
+
+ up_block_task(rtcb, TSTATE_WAIT_SIG);
+
+ /* We no longer need the watchdog */
+
+ wd_delete(wdog);
+ }
+ }
+
+ /* No timeout, just wait */
+
+ else
+ {
+ /* And wait until one of the unblocked signals is posted */
+
+ up_block_task(rtcb, TSTATE_WAIT_SIG);
+ }
+
+ /* We are running again, clear the sigwaitmask */
+
+ rtcb->sigwaitmask = NULL_SIGNAL_SET;
+
+ /* When we awaken, the cause will be in the TCB. Return the signal
+ * info to the caller if so requested
+ */
+
+ if (info)
+ {
+ *info = rtcb->sigunbinfo;
+ }
+ irqrestore(saved_state);
+
+ /* The return value is the number of the signal that awakened us */
+
+ ret = info->si_signo;
+ }
+ sched_unlock();
+
+ return ret;
+}
+
diff --git a/nuttx/sched/sig_unmaskpendingsignal.c b/nuttx/sched/sig_unmaskpendingsignal.c
new file mode 100644
index 0000000000..c27ad12668
--- /dev/null
+++ b/nuttx/sched/sig_unmaskpendingsignal.c
@@ -0,0 +1,135 @@
+/************************************************************
+ * sig_unmaskpendingsignal.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <sched.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sig_unmaskpendingsignal
+ *
+ * Description:
+ * Based upon the current setting of the sigprocmask, this
+ * function unmasks and processes any pending signals. This
+ * function should be called whenever the sigprocmask is
+ * changed.
+ ************************************************************/
+
+void sig_unmaskpendingsignal(void)
+{
+ _TCB *rtcb = (_TCB*)g_readytorun.head;
+ sigset_t unmaskedset;
+ sigpendq_t *pendingsig;
+ int signo;
+
+ /* Prohibit any context switches until we are done with this.
+ * We may still be performing signal operations from interrupt
+ * handlers, however, none of the pending signals that we
+ * are concerned with here should be effected.
+ */
+
+ sched_lock();
+
+ /* Get the set of pending signals that were just unmasked. The
+ * following operation should be safe because the sigprocmask
+ * can only be changed on this thread of execution.
+ */
+
+ unmaskedset = ~(rtcb->sigprocmask) & sig_pendingset(rtcb);
+
+ /* Loop while there are unmasked pending signals to be processed. */
+
+ while (unmaskedset != NULL_SIGNAL_SET)
+ {
+ /* Pending signals will be processed from lowest numbered signal
+ * to highest
+ */
+
+ signo = sig_lowest(&unmaskedset);
+ if (signo != ERROR)
+ {
+ /* Remove the signal from the set of unmasked signals. NOTE:
+ * this implicitly assumes that only one instance for a given
+ * signal number is pending.
+ */
+
+ sigdelset(&unmaskedset, signo);
+
+ /* Remove the pending signal from the list of pending signals */
+
+ if ((pendingsig = sig_removependingsignal(rtcb, signo)) != NULL)
+ {
+ /* If there is one, then process it like a normal signal */
+
+ sig_received(rtcb, &pendingsig->info);
+
+ /* Then remove it from the pending signal list */
+
+ sig_releasependingsignal(pendingsig);
+ }
+ }
+ }
+
+ sched_unlock();
+}
diff --git a/nuttx/sched/sig_waitinfo.c b/nuttx/sched/sig_waitinfo.c
new file mode 100644
index 0000000000..3d539defb7
--- /dev/null
+++ b/nuttx/sched/sig_waitinfo.c
@@ -0,0 +1,89 @@
+/************************************************************
+ * sig_waitinfo.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <signal.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sigwaitinfo
+ *
+ * Description:
+ * This function is equivalent to sigtimedwait with a NULL
+ * timeout parameter. (see above).
+ *
+ * Parameters:
+ * set - The pending signal set
+ * info - The returned value
+ *
+ * Return Value:
+ * Signal number that cause the wait to be terminated, otherwise
+ * -1 (ERROR) is returned.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+int sigwaitinfo(const sigset_t *set, struct siginfo *info)
+{
+ return sigtimedwait(set, info, NULL);
+}
diff --git a/nuttx/sched/sleep.c b/nuttx/sched/sleep.c
new file mode 100644
index 0000000000..38eefd2006
--- /dev/null
+++ b/nuttx/sched/sleep.c
@@ -0,0 +1,101 @@
+/************************************************************
+ * sleep.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <signal.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: sleep
+ *
+ * Description:
+ * As typically declared in unistd.h. sleep() is a simple
+ * application of sigtimedwait.
+ *
+ * Parameters:
+ * seconds
+ *
+ * Returned Value:
+ * Zero if the requested time has elapsed, or the number
+ * of seconds left to sleep.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+unsigned int sleep(unsigned int seconds)
+{
+ sigset_t set;
+ struct timespec ts;
+ struct siginfo value;
+
+ if (seconds)
+ {
+ (void)sigemptyset(&set);
+ ts.tv_sec = seconds;
+ ts.tv_nsec = 0;
+ (void)sigtimedwait(&set, &value, &ts);
+ }
+
+ return 0;
+}
diff --git a/nuttx/sched/task_create.c b/nuttx/sched/task_create.c
new file mode 100644
index 0000000000..ecfd68056d
--- /dev/null
+++ b/nuttx/sched/task_create.c
@@ -0,0 +1,498 @@
+/************************************************************
+ * task_create.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <sched.h>
+#include <string.h>
+#include <errno.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+static void task_start(void);
+static STATUS task_assignpid(_TCB* tcb);
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: task_start
+ *
+ * Description:
+ * This function is the low level entry point
+ * into the main thread of execution of a task. It receives
+ * initial control when the task is started and calls main
+ * entry point of the newly started task.
+ *
+ * Inputs:
+ * None
+ *
+ * Return:
+ * None
+ *
+ ************************************************************/
+
+static void task_start(void)
+{
+ _TCB *tcb = (_TCB*)g_readytorun.head;
+ int argc;
+
+ /* Count how many non-null arguments we are passing */
+
+ for (argc = 1; argc <= NUM_TASK_ARGS; argc++)
+ {
+ /* The first non-null argument terminates the list */
+
+ if (!tcb->argv[argc])
+ {
+ break;
+ }
+ }
+
+ /* Call the 'main' entry point passing argc and argv. If/when
+ * the task returns, */
+
+ exit(tcb->entry.main(argc, tcb->argv));
+}
+
+/************************************************************
+ * Name: task_assignpid
+ *
+ * Description:
+ * This function assigns the next unique task ID to a task.
+ *
+ * Inputs:
+ * tcb - TCB of task
+ *
+ * Return:
+ * OK on success; ERROR on failure (errno is not set)
+ *
+ ************************************************************/
+
+static STATUS task_assignpid(_TCB *tcb)
+{
+ pid_t next_pid;
+ int hash_ndx;
+ int tries = 0;
+
+ /* Disable pre-emption. This should provide sufficient protection
+ * for the following operation.
+ */
+
+ (void)sched_lock();
+
+ /* We'll try every allowable pid */
+
+ for (tries = 0; tries < MAX_TASKS_ALLOWED; tries++)
+ {
+ /* Get the next process ID candidate */
+
+ next_pid = ++g_lastpid;
+
+ /* Verify that the next_pid is in the valid range */
+
+ if (next_pid <= 0)
+ {
+ g_lastpid = 1;
+ next_pid = 1;
+ }
+
+ /* Get the hash_ndx associated with the next_pid */
+
+ hash_ndx = PIDHASH(next_pid);
+
+ /* Check if there is a (potential) duplicate of this pid */
+
+ if (!g_pidhash[hash_ndx].tcb)
+ {
+ g_pidhash[hash_ndx].tcb = tcb;
+ g_pidhash[hash_ndx].pid = next_pid;
+ tcb->pid = next_pid;
+ (void)sched_unlock();
+ return OK;
+ }
+ }
+
+ /* If we get here, then the g_pidhash[] table is completely full.
+ * We cannot allow another task to be started.
+ */
+
+ (void)sched_unlock();
+ return ERROR;
+}
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: _task_init and task_init
+ *
+ * Description:
+ * These functions initializes a Task Control Block (TCB)
+ * in preparation for starting a new thread. _task_init()
+ * is an internal version of the function that has some
+ * additional control arguments and task_init() is a wrapper
+ * function that creates a VxWorks-like user API.
+ * task_init() is, otherwise, not used by the OS.
+ *
+ * _task_init() is called from task_init() and task_start().\
+ * It is also called from pthread_create() to create a
+ * a pthread (distinguished by the pthread argument).
+ *
+ * Unlike task_create(), task_init() does not activate the
+ * task. This must be done by calling task_activate()
+ * afterward.
+ *
+ * Input Parameters:
+ * tcb - Address of the new task's TCB
+ * name - Name of the new task (not used)
+ * priority - Priority of the new task
+ * entry - Entry point of a new task
+ * main - Application start point of the new task
+ * pthread - TRUE is the task emulates pthread behavior
+ * arg1-4 - Four required task arguments to pass to
+ * the task when it is started.
+ *
+ * Return Value:
+ * OK on success; ERROR on failure.
+ *
+ * This function can only failure is it is unable to assign
+ * a new, unique task ID to the TCB (errno is not set).
+ *
+ ************************************************************/
+
+STATUS _task_init(_TCB *tcb, char *name, int priority,
+ start_t start, main_t main, boolean pthread,
+ char *arg1, char *arg2, char *arg3, char *arg4)
+{
+ STATUS ret;
+
+ /* Assign a unique task ID to the task. */
+
+ ret = task_assignpid(tcb);
+ if (ret == OK)
+ {
+ /* Save task priority and entry point in the TCB */
+
+ tcb->init_priority = (ubyte)priority;
+ tcb->sched_priority = (ubyte)priority;
+ tcb->start = start;
+ tcb->entry.main = main;
+
+#if CONFIG_TASK_NAME_SIZE > 0
+ /* Give a name to the unnamed threads */
+
+ if (!name)
+ {
+ name = "no name";
+ }
+
+ /* copy the name into the TCB */
+
+ strncpy(tcb->name, name, CONFIG_TASK_NAME_SIZE);
+#endif /* CONFIG_TASK_NAME_SIZE */
+
+ /* Save the arguments in the TCB */
+
+#if CONFIG_TASK_NAME_SIZE > 0
+ tcb->argv[0] = tcb->name;
+#else
+ tcb->argv[0] = "no name";
+#endif
+
+ /* For pthreads, args are strictly pass-by-value; the char*
+ * arguments wrap some unknown value cast to char*. However,
+ * for tasks, the life of the argument must be as long as
+ * the life of the task and the arguments must be strings.
+ * So for tasks, we have to to dup the strings.
+ */
+
+ if (!pthread)
+ {
+ /* The first NULL argument terminates the list of
+ * arguments.
+ */
+
+ if (arg1)
+ {
+ tcb->argv[1] = strdup(arg1);
+ if (arg2)
+ {
+ tcb->argv[2] = strdup(arg2);
+ if (arg3)
+ {
+ tcb->argv[3] = strdup(arg3);
+ if (arg4)
+ {
+ tcb->argv[4] = strdup(arg4);
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ /* Mark this task as a pthread */
+
+ tcb->flags |= TCB_FLAG_PTHREAD;
+
+ /* And just copy the argument. (For pthreads, there
+ * is really only a single argument, arg1).
+ */
+
+ tcb->argv[1] = arg1;
+ tcb->argv[2] = arg2;
+ tcb->argv[3] = arg3;
+ tcb->argv[4] = arg4;
+ }
+
+ /* Initialize other (non-zero) elements of the TCB */
+
+ tcb->sigprocmask = ALL_SIGNAL_SET;
+ tcb->task_state = TSTATE_TASK_INVALID;
+
+ /* Initialize the processor-specific portion of the TCB */
+
+ up_initial_state(tcb);
+
+ /* Add the task to the inactive task list */
+
+ sched_lock();
+ dq_addfirst((dq_entry_t*)tcb, &g_inactivetasks);
+ tcb->task_state = TSTATE_TASK_INACTIVE;
+ sched_unlock();
+ }
+
+ return ret;
+}
+
+/************************************************************
+ * Name: _task_init and task_init
+ *
+ * Description:
+ * This is a wrapper around the internal _task_init() that
+ * provides a VxWorks-like API. See _task_init() for
+ * further information.
+ *
+ * Input Parameters:
+ * tcb - Address of the new task's TCB
+ * name - Name of the new task (not used)
+ * priority - Priority of the new task
+ * stack - start of the pre-allocated stack
+ * stack_size - size (in bytes) of the stack allocated
+ * entry - Application start point of the new task
+ * arg1-4 - Four required task arguments to pass to
+ * the task when it is started.
+ *
+ * Return Value:
+ * see _task_init()
+ *
+ ************************************************************/
+
+STATUS task_init(_TCB *tcb, char *name, int priority,
+ uint32 *stack, uint32 stack_size, main_t entry,
+ char *arg1, char *arg2, char *arg3, char *arg4)
+{
+ up_use_stack(tcb, stack, stack_size);
+ return _task_init(tcb, name, priority, task_start, entry,
+ FALSE, arg1, arg2, arg3, arg4);
+}
+
+/************************************************************
+ * Name: task_activate
+ *
+ * Description:
+ * This function activates tasks initialized by _task_init().
+ * Without activation, a task is ineligible for execution
+ * by the scheduler.
+ *
+ * Input Parameters:
+ * tcb - The TCB for the task for the task (same as the
+ * task_init argument.
+ *
+ * Return Value:
+ * Always returns OK
+ *
+ ************************************************************/
+
+STATUS task_activate(_TCB *tcb)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION
+ uint32 savedState;
+#endif
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION
+ savedState = irqsave();
+
+ /* Check if this is really a re-start */
+
+ if (tcb->task_state != TSTATE_TASK_INACTIVE)
+ {
+ /* Inform the instrumentation layer that the task
+ * has stopped
+ */
+
+ sched_note_stop(tcb);
+ }
+
+ /* Inform the instrumentation layer that the task
+ * has started
+ */
+
+ sched_note_start(tcb);
+ irqrestore(savedState);
+#endif
+
+ up_unblock_task(tcb);
+ return OK;
+}
+
+/************************************************************
+ * Name: task_create
+ *
+ * Description:
+ * This function creates and activates a new task with a
+ * specified priority and returns its system-assigned ID.
+ *
+ * The entry address entry is the address of the "main"
+ * function of the task. This function will be called once
+ * the C environment has been set up. The specified
+ * function will be called with four arguments. Should
+ * the specified routine return, a call to exit() will
+ * automatically be made.
+ *
+ * Note that four (and only four) arguments must be passed for
+ * the spawned functions.
+ *
+ * Input Parameters:
+ * name - Name of the new task
+ * priority - Priority of the new task
+ * stack_size - size (in bytes) of the stack needed
+ * entry - Entry point of a new task
+ * arg* - Ten required task arguments to pass to func
+ *
+ * Return Value:
+ * Returns the non-zero process ID of the new task or
+ * ERROR if memory is insufficient or the task cannot be
+ * created (errno is not set).
+ *
+ ************************************************************/
+
+int task_create(char *name, int priority,
+ int stack_size, main_t entry,
+ char *arg1, char *arg2, char *arg3, char *arg4)
+{
+ _TCB *tcb;
+ STATUS status;
+ pid_t pid;
+
+ /* Allocate a TCB for the new task. */
+
+ tcb = (_TCB*)kzmalloc(sizeof(_TCB));
+ if (!tcb)
+ {
+ *get_errno_ptr() = ENOMEM;
+ return ERROR;
+ }
+
+ /* Associate file descriptors with the new task */
+
+ if (sched_setuptaskfiles(tcb) != OK)
+ {
+ sched_releasetcb(tcb);
+ return ERROR;
+ }
+
+ /* Allocate the stack for the TCB */
+
+ status = up_create_stack(tcb, stack_size);
+ if (status != OK)
+ {
+ sched_releasetcb(tcb);
+ return ERROR;
+ }
+
+ /* Initialize the task control block */
+
+ status = _task_init(tcb, name, priority, task_start, entry,
+ FALSE, arg1, arg2, arg3, arg4);
+ if (status != OK)
+ {
+ sched_releasetcb(tcb);
+ return ERROR;
+ }
+
+ /* Get the assigned pid before we start the task */
+
+ pid = (int)tcb->pid;
+
+ /* Activate the task */
+
+ status = task_activate(tcb);
+ if (status != OK)
+ {
+ dq_rem((dq_entry_t*)tcb, &g_inactivetasks);
+ sched_releasetcb(tcb);
+ return ERROR;
+ }
+
+ return pid;
+}
diff --git a/nuttx/sched/task_delete.c b/nuttx/sched/task_delete.c
new file mode 100644
index 0000000000..5a28c3d426
--- /dev/null
+++ b/nuttx/sched/task_delete.c
@@ -0,0 +1,165 @@
+/************************************************************
+ * task_delete.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <sched.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: task_delete
+ *
+ * Description:
+ * This function causes a specified task to cease to exist.
+ * Its stack and TCB will be deallocated. This function
+ * is the companion to task_create().
+ *
+ * Inputs:
+ * pid - The task ID of the task to delete. A pid of zero
+ * signifies the calling task.
+ *
+ * Return Value:
+ * OK on success; or ERROR on failure
+ *
+ * This function can fail if the provided pid does not
+ * correspond to a task (errno is not set)
+ *
+ ************************************************************/
+
+STATUS task_delete(pid_t pid)
+{
+ _TCB *rtcb;
+ _TCB *dtcb;
+ uint32 saved_state;
+ STATUS ret = ERROR;
+
+ /* Check if the task to delete is the calling task */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ if (pid == 0 || pid == rtcb->pid)
+ {
+ /* If it is, then what we really wanted to do was exit.
+ * Note that we don't bother to unlock the TCB since
+ * it will be going away.
+ */
+
+ exit(EXIT_SUCCESS);
+ }
+
+ /* Make sure the task does not become ready-to-run while
+ * we are futzing with its TCB by locking ourselves as the
+ * executing task.
+ *
+ * Frist, find for the TCB associated with matching pid
+ */
+
+ dtcb = sched_gettcb(pid);
+ if (!dtcb)
+ {
+ /* This pid does not correspond to any known task */
+
+ sched_unlock();
+ return ERROR;
+ }
+
+ /* Verify our internal sanity */
+
+ if (dtcb->task_state == TSTATE_TASK_RUNNING ||
+ dtcb->task_state >= NUM_TASK_STATES)
+ {
+ sched_unlock();
+ PANIC(OSERR_BADDELETESTATE);
+ }
+
+ saved_state = irqsave();
+
+ /* Inform the instrumentation layer that the task has stopped */
+
+ sched_note_stop(dtcb);
+
+ /* Remove the task from the OS's tasks lists. */
+
+ dq_rem((dq_entry_t*)dtcb, g_tasklisttable[dtcb->task_state].list);
+ dtcb->task_state = TSTATE_TASK_INVALID;
+ irqrestore(saved_state);
+
+ /* At this point, the TCB should no longer be accessible to the system */
+
+ sched_unlock();
+
+ /* Deallocate anything left in the TCB's queues */
+
+ sig_cleanup(dtcb); /* Deallocate Signal lists */
+
+ /* Deallocate its TCB */
+
+ sched_releasetcb(dtcb);
+ return ret;
+}
diff --git a/nuttx/sched/task_restart.c b/nuttx/sched/task_restart.c
new file mode 100644
index 0000000000..6d4b7a83a5
--- /dev/null
+++ b/nuttx/sched/task_restart.c
@@ -0,0 +1,178 @@
+/************************************************************
+ * task_restart.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <nuttx/arch.h>
+#include <nuttx/os_external.h>
+#include "os_internal.h"
+#include "sig_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Name: task_restart
+ *
+ * Description:
+ * This function "restarts" a task. The task is first
+ * terminated and then reinitialized with same ID, priority,
+ * original entry point, stack size, and parameters it had
+ * when it was first started.
+ *
+ * Inputs:
+ * pid - The task ID of the task to delete. An ID of zero
+ * signifies the calling task.
+ *
+ * Return Value:
+ * OK on sucess; ERROR on failure.
+ *
+ * This function can fail if:
+ * (1) A pid of zero or the pid of the calling task is
+ * provided (functionality not implemented)
+ * (2) The pid is not associated with any task known to
+ * the system.
+ *
+ ************************************************************/
+
+STATUS task_restart(pid_t pid)
+{
+ _TCB *rtcb;
+ _TCB *tcb;
+ STATUS status;
+ uint32 state;
+
+ /* Make sure this task does not become ready-to-run while
+ * we are futzing with its TCB
+ */
+
+ sched_lock();
+
+ /* Check if the task to restart is the calling task */
+
+ rtcb = (_TCB*)g_readytorun.head;
+ if ((pid == 0) || (pid == rtcb->pid))
+ {
+ /* Not implemented */
+
+ return ERROR;
+ }
+
+ /* We are restarting some other task than ourselves */
+
+ else
+ {
+ /* Find for the TCB associated with matching pid */
+
+ tcb = sched_gettcb(pid);
+ if (!tcb)
+ {
+ /* There is no TCB with this pid */
+
+ return ERROR;
+ }
+
+ /* Remove the TCB from whatever list it is in. At this point, the
+ * TCB should no longer be accessible to the system
+ */
+
+ state = irqsave();
+ dq_rem((dq_entry_t*)tcb, g_tasklisttable[tcb->task_state].list);
+ tcb->task_state = TSTATE_TASK_INVALID;
+ irqrestore(state);
+
+ /* Deallocate anything left in the TCB's queues */
+
+ sig_cleanup(tcb); /* Deallocate Signal lists */
+
+ /* Reset the task priority */
+
+ tcb->sched_priority = tcb->init_priority;
+
+ /* Re-initialize the processor-specific portion of the TCB
+ * This will reset the entry point and the start-up parameters
+ */
+
+ up_initial_state(tcb);
+
+ /* Add the task to the inactive task list */
+
+ dq_addfirst((dq_entry_t*)tcb, &g_inactivetasks);
+ tcb->task_state = TSTATE_TASK_INACTIVE;
+
+ /* Activate the task */
+
+ status = task_activate(tcb);
+ if (status != OK)
+ {
+ dq_rem((dq_entry_t*)tcb, &g_inactivetasks);
+ sched_releasetcb(tcb);
+ return ERROR;
+ }
+ }
+
+ sched_unlock();
+ return OK;
+}
diff --git a/nuttx/sched/usleep.c b/nuttx/sched/usleep.c
new file mode 100644
index 0000000000..69e379c7bc
--- /dev/null
+++ b/nuttx/sched/usleep.c
@@ -0,0 +1,98 @@
+/************************************************************
+ * usleep.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <signal.h>
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Function Prototypes
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: usleep
+ *
+ * Description:
+ * BSD version as typically declared in unistd.h. suleep()
+ * is a simple application of sigtimedwait.
+ *
+ * Parameters:
+ * seconds
+ *
+ * Returned Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+void usleep(unsigned long usec)
+{
+ sigset_t set;
+ struct timespec ts;
+ struct siginfo value;
+
+ if (usec)
+ {
+ (void)sigemptyset(&set);
+ ts.tv_sec = usec / 1000000;
+ ts.tv_nsec = usec % 1000;
+ (void)sigtimedwait(&set, &value, &ts);
+ }
+}
diff --git a/nuttx/sched/wd_cancel.c b/nuttx/sched/wd_cancel.c
new file mode 100644
index 0000000000..9a3a223fdc
--- /dev/null
+++ b/nuttx/sched/wd_cancel.c
@@ -0,0 +1,154 @@
+/************************************************************
+ * wd_cancel.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <assert.h>
+#include <wdog.h>
+#include <nuttx/arch.h>
+#include "os_internal.h"
+#include "wd_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: wd_cancel
+ *
+ * Description:
+ * This function cancels a currently running watchdog timer.
+ * Watchdog timers may be canceled from the interrupt level.
+ *
+ * Parameters:
+ * wdid - ID of the watchdog to cancel.
+ *
+ * Return Value:
+ * OK or ERROR
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+STATUS wd_cancel (WDOG_ID wdid)
+{
+ wdog_t *curr;
+ wdog_t *prev;
+ uint32 saved_state;
+ STATUS ret = ERROR;
+
+ /* Prohibit timer interactions with the timer queue until the
+ * cancellation is complete
+ */
+
+ saved_state = irqsave();
+
+ /* Make sure that the the watchdog is still active */
+
+ if (wdid->active)
+ {
+ /* Search the g_wdactivelist for the target FCB. We can't use sq_rem
+ * to do this because there are additional operations that need to be
+ * done.
+ */
+
+ prev = NULL;
+ curr = (wdog_t*)g_wdactivelist.head;
+
+ while((curr) && (curr != wdid))
+ {
+ prev = curr;
+ curr = curr->next;
+ }
+
+ /* Check if the watchdog was found in the list. If not, then an OS
+ * error has occurred because the watchdog is marked active!
+ */
+
+ if (!curr)
+ {
+ PANIC(OSERR_WDOGNOTFOUND);
+ }
+ else
+ {
+ /* If there is a watchdog in the timer queue after the one that
+ * is being canceled, then it inherits the remaining ticks.
+ */
+
+ if (curr->next) curr->next->lag += curr->lag;
+
+ /* Now, remove the watchdog from the timer queue */
+
+ if (prev)
+ (void)sq_remafter((sq_entry_t*)prev, &g_wdactivelist);
+ else
+ (void)sq_remfirst(&g_wdactivelist);
+ wdid->next = NULL;
+
+ /* If we made it this far, then we were successful */
+
+ ret = OK;
+ }
+
+ /* Mark the watchdog inactive */
+
+ wdid->active = FALSE;
+ }
+ irqrestore(saved_state);
+ return ret;
+}
diff --git a/nuttx/sched/wd_create.c b/nuttx/sched/wd_create.c
new file mode 100644
index 0000000000..14a1c54104
--- /dev/null
+++ b/nuttx/sched/wd_create.c
@@ -0,0 +1,105 @@
+/************************************************************
+ * wd_create.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <wdog.h>
+#include <queue.h>
+#include <nuttx/arch.h>
+#include "wd_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: wd_create
+ *
+ * Description:
+ * The wd_create function will create a watchdog by
+ * allocating it from the free list.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * Pointer to watchdog (i.e., the watchdog ID), or NULL
+ * if insufficient watchdogs are available.
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+WDOG_ID wd_create (void)
+{
+ wdog_t *wdog;
+ sint32 saved_state;
+
+ saved_state = irqsave();
+ wdog = (wdog_t*)sq_remfirst(&g_wdfreelist);
+ irqrestore(saved_state);
+
+ /* Indicate that the watchdog is not actively timing */
+
+ if (wdog)
+ {
+ wdog->next = NULL;
+ wdog->active = FALSE;
+ }
+ return (WDOG_ID)wdog;
+}
diff --git a/nuttx/sched/wd_delete.c b/nuttx/sched/wd_delete.c
new file mode 100644
index 0000000000..b4039cce00
--- /dev/null
+++ b/nuttx/sched/wd_delete.c
@@ -0,0 +1,108 @@
+/************************************************************
+ * wd_delete.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <wdog.h>
+#include <queue.h>
+#include <nuttx/arch.h>
+#include "wd_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: wd_delete
+ *
+ * Description:
+ * The wd_delete function will deallocate a watchdog by
+ * returning it to the free pool of watchdogs. The watchdog
+ * will be removed from the timer queue if has been started.
+ *
+ * Parameters:
+ * wdId - The watchdog ID to delete. This is actually a
+ * pointer to a watchdog structure.
+ *
+ * Return Value:
+ * Returns OK or ERROR
+ *
+ * Assumptions:
+ * The caller has assured that the watchdog is no longer
+ * in use.
+ *
+ ************************************************************/
+
+STATUS wd_delete (WDOG_ID wdId)
+{
+ uint32 saved_state;
+
+ /* Check if the watchdog has been started. */
+
+ saved_state = irqsave();
+ if (wdId->active) wd_cancel(wdId);
+
+ /* Put the watchdog back on the free list */
+
+ sq_addlast((sq_entry_t*)wdId, &g_wdfreelist);
+ irqrestore(saved_state);
+
+ /* Return success */
+
+ return OK;
+}
diff --git a/nuttx/sched/wd_initialize.c b/nuttx/sched/wd_initialize.c
new file mode 100644
index 0000000000..5d979bb3df
--- /dev/null
+++ b/nuttx/sched/wd_initialize.c
@@ -0,0 +1,136 @@
+/************************************************************
+ * wd_initialize.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <queue.h>
+#include <nuttx/kmalloc.h>
+#include "os_internal.h"
+#include "wd_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/* The g_wdfreelist data structure is a singly linked list
+ * of watchdogs available to the system for delayed function
+ * use.
+ */
+
+sq_queue_t g_wdfreelist;
+
+/* g_wdpool is a pointer to a list of pre-allocated watchdogs.
+ * The number of watchdogs in the pool is a configuration
+ * item.
+ */
+
+wdog_t *g_wdpool;
+
+/* The g_wdactivelist data structure is a singly linked list
+ * ordered by watchdog expiration time. When watchdog timers
+ * expire,the functions on this linked list are removed and
+ * the function is called.
+ */
+
+sq_queue_t g_wdactivelist;
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: wd_initialize
+ *
+ * Description:
+ * This function initalized the watchdog data structures
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ * This function must be called early in the initialization
+ * sequence before the timer interrupt is attached and
+ * before any watchdog services are used.
+ *
+ ************************************************************/
+
+void wd_initialize(void)
+{
+ /* Initialize the free watchdog list */
+
+ sq_init(&g_wdfreelist);
+
+ /* The g_wdfreelist must be loaded at initialization time to hold the
+ * configured number of watchdogs.
+ */
+
+ g_wdpool = (wdog_t*)kmalloc(sizeof(wdog_t) * CONFIG_PREALLOC_WDOGS);
+ if (g_wdpool)
+ {
+ wdog_t *wdog = g_wdpool;
+ int i;
+
+ for (i = 0; i < CONFIG_PREALLOC_WDOGS; i++)
+ {
+ sq_addlast((sq_entry_t*)wdog++, &g_wdfreelist);
+ }
+ }
+
+ /* The g_wdactivelist queue must be reset at initialization time. */
+
+ sq_init(&g_wdactivelist);
+}
diff --git a/nuttx/sched/wd_internal.h b/nuttx/sched/wd_internal.h
new file mode 100644
index 0000000000..9198387cca
--- /dev/null
+++ b/nuttx/sched/wd_internal.h
@@ -0,0 +1,121 @@
+/************************************************************
+ * wd_internal.h
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#ifndef __WD_INTERNAL_H
+#define __WD_INTERNAL_H
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <wdog.h>
+#include <nuttx/compiler.h>
+
+/************************************************************
+ * Compilations Switches
+ ************************************************************/
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Public Type Declarations
+ ************************************************************/
+
+/* This is the watchdog structure. The WDOG_ID is a pointer to
+ * a watchdog structure.
+ */
+
+struct wdog_s
+{
+ struct wdog_s *next; /* Support for singly linked lists. */
+ wdentry_t func; /* Function to execute when delay expires */
+ sint32 lag; /* Timer associated with the delay */
+ uint32 parm[4]; /* Four parameters passed to func */
+ boolean active; /* TRUE if the watchdog is actively timing */
+};
+
+typedef struct wdog_s wdog_t;
+typedef struct wdog_s *WDOG_ID;
+
+/************************************************************
+ * Public Variables
+ ************************************************************/
+
+/* The g_wdfreelist data structure is a singly linked list
+ * of watchdogs available to the system for delayed function
+ * use.
+ */
+
+extern sq_queue_t g_wdfreelist;
+
+/* g_wdpool is a pointer to a list of pre-allocated watchdogs.
+ * The number of watchdogs in the pool is a configuration
+ * item.
+ */
+
+extern wdog_t *g_wdpool;
+
+/* The g_wdactivelist data structure is a singly linked list
+ * ordered by watchdog expiration time. When watchdog timers
+ * expire,the functions on this linked list are removed and
+ * the function is called.
+ */
+
+extern sq_queue_t g_wdactivelist;
+
+/************************************************************
+ * Public Function Prototypes
+ ************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C" {
+#else
+#define EXTERN extern
+#endif
+
+EXTERN void weak_function wd_initialize(void);
+EXTERN void weak_function wd_timer(void);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __WD_INTERNAL_H */
+
diff --git a/nuttx/sched/wd_start.c b/nuttx/sched/wd_start.c
new file mode 100644
index 0000000000..04ece34938
--- /dev/null
+++ b/nuttx/sched/wd_start.c
@@ -0,0 +1,312 @@
+/************************************************************
+ * wd_start.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+/************************************************************
+ * Included Files
+ ************************************************************/
+
+#include <sys/types.h>
+#include <wdog.h>
+#include <unistd.h>
+#include <sched.h>
+#include "wd_internal.h"
+
+/************************************************************
+ * Definitions
+ ************************************************************/
+
+/************************************************************
+ * Private Type Declarations
+ ************************************************************/
+
+/************************************************************
+ * Global Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Variables
+ ************************************************************/
+
+/************************************************************
+ * Private Functions
+ ************************************************************/
+
+/************************************************************
+ * Public Functions
+ ************************************************************/
+
+/************************************************************
+ * Function: wd_start
+ *
+ * Description:
+ * This function adds a watchdog to the timer queue. The
+ * specified watchdog function will be called from the
+ * interrupt level after the specified number of ticks has
+ * elapsed. Watchdog timers may be started from the
+ * interrupt level.
+ *
+ * Watchdog timers execute in the address enviroment that
+ * was in effect when wd_start() is called.
+ *
+ * Watchdog timers execute only once.
+ *
+ * To replace either the timeout delay or the function to
+ * be executed, call wd_start again with the same wdId; only
+ * the most recent wdStart() on a given watchdog ID has
+ * any effect.
+ *
+ * Parameters:
+ * wdId = watchdog ID
+ * delay = Delay count in clock ticks
+ * wdentry = function to call on timeout
+ * parm1..4 = parameters to pass to wdentry
+ *
+ * Return Value:
+ * OK or ERROR
+ *
+ * Assumptions:
+ * The watchdog routine runs in the context of the timer interrupt
+ * handler and is subject to all ISR restrictions.
+ *
+ ************************************************************/
+
+STATUS wd_start(WDOG_ID wdId, int delay, wdentry_t wdentry,
+ int parm1, int parm2, int parm3, int parm4)
+{
+ wdog_t *curr;
+ wdog_t *prev;
+ wdog_t *next;
+ sint32 now;
+ sint32 saved_state;
+
+ /* Verify the wdId */
+
+ if (!wdId)
+ {
+ return ERROR;
+ }
+
+ /* Check if the watchdog has been started. If so, stop it.
+ * NOTE: There is a race condition here... the caller may receive
+ * the watchdog between the time that wd_start is called and
+ * the critical section is established.
+ */
+
+ saved_state = irqsave();
+ if (wdId->active)
+ {
+ wd_cancel(wdId);
+ }
+
+ /* Save the data in the watchdog structure */
+
+ wdId->func = wdentry; /* Function to execute when delay expires */
+ wdId->parm[0] = parm1; /* Same as the parameter to pass */
+ wdId->parm[1] = parm2; /* 2nd parameter not used */
+ wdId->parm[2] = parm3; /* 3rd parameter not used */
+ wdId->parm[3] = parm4; /* 4th parameter not used */
+
+ /* Calculate delay+1, forcing the delay into a range that we can handle */
+
+ if (delay <= 0)
+ {
+ delay = 1;
+ }
+ else if (++delay <= 0)
+ {
+ delay--;
+ }
+
+ /* Do the easy case first -- when the watchdog timer queue is empty. */
+
+ if (g_wdactivelist.head == NULL)
+ {
+ sq_addlast((sq_entry_t*)wdId,&g_wdactivelist);
+ }
+
+ /* There are other active watchdogs in the timer queue */
+
+ else
+ {
+ now = 0;
+ prev = curr = (wdog_t*)g_wdactivelist.head;
+
+ /* Advance to positive time */
+
+ while ((now += curr->lag) < 0 && curr->next)
+ {
+ prev = curr;
+ curr = curr->next;
+ }
+
+ /* Advance past shorter delays */
+
+ while (now <= delay && curr->next)
+ {
+ prev = curr;
+ curr = curr->next;
+ now += curr->lag;
+ }
+
+ /* Check if the new wdId must be inserted before the curr. */
+
+ if (delay < now)
+ {
+ /* The relative delay time is smaller or equal to the current delay
+ * time, so decrement the current delay time by the new relative
+ * delay time.
+ */
+
+ delay -= (now - curr->lag);
+ curr->lag -= delay;
+
+ /* Insert the new watchdog in the list */
+
+ if (curr == (wdog_t*)g_wdactivelist.head)
+ {
+ sq_addfirst((sq_entry_t*)wdId, &g_wdactivelist);
+ }
+ else
+ {
+ sq_addafter((sq_entry_t*)prev, (sq_entry_t*)wdId,
+ &g_wdactivelist);
+ }
+ }
+
+ /* The new watchdog delay time is greater than the curr delay time,
+ * so the new wdId must be inserted after the curr. This only occurs
+ * if the wdId is to be added to the end of the list.
+ */
+
+ else
+ {
+ delay -= now;
+ if (!curr->next)
+ {
+ sq_addlast((sq_entry_t*)wdId, &g_wdactivelist);
+ }
+ else
+ {
+ next = curr->next;
+ next->lag -= delay;
+ sq_addafter((sq_entry_t*)curr, (sq_entry_t*)wdId,
+ &g_wdactivelist);
+ }
+ }
+ }
+
+ /* Put the lag into the watchdog structure and mark it as active. */
+
+ wdId->lag = delay;
+ wdId->active = TRUE;
+
+ irqrestore(saved_state);
+ return OK;
+}
+
+/************************************************************
+ * Function: wd_timer
+ *
+ * Description:
+ * This function is called from the timer interrupt
+ * handler to determine if it is time to execute a watchdog
+ * function. If so, the watchdog function will be executed
+ * in the context of the timer interrupt handler.
+ *
+ * Parameters:
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+void wd_timer(void)
+{
+ pid_t pid;
+ wdog_t *wdog;
+
+ /* Check if there are any active watchdogs to process */
+
+ if (g_wdactivelist.head)
+ {
+ /* There are. Decrement the lag counter */
+
+ --(((wdog_t*)g_wdactivelist.head)->lag);
+
+ /* Check if the watchdog at the head of the list is ready to run */
+
+ if (((wdog_t*)g_wdactivelist.head)->lag <= 0)
+ {
+ /* Process the watchdog at the head of the list as well as any
+ * other watchdogs that became ready to run at this time
+ */
+
+ while (g_wdactivelist.head &&
+ ((wdog_t*)g_wdactivelist.head)->lag <= 0)
+ {
+ /* Remove the watchdog from the head of the list */
+
+ wdog = (wdog_t*)sq_remfirst(&g_wdactivelist);
+
+ /* If there is another watchdog behind this one, update its
+ * its lag (this shouldn't be necessary).
+ */
+
+ if (g_wdactivelist.head)
+ {
+ ((wdog_t*)g_wdactivelist.head)->lag += wdog->lag;
+ }
+
+ /* Indicate that the watchdog is no longer activer. */
+
+ wdog->active = FALSE;
+
+ /* Get the current task's process ID. We'll need this later to
+ * see if the watchdog function caused a context switch.
+ */
+
+ pid = getpid();
+
+ /* Execute the watchdog function */
+
+ (*wdog->func)(wdog->parm[0], wdog->parm[1],
+ wdog->parm[2] ,wdog->parm[3]);
+ }
+ }
+ }
+}
diff --git a/nuttx/tools/Makefile.mkconfig b/nuttx/tools/Makefile.mkconfig
new file mode 100644
index 0000000000..768ebccf47
--- /dev/null
+++ b/nuttx/tools/Makefile.mkconfig
@@ -0,0 +1,43 @@
+############################################################
+# Makefile.mkconfig
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################
+
+all: mkconfig
+default: mkconfig
+
+mkconfig: mkconfig.c
+ gcc -O2 -Wall -o mkconfig mkconfig.c
+
+clean:
+ rm -f mkconfig *~
diff --git a/nuttx/tools/configure.sh b/nuttx/tools/configure.sh
new file mode 100755
index 0000000000..7275e8f181
--- /dev/null
+++ b/nuttx/tools/configure.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+# configure.sh
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+#set -x
+
+ARCH=$1
+WD=`pwd`
+TOPDIR=${WD}/..
+
+function show_usage ()
+{
+ echo "${0} <arch>"
+ exit 1
+}
+
+if [ "${ARCH}X" = "X" ]; then
+ echo "Missing argument"
+ show_usage
+fi
+
+ARCHDIR=${TOPDIR}/arch/${ARCH}
+if [ ! -d ${ARCHDIR} ]; then
+ echo "Directory ${ARCHDIR} does not exist"
+ show_usage
+fi
+
+if [ ! -r ${ARCHDIR}/Make.defs ]; then
+ echo "File ${ARCHDIR}/Make.defs does not exist"
+ exit 1
+fi
+
+if [ ! -r ${ARCHDIR}/setenv.sh ]; then
+ echo "File ${ARCHDIR}/setenv.sh does not exist"
+ exit 1
+fi
+
+if [ ! -r ${ARCHDIR}/defconfig ]; then
+ echo "File ${ARCHDIR}/defconfig does not exist"
+ exit 1
+fi
+
+cp -f ${ARCHDIR}/Make.defs ${TOPDIR}/. || \
+ { echo "Failed to copy ${ARCHDIR}/Make.defs" ; exit 1 ; }
+cp -f ${ARCHDIR}/setenv.sh ${TOPDIR}/. || \
+ { echo "Failed to copy ${ARCHDIR}/setenv.sh" ; exit 1 ; }
+cp -f ${ARCHDIR}/defconfig ${TOPDIR}/.config || \
+ { echo "Failed to copy ${ARCHDIR}/defconfig" ; exit 1 ; }
+
diff --git a/nuttx/tools/mkconfig.c b/nuttx/tools/mkconfig.c
new file mode 100644
index 0000000000..6de8ddd930
--- /dev/null
+++ b/nuttx/tools/mkconfig.c
@@ -0,0 +1,239 @@
+/************************************************************
+ * mkconfig.c
+ *
+ * Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ************************************************************/
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define DEFCONFIG "defconfig"
+#define LINESIZE 256
+
+static char line[LINESIZE+1];
+
+static char *skip_space(char *ptr)
+{
+ while (*ptr && isspace(*ptr)) ptr++;
+ return ptr;
+}
+
+static char *find_end(char *ptr)
+{
+ while (*ptr && (isalnum(*ptr) || *ptr == '_')) ptr++;
+ return ptr;
+}
+
+static char *find_comment(char *ptr)
+{
+ while (*ptr && *ptr != '"' && *ptr != '\n') ptr++;
+ if (*ptr == '"') ptr++;
+ return ptr;
+}
+
+static char *read_line(FILE *stream)
+{
+ char *ptr;
+
+ for (;;)
+ {
+ line[LINESIZE] = '\0';
+ if (!fgets(line, LINESIZE, stream))
+ {
+ return NULL;
+ }
+ else
+ {
+ ptr = skip_space(line);
+ if (*ptr && *ptr != '#' && *ptr != '\n')
+ {
+ return ptr;
+ }
+ }
+ }
+}
+
+static void parse_line(char *ptr, char **varname, char **varval)
+{
+ *varname = ptr;
+ *varval = NULL;
+
+ ptr = find_end(ptr);
+ if (*ptr && *ptr != '=')
+ {
+ *ptr = '\0';
+ ptr = skip_space(ptr + 1);
+ }
+
+ if (*ptr == '=')
+ {
+ *ptr = '\0';
+ ptr = skip_space(ptr + 1);
+ if (*ptr)
+ {
+ *varval = ptr;
+ if (*ptr == '"')
+ {
+ ptr = find_comment(ptr);
+ }
+ else
+ {
+ ptr = find_end(ptr);
+ }
+ *ptr = '\0';
+ }
+ }
+}
+
+static void parse_file(FILE *stream)
+{
+ char *varname;
+ char *varval;
+ char *ptr;
+
+ do
+ {
+ ptr = read_line(stream);
+ if (ptr)
+ {
+ parse_line(ptr, &varname, &varval);
+ if (varname)
+ {
+ if (!varval || strcmp(varval, "n") == 0)
+ {
+ printf("#undef %s\n", varname);
+ }
+ else if (strcmp(varval, "y") == 0)
+ {
+ printf("#define %s 1\n", varname);
+ }
+ else
+ {
+ printf("#define %s %s\n", varname, varval);
+ }
+ }
+ }
+ }
+ while (ptr);
+}
+
+static void show_usage(const char *progname)
+{
+ fprintf(stderr, "USAGE: %s <abs path to defconfig>\n", progname);
+ exit(1);
+}
+
+int main(int argc, char **argv, char **envp)
+{
+ char *filepath;
+ FILE *stream;
+ int status;
+
+ if (argc != 2)
+ {
+ fprintf(stderr, "Unexpected number of arguments\n");
+ show_usage(argv[0]);
+ }
+
+ status = asprintf(&filepath, "%s/" DEFCONFIG, argv[1]);
+ if (status < 0)
+ {
+ fprintf(stderr, "asprintf failed\n");
+ exit(2);
+ }
+
+ stream= fopen(filepath, "r");
+ if (!stream)
+ {
+ fprintf(stderr, "open %s failed: %s\n", filepath, strerror(errno));
+ exit(3);
+ }
+
+ printf("/* config.h -- Autogenerated! Do not edit. */\n\n");
+ printf("#ifndef __ARCH_CONFIG_H\n");
+ printf("#define __ARCH_CONFIG_H\n\n");
+ printf("/* Architecture-specific options *************************/\n\n");
+ parse_file(stream);
+ printf("\n/* Sanity Checks *****************************************/\n\n");
+ printf("/* The correct way to disable RR scheduling is to set the\n");
+ printf(" * timeslice to zero.\n");
+ printf(" */\n\n");
+ printf("#ifndef CONFIG_RR_INTERVAL\n");
+ printf("# define CONFIG_RR_INTERVAL 0\n");
+ printf("#endif\n\n");
+ printf("/* The correct way to disable filesystem supuport is to set the\n");
+ printf(" * number of file descriptors to zero.\n");
+ printf(" */\n\n");
+ printf("#ifndef CONFIG_NFILE_DESCRIPTORS\n");
+ printf("# define CONFIG_NFILE_DESCRIPTORS 0\n");
+ printf("#endif\n\n");
+ printf("/* If a console is selected, then make sure that there are\n");
+ printf(" * resources for 3 file descriptors and, if any streams are\n");
+ printf(" * selected, also for 3 file streams.\n");
+ printf(" */\n\n");
+ printf("#ifdef CONFIG_DEV_CONSOLE\n");
+ printf("# if CONFIG_NFILE_DESCRIPTORS < 3\n");
+ printf("# undef CONFIG_NFILE_DESCRIPTORS\n");
+ printf("# define CONFIG_NFILE_DESCRIPTORS 3\n");
+ printf("# endif\n\n");
+ printf("# if CONFIG_NFILE_STREAMS > 0 && CONFIG_NFILE_STREAMS < 3\n");
+ printf("# undef CONFIG_NFILE_STREAMS\n");
+ printf("# define CONFIG_NFILE_STREAMS 3\n");
+ printf("# endif\n");
+ printf("#endif\n\n");
+ printf("/* If no file descriptors are configured, then make certain no\n");
+ printf(" * streams are configured either.\n");
+ printf(" */\n\n");
+ printf("#if CONFIG_NFILE_DESCRIPTORS == 0\n");
+ printf("# undef CONFIG_NFILE_STREAMS\n");
+ printf("# define CONFIG_NFILE_STREAMS 0\n");
+ printf("#endif\n\n");
+ printf("/* If no file streams are configured, then make certain that\n");
+ printf(" * buffered I/O support is disabled.\n");
+ printf(" */\n\n");
+ printf("#if CONFIG_NFILE_STREAMS == 0\n");
+ printf("# undef CONFIG_STDIO_BUFFER_SIZE\n");
+ printf("# define CONFIG_STDIO_BUFFER_SIZE 0\n");
+ printf("#endif\n\n");
+ printf("/* Verbose debug only makes sense if debug is enabled */\n\n");
+ printf("#ifndef CONFIG_DEBUG\n");
+ printf("# undef CONFIG_DEBUG_VERBOSE\n");
+ printf("#endif\n\n");
+ printf("#endif /* __ARCH_CONFIG_H */\n");
+ fclose(stream);
+ return 0;
+}
diff --git a/nuttx/tools/mkdeps.sh b/nuttx/tools/mkdeps.sh
new file mode 100755
index 0000000000..086778fbe4
--- /dev/null
+++ b/nuttx/tools/mkdeps.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+# mkdeps.sh
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+#
+# Usage:
+
+function show_usage ()
+{
+ echo "$0 CC -- CFLAGS -- files"
+ exit 1
+}
+
+cc=
+cflags=
+files=
+args=
+
+# Accumulate CFLAGS up to "--"
+for i in $* ; do
+ case $i in
+ --*)
+ cc=$cflags
+ cflags=$args
+ args=
+ ;;
+ *)
+ args="$args $i"
+ ;;
+ esac
+done
+files=$args
+
+if [ -z "$cc" ]; then
+ echo "No compiler specified"
+ exit 1
+fi
+
+if [ -z "$files" ]; then
+ echo "No files specified"
+ exit 2
+fi
+
+for file in $files ; do
+ $cc -M $cflags $file || \
+ { echo "$cc -M $cflags $file FAILED" ; exit 3 ; }
+done
+
diff --git a/nuttx/tools/zipme.sh b/nuttx/tools/zipme.sh
new file mode 100755
index 0000000000..7203a82132
--- /dev/null
+++ b/nuttx/tools/zipme.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+# zipme.sh
+#
+# Copyright (C) 2007 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name Gregory Nutt nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+set -x
+
+WD=`pwd`
+DATECODE=$1
+
+TAR="tar cvf"
+ZIP=bzip2
+
+# Make sure we know what is going on
+
+if [ -z ${DATECODE} ] ; then
+ echo "You must supply a date code like MMDDYY as a parameter"
+ exit 1;
+fi
+
+MYNAME=`basename $0`
+
+if [ -x ${WD}/${MYNAME} ] ; then
+ NUTTX=${WD}/..
+else
+ if [ -x ${WD}/tools/${MYNAME} ] ; then
+ NUTTX=${WD}
+ else
+ echo "You must cd NUTTX directory to execute this script."
+ exit 1
+ fi
+fi
+PROJECTS=${NUTTX}/..
+
+cd ${PROJECTS} || \
+ { echo "Failed to cd to ${PROJECTS}" ; exit 1 ; }
+
+if [ ! -d nuttx ] ; then
+ echo "${PROJECTS}/nuttx does not exist!"
+ exit 1;
+fi
+
+TAR_NAME=nuttx-${DATECODE}.tar
+ZIP_NAME=${TAR_NAME}.bz2
+
+# Prepare the nuttx directory
+
+find nuttx -name '*~' -exec rm -f '{}' ';' || \
+ { echo "Removal of emacs garbage failed!" ; exit 1 ; }
+find nuttx -name '*.swp' -exec rm -f '{}' ';' || \
+ { echo "Removal of VI garbage failed!" ; exit 1 ; }
+
+make -C ${NUTTX} distclean
+
+# Remove any previous tarballs
+
+if [ -f ${TAR_NAME} ] ; then
+ echo "Removing ${PROJECTS}/${TAR_NAME}"
+ rm -f ${TAR_NAME} || \
+ { echo "rm ${TAR_NAME} failed!" ; exit 1 ; }
+fi
+
+if [ -f ${ZIP_NAME} ] ; then
+ echo "Removing ${PROJECTS}/${ZIP_NAME}"
+ rm -f ${ZIP_NAME} || \
+ { echo "rm ${ZIP_NAME} failed!" ; exit 1 ; }
+fi
+
+# Then zip it
+
+${TAR} ${TAR_NAME} nuttx || \
+ { echo "tar of ${TAR_NAME} failed!" ; exit 1 ; }
+${ZIP} ${TAR_NAME} || \
+ { echo "zip of ${TAR_NAME} failed!" ; exit 1 ; }
+
+cd ${NUTTX}