Struct ProcessLock

Source
pub struct ProcessLock(/* private fields */);

Implementations§

Source§

impl ProcessLock

提供进程间的共享内存模块 其中unix使用semget, semctl, semop, 其中用特殊的标识位SEM_UNDO来实现, 防止进程意外退出导致的死锁 其实windows使用CreateMutex, ReleaseMutex, WaitForSingleObject来实现

§Examples, Open Double Process for same

extern crate process_lock;
use std::time::{Duration, Instant};
use process_lock::*;
fn main () {
    let mut lock = ProcessLock::new(String::from(".process_lock"), None).unwrap();
    for i in 0..100 {
       let now = Instant::now();
       {
           let _guard = lock.lock().unwrap();
           println!("success get the {} lock lock all use time ===== {}", i, now.elapsed().as_secs());
           let ten_millis = ::std::time::Duration::from_millis(2000);
           ::std::thread::sleep(ten_millis);
       }
       let ten_millis = ::std::time::Duration::from_millis(100);
       ::std::thread::sleep(ten_millis);
    }
}
Source

pub fn new(name: String, path_name: Option<String>) -> Result<ProcessLock>

首先打开命名的进程锁, 如果打开失败, 则创建一个新的进程锁, 并赋与初始变量

Source

pub fn trylock(&self) -> Result<Option<LockGuard>>

立即返回获取进程锁, 如果成功则返回LockGuard, 如果失败则返回Ok(None), 如果发生错误则返回Error

Source

pub fn lock(&self) -> Result<LockGuard>

无限等待获取进程锁, 如果成功返回LockGuard, 如果被打断则系统发生错误, 可查看Error处理具体的错误 LockGuard析构会自动解锁

Source

pub fn unlock(&self) -> Result<()>

释放进程锁, 通常情况不主动调用, 由LockGuard的生存周期来控制锁的占用周期

Source

pub fn destory(&mut self) -> Result<()>

销毁进程锁, 把handle或者sem_id主动删除

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.