Nut/OS  5.0.5
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 THE COPYRIGHT HOLDERS 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 THE
00021  * COPYRIGHT OWNER 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 #include <pthread.h>
00061 #include <signal.h>
00062 #include <sys/thread.h>
00063 #include <stdio.h>
00064 #include <stdlib.h>
00065 
00066 extern uint16_t main_cs_level;
00067 extern sigset_t irq_signal;
00068 extern pthread_cond_t irq_cv;
00069 extern uint16_t int_disabled;
00070 
00071 extern FILE *__os_trs;
00072 extern uint8_t __os_trf;
00073 
00074 #define AtomicInc(p)     (++(*p))
00075 #define AtomicDec(p)     (--(*p))
00076 
00077 extern void NutExitCritical(void);
00078 extern void NutEnterCritical(void);
00079 
00080 
00081 // uncommenting the following causes a segmentation fault because stdout isn't defined at startup.
00082 // #define CRITSECT_TRACE
00083 
00084 #ifndef CRITSECT_TRACE
00085 #define NutEnterCritical()                                                  \
00086     pthread_sigmask(SIG_BLOCK, &irq_signal, 0);         \
00087     int_disabled = 1;                                   \
00088     if (runningThread) {                                \
00089         runningThread->td_cs_level++;                   \
00090     } else {                                            \
00091         main_cs_level++;                                \
00092     }                                                   \
00093     pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00094 
00095 
00096 #define NutExitCritical()                                                           \
00097     pthread_sigmask(SIG_BLOCK, &irq_signal, 0);         \
00098     if (runningThread) {                                \
00099         if (--runningThread->td_cs_level == 0) {        \
00100             int_disabled = 0;                           \
00101             pthread_cond_signal(&irq_cv);               \
00102         }                                               \
00103     } else {                                            \
00104         if (--main_cs_level == 0) {                     \
00105             int_disabled = 0;                           \
00106             pthread_cond_signal(&irq_cv);               \
00107         }                                               \
00108     }                                                   \
00109     pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00110 
00111 #else
00112 
00113 #define NutEnterCritical()                                           \
00114     pthread_sigmask(SIG_BLOCK, &irq_signal, 0);         \
00115     int_disabled = 1;                                   \
00116     if (runningThread) {                                \
00117         if (runningThread->td_cs_level==0)                                              \
00118             printf("Entered a: %s.%d - %s\n", __FILE__, __LINE__, runningThread->td_name);  \
00119         runningThread->td_cs_level++;                   \
00120     } else {                                            \
00121         if (main_cs_level==0)                                               \
00122             printf("Entered b: %s.%d - %s\n", __FILE__, __LINE__, "ROOT");  \
00123         main_cs_level++;                                \
00124     }                                                   \
00125     pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00126 
00127 #define NutExitCritical()                                                                       \
00128     pthread_sigmask(SIG_BLOCK, &irq_signal, 0);         \
00129     if (runningThread) {                                \
00130         if (--runningThread->td_cs_level == 0) {        \
00131             int_disabled = 0;                           \
00132             printf("Left a: %s.%d - %s\n", __FILE__, __LINE__, runningThread->td_name); \
00133             pthread_cond_signal(&irq_cv);               \
00134         }                                               \
00135     } else {                                            \
00136         if (--main_cs_level == 0) {                     \
00137             int_disabled = 0;                           \
00138             printf("Left a: %s.%d - %s\n", __FILE__, __LINE__, "ROOT"); \
00139             pthread_cond_signal(&irq_cv);               \
00140         }                                               \
00141     }                                                   \
00142     pthread_sigmask(SIG_UNBLOCK, &irq_signal, 0);
00143 #endif
00144 
00145 #define NutJumpOutCritical() NutExitCritical()