Coder Social home page Coder Social logo

shaneutt / p5-ipc-semaphore-set Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 8 KB

A simple Perl5 interface to kernel semaphores

Home Page: https://metacpan.org/pod/IPC::Semaphore::Set

Perl 100.00%
perl perl5 perl-module semaphore semaphore-sets ipc mutex

p5-ipc-semaphore-set's Introduction

IPC::Semaphore::Set

Build Status

POSIX semaphores allow processes and threads to synchronize their actions.

This module abstracts away POSIX semaphores into an IPC::Semaphore::Set object, which is a set of resources that provides mutexes for a key.

Installation

You can install this module from CPAN directly:

sudo cpan -i IPC::Semaphore::Set

Or manually build and install the module via the repository with Dist::Zilla:

cpan -i Dist::Zilla Dist::Zilla::Plugin::VersionFromModule Dist::Zilla::Plugin::AutoPrereqs Dist::Zilla::PluginBundle::Basic
dzil test
dzil install

Synopsis

Basics

In the simplest case you may want to provide a single mutex for a key:

my $semset = IPC::Semaphore::Set->new(key_name => 'my_lock_name');

In the above example, my_lock_name refers to some arbitrary resource that my system processes need access to, but should only allow one process to use it at a time.

The $semset object above will provide a single resource that can be locked once (this is the default configuration).

We can wait for a lock and then do our work with the following line:

$semset->resource->lockWait;
# ... do our work

Resource locks are released when the $semset object goes out of scope, or you can explicitly unlock in your code (TODO):

# ... our work is done
$semset->resource->unlock;

Multiple Resources and Availability

For a more complex use case, let's imagine I have five arbitrary devices connected to my current computer

and each of those devices has ten channels for whatever work it is they do.

I could use IPC::Semaphore::Set to manage locking for those devices from local programs on my machine.

For instance I might create the following object:

my $semset = IPC::Semaphore::Set->new(
    key_name     => 'my_device',
    resources    => 5,
    availability => 10,
);

This would create a $semset object that would represent my five devices and allow those devices each ten simultaneous locks.

I could then lock several resources as many times as I need and do my work with the resource elsewhere:

$semset->resource(0)->lockWait;
$semset->resource(1)->lockWait;
$semset->resource(1)->lockWait;
# ... do our work

Keep in mind that the key_name provided is only for your benefit and that the locks here are arbitrary.

This module just provides mutexes so that your applications/processes/threads can keep track of and coordinate resource limitations with each other.

p5-ipc-semaphore-set's People

Contributors

shaneutt avatar

Stargazers

 avatar

Watchers

 avatar

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.