caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Mutex and posix
@ 2005-01-12 10:53 Luca Pascali
  2005-01-12 15:56 ` [Caml-list] " Xavier Leroy
  2005-01-15 17:49 ` Alex Baretta
  0 siblings, 2 replies; 10+ messages in thread
From: Luca Pascali @ 2005-01-12 10:53 UTC (permalink / raw)
  To: caml-list

Just a little question, my curiosity about the thread module.

I found in Posix (this is from 'info libc' page on section Mutexes) 
these three functions

Function: int pthread_mutex_lock (pthread_mutex_t *mutex))
Function: int pthread_mutex_trylock (pthread_mutex_t *MUTEX)
Function: int pthread_mutex_timedlock (pthread_mutex_t *MUTEX, const 
struct timespec *ABSTIME)

1) for waiting indefinetly for a mutex,
2) failing immediatly if a mutex is locked,
3) wait for a specified amount of time and failing if mutex is still 
locked when time is expired

Module Mutex, provides an interface only to the first two functions: 
lock and try_lock.

My question is:
is there any reason for this situation?

Polling continously is different. If I have two threads that are running 
with scantimes one multiple of the other, it is possible that one of the 
two threads (the slower one) fails always or almost always the try_lock 
command.

Luca

-- 
*********************************************************************
Luca Pascali
luca@barettadeit.com
asxcaml-guru@barettadeit.com

http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. 02 370 111 55
fax. 02 370 111 54

Our technology:
http://www.asxcaml.org/
http://www.freerp.org/


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Mutex and posix
  2005-01-12 10:53 Mutex and posix Luca Pascali
@ 2005-01-12 15:56 ` Xavier Leroy
  2005-01-12 17:29   ` Luca Pascali
  2005-01-15 17:49 ` Alex Baretta
  1 sibling, 1 reply; 10+ messages in thread
From: Xavier Leroy @ 2005-01-12 15:56 UTC (permalink / raw)
  To: Luca Pascali; +Cc: caml-list

> Just a little question, my curiosity about the thread module.
> 
> I found in Posix (this is from 'info libc' page on section Mutexes) 
> these three functions
> 
> Function: int pthread_mutex_lock (pthread_mutex_t *mutex))
> Function: int pthread_mutex_trylock (pthread_mutex_t *MUTEX)
> Function: int pthread_mutex_timedlock (pthread_mutex_t *MUTEX, const 
> struct timespec *ABSTIME)

The latter is a recent addition to the POSIX threads API -- it's not
in the original POSIX threads spec (POSIX 1003.1c-1995).  I wouldn't
rely on this function being available in all POSIX threads
implementations.

> Polling continously is different. If I have two threads that are running 
> with scantimes one multiple of the other, it is possible that one of the 
> two threads (the slower one) fails always or almost always the try_lock 
> command.

It's hard to give useful suggestions without knowing more about your
application, but it could be the case that you're using mutexes to do
things they are not really designed for, i.e. plain mutual exclusion,
for which neither trylock nor timedlock are needed.

Maybe your application needs a more complex but better suited
synchronization mechanism, which can generally be built on top of
mutexes and conditions, or (at a higher semantic level) Concurrent
ML-style events.

- Xavier Leroy


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Mutex and posix
  2005-01-12 15:56 ` [Caml-list] " Xavier Leroy
@ 2005-01-12 17:29   ` Luca Pascali
  2005-01-15 17:53     ` Alex Baretta
  0 siblings, 1 reply; 10+ messages in thread
From: Luca Pascali @ 2005-01-12 17:29 UTC (permalink / raw)
  To: caml-list

Xavier Leroy wrote:

>[...]
>The latter is a recent addition to the POSIX threads API -- it's not
>in the original POSIX threads spec (POSIX 1003.1c-1995).  I wouldn't
>rely on this function being available in all POSIX threads
>implementations.
>
>  
>
I didn't know this.

>>Polling continously is different. If I have two threads that are running 
>>with scantimes one multiple of the other, it is possible that one of the 
>>two threads (the slower one) fails always or almost always the try_lock 
>>command.
>>    
>>
>
>It's hard to give useful suggestions without knowing more about your
>application, but it could be the case that you're using mutexes to do
>things they are not really designed for, i.e. plain mutual exclusion,
>for which neither trylock nor timedlock are needed.
>
>Maybe your application needs a more complex but better suited
>synchronization mechanism, which can generally be built on top of
>mutexes and conditions, or (at a higher semantic level) Concurrent
>ML-style events.
>
>- Xavier Leroy
>
>  
>
I haven't an application.
I was just thinking about how I could port some programs organizations 
into Ocaml.

The case that rised my question was:
If I have a shared resource (let's say a pipe or a queue, or a generic 
file descriptor) for performing, for example, asyncronous communication 
between threads, it's quite dangerous in terms of deadlocks to perform 
myself a polling or locking one thread indefinetly until the resource is 
freed.
Polling with try_lock can bring to a deadlock or to a long freezing if 
one thread locks the mutex periodically, let's say, every 100 ms for 10 
ms and the other one looks for the same mutex periodically about every 
1200 ms. The probabilty to fall into the locking period is incredibly high.

If I'm not wrong, the timed mutex is handled by the operating system, so 
when the mutex is released the OS directly assigns it to the first 
waiting requesting thread, at least if the mutex cannot be locked during 
the specified time, function returns.

Anyway, knowing that this function is not actually available (I hope it 
will be in the future), it's obvious that I will look for another 
solution, maybe changing the syncronization method.

Thanks for the answer

Luca

-- 
*********************************************************************
Luca Pascali
luca@barettadeit.com
asxcaml-guru@barettadeit.com

http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. 02 370 111 55
fax. 02 370 111 54

Our technology:
http://www.asxcaml.org/
http://www.freerp.org/


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Mutex and posix
  2005-01-12 10:53 Mutex and posix Luca Pascali
  2005-01-12 15:56 ` [Caml-list] " Xavier Leroy
@ 2005-01-15 17:49 ` Alex Baretta
  2005-01-15 19:37   ` Brian Hurt
  2005-01-17 12:33   ` Luca Pascali
  1 sibling, 2 replies; 10+ messages in thread
From: Alex Baretta @ 2005-01-15 17:49 UTC (permalink / raw)
  To: Luca Pascali; +Cc: caml-list

Luca Pascali wrote:
> Just a little question, my curiosity about the thread module.
> 
> I found in Posix (this is from 'info libc' page on section Mutexes) 
> these three functions
> 
> Function: int pthread_mutex_lock (pthread_mutex_t *mutex))
> Function: int pthread_mutex_trylock (pthread_mutex_t *MUTEX)
> Function: int pthread_mutex_timedlock (pthread_mutex_t *MUTEX, const 
> struct timespec *ABSTIME)

You can probably simulate the third one fairly well by resorting to 
Unix.select as a timer, but why in the world would you want to lock for 
a while and then forget about it?

Besides --- given Machiavelli works in an asynchronous, event-modeled 
world --- why would want to use synchronous primitives such as timedlock?

Alex


-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Mutex and posix
  2005-01-12 17:29   ` Luca Pascali
@ 2005-01-15 17:53     ` Alex Baretta
  0 siblings, 0 replies; 10+ messages in thread
From: Alex Baretta @ 2005-01-15 17:53 UTC (permalink / raw)
  To: Luca Pascali; +Cc: caml-list

Luca Pascali wrote:
> Xavier Leroy wrote:
> 
>> [...]
>> The latter is a recent addition to the POSIX threads API -- it's not
>> in the original POSIX threads spec (POSIX 1003.1c-1995).  I wouldn't
>> rely on this function being available in all POSIX threads
>> implementations.
>>
>>  
>>
> I didn't know this.
> 
>>> Polling continously is different. If I have two threads that are 
>>> running with scantimes one multiple of the other, it is possible that 
>>> one of the two threads (the slower one) fails always or almost always 
>>> the try_lock command.
>>>   
>>
>>
>> It's hard to give useful suggestions without knowing more about your
>> application, but it could be the case that you're using mutexes to do
>> things they are not really designed for, i.e. plain mutual exclusion,
>> for which neither trylock nor timedlock are needed.
>>
>> Maybe your application needs a more complex but better suited
>> synchronization mechanism, which can generally be built on top of
>> mutexes and conditions, or (at a higher semantic level) Concurrent
>> ML-style events.
>>
>> - Xavier Leroy
>>
>>  
>>
> I haven't an application.
> I was just thinking about how I could port some programs organizations 
> into Ocaml.
> 
> The case that rised my question was:
> If I have a shared resource (let's say a pipe or a queue, or a generic 
> file descriptor) for performing, for example, asyncronous communication 
> between threads, it's quite dangerous in terms of deadlocks to perform 
> myself a polling or locking one thread indefinetly until the resource is 
> freed.
> Polling with try_lock can bring to a deadlock or to a long freezing if 
> one thread locks the mutex periodically, let's say, every 100 ms for 10 
> ms and the other one looks for the same mutex periodically about every 
> 1200 ms. The probabilty to fall into the locking period is incredibly high.

The fact that a program is deadlock free does not depend on the 
synchronization API but only on the algorithm. If the program deadlocks, 
the program is faulty. You can actually build correctness proofs in 
concurrent computation models to make really sure that your algorithm 
works fine. Never rely on OS timing unless you are modeling a time 
dependent system.



Alex


-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Mutex and posix
  2005-01-15 17:49 ` Alex Baretta
@ 2005-01-15 19:37   ` Brian Hurt
  2005-01-17  8:52     ` Alex Baretta
  2005-01-17 12:33   ` Luca Pascali
  1 sibling, 1 reply; 10+ messages in thread
From: Brian Hurt @ 2005-01-15 19:37 UTC (permalink / raw)
  To: Alex Baretta; +Cc: Luca Pascali, caml-list

On Sat, 15 Jan 2005, Alex Baretta wrote:

> Luca Pascali wrote:
> > Just a little question, my curiosity about the thread module.
> > 
> > I found in Posix (this is from 'info libc' page on section Mutexes) 
> > these three functions
> > 
> > Function: int pthread_mutex_lock (pthread_mutex_t *mutex))
> > Function: int pthread_mutex_trylock (pthread_mutex_t *MUTEX)
> > Function: int pthread_mutex_timedlock (pthread_mutex_t *MUTEX, const 
> > struct timespec *ABSTIME)
> 
> You can probably simulate the third one fairly well by resorting to 
> Unix.select as a timer, but why in the world would you want to lock for 
> a while and then forget about it?

No, you misunderstand what the function does.  The function won't wait 
more than ABSTIME to acquire the lock.  _mutex_lock() waits forever to get 
the lock, _mutex_trylock doesn't wait at all, and _mutex_timedlock only 
waits so long to get the lock.

Brian



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Mutex and posix
  2005-01-15 19:37   ` Brian Hurt
@ 2005-01-17  8:52     ` Alex Baretta
  2005-01-19  3:31       ` Brian Hurt
  0 siblings, 1 reply; 10+ messages in thread
From: Alex Baretta @ 2005-01-17  8:52 UTC (permalink / raw)
  To: Brian Hurt, Ocaml, Luca Pascali

Brian Hurt wrote:
> On Sat, 15 Jan 2005, Alex Baretta wrote:
> 
> 
>>Luca Pascali wrote:
>>
>>>Just a little question, my curiosity about the thread module.
>>>
>>>I found in Posix (this is from 'info libc' page on section Mutexes) 
>>>these three functions
>>>
>>>Function: int pthread_mutex_lock (pthread_mutex_t *mutex))
>>>Function: int pthread_mutex_trylock (pthread_mutex_t *MUTEX)
>>>Function: int pthread_mutex_timedlock (pthread_mutex_t *MUTEX, const 
>>>struct timespec *ABSTIME)
>>
>>You can probably simulate the third one fairly well by resorting to 
>>Unix.select as a timer, but why in the world would you want to lock for 
>>a while and then forget about it?
> 
> 
> No, you misunderstand what the function does.  The function won't wait 
> more than ABSTIME to acquire the lock.  _mutex_lock() waits forever to get 
> the lock, _mutex_trylock doesn't wait at all, and _mutex_timedlock only 
> waits so long to get the lock.
> 
> Brian

I am aware of this. But, if one really needs the timed primitive, one 
can always simulate it by sampling the mutex state repeatedly with 
_trylock. Unix.select serves the purpose of controlling the sampling time.

Notwithstanding this, when the need arises for a timed primitive in a 
concurrent algorithm, my suggestion is always to attempt to remodel the 
problem rather than the API.

Alex

-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Mutex and posix
  2005-01-15 17:49 ` Alex Baretta
  2005-01-15 19:37   ` Brian Hurt
@ 2005-01-17 12:33   ` Luca Pascali
  1 sibling, 0 replies; 10+ messages in thread
From: Luca Pascali @ 2005-01-17 12:33 UTC (permalink / raw)
  To: caml-list

Alex Baretta wrote:

> Luca Pascali wrote:
>
>> Just a little question, my curiosity about the thread module.
>>
>> I found in Posix (this is from 'info libc' page on section Mutexes) 
>> these three functions
>>
>> Function: int pthread_mutex_lock (pthread_mutex_t *mutex))
>> Function: int pthread_mutex_trylock (pthread_mutex_t *MUTEX)
>> Function: int pthread_mutex_timedlock (pthread_mutex_t *MUTEX, const 
>> struct timespec *ABSTIME)
>
>
> You can probably simulate the third one fairly well by resorting to 
> Unix.select as a timer, but why in the world would you want to lock 
> for a while and then forget about it?
>
> Besides --- given Machiavelli works in an asynchronous, event-modeled 
> world --- why would want to use synchronous primitives such as timedlock?
>
> Alex
>
>

It's not about Machiavelli. It was just a curiosity (We spoken 
personally a few months ago).
I staied with the doubt about the missing of this function and I thought 
that asking to the list was the best thing to do for having an answer. 
And it was.

Luca

-- 
*********************************************************************
Luca Pascali
luca@barettadeit.com
asxcaml-guru@barettadeit.com

http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. 02 370 111 55
fax. 02 370 111 54

Our technology:
http://www.asxcaml.org/
http://www.freerp.org/


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Mutex and posix
  2005-01-17  8:52     ` Alex Baretta
@ 2005-01-19  3:31       ` Brian Hurt
  2005-01-19  9:34         ` Alex Baretta
  0 siblings, 1 reply; 10+ messages in thread
From: Brian Hurt @ 2005-01-19  3:31 UTC (permalink / raw)
  To: Alex Baretta; +Cc: Ocaml, Luca Pascali

On Mon, 17 Jan 2005, Alex Baretta wrote:

> > 
> > No, you misunderstand what the function does.  The function won't wait 
> > more than ABSTIME to acquire the lock.  _mutex_lock() waits forever to get 
> > the lock, _mutex_trylock doesn't wait at all, and _mutex_timedlock only 
> > waits so long to get the lock.
> > 
> > Brian
> 
> I am aware of this. But, if one really needs the timed primitive, one 
> can always simulate it by sampling the mutex state repeatedly with 
> _trylock. Unix.select serves the purpose of controlling the sampling time.
> 
> Notwithstanding this, when the need arises for a timed primitive in a 
> concurrent algorithm, my suggestion is always to attempt to remodel the 
> problem rather than the API.

Um, no.  What happens if the mutex is unlocked for some period of time 
between your polls, but relocked before you poll the lock again?  It's 
very easy to hit livelock conditions doing this.

Brian



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Mutex and posix
  2005-01-19  3:31       ` Brian Hurt
@ 2005-01-19  9:34         ` Alex Baretta
  0 siblings, 0 replies; 10+ messages in thread
From: Alex Baretta @ 2005-01-19  9:34 UTC (permalink / raw)
  To: Brian Hurt; +Cc: Ocaml, Luca Pascali

Brian Hurt wrote:
> On Mon, 17 Jan 2005, Alex Baretta wrote:
> 
>>I am aware of this. But, if one really needs the timed primitive, one 
>>can always simulate it by sampling the mutex state repeatedly with 
>>_trylock. Unix.select serves the purpose of controlling the sampling time.

> 
> Um, no.  What happens if the mutex is unlocked for some period of time 
> between your polls, but relocked before you poll the lock again?  It's 
> very easy to hit livelock conditions doing this.
> 
> Brian

Of course it is. My solution is only a kludge to simulate an OS 
primitive on a system where it is not available. I am not stating that 
my strategy yields a POSIX compliant timedlock.

Alex


-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-01-19  9:34 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-12 10:53 Mutex and posix Luca Pascali
2005-01-12 15:56 ` [Caml-list] " Xavier Leroy
2005-01-12 17:29   ` Luca Pascali
2005-01-15 17:53     ` Alex Baretta
2005-01-15 17:49 ` Alex Baretta
2005-01-15 19:37   ` Brian Hurt
2005-01-17  8:52     ` Alex Baretta
2005-01-19  3:31       ` Brian Hurt
2005-01-19  9:34         ` Alex Baretta
2005-01-17 12:33   ` Luca Pascali

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).