Advertisement
Guest User

Untitled

a guest
Oct 26th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.56 KB | None | 0 0
  1. class Lock {
  2.  
  3.     private val mutex = nativeHeap.alloc<pthread_mutex_t>()
  4.  
  5.     init {
  6.         freeze()
  7.         pthread_mutex_init(mutex.ptr, null)
  8.     }
  9.  
  10.     fun lock() {
  11.         pthread_mutex_lock(mutex.ptr)
  12.     }
  13.  
  14.     fun unlock() {
  15.         pthread_mutex_unlock(mutex.ptr)
  16.     }
  17.  
  18.     fun destroy() {
  19.         pthread_mutex_destroy(mutex.ptr)
  20.         nativeHeap.free(mutex)
  21.     }
  22.  
  23.     fun <T> use(block: () -> T): T {
  24.         try {
  25.             lock()
  26.             return block()
  27.         } finally {
  28.             unlock()
  29.         }
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement