Giter VIP home page Giter VIP logo

single-instance's Introduction

single-instance

Crates.io Build Status

single-instance provides a single API to check if there are any other running instance.

Detail

On windows, init SingleInstance will create a mutex named by given &str then check error code by calling GetLastError. On linux init will bind abstract unix domain socket with given name . On macos, init will create or open a file which path is given &str, then call flock to apply an advisory lock on the open file.

[dependencies]
single-instance = "0.3"

Examples

extern crate single_instance;

use single_instance::SingleInstance;

fn main() {
    {
        let instance_a = SingleInstance::new("whatever").unwrap();
        assert!(instance_a.is_single());
        let instance_b = SingleInstance::new("whatever").unwrap();
        assert!(!instance_b.is_single());
    }
    let instance_c = SingleInstance::new("whatever").unwrap();
    assert!(instance_c.is_single());
}

Ensuring the SingleInstance stays during lifetime of the process

Users should ensure that instance of SingleInstace should lives in lifetime of the calling process. This could be achieved in multiple ways. Few ways are:

  • Using std::sync::Once
static mut SINGLE_INSTANCE_VAL : Option<SingleInstance> = None;
static SINGLE_INSTANCE_VAL_LOCK: Once = Once::new();

pub fn ensure_single_instance(uniq_id: &str) -> bool {
    let instance =  SingleInstance::new(&uniq_id);
    match  instance{
        Ok(inst) => {
            let single = inst.is_single();
            if single {
                unsafe {
                    SINGLE_INSTANCE_VAL_LOCK.call_once(|| {
                        SINGLE_INSTANCE_VAL = Some(inst);
                    })
                }
            }
            single
        },
        Err(e) => {
            false
        }
    }
}
  • Using Box::leak. This example is in examples/multi_instance_server.rs
pub fn ensure_single_instance(uniq_id: &str) -> bool {
   let instance = Box::new(SingleInstance::new(uniq_id).unwrap());
   if instance.is_single() {
       Box::leak(instance);
       true
   }else {
       false
   }
}

single-instance's People

Contributors

wlbf avatar gssmahadevan avatar woodruffw avatar ancwrd1 avatar jwalton avatar

Stargazers

Wankko Ree avatar Mikachu2333 avatar ChaseC avatar Andrej Mihajlov avatar  avatar  avatar Veniamin Albaev avatar Kevin Yue avatar Markus avatar  avatar Carlo Corradini avatar Madis Liias avatar GyDi avatar Alex Mit avatar 张伯雨 avatar Thomas Versteeg avatar San avatar Justin Sun avatar  avatar Orvar Segerström avatar John Ingve Olsen avatar  avatar Andy Bao avatar Jose F. Fernandez avatar Colin Yates avatar Michael Yang avatar Max Goncharenko avatar  avatar Xudong Huang avatar ypcpy avatar Denis Kolodin avatar DoubleClick avatar Ben Lovy avatar Harry avatar

Watchers

James Cloos avatar  avatar  avatar

single-instance's Issues

False positive determining if application is already running

There is an issue with this library where it may prevent an application from running when it actually isn't running yet.

For instance, I might try to run the application and single instance triggers, claiming it is "already running";
I then realise that my editor is editing a file with a similar name to the application.
When quitting the editor, the single instance crate no longer blocks and allows me to run the application.

I have not viewed the source but this suggests SingleInstance does the equivalent of a "grep" on the process list, disregarding where the application name string occurs in the list (as program name or as another command line argument), leading to false positives.

It would be more reliable to create a mutex, for instance a file with a deterministic name and keep a file lock on it for the duration of the run, so that a second instance would fail to acquire this lock until the first application has quit or crashed.

macOS not listed as supported platform on docs.rs

Hello,

according to the information over at docs.rs, this crate is supported on Windows 32-bit, Windows 64-bit and Linux 64-bit. However, the source code in v0.1.2 suggests that macOS is now also supported. So apparently the information on docs.rs needs to be updated to reflect this.

Best regards
q:-) <= Quang

Unresolved import `self::inner`

error[E0432]: unresolved import self::inner
--> /home/xxx/.cargo/git/checkouts/single-instance-xxx/src/lib.rs:32:15
|
32 | pub use self::inner::*;
| ^^^^^ maybe a missing crate inner?

single-instance version: git master
target: x86_64-pc-windows-gnu

single-instance 0.1.1 on crates.io is fine.

SingleInstance is not Send on Windows platform

When trying to use the SingleInstance on Windows in the concurrent or asynchronous context the compilation errors are raised about data structure being not Send (and eventually Sync):

the trait `Send` is not implemented for `*mut c_void`

I think it is related to the internal implementation of single-instance. Would be nice if it's made Send + Sync explicitly on Windows.

On macOS and Linux this doesn't happen.

Consider using an abstract Unix domain socket on Linux

Hey there! I was looking into writing my own crate for process singletons, and I ran into this one. Nice work!

I'm just dropping in to offer a suggestion for improving this crate, at least on Linux: instead of acquiring a lock on a file, you could instead create a Unix domain socket in the "abstract" namespace. This has a few advantages:

  • The abstract namespace never touches disk, meaning that you don't have to worry about filesystem permissions
  • Unix domain sockets already have create-exclusive behavior, reducing the number of system calls
  • Unix domain sockets are automatically collected on process destruction, meaning that you don't need a Drop (which won't fire on program interrupts or aborts)

The nix crate has support for creating domain sockets in the abstract namespace. I'd be happy to work on this feature, if you're interested.

[Feature Request] Activate Instance

Since both Linux and Windows now use something akin of domains, it makes me believe it should be somewhat possible to implement a function that brings the window to the foreground.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.