thread.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2006 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  * -
00033  * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk>
00034  *
00035  * This file is distributed in the hope that it will be useful, but WITHOUT
00036  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00037  * FITNESS FOR A PARTICULAR PURPOSE.
00038  *
00039  * You can redistribute this file and/or modify it under the terms of the GNU
00040  * General Public License (GPL) as published by the Free Software Foundation;
00041  * either version 2 of the License, or (at your discretion) any later version.
00042  * See the accompanying file "copying-gpl.txt" for more details.
00043  *
00044  * As a special exception to the GPL, permission is granted for additional
00045  * uses of the text contained in this file.  See the accompanying file
00046  * "copying-liquorice.txt" for details.
00047  */
00048 
00190 #include <cfg/os.h>
00191 #include <cfg/memory.h>
00192 
00193 #include <string.h>
00194 
00195 #include <sys/types.h>
00196 #include <sys/heap.h>
00197 #include <sys/atom.h>
00198 #include <sys/timer.h>
00199 #include <sys/event.h>
00200 #include <sys/thread.h>
00201 
00202 #ifdef NUTDEBUG
00203 #include <sys/osdebug.h>
00204 #endif
00205 
00206 #ifdef NUTTRACER
00207 #include <sys/tracer.h>
00208 #endif
00209 
00214 
00215 #if defined(NUT_CRITICAL_NESTING) && !defined(NUT_CRITICAL_NESTING_STACK)
00216 u_int critical_nesting_level;
00217 #endif
00218 
00219 #if defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
00220 // prototype
00221 extern void NutUnixThreadYieldHook(void);  // from unix_nutinit.c
00222 #endif
00223 
00230 NUTTHREADINFO * runningThread;
00231 
00238 NUTTHREADINFO * killedThread;
00239 
00248 NUTTHREADINFO * nutThreadList;
00249 
00257 NUTTHREADINFO * runQueue;
00258 
00259 
00260 
00271 void NutThreadAddPriQueue(NUTTHREADINFO * td, NUTTHREADINFO * volatile *tqpp)
00272 {
00273     NUTTHREADINFO *tqp;
00274 
00275     td->td_queue = (HANDLE) tqpp;
00276     td->td_qpec = 0;            // start with clean event count
00277 
00278     /*
00279      * Be most careful not to override an intermediate event from interrupt 
00280      * context, which may change a queue from empty to signaled state. Many 
00281      * thanks to Michael Jones, who detected and corrected this bug.
00282      */
00283     NutEnterCritical();
00284     tqp = *tqpp;
00285 
00286     if (tqp == SIGNALED) {
00287         tqp = 0;
00288         td->td_qpec++;          // transfer the signaled state 
00289     } else if (tqp) {
00290         NutExitCritical();      // there are other threads in queue
00291                         // so its save to leave critical.          
00292 
00293         while (tqp && tqp->td_priority <= td->td_priority) {
00294             tqpp = &tqp->td_qnxt;
00295             tqp = tqp->td_qnxt;
00296         }
00297 
00298         NutEnterCritical();     // back into critical
00299     }
00300 
00301     td->td_qnxt = tqp;
00302 
00303     *tqpp = td;
00304     if (td->td_qnxt && td->td_qnxt->td_qpec) {
00305         td->td_qpec += td->td_qnxt->td_qpec; // don't overwrite count
00306         td->td_qnxt->td_qpec = 0;
00307     }
00308     NutExitCritical();
00309 }
00310 
00321 void NutThreadRemoveQueue(NUTTHREADINFO * td, NUTTHREADINFO * volatile *tqpp)
00322 {
00323     NUTTHREADINFO *tqp;
00324 
00325     NutEnterCritical();
00326     tqp = *tqpp;
00327     NutExitCritical();
00328 
00329     if (tqp != SIGNALED) {
00330         while (tqp) {
00331             if (tqp == td) {
00332                 NutEnterCritical();
00333                 *tqpp = td->td_qnxt;
00334                 if (td->td_qpec) {
00335                     if (td->td_qnxt) {
00336                         td->td_qnxt->td_qpec = td->td_qpec;
00337                     }
00338                     td->td_qpec = 0;
00339                 }
00340                 NutExitCritical();
00341 
00342                 td->td_qnxt = 0;
00343                 td->td_queue = 0;
00344                 break;
00345             }
00346             tqpp = &tqp->td_qnxt;
00347             tqp = tqp->td_qnxt;
00348         }
00349     }
00350 }
00351 
00362 void NutThreadResume(void)
00363 {
00364     NUTTHREADINFO *td;
00365     NUTTHREADINFO **qhp;
00366     NUTTHREADINFO *tqp;
00367     u_int cnt;
00368 
00369     /*
00370      * Process events that have been posted from interrupt context.
00371      */
00372     td = nutThreadList;
00373     while (td) {
00374         NutEnterCritical();
00375         cnt = td->td_qpec;
00376         NutExitCritical();
00377         if (cnt) {
00378             /* In order to reduce context switching time, it is sufficient 
00379              * to remove the thread on top of the priority ordered list. */
00380             qhp = (NUTTHREADINFO **)(td->td_queue);
00381             NutEnterCritical();
00382             td->td_qpec--;
00383             tqp = *qhp;
00384             NutExitCritical();
00385             if (tqp != SIGNALED) {
00386                 NutEventPostAsync((HANDLE *)qhp);
00387             }
00388         }
00389         td = td->td_next;
00390     }
00391 
00392     /*
00393      * Process elapsed timers. Must be done after processing the
00394      * events from interupt routines.
00395      */
00396     NutTimerProcessElapsed();
00397 
00398     /* Check for context switch. */
00399     if (runningThread != runQueue) {
00400 #ifdef NUTTRACER
00401         TRACE_ADD_ITEM(TRACE_TAG_THREAD_YIELD,(int)runningThread);
00402 #endif
00403 
00404         if (runningThread->td_state == TDS_RUNNING) {
00405             runningThread->td_state = TDS_READY;
00406         }
00407         NutEnterCritical();
00408         NutThreadSwitch();
00409         NutExitCritical();
00410     }
00411 }
00412 
00430 void NutThreadWake(HANDLE timer, HANDLE th)
00431 {
00432     /* clear pointer on timer and waiting queue */
00433     ((NUTTHREADINFO *) th)->td_timer = 0;
00434     ((NUTTHREADINFO *) th)->td_state = TDS_READY;
00435     NutThreadAddPriQueue(th, (NUTTHREADINFO **) & runQueue);
00436 }
00437 
00446 void NutThreadYield(void)
00447 {
00448 
00449 #if defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
00450     NutEnterCritical();
00451     NutUnixThreadYieldHook();
00452     NutExitCritical();
00453 #endif
00454 
00455     /*
00456      * Remove current thread from runQueue and reinsert it.
00457      * The idle thread is the last one in the queue and will
00458      * never be removed.
00459      */
00460     if (runningThread->td_qnxt) {
00461         NutThreadRemoveQueue(runningThread, (NUTTHREADINFO **) & runQueue);
00462         NutThreadAddPriQueue(runningThread, (NUTTHREADINFO **) & runQueue);
00463     }
00464 
00465     /* Continue with the highest priority thread, which is ready to run. */
00466     NutThreadResume();
00467 }
00468 
00496 uint8_t NutThreadSetPriority(uint8_t level)
00497 {
00498     uint8_t last = runningThread->td_priority;
00499 
00500     /*
00501      * Remove the thread from the run queue and re-insert it with a new
00502      * priority, if this new priority level is below 255. A priotity of
00503      * 255 will kill the thread.
00504      */
00505     NutThreadRemoveQueue(runningThread, &runQueue);
00506     runningThread->td_priority = level;
00507     if (level < 255) {
00508         NutThreadAddPriQueue(runningThread, (NUTTHREADINFO **) & runQueue);
00509     } else {
00510         NutThreadKill();
00511     }
00512 
00513     /*
00514      * Are we still on top of the queue? If yes, then change our status
00515      * back to running, otherwise do a context switch.
00516      */
00517     if (runningThread == runQueue) {
00518         runningThread->td_state = TDS_RUNNING;
00519     } else {
00520         runningThread->td_state = TDS_READY;
00521 #ifdef NUTTRACER
00522         TRACE_ADD_ITEM(TRACE_TAG_THREAD_SETPRIO,(int)runningThread);
00523 #endif
00524 
00525         NutEnterCritical();
00526         NutThreadSwitch();
00527         NutExitCritical();
00528     }
00529 
00530     return last;
00531 }
00532 
00543 void NutThreadExit(void)
00544 {
00545     NutThreadSetPriority(255);
00546 }
00547 
00557 void NutThreadDestroy(void)
00558 {
00559     if (killedThread) {
00560         NutStackFree(killedThread->td_memory);
00561         killedThread = 0;
00562     }
00563 }
00564 
00572 void NutThreadKill(void)
00573 {
00574 
00575     NUTTHREADINFO *pCur = nutThreadList;
00576     NUTTHREADINFO **pp = (NUTTHREADINFO **) & nutThreadList;
00577 
00578     /* Free up any unfinished already killed threads. */
00579     NutThreadDestroy();
00580 
00581     /* Remove from the thread list. */
00582     while (pCur) {
00583         if (pCur == runningThread) {
00584             *pp = pCur->td_next;
00585             break;
00586         }
00587 
00588         pp = (NUTTHREADINFO **) & pCur->td_next;
00589         pCur = pCur->td_next;
00590     }
00591 
00592     /* Schedule for cleanup. */
00593     killedThread = runningThread;
00594 }
00595 
00605 HANDLE GetThreadByName(char * name)
00606 {
00607     NUTTHREADINFO *tdp;
00608 
00609     for (tdp = nutThreadList; tdp; tdp = tdp->td_next) {
00610         if (strcmp(tdp->td_name, name) == 0)
00611             return tdp;
00612     }
00613     return NULL;
00614 }
00615 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/