1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - include
3   File:     thread.h
4 
5   Copyright 2003-2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_OS_THREAD_H_
19 #define NITRO_OS_THREAD_H_
20 
21 #include <nitro/misc.h>
22 #include <nitro/os/common/context.h>
23 #include <nitro/os/common/callTrace.h>
24 #include <nitro/version.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 //---- build switch to let number of thread infinity, for version 3.0 or later
31 #if SDK_VERSION_MAJOR >= 3
32 #define SDK_THREAD_INFINITY           1
33 #endif
34 
35 #ifdef SDK_FINALROM
36 // define if callTrace system not used
37 #define OS_NO_CALLTRACE
38 #endif
39 
40 
41 #ifndef SDK_THREAD_INFINITY
42 //---- maximum number of thread
43 #define OS_THREAD_MAX_NUM             16        // changed 8 to 16 (2004/5/26)
44 #endif
45 
46 //---- priority of thread that calls OS_InitThread()
47 #define OS_THREAD_LAUNCHER_PRIORITY   16
48 
49 //---- range of thread priority
50 #define OS_THREAD_PRIORITY_MIN        0
51 #define OS_THREAD_PRIORITY_MAX        31
52 
53 //----------------------------------------------------------------------------
54 typedef struct _OSThread OSThread;
55 
56 #ifdef SDK_THREAD_INFINITY
57 typedef struct _OSThreadQueue OSThreadQueue;
58 typedef struct _OSThreadLink OSThreadLink;
59 typedef struct _OSMutexQueue OSMutexQueue;
60 typedef struct _OSMutexLink OSMutexLink;
61 typedef struct OSMutex OSMutex;
62 #endif
63 
64 typedef struct OSiAlarm OSAlarm;
65 
66 #ifdef SDK_THREAD_INFINITY
67 struct _OSThreadQueue
68 {
69     OSThread *head;
70     OSThread *tail;
71 };
72 
73 struct _OSThreadLink
74 {
75     OSThread *prev;
76     OSThread *next;
77 };
78 
79 struct _OSMutexQueue
80 {
81     OSMutex *head;
82     OSMutex *tail;
83 };
84 
85 struct _OSMutexLink
86 {
87     OSMutex *next;
88     OSMutex *prev;
89 };
90 
91 #endif
92 //---------------- Thread status
93 typedef enum
94 {
95     OS_THREAD_STATE_WAITING = 0,
96     OS_THREAD_STATE_READY = 1,
97     OS_THREAD_STATE_TERMINATED = 2
98 }
99 OSThreadState;
100 
101 #ifndef SDK_THREAD_INFINITY
102 //---------------- thread queue
103 #if     ( OS_THREAD_MAX_NUM <= 16 )
104 typedef u16 OSThreadQueue;
105 #define OS_SIZEOF_OSTHREADQUEUE  16
106 #elif   ( OS_THREAD_MAX_NUM <= 32 )
107 typedef u32 OSThreadQueue;
108 #define OS_SIZEOF_OSTHREADQUEUE  32
109 #else
110 Error:no bit field any more
111 #endif
112 #endif
113 #define OS_THREAD_SPECIFIC_MAX  3
114 typedef void (*OSThreadDestructor) (void *);
115 
116 //---------------- Thread structure
117 struct _OSThread
118 {
119     OSContext context;
120     OSThreadState state;
121     OSThread *next;
122     u32     id;
123     u32     priority;
124 
125     void   *profiler;
126 
127 #ifdef SDK_THREAD_INFINITY
128     OSThreadQueue *queue;
129     OSThreadLink link;
130 #endif
131 
132 #ifndef SDK_THREAD_INFINITY
133     void   *mutex;
134     void   *mutexQueueHead;
135     void   *mutexQueueTail;
136 #else
137     OSMutex *mutex;                    // OSMutex
138     OSMutexQueue mutexQueue;           // OSMutexQueue
139 #endif
140 
141     u32     stackTop;                  // for stack overflow
142     u32     stackBottom;               // for stack underflow
143     u32     stackWarningOffset;
144 
145 #ifndef SDK_THREAD_INFINITY
146     OSThreadQueue joinQueue;           // for wakeup threads in thread termination
147 #if OS_SIZEOF_OSTHREADQUEUE == 16
148     u16     padding;
149 #endif
150 #else
151     OSThreadQueue joinQueue;
152 
153     void   *specific[OS_THREAD_SPECIFIC_MAX];   // for internal use
154     OSAlarm *alarmForSleep;            // OSAlarm for sleeping
155     OSThreadDestructor destructor;     // thread destructor
156     void   *userParameter;             // for user
157 
158     int     systemErrno;
159 #endif
160 };
161 
162 //---------------- Thread & context packed structure
163 typedef struct OSThreadInfo
164 {
165     u16     isNeedRescheduling;
166 #ifndef SDK_THREAD_INFINITY
167     u16     max_entry;
168     u16     irqDepth;
169     u16     padding;
170 #else
171     u16     irqDepth;
172 #endif
173     OSThread *current;
174     OSThread *list;
175     void   *switchCallback;            // type: OSSwitchThreadCallback
176 #ifndef SDK_THREAD_INFINITY
177     OSThread *entry[OS_THREAD_MAX_NUM];
178 #endif
179 }
180 OSThreadInfo;
181 
182 // offset
183 #ifndef SDK_THREAD_INFINITY
184 #define OS_THREADINFO_OFFSET_ISNEEDRESCHEDULING     0
185 #define OS_THREADINFO_OFFSET_MAX_ENTRY              2
186 #define OS_THREADINFO_OFFSET_IRQDEPTH               4
187 #define OS_THREADINFO_OFFSET_PADDING                6
188 #define OS_THREADINFO_OFFSET_CURRENT                8
189 #define OS_THREADINFO_OFFSET_LIST                   12
190 #define OS_THREADINFO_OFFSET_SWITCHCALLBACK         16
191 #define OS_THREADINFO_OFFSET_ENTRY                  20
192 #else  // ifndef SDK_THREAD_INFINITY
193 #define OS_THREADINFO_OFFSET_ISNEEDRESCHEDULING     0
194 #define OS_THREADINFO_OFFSET_IRQDEPTH               2
195 #define OS_THREADINFO_OFFSET_CURRENT                4
196 #define OS_THREADINFO_OFFSET_LIST                   8
197 #define OS_THREADINFO_OFFSET_SWITCHCALLBACK         12
198 #define OS_THREADINFO_OFFSET_ENTRY                  16
199 #endif
200 
201 //----------------  CONTEXT OFFSET
202 #define OS_THREAD_OFFSET_CONTEXT                    0
203 #define OS_THREAD_OFFSET_STATE          (sizeof(OSContext))
204 #define OS_THREAD_OFFSET_NEXT           (sizeof(OSContext)+sizeof(OSThreadState))
205 #define OS_THREAD_OFFSET_ID             (OS_THREAD_OFFSET_NEXT+sizeof(OSThread*))
206 
207 //---------------- thread stack overflow status
208 typedef enum
209 {
210     OS_STACK_NO_ERROR = 0,
211     OS_STACK_OVERFLOW = 1,
212     OS_STACK_ABOUT_TO_OVERFLOW = 2,
213     OS_STACK_UNDERFLOW = 3
214 }
215 OSStackStatus;
216 
217 //---------------- thread switch callback
218 typedef void (*OSSwitchThreadCallback) (OSThread *from, OSThread *to);
219 
220 //---------------- thread resource
221 typedef struct OSThreadResource
222 {
223 	int num;
224 }
225 OSThreadResource;
226 
227 //----------------------------------------------------------------------------
228 //---- private function ( don't use these OSi_* function )
229 void    OSi_CheckStack(const char *file, int line, const OSThread *thread);
230 u32     OSi_GetSystemStackPointer(void);
231 u32     OSi_GetCurrentStackPointer(void);
232 OSThread *OSi_GetIdleThread(void);
233 
234 /*---------------------------------------------------------------------------*
235   Name:         OS_InitThread
236 
237   Description:  Initialize Thread System
238 
239   Arguments:    None
240 
241   Returns:      None
242  *---------------------------------------------------------------------------*/
243 void    OS_InitThread(void);
244 
245 /*---------------------------------------------------------------------------*
246   Name:         OS_IsThreadAvailable
247 
248   Description:  check if thread system is available
249 
250   Arguments:    None
251 
252   Returns:      TRUE if available, FALSE if not
253  *---------------------------------------------------------------------------*/
254 BOOL    OS_IsThreadAvailable(void);
255 
256 /*---------------------------------------------------------------------------*
257   Name:         OS_CreateThread
258 
259   Description:  Create a new Thread
260 
261   Arguments:    thread      pointer of thread structure
262                 func        function to start thread
263                 arg         argument for func
264                 stack       stack bottom address
265                 stackSize   stack size (byte. must be aligned by 4)
266                 prio        thread priority
267 
268   Returns:      None
269  *---------------------------------------------------------------------------*/
270 void    OS_CreateThread(OSThread *thread,
271                         void (*func) (void *), void *arg, void *stack, u32 stackSize, u32 prio);
272 
273 
274 /*---------------------------------------------------------------------------*
275   Name:         OS_ExitThread
276 
277   Description:  Exit thread
278 
279   Arguments:    None
280 
281   Returns:      None
282  *---------------------------------------------------------------------------*/
283 void    OS_ExitThread(void);
284 
285 
286 /*---------------------------------------------------------------------------*
287   Name:         OS_DestroyThread
288 
289   Description:  destroy specified thread.
290 
291   Arguments:    thread: thread to be destroyed
292 
293   Returns:      None
294  *---------------------------------------------------------------------------*/
295 void    OS_DestroyThread(OSThread *thread);
296 
297 
298 /*---------------------------------------------------------------------------*
299   Name:         OS_KillThread
300 
301   Description:  switch PC to thread destructor to finalize thread
302 
303   Arguments:    thread : thread to wait to finish
304                 flag   : argument for destructor
305 
306   Returns:      None
307  *---------------------------------------------------------------------------*/
308 #ifdef SDK_THREAD_INFINITY
309 void    OS_KillThread(OSThread *thread, void *arg);
310 void    OS_KillThreadWithPriority(OSThread *thread, void *arg, u32 prio);
311 #endif
312 
313 
314 /*---------------------------------------------------------------------------*
315   Name:         OS_JoinThread
316 
317   Description:  wait for specified thread to terminated
318 
319   Arguments:    thread : thraead to wait to finish
320 
321   Returns:      None
322  *---------------------------------------------------------------------------*/
323 void    OS_JoinThread(OSThread *thread);
324 
325 
326 /*---------------------------------------------------------------------------*
327   Name:         OS_IsThreadTeminated
328 
329   Description:  check thread status whether it's terminated
330 
331   Arguments:    thread : pointer to thread to be examined
332 
333   Returns:      TRUE if the thread is terminated. FALSE if not
334  *---------------------------------------------------------------------------*/
335 BOOL    OS_IsThreadTerminated(const OSThread *thread);
336 
337 /*---------------------------------------------------------------------------*
338   Name:         OS_GetThreadStatus
339 
340   Description:  get thread status
341 
342   Arguments:    thread : pointer to thread
343 
344   Returns:
345  *---------------------------------------------------------------------------*/
346 OSThreadState  OS_GetThreadStatus(const OSThread *thread);
347 
348 /*---------------------------------------------------------------------------*
349   Name:         OS_SelectThread
350 
351   Description:  Select thread to execute
352 
353   Arguments:    None
354 
355   Returns:      thread to execute
356  *---------------------------------------------------------------------------*/
357 OSThread *OS_SelectThread(void);
358 
359 
360 /*---------------------------------------------------------------------------*
361   Name:         OS_RescheduleThread
362 
363   Description:  do rescheduling threads
364 
365   Arguments:    None
366 
367   Returns:      None
368  *---------------------------------------------------------------------------*/
369 void    OS_RescheduleThread(void);
370 
371 /*---------------------------------------------------------------------------*
372   Name:         OS_YieldThread
373 
374   Description:  do thread rescheduling. current thread relinquish CPU
375                 to give chance of running to other threads which has same
376                 priority.
377 
378   Arguments:    None
379 
380   Returns:      None
381  *---------------------------------------------------------------------------*/
382 extern void OS_YieldThread(void);
383 
384 
385 /*---------------------------------------------------------------------------*
386   Name:         OS_SleepThread
387 
388   Description:  sleep current thread
389 
390   Arguments:    thread         Thread queue
391 
392   Returns:      None
393  *---------------------------------------------------------------------------*/
394 void    OS_SleepThread(OSThreadQueue *queue);
395 
396 
397 /*---------------------------------------------------------------------------*
398   Name:         OS_SleepThreadDirect
399 
400   Description:  Gets the thread into sleep status directly
401 
402   Arguments:    thread            thread to sleep
403                 queue             waiting list queue (or NULL)
404 
405   Returns:      none
406  *---------------------------------------------------------------------------*/
407 void    OS_SleepThreadDirect(OSThread *thread, OSThreadQueue *queue);
408 
409 /*---------------------------------------------------------------------------*
410   Name:         OS_WakeupThread
411 
412   Description:  wake up threads by queue
413 
414   Arguments:    queue         Thread queue
415 
416   Returns:      None
417  *---------------------------------------------------------------------------*/
418 void    OS_WakeupThread(OSThreadQueue *queue);
419 
420 
421 /*---------------------------------------------------------------------------*
422   Name:         OS_WakeupThreadDirect
423 
424   Description:  wake up thread by specifying thread directly
425 
426   Arguments:    thread        Thread to wake up
427 
428   Returns:      None
429  *---------------------------------------------------------------------------*/
430 void    OS_WakeupThreadDirect(OSThread *thread);
431 
432 
433 /*---------------------------------------------------------------------------*
434   Name:         OS_DumpThreadList
435 
436   Description:  Dump All Thread Infomation (for DEBUG)
437 
438   Arguments:    None
439 
440   Returns:      None
441  *---------------------------------------------------------------------------*/
442 void    OS_DumpThreadList(void);
443 
444 
445 /*---------------------------------------------------------------------------*
446   Name:         OS_GetNumberOfThread
447 
448   Description:  Get number of thread which exists in system
449 
450   Arguments:    None
451 
452   Returns:      number of thread which exists in system
453  *---------------------------------------------------------------------------*/
454 int     OS_GetNumberOfThread(void);
455 
456 /*==== static inlie functions ====*/
457 /*---------------------------------------------------------------------------*
458   Name:         OS_GetThreadInfo
459 
460   Description:  Get pointer of system thread info structure.
461 
462   Arguments:    None
463 
464   Returns:      pointer of thread info structure
465  *---------------------------------------------------------------------------*/
466 extern OSThreadInfo OSi_ThreadInfo;
467 
OS_GetThreadInfo(void)468 static inline OSThreadInfo *OS_GetThreadInfo(void)
469 {
470     return &OSi_ThreadInfo;
471 }
472 
473 /*---------------------------------------------------------------------------*
474   Name:         OS_GetMaxThreadId
475 
476   Description:  Gets Max id number of available thread number
477 
478   Arguments:    None
479 
480   Returns:      Max id of available thread number
481  *---------------------------------------------------------------------------*/
OS_GetMaxThreadId(void)482 static inline u32 OS_GetMaxThreadId(void)
483 {
484 #ifndef SDK_THREAD_INFINITY
485     return OS_GetThreadInfo()->max_entry;
486 #else
487     return 0x7fffffff;                 // (=maximin number of int)
488 #endif
489 }
490 
491 /*---------------------------------------------------------------------------*
492   Name:         OS_GetThread
493 
494   Description:  Gets pointer to thread which id is specified
495 
496   Arguments:    id : thread id to get thread
497 
498   Returns:      pointer to thread which id is specified
499  *---------------------------------------------------------------------------*/
500 #ifndef SDK_THREAD_INFINITY
OS_GetThread(u32 id)501 static inline OSThread *OS_GetThread(u32 id)
502 {
503     SDK_ASSERTMSG(id < OS_THREAD_MAX_NUM, "Thread id illegal\n");
504     return OS_GetThreadInfo()->entry[id];
505 }
506 #else
507 extern OSThread *OS_GetThread(u32 id);
508 #endif
509 
510 /*---------------------------------------------------------------------------*
511   Name:         OS_GetThreadId
512 
513   Description:  Gets id of specified thread
514 
515   Arguments:    thread      pointer to thread
516 
517   Returns:      id of specified thread
518  *---------------------------------------------------------------------------*/
OS_GetThreadId(const OSThread * thread)519 static inline u32 OS_GetThreadId(const OSThread *thread)
520 {
521     SDK_ASSERTMSG(thread, "null thread pointer.");
522     return thread->id;
523 }
524 
525 /*---------------------------------------------------------------------------*
526   Name:         OS_GetThreadState
527 
528   Description:  Gets state of specified thread
529 
530   Arguments:    thread      pointer to thread
531 
532   Returns:      state of specified thead
533  *---------------------------------------------------------------------------*/
OS_GetThreadState(const OSThread * thread)534 static inline OSThreadState OS_GetThreadState(const OSThread *thread)
535 {
536     SDK_ASSERTMSG(thread, "null thread pointer.");
537     return thread->state;
538 }
539 
540 /*---------------------------------------------------------------------------*
541   Name:         OS_GetThreadContext
542 
543   Description:  Gets pointer to context of specified thread
544 
545   Arguments:    thread      pointer to thread
546 
547   Returns:      pointer to context of specified thread
548  *---------------------------------------------------------------------------*/
OS_GetThreadContext(const OSThread * thread)549 static inline OSContext *OS_GetThreadContext(const OSThread *thread)
550 {
551     SDK_ASSERTMSG(thread, "null thread pointer.");
552     return (OSContext *)&thread->context;
553 }
554 
555 /*---------------------------------------------------------------------------*
556   Name:         OS_IsThreadRunnable
557 
558   Description:  Check if thread is runnable
559 
560   Arguments:    thread   pointer to thread
561 
562   Returns:      non zero if thread is runnable
563  *---------------------------------------------------------------------------*/
OS_IsThreadRunnable(const OSThread * thread)564 static inline BOOL OS_IsThreadRunnable(const OSThread *thread)
565 {
566     return thread->state == OS_THREAD_STATE_READY;
567 }
568 
569 /*---------------------------------------------------------------------------*
570   Name:         OS_InitThreadQueue
571 
572   Description:  Initialize thread queue
573 
574   Arguments:    queue   pointer to thread queue
575 
576   Returns:      None
577  *---------------------------------------------------------------------------*/
OS_InitThreadQueue(OSThreadQueue * queue)578 static inline void OS_InitThreadQueue(OSThreadQueue *queue)
579 {
580 #ifndef SDK_THREAD_INFINITY
581     *queue = 0;
582 #else
583     queue->head = queue->tail = NULL;
584 #endif
585 }
586 
587 /*---------------------------------------------------------------------------*
588   Name:         OS_GetCurrentThread
589 
590   Description:  Gets pointer to the current thread
591 
592   Arguments:    None
593 
594   Returns:      Pointer to the current thread
595  *---------------------------------------------------------------------------*/
OS_GetCurrentThread(void)596 static inline OSThread *OS_GetCurrentThread(void)
597 {
598     return OS_GetThreadInfo()->current;
599 }
600 
601 /*---------------------------------------------------------------------------*
602   Name:         OS_SetCurrentThread
603 
604   Description:  Saves pointer to the current thread
605 
606   Arguments:    thread : thread to be current thread
607 
608   Returns:      Pointer to the current thread
609  *---------------------------------------------------------------------------*/
OS_SetCurrentThread(OSThread * thread)610 static inline void OS_SetCurrentThread(OSThread *thread)
611 {
612     OS_GetThreadInfo()->current = thread;
613 }
614 
615 /*==== stack check ====*/
616 /*---------------------------------------------------------------------------*
617   Name:         OS_SetThreadStackWarningOffset
618 
619   Description:  Set warning level for stack checker
620 
621   Arguments:    thread     thread to set
622                 offset     offset from stack top. must be multiple of 4
623 
624   Returns:      None
625  *---------------------------------------------------------------------------*/
626 void    OS_SetThreadStackWarningOffset(OSThread *thread, u32 offset);
627 
628 
629 /*---------------------------------------------------------------------------*
630   Name:         OS_GetStackStatus
631 
632   Description:  check thread stack. check each CheckNUM.
633                 return result.
634 
635   Arguments:    thread   thread checked
636 
637   Returns:      0                            no error
638                 OS_STACK_OVERFLOW            overflow
639                 OS_STACK_ABOUT_TO_OVERFLOW   about to overflow
640                 OS_STACK_UNDERFLOW           underflow
641  *---------------------------------------------------------------------------*/
642 OSStackStatus OS_GetStackStatus(const OSThread *thread);
643 
644 /*---------------------------------------------------------------------------*
645   Name:         OS_CheckStack
646 
647   Description:  check thread stack. check each CheckNum.
648                 if changed, display warning and halt.
649 
650   Arguments:    thread   thread to check stack
651 
652   Returns:      None.
653                 ( if error occurred, never return )
654  *---------------------------------------------------------------------------*/
655 #if !defined(SDK_FINALROM) && !defined(SDK_NO_MESSAGE)
656 #define  OS_CheckStack( thread )  OSi_CheckStack( __FILE__, __LINE__, (const OSThread*)thread );
657 #else
658 #define  OS_CheckStack( thread )  ((void)0)
659 #endif
660 
661 /*---------------------------------------------------------------------------*
662   Name:         OS_SetThreadPriority
663 
664   Description:  change priority of thread
665 
666   Arguments:    thread     thread to set priority
667                 prio       new priority to be set
668 
669   Returns:      TRUE if success
670  *---------------------------------------------------------------------------*/
671 BOOL    OS_SetThreadPriority(OSThread *thread, u32 prio);
672 
673 
674 /*---------------------------------------------------------------------------*
675   Name:         OS_GetThreadPriority
676 
677   Description:  get priority of thread
678 
679   Arguments:    thread     thread to get priority
680 
681   Returns:      priority
682  *---------------------------------------------------------------------------*/
683 u32     OS_GetThreadPriority(const OSThread *thread);
684 
685 
686 /*---------------------------------------------------------------------------*
687   Name:         OS_Sleep
688 
689   Description:  sleep specified period
690 
691   Arguments:    msec       sleeping period. ( milliseconds )
692 
693   Returns:      None.
694  *---------------------------------------------------------------------------*/
695 void    OS_Sleep(u32 msec);
696 
697 
698 /*---------------------------------------------------------------------------*
699   Name:         OS_SetSwitchThreadCallback
700 
701   Description:  set callback called at switching thread
702 
703   Arguments:    callback      callback function
704 
705   Returns:      previous callback function before set callback now
706  *---------------------------------------------------------------------------*/
707 OSSwitchThreadCallback OS_SetSwitchThreadCallback(OSSwitchThreadCallback callback);
708 
709 
710 // notice: substans is in os_callTrace.c.
711 //         define here because of OSThread declaration.
712 /*---------------------------------------------------------------------------*
713   Name:         OS_DumpThreadCallTrace
714 
715   Description:  dump callStack of thread
716 
717   Arguments:    thread :    thread
718 
719   Returns:      None
720  *---------------------------------------------------------------------------*/
721 void    OS_DumpThreadCallTrace(const OSThread *thread);
722 
723 
724 
725 /*---------------------------------------------------------------------------*
726   Name:         OS_DisableScheduler
727 
728   Description:  disable scheduler
729 
730   Arguments:    None
731 
732   Returns:      Previous scheduler suspend count.
733                 Suspended if value >= 0.
734  *---------------------------------------------------------------------------*/
735 u32     OS_DisableScheduler(void);
736 
737 /*---------------------------------------------------------------------------*
738   Name:         OS_EnableScheduler
739 
740   Description:  enable scheduler
741 
742   Arguments:    None
743 
744   Returns:      Previous scheduler suspend count.
745                 Suspended if value >= 0.
746  *---------------------------------------------------------------------------*/
747 u32     OS_EnableScheduler(void);
748 
749 
750 #ifdef SDK_THREAD_INFINITY
751 /*---------------------------------------------------------------------------*
752   Name:         OS_SetThreadDestructor
753 
754   Description:  set thread destructor, which is called when that thread exits.
755 
756   Arguments:    thread : thread pointer
757                 dtor   : destructor function
758 
759   Returns:      None
760  *---------------------------------------------------------------------------*/
761 void    OS_SetThreadDestructor(OSThread *thread, OSThreadDestructor dtor);
762 
763 /*---------------------------------------------------------------------------*
764   Name:         OS_GetThreadDestructor
765 
766   Description:  get thread destructor which is set
767 
768   Arguments:    thread : thread pointer
769 
770   Returns:      destructor function
771  *---------------------------------------------------------------------------*/
772 OSThreadDestructor OS_GetThreadDestructor(const OSThread *thread);
773 
774 /*---------------------------------------------------------------------------*
775   Name:         OS_SetThreadParameter
776 
777   Description:  set user parameter which is allowed to use freely.
778 
779   Arguments:    thread    : thread pointer
780                 parameter : user parameter
781 
782   Returns:      None
783  *---------------------------------------------------------------------------*/
784 void    OS_SetThreadParameter(OSThread *thread, void *parameter);
785 
786 /*---------------------------------------------------------------------------*
787   Name:         OS_GetThreadParameter
788 
789   Description:  get user parameter which is set
790 
791   Arguments:    thread : thread pointer
792 
793   Returns:      user parameter which is set
794  *---------------------------------------------------------------------------*/
795 void   *OS_GetThreadParameter(const OSThread *thread);
796 
797 /*---------------------------------------------------------------------------*
798   Name:         OS_GetErrno
799 
800   Description:  get system error number.
801 
802   Arguments:    None.
803 
804   Returns:      error number
805  *---------------------------------------------------------------------------*/
806 int     OS_GetErrno(void);
807 #endif
808 
809 /*---------------------------------------------------------------------------*
810   Name:         OS_IsThreadInList
811 
812   Description:  check if the specified thread is in the thread list
813 
814   Arguments:    thread : thread
815 
816   Returns:      TRUE if thread is in the thread list
817  *---------------------------------------------------------------------------*/
818 BOOL    OS_IsThreadInList(const OSThread *thread);
819 
820 /*---------------------------------------------------------------------------*
821   Name:         OS_SetThreadDestructorStack
822 
823   Description:  specify stack area to call thread destructor
824 
825   Arguments:    stack       stack bottom address
826 
827   Returns:      None
828  *---------------------------------------------------------------------------*/
829 void    OS_SetThreadDestructorStack(void *stack);
830 
831 
832 #ifdef SDK_THREAD_INFINITY
833 //================================================================================
834 //              The following functions are for internal use. Not for user.
835 //================================================================================
836 /*---------------------------------------------------------------------------*
837   Name:         OSi_RemoveMutexLinkFromQueue
838 
839   Description:  remove mutex from mutex queue
840 
841   Arguments:    queue  : mutex queue
842 
843   Returns:      mutex pointer which is removed
844  *---------------------------------------------------------------------------*/
845 extern OSMutex *OSi_RemoveMutexLinkFromQueue(OSMutexQueue * queue);
846 
847 /*---------------------------------------------------------------------------*
848   Name:         OSi_SetSystemErrno
849 
850   Description:  set system error number.
851 
852   Arguments:    thread : thread to set error number
853                 errno  : error number to set
854 
855   Returns:      None
856  *---------------------------------------------------------------------------*/
857 void    OSi_SetSystemErrno(OSThread *thread, int errno);
858 
859 /*---------------------------------------------------------------------------*
860   Name:         OSi_GetSystemErrno
861 
862   Description:  get system error number.
863 
864   Arguments:    thread : thread to set error number
865 
866   Returns:      error number
867  *---------------------------------------------------------------------------*/
868 int     OSi_GetSystemErrno(const OSThread *thread);
869 
870 
871 #define OSi_SPECIFIC_CPS   0
872 
873 /*---------------------------------------------------------------------------*
874   Name:         OSi_SetSpecificData
875 
876   Description:  set system specific data
877 
878   Arguments:    thread : thread to set data
879                 index  : index of specific array
880                 data   : data to set
881 
882   Returns:      None
883  *---------------------------------------------------------------------------*/
OSi_SetSpecificData(OSThread * thread,int index,void * data)884 static inline void OSi_SetSpecificData(OSThread *thread, int index, void *data)
885 {
886     SDK_ASSERT(thread && 0 <= index && index < OS_THREAD_SPECIFIC_MAX);
887     thread->specific[index] = data;
888 }
889 
890 /*---------------------------------------------------------------------------*
891   Name:         OSi_GetSpecificData
892 
893   Description:  get system specific data
894 
895   Arguments:    thread : thread to get data
896                 index  : index of specific array
897 
898   Returns:      error number
899  *---------------------------------------------------------------------------*/
OSi_GetSpecificData(const OSThread * thread,int index)900 static inline void *OSi_GetSpecificData(const OSThread *thread, int index)
901 {
902     SDK_ASSERT(thread && 0 <= index && index < OS_THREAD_SPECIFIC_MAX);
903     return thread->specific[index];
904 }
905 #endif
906 
907 
908 //================================================================================
909 //              The following functions are for operations of thread struct.
910 //              use carefully.
911 //================================================================================
912 /*---------------------------------------------------------------------------*
913   Name:         OS_GetThreadList
914 
915   Description:  get first thread of thread list.
916 
917   Arguments:    None
918 
919   Returns:      first thread of thread list
920  *---------------------------------------------------------------------------*/
OS_GetThreadList(void)921 static inline OSThread *OS_GetThreadList(void)
922 {
923     return OS_GetThreadInfo()->list;
924 }
925 
926 
927 /*---------------------------------------------------------------------------*
928   Name:         OS_GetNextThread
929 
930   Description:  get thread which is linked next in thread list
931 
932   Arguments:    thread : thread to get next thread
933 
934   Returns:      next thread. NULL means no next thread ( specified thread may be last )
935  *---------------------------------------------------------------------------*/
OS_GetNextThread(const OSThread * thread)936 static inline OSThread *OS_GetNextThread(const OSThread *thread)
937 {
938     SDK_ASSERT(thread);
939     return thread->next;
940 }
941 
942 //================================================================================
943 //    for DEBUG
944 //================================================================================
945 /*---------------------------------------------------------------------------*
946   Name:         OS_GetThreadResource
947 
948   Description:  store resources of thread to specified pointer
949 
950   Arguments:    resource       pointer to store thread resources
951 
952   Returns:      TRUE  ... success (always return this now)
953                 FALSE ... fail
954  *---------------------------------------------------------------------------*/
955 BOOL OS_GetThreadResource(OSThreadResource *resource);
956 
957 #ifdef __cplusplus
958 } /* extern "C" */
959 #endif
960 
961 /* NITRO_OS_THREAD_H_ */
962 #endif
963