atom.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2005 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 
00034 /*
00035  * $Log: atom.h,v $
00036  * Revision 1.2  2006/01/26 15:34:49  going_nuts
00037  * adapted to new interrupt handling scheme for unix emulation
00038  * now uses Unix timer and runs without interrupts unless you emulate other hardware
00039  *
00040  * Revision 1.1  2005/06/06 10:49:35  haraldkipp
00041  * Building outside the source tree failed. All header files moved from
00042  * arch/cpu/include to include/arch/cpu.
00043  *
00044  * Revision 1.1  2005/05/27 17:41:52  drsung
00045  * Moved the file.
00046  *
00047  * Revision 1.1  2005/05/26 10:08:42  drsung
00048  * Moved the platform dependend code from include/sys/atom.h to this file.
00049  *
00050  *
00051  */
00052 
00053 #ifndef _SYS_ATOM_H_
00054 #error "Do not include this file directly. Use sys/atom.h instead!"
00055 #endif
00056 
00057 __BEGIN_DECLS
00058 #include <pthread.h>
00059 #include <signal.h>
00060 #include <sys/thread.h>
00061 #include <stdio.h>
00062 #include <stdlib.h>
00063 
00064 extern u_short main_cs_level;
00065 extern sigset_t irq_signal;
00066 extern pthread_cond_t irq_cv;
00067 extern u_short int_disabled;
00068 
00069 extern FILE *__os_trs;
00070 extern u_char __os_trf;
00071 
00072 #define AtomicInc(p)     (++(*p))
00073 #define AtomicDec(p)     (--(*p))
00074 
00075 extern void NutExitCritical(void);
00076 extern void NutEnterCritical(void);
00077 
00078 
00079 // uncommenting the following causes a segmentation fault because stdout isn't defined at startup.
00080 // #define CRITSECT_TRACE
00081 
00082 #ifndef CRITSECT_TRACE
00083 #define NutEnterCritical()                                                  \
00084     pthread_sigmask(SIG_BLOCK, &irq_signal, 0);         \
00085     int_disabled = 1;                                   \
00086     if (runningThread) {                                \
00087         runningThread->td_cs_level++;                   \
00088     } else {                                            \
00089         main_cs_level++;                                \
00090     }                                                   \
00091     pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00092 
00093 
00094 #define NutExitCritical()                                                           \
00095     pthread_sigmask(SIG_BLOCK, &irq_signal, 0);         \
00096     if (runningThread) {                                \
00097         if (--runningThread->td_cs_level == 0) {        \
00098             int_disabled = 0;                           \
00099             pthread_cond_signal(&irq_cv);               \
00100         }                                               \
00101     } else {                                            \
00102         if (--main_cs_level == 0) {                     \
00103             int_disabled = 0;                           \
00104             pthread_cond_signal(&irq_cv);               \
00105         }                                               \
00106     }                                                   \
00107     pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00108 
00109 #else
00110 
00111 #define NutEnterCritical()                                           \
00112     pthread_sigmask(SIG_BLOCK, &irq_signal, 0);         \
00113     int_disabled = 1;                                   \
00114     if (runningThread) {                                \
00115         if (runningThread->td_cs_level==0)                                              \
00116             printf("Entered a: %s.%d - %s\n", __FILE__, __LINE__, runningThread->td_name);  \
00117         runningThread->td_cs_level++;                   \
00118     } else {                                            \
00119         if (main_cs_level==0)                                               \
00120             printf("Entered b: %s.%d - %s\n", __FILE__, __LINE__, "ROOT");  \
00121         main_cs_level++;                                \
00122     }                                                   \
00123     pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00124 
00125 #define NutExitCritical()                                                                       \
00126     pthread_sigmask(SIG_BLOCK, &irq_signal, 0);         \
00127     if (runningThread) {                                \
00128         if (--runningThread->td_cs_level == 0) {        \
00129             int_disabled = 0;                           \
00130             printf("Left a: %s.%d - %s\n", __FILE__, __LINE__, runningThread->td_name); \
00131             pthread_cond_signal(&irq_cv);               \
00132         }                                               \
00133     } else {                                            \
00134         if (--main_cs_level == 0) {                     \
00135             int_disabled = 0;                           \
00136             printf("Left a: %s.%d - %s\n", __FILE__, __LINE__, "ROOT"); \
00137             pthread_cond_signal(&irq_cv);               \
00138         }                                               \
00139     }                                                   \
00140     pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00141 #endif
00142 
00143 #define NutJumpOutCritical() NutExitCritical()
00144 
00145 __END_DECLS

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