about summary refs log tree commit diff
path: root/src/libstd/sys/unix/thread.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/sys/unix/thread.rs')
-rw-r--r--src/libstd/sys/unix/thread.rs20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs
index 72cdb9440b8..d94e11a5207 100644
--- a/src/libstd/sys/unix/thread.rs
+++ b/src/libstd/sys/unix/thread.rs
@@ -222,7 +222,7 @@ pub mod guard {
 #[cfg_attr(test, allow(dead_code))]
 pub mod guard {
     use libc;
-    use libc::mmap;
+    use libc::{mmap, munmap};
     use libc::{PROT_NONE, MAP_PRIVATE, MAP_ANON, MAP_FAILED, MAP_FIXED};
     use ops::Range;
     use sys::os;
@@ -336,6 +336,24 @@ pub mod guard {
         }
     }
 
+    pub unsafe fn deinit() {
+        if !cfg!(target_os = "linux") {
+            if let Some(mut stackaddr) = get_stack_start() {
+                // Ensure address is aligned. Same as above.
+                let remainder = (stackaddr as usize) % PAGE_SIZE;
+                if remainder != 0 {
+                    stackaddr = ((stackaddr as usize) + PAGE_SIZE - remainder)
+                        as *mut libc::c_void;
+                }
+
+                // Undo the guard page mapping.
+                if munmap(stackaddr, PAGE_SIZE) != 0 {
+                    panic!("unable to deallocate the guard page");
+                }
+            }
+        }
+    }
+
     #[cfg(any(target_os = "macos",
               target_os = "bitrig",
               target_os = "openbsd",