1/* 2 * Copyright 2016, Data61 3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO) 4 * ABN 41 687 119 230. 5 * 6 * This software may be distributed and modified according to the terms of 7 * the BSD 2-Clause license. Note that NO WARRANTY is provided. 8 * See "LICENSE_BSD2.txt" for details. 9 * 10 * @TAG(D61_BSD) 11 */ 12 13#include <assert.h> 14#include <string.h> 15#include <stdlib.h> 16#include <sel4/sel4.h> 17 18#include <refos/refos.h> 19#include <refos/sync.h> 20#include <refos-util/cspace.h> 21#include <refos-rpc/proc_client.h> 22#include <refos-rpc/proc_client_helper.h> 23 24/*! @file 25 @brief Basic kernel synchronisation library. */ 26 27#define SYNC_ASYNC_BADGE_MAGIC 0x4188A 28 29struct sync_mutex_ { 30 seL4_CPtr mapping; 31}; 32 33sync_mutex_t 34sync_create_mutex() 35{ 36 sync_mutex_t mutex; 37 38 /* Create the mutex struct. */ 39 mutex = (sync_mutex_t) malloc(sizeof(struct sync_mutex_)); 40 if (!mutex) 41 return NULL; 42 43 /* Create the endpoint. */ 44 mutex->mapping = proc_new_async_endpoint_badged(SYNC_ASYNC_BADGE_MAGIC); 45 if (REFOS_GET_ERRNO() != ESUCCESS || mutex->mapping == 0) { 46 free(mutex); 47 return NULL; 48 } 49 50 /* Prime the endpoint. */ 51 seL4_Signal(mutex->mapping); 52 return mutex; 53} 54 55void 56sync_destroy_mutex(sync_mutex_t mutex) 57{ 58 proc_del_async_endpoint(mutex->mapping); 59 free(mutex); 60} 61 62void 63sync_acquire(sync_mutex_t mutex) 64{ 65 seL4_Word badge; 66 assert(mutex); 67 seL4_MessageInfo_t msginfo = seL4_Recv(mutex->mapping, &badge); 68 assert(badge == SYNC_ASYNC_BADGE_MAGIC); 69 (void) msginfo; 70 71} 72 73void 74sync_release(sync_mutex_t mutex) 75{ 76 /* Release the lock and wake the next thread up. */ 77 seL4_Signal(mutex->mapping); 78} 79 80int 81sync_try_acquire(sync_mutex_t mutex) 82{ 83 seL4_Word badge = 0; 84 seL4_MessageInfo_t info = seL4_Poll(mutex->mapping, &badge); 85 (void) info; 86 return badge == SYNC_ASYNC_BADGE_MAGIC; 87} 88