From 73855a67739a03240fff9b838ea05a8d2a630be0 Mon Sep 17 00:00:00 2001
From: Terraneo Federico <fede.tft@hotmail.it>
Date: Sun, 23 Sep 2012 15:45:20 +0200
Subject: [PATCH] Allow pauseKernel() within InterruptDisableLock

---
 miosix/config/miosix_settings.h  |  2 +-
 miosix/doc/textdoc/Changelog.txt |  6 ++++++
 miosix/kernel/kernel.cpp         | 22 ++++++++++++++++------
 miosix/util/version.cpp          |  2 +-
 4 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/miosix/config/miosix_settings.h b/miosix/config/miosix_settings.h
index 911f483a..879e4424 100644
--- a/miosix/config/miosix_settings.h
+++ b/miosix/config/miosix_settings.h
@@ -69,7 +69,7 @@ namespace miosix {
 /// \def WITH_FILESYSTEM
 /// Allows to enable/disable filesystem support.
 /// By default it is defined (filesystem support is enabled)
-//#define WITH_FILESYSTEM
+#define WITH_FILESYSTEM
     
 /// \def SYNC_AFTER_WRITE
 /// Increases filesystem write robustness. After each write operation the
diff --git a/miosix/doc/textdoc/Changelog.txt b/miosix/doc/textdoc/Changelog.txt
index 9ac9fb04..8c951d5c 100644
--- a/miosix/doc/textdoc/Changelog.txt
+++ b/miosix/doc/textdoc/Changelog.txt
@@ -1,5 +1,11 @@
 Changelog for Miosix np embedded OS
 
+v1.61
+- Modified the implementation of pauseKernel() and restartKernel() so as to
+  be able to call them safely within an InterruptDisableLock block. This allows
+  allocating memory with malloc/new when interrupts are disabled in an
+  InterruptDisableLock (but not in a FastInterruptDisableLock). This change
+  only relaxes a constraint, so it does not break existing code.
 v1.60
 - Added new board: bitsboard. This is effectively an stm32f4discovery tweaked
   be used in the bits project: bits.poul.org
diff --git a/miosix/kernel/kernel.cpp b/miosix/kernel/kernel.cpp
index e6126982..e4fd73b9 100644
--- a/miosix/kernel/kernel.cpp
+++ b/miosix/kernel/kernel.cpp
@@ -125,7 +125,11 @@ void pauseKernel()
     miosix_private::doDisableInterrupts();
     if(kernel_running==0xff) errorHandler(NESTING_OVERFLOW);
     kernel_running++;
-    if(kernel_started==true) miosix_private::doEnableInterrupts();
+    
+    //Check interruptDisableNesting to allow pauseKernel() while interrupts
+    //are disabled with an InterruptDisableLock
+    if(interruptDisableNesting==0 && kernel_started==true)
+        miosix_private::doEnableInterrupts();
 }
 
 void restartKernel()
@@ -137,12 +141,18 @@ void restartKernel()
         errorHandler(PAUSE_KERNEL_NESTING);
     }
     kernel_running--;
-    if(kernel_started==true) miosix_private::doEnableInterrupts();
     
-    if((kernel_running==0)&&tick_skew)//If we missed some tick yield immediately
-    { 
-        tick_skew=false;
-        Thread::yield();
+    //Check interruptDisableNesting to allow pauseKernel() while interrupts
+    //are disabled with an InterruptDisableLock
+    if(interruptDisableNesting==0)
+    {
+        if(kernel_started==true) miosix_private::doEnableInterrupts();
+    
+        if((kernel_running==0)&&tick_skew)//If we missed some tick yield immediately
+        { 
+            tick_skew=false;
+            Thread::yield();
+        }
     }
 }
 
diff --git a/miosix/util/version.cpp b/miosix/util/version.cpp
index 74709770..76dd6ab0 100644
--- a/miosix/util/version.cpp
+++ b/miosix/util/version.cpp
@@ -37,7 +37,7 @@ namespace miosix {
 #define AU
 #endif
 
-const char AU ver[]="Miosix v1.60 (" _MIOSIX ", " __DATE__ " " __TIME__ CV ")";
+const char AU ver[]="Miosix v1.61 (" _MIOSIX ", " __DATE__ " " __TIME__ CV ")";
 
 const char *getMiosixVersion()
 {
-- 
GitLab