thread.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <cfg/os.h>
00042 #include <pthread.h>
00043 #include <stdio.h>
00044
00045
00046
00047 #include <string.h>
00048
00049 #include <sys/types.h>
00050 #include <sys/heap.h>
00051 #include <sys/atom.h>
00052
00053 #include <sys/thread.h>
00054
00055 #ifdef NUTDEBUG
00056 #include <sys/osdebug.h>
00057 #endif
00058
00059
00064
00065
00066 pthread_attr_t attr;
00067
00068
00069 pthread_mutex_t thread_mutex;
00070
00071
00072 pthread_cond_t main_cv;
00073
00074
00075 uint16_t main_cs_level;
00076
00077
00078 void NutThreadInit(void)
00079 {
00080
00081 pthread_mutex_init(&thread_mutex, NULL);
00082
00083 pthread_attr_init(&attr);
00084 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
00085
00086
00087 pthread_cond_init(&main_cv, NULL);
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 static void *NutThreadEntry(void *arg);
00097 static void *NutThreadEntry(void *arg)
00098 {
00099
00100 NUTTHREADINFO *td = (NUTTHREADINFO *) arg;
00101
00102
00103 pthread_mutex_lock(&thread_mutex);
00104 pthread_cond_signal(&main_cv);
00105
00106
00107 pthread_cond_wait(&td->td_cv, &thread_mutex);
00108 pthread_mutex_unlock(&thread_mutex);
00109
00110
00111 td->td_cs_level = 0;
00112
00113
00114 pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00115
00116
00117 td->td_fn(td->td_arg);
00118
00119
00120
00121
00122 NutThreadExit();
00123
00124
00125 pthread_exit(NULL);
00126
00127
00128 return NULL;
00129 }
00130
00143 void NutThreadSwitch(void);
00144 void NutThreadSwitch(void)
00145 {
00146 NUTTHREADINFO *myself;
00147
00148 NutEnterCritical();
00149
00150
00151 pthread_mutex_lock(&thread_mutex);
00152
00153
00154 myself = runningThread;
00155 if (runningThread != runQueue) {
00156
00157
00158 runningThread = runQueue;
00159 runningThread->td_state = TDS_RUNNING;
00160
00161 pthread_cond_signal(&runQueue->td_cv);
00162 pthread_cond_wait(&myself->td_cv, &thread_mutex);
00163
00164 }
00165 pthread_mutex_unlock(&thread_mutex);
00166
00167 NutExitCritical();
00168 }
00169
00170
00171
00172
00191 HANDLE NutThreadCreate(char * name, void (*fn) (void *), void *arg, size_t stackSize)
00192 {
00193 NUTTHREADINFO *td;
00194 const uintptr_t *paddr;
00195
00196 paddr = (const uintptr_t *) fn;
00197
00198 NutEnterCritical();
00199
00200
00201
00202
00203 if ((td = NutHeapAlloc(sizeof(NUTTHREADINFO))) == 0) {
00204 NutExitCritical();
00205 return 0;
00206 }
00207
00208
00209 memcpy(td->td_name, name, sizeof(td->td_name) - 1);
00210 td->td_name[sizeof(td->td_name) - 1] = 0;
00211
00212
00213 td->td_cs_level = 1;
00214
00215
00216
00217
00218 td->td_priority = 64;
00219 td->td_next = nutThreadList;
00220 nutThreadList = td;
00221 td->td_state = TDS_READY;
00222 td->td_timer = 0;
00223 td->td_queue = 0;
00224 #ifdef NUTDEBUG
00225 if (__os_trf) {
00226 fprintf(__os_trs, "Cre<%08lx>\n", (uintptr_t) td);
00227 }
00228 #endif
00229 NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
00230 #ifdef NUTDEBUG
00231 if (__os_trf)
00232 NutDumpThreadList(__os_trs);
00233 #endif
00234
00235
00236 pthread_cond_init(&td->td_cv, NULL);
00237
00238 td->td_fn = fn;
00239 td->td_arg = arg;
00240
00241
00242
00243
00244
00245
00246
00247 if (runningThread == 0) {
00248
00249
00250
00251 NutExitCritical();
00252
00253
00254 runningThread = runQueue;
00255 runningThread->td_state = TDS_RUNNING;
00256
00257
00258 runningThread->td_cs_level = 0;
00259 pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00260
00261
00262 fn(arg);
00263
00264 printf("Nut/OS Application terminated.\n\r");
00265 exit(0);
00266 };
00267
00268
00269 pthread_mutex_lock(&thread_mutex);
00270 pthread_create(&td->td_pthread, &attr, NutThreadEntry, (void *) td);
00271
00272
00273 pthread_cond_wait(&main_cv, &thread_mutex);
00274 pthread_mutex_unlock(&thread_mutex);
00275
00276
00277
00278
00279
00280
00281 if (runningThread != runQueue) {
00282 runningThread->td_state = TDS_READY;
00283 #ifdef NUTDEBUG
00284 if (__os_trf)
00285 fprintf(__os_trs, "New<%08lx %08lx>\n", (uintptr_t) runningThread, (uintptr_t) runQueue);
00286 #endif
00287 NutThreadSwitch();
00288 }
00289 NutExitCritical();
00290
00291 return td;
00292 }
00293