Skip to content

Commit

Permalink
Use winapi/kernel32 for Windows FFI
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Atashian <retep998@gmail.com>
  • Loading branch information
retep998 committed Jun 16, 2015
1 parent 03189fa commit c121ce2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
2 changes: 2 additions & 0 deletions remutex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ version = "0.0.1"

[dependencies]
libc = "*"
winapi = "0.1"
kernel32-sys = "0.1"
45 changes: 15 additions & 30 deletions remutex/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,45 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate winapi;
extern crate kernel32;

use std::mem;
use std::cell::UnsafeCell;

pub struct ReentrantMutex { inner: UnsafeCell<ffi::CRITICAL_SECTION> }
pub struct ReentrantMutex { inner: UnsafeCell<winapi::CRITICAL_SECTION> }

unsafe impl Send for ReentrantMutex {}
unsafe impl Sync for ReentrantMutex {}

impl ReentrantMutex {
#[inline]
pub unsafe fn uninitialized() -> ReentrantMutex {
mem::uninitialized()
}

pub unsafe fn init(&mut self) -> ReentrantMutex {
ffi::InitializeCriticalSection(self.inner.get());
#[inline]
pub unsafe fn init(&mut self) {
kernel32::InitializeCriticalSection(self.inner.get());
}

#[inline]
pub unsafe fn lock(&self) {
ffi::EnterCriticalSection(self.inner.get());
kernel32::EnterCriticalSection(self.inner.get());
}

#[inline]
pub unsafe fn try_lock(&self) -> bool {
ffi::TryEnterCriticalSection(self.inner.get()) != 0
kernel32::TryEnterCriticalSection(self.inner.get()) != 0
}

#[inline]
pub unsafe fn unlock(&self) {
ffi::LeaveCriticalSection(self.inner.get());
kernel32::LeaveCriticalSection(self.inner.get());
}

#[inline]
pub unsafe fn destroy(&self) {
ffi::DeleteCriticalSection(self.inner.get());
}
}

mod ffi {
use libc::{LPVOID, LONG, HANDLE, c_ulong};
pub type ULONG_PTR = c_ulong;

#[repr(C)]
pub struct CRITICAL_SECTION {
CriticalSectionDebug: LPVOID,
LockCount: LONG,
RecursionCount: LONG,
OwningThread: HANDLE,
LockSemaphore: HANDLE,
SpinCount: ULONG_PTR
}

extern "system" {
pub fn InitializeCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
pub fn EnterCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
pub fn TryEnterCriticalSection(CriticalSection: *mut CRITICAL_SECTION) -> BOOLEAN;
pub fn LeaveCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
pub fn DeleteCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
kernel32::DeleteCriticalSection(self.inner.get());
}
}

0 comments on commit c121ce2

Please sign in to comment.