@@ -20,18 +20,24 @@ module System.Posix.Semaphore
20
20
semPost , semGetValue )
21
21
where
22
22
23
+ #include "HsUnix.h"
23
24
#include <semaphore.h>
24
25
#include <fcntl.h>
25
26
26
27
import Foreign.C
27
28
import Foreign.ForeignPtr hiding (newForeignPtr )
28
29
import Foreign.Concurrent
29
- import Foreign.Marshal
30
30
import Foreign.Ptr
31
- import Foreign.Storable
32
31
import System.Posix.Types
33
32
import Control.Concurrent
34
33
import Data.Bits
34
+ #if !defined(HAVE_SEM_GETVALUE)
35
+ import System.IO.Error ( ioeSetLocation )
36
+ import GHC.IO.Exception ( unsupportedOperation )
37
+ #else
38
+ import Foreign.Marshal
39
+ import Foreign.Storable
40
+ #endif
35
41
36
42
data OpenSemFlags = OpenSemFlags { semCreate :: Bool ,
37
43
-- ^ If true, create the semaphore if it
@@ -102,15 +108,24 @@ semPost (Semaphore fptr) = withForeignPtr fptr semPost'
102
108
103
109
-- | Return the semaphore's current value.
104
110
semGetValue :: Semaphore -> IO Int
111
+ #ifdef HAVE_SEM_GETVALUE
105
112
semGetValue (Semaphore fptr) = withForeignPtr fptr semGetValue'
106
113
where semGetValue' sem = alloca (semGetValue_ sem)
107
114
115
+
108
116
semGetValue_ :: Ptr () -> Ptr CInt -> IO Int
109
117
semGetValue_ sem ptr = do throwErrnoIfMinus1Retry_ " semGetValue" $
110
118
sem_getvalue sem ptr
111
119
cint <- peek ptr
112
120
return $ fromEnum cint
113
121
122
+ foreign import capi safe " semaphore.h sem_getvalue"
123
+ sem_getvalue :: Ptr () -> Ptr CInt -> IO Int
124
+ #else
125
+ {-# WARNING semGetValue "operation will throw 'IOError' \"unsupported operation\" (CPP guard: @#if HAVE_SEM_GETVALUE@)" #-}
126
+ semGetValue _ = ioError (ioeSetLocation unsupportedOperation " semGetValue" )
127
+ #endif
128
+
114
129
foreign import capi safe " semaphore.h sem_open"
115
130
sem_open :: CString -> CInt -> CMode -> CUInt -> IO (Ptr () )
116
131
foreign import capi safe " semaphore.h sem_close"
@@ -124,5 +139,3 @@ foreign import capi safe "semaphore.h sem_trywait"
124
139
sem_trywait :: Ptr () -> IO CInt
125
140
foreign import capi safe " semaphore.h sem_post"
126
141
sem_post :: Ptr () -> IO CInt
127
- foreign import capi safe " semaphore.h sem_getvalue"
128
- sem_getvalue :: Ptr () -> Ptr CInt -> IO Int
0 commit comments