Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Matej Kosik <5764c029b688c1c0d24a2e97cd764f@gmail.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] semaphore puzzle
Date: Tue, 01 Oct 2013 13:30:30 +0200	[thread overview]
Message-ID: <1380627030.23931.15.camel@zotac> (raw)
In-Reply-To: <5249C348.9070408@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2797 bytes --]

Hi,

Ocamlnet includes support for semaphores (at least for Unix platforms). 

Am Montag, den 30.09.2013, 19:30 +0100 schrieb Matej Kosik:
> Hi,
> 
> I am trying to find the most simple and, if possible, clean way to synchronize my threads.
> 
> In this particular case, semaphores would be ideal
> (alternatively, unbounded channels)
> 
>   (* Create a new semaphore with a given initial value. *)
>   val create : int -> t

Netsys_posix.sem_create (or sem_open or sem_init).

> 
>   (* V *)
>   val up : t -> unit

Netsys_posix.sem_post

>   (* P *)
>   val down : t -> unit

Netsys_posix.sem_wait with option SEM_WAIT_BLOCK.


>   val try_down : t -> bool

Netsys_posix.sem_wait with option SEM_WAIT_NONBLOCK.

> The "down" or "try_down" are almost the solutions, but not quite.
> 
> "down" may block forever ---> there is no timeout at all
>                               (I need some)
> 
> "try_down" --->  the timeout is zero
>                  (I prefer a non-zero timeout)
> 
> Obviously, I could use "try_down" in a loop, checking the system time myself and then either give up or actually manage to decrement the semaphore.
> 
> Is there a better solution than this?

The POSIX API specifies sem_timedwait, but I haven't included it in my
bindings, as it is optional in POSIX. What you could do as workaround is
to start a helper thread which will sem_post after the timeout. I admit
that it is difficult to find a clean solution here without race
conditions.

> (There is a Unix module, there are signals, but I am not sure whether it is safe to use them in multithreaded program.
>  At least, I did not have a luck.)

The signal handling as provided by Unix (or better by the OCaml runtime)
is not sufficient for this kind of programming.

An alternative might be to use a pipe as returned by Unix.pipe. The
number of bytes in the pipe buffer is the semaphore counter:

post = add a byte to the buffer
wait = read a byte from the buffer

You can use Unix.select to specify a timeout when waiting. The solution
works as long as the pipe buffer doesn't fill up (at least 4096 bytes
are guaranteed). You can totally avoid that if you combine the pipe with
an additional counter (i.e. semaphore counter = pipe buffer + additional
counter), and you use the pipe only when the additional counter is 0.

Gerd


> 
> Thanks in advance for patience &| help.
> 

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
My OCaml site:          http://www.camlcity.org
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

      reply	other threads:[~2013-10-01 11:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-30 18:30 Matej Kosik
2013-10-01 11:30 ` Gerd Stolpmann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1380627030.23931.15.camel@zotac \
    --to=info@gerd-stolpmann.de \
    --cc=5764c029b688c1c0d24a2e97cd764f@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox