Mercury Bugs - mercury
View Issue Details
0000357mercuryBugpublic2014-09-02 16:052016-10-03 10:58
Reporterjuliensf 
Assigned Tojuliensf 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionfixed 
Platformx86_64OSOS XOS Version10.9
Product Version 
Target Version15.11Fixed in Version 
Summary0000357: Parallel conjunction broken on OS X
DescriptionParallel conjunctions are broken on OS X (I used asm_fast.par.gc, but
this presumably applies to the other grades as well), because although OS X defines the function
sem_init (called from mercury_context.c), it defines it to only return an error.

Possible solutions to this are either to use named semaphores, e.g. with sem_open, or to use
Grand Central Dispatch. (I'm not sure how well the latter would work with GCC though.)
TagsNo tags attached.
Attached Files

Notes
(0000779)
wangp   
2014-09-02 16:24   
Or just condition variables, though presumably they perform worse so we would want to stick to semaphores on better platforms.
(0000780)
juliensf   
2014-09-02 16:25   
This would have shown up more obviously had the return value of sem_init been checked!

There are also a bunch of calls to various pthread_* functions in the runtime, for example pthread_mutex_init and pthread_attr_init, where the return values are not checked.
(0000921)
pbone   
2016-09-29 14:04   
I remember some things about writing this code but not others.

In particular I remember choosing semaphores for a very good reason, but I don't remember what that reason was :-(

I'm not sure what should be used instead on OS X. Except that if we want to use GCD, then we should essentially write a different implementation for the N:M threading on OS X, as GCD will (and should) handle this for us. I suspect windows is a similar case as it supports fibers. However that's about all I know of either platform/API.

So I'd recommend trying to replace this with named semaphores, if those are supported/work, or maybe condition variables - again there's a good reason why I didn't use those but I don't know what it is. And in the longer term consider writing an alternative implementation of all this code (maybe keep the work stealing) for OS X.
(0000922)
juliensf   
2016-09-29 14:13   
Hi Paul,

Using GCD to do the N:M threading is a separate issue; the semaphore implementation in libdispatch is close enough to POSIX unnamed semaphores that it should be easy enough to use as a drop-in replacement. Using GCD in general on OS X is a much larger change.
(0000923)
pbone   
2016-09-29 14:24   
Agreed, which is why I said that's a longer term change, and by implication not really part of this bug. However I wasn't aware of the primitives provided by libdispatch so I didn't know that's what you meant when you said GCD. It sounds reasonable to me.
(0000925)
juliensf   
2016-10-03 10:58   
Fixed in commit 1fc495c.

Issue History
2014-09-02 16:05juliensfNew Issue
2014-09-02 16:24wangpNote Added: 0000779
2014-09-02 16:25juliensfNote Added: 0000780
2015-10-21 10:40pboneTarget Version => 15.11
2016-09-29 11:37juliensfAssigned To => juliensf
2016-09-29 11:37juliensfStatusnew => assigned
2016-09-29 14:04pboneNote Added: 0000921
2016-09-29 14:13juliensfNote Added: 0000922
2016-09-29 14:24pboneNote Added: 0000923
2016-10-03 10:58juliensfStatusassigned => resolved
2016-10-03 10:58juliensfResolutionopen => fixed
2016-10-03 10:58juliensfNote Added: 0000925