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