4 #include <VP_Os/vp_os_thread.h>
6 typedef struct _pthread_data_t
8 uint32_t free; // bit array representing thread array allocation state (0 means free)
9 SUP_THREAD thread[32]; // thread array
10 struct _pthread_data_t* next;
13 static pthread_data_t* threadTab = NULL;
15 static INLINE uint32_t bittest(uint32_t word, uint32_t pos)
17 return (word & (1 << pos));
20 static INLINE uint32_t bitset(uint32_t word, uint32_t pos)
22 return word |= (1 << pos);
25 static INLINE uint32_t bitreset(uint32_t word, uint32_t pos)
27 return word &= ~(1 << pos);
30 static int32_t findFreeIndex(uint32_t word)
35 index = *(int32_t*)&word;
52 static SUP_THREAD* findFreeSlot(void)
55 pthread_data_t *tab, *prev;
62 index = findFreeIndex( tab->free );
65 tab->free = bitset( tab->free, index );
66 return &tab->thread[index];
73 // If we are here it means we lack free memory
77 prev->next = (pthread_data_t*)calloc( 1, sizeof(pthread_data_t) );
82 tab = (pthread_data_t*)calloc( 1, sizeof(pthread_data_t) );
87 tab->free = bitset( tab->free, 0 );
88 return &tab->thread[0];
91 void vp_os_thread_create(THREAD_ROUTINE entry, THREAD_PARAMS data, THREAD_HANDLE *handle, ...)
96 unsigned int stack_size;
100 va_start(va, (char*)handle);
101 priority = va_arg(va, int32_t);
102 name = va_arg(va, char *);
103 stack_base = va_arg(va, void *);
104 stack_size = va_arg(va, unsigned int);
107 SUP_THREAD* thread = findFreeSlot();
109 sup_thread_create(handle, thread, priority, entry, data, stack_size, name);
110 sup_thread_resume(*handle);
113 void vp_os_thread_join(THREAD_HANDLE handle)
117 THREAD_HANDLE vp_os_thread_self(void)
119 return sup_thread_current();
122 void vp_os_thread_suspend(THREAD_HANDLE handle)
124 sup_thread_suspend(handle);
127 void vp_os_thread_resume(THREAD_HANDLE handle)
129 sup_thread_resume(handle);
132 void vp_os_thread_yield(void)
137 void vp_os_thread_priority(THREAD_HANDLE handle, int32_t priority)
139 sup_thread_setpriority(handle, priority);