~jojo/Carth

ref: 749208029494a7c48ce04444a513424f4b998416 Carth/std/sync.carth -rw-r--r-- 1009 bytes
74920802JoJo update TODO 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(import io)

;; TODO: Enforce usage invariants with linear types.

;; The size of a pthread_mutex_t can be found in ~pthreadtypes-arch.h~.
(define: sizeof_pthread_mutex_t Nat (cast 40))

(data Mutex (Mutex (Box Unit)))

(extern pthread_mutex_init (Fun Mutex (Box Unit) Int32))
(extern pthread_mutex_destroy (Fun Mutex Int32))
(extern pthread_mutex_lock (Fun Mutex Int32))
(extern pthread_mutex_trylock (Fun Mutex Int32))
(extern pthread_mutex_unlock (Fun Mutex Int32))

(define: mutex/new
    (IO Mutex)
  (io/wrap (let ((mx (Mutex (cast-ptr (id@"GC_malloc" sizeof_pthread_mutex_t))))
                 (attrs (mem/unsafe-null Unit)))
             (seq (pthread_mutex_init mx attrs)
                  mx))))

(define: (mutex/lock mx) (Fun Mutex (IO Unit))
  (io/wrap (seq (pthread_mutex_lock mx) Unit)))

(define: (mutex/try-lock mx) (Fun Mutex (IO Bool))
  (io/wrap (= (pthread_mutex_trylock mx) (cast 0))))

(define: (mutex/release mx) (Fun Mutex (IO Unit))
  (io/wrap (seq (pthread_mutex_unlock mx) Unit)))