1 #ifndef MUTEX_H 2 #define MUTEX_H 3 4 /** 5 * Copyright (c) 2019, The Linux Foundation. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials provided 15 * with the distribution. 16 * * Neither the name of The Linux Foundation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 29 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 30 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #if (defined __qdsp6__) || (defined __hexagon__) 34 #include "qurt_mutex.h" 35 36 #define RW_MUTEX_T qurt_mutex_t 37 #define RW_MUTEX_CTOR(mut) qurt_mutex_init(& (mut)) 38 #define RW_MUTEX_LOCK_READ(mut) qurt_mutex_lock(& (mut)) 39 #define RW_MUTEX_UNLOCK_READ(mut) qurt_mutex_unlock(& (mut)) 40 #define RW_MUTEX_LOCK_WRITE(mut) qurt_mutex_lock(& (mut)) 41 #define RW_MUTEX_UNLOCK_WRITE(mut) qurt_mutex_unlock(& (mut)) 42 #define RW_MUTEX_DTOR(mut) qurt_mutex_destroy(& (mut)) 43 44 #elif (1 == __linux) || (1 == __linux__) || (1 == __gnu_linux__) || (1 == linux) 45 46 #include <pthread.h> 47 #include <stdlib.h> 48 #include <stdio.h> 49 50 /* asserts may be compiled out, this should always be present */ 51 #define ABORT_FAIL( ff ) \ 52 do {\ 53 if(! (ff) ) {\ 54 fprintf(stderr, "assertion \"%s\" failed: file \"%s\", line %d\n", #ff, __FILE__, __LINE__);\ 55 abort();\ 56 }\ 57 } while(0) 58 59 #define RW_MUTEX_T pthread_rwlock_t 60 #define RW_MUTEX_CTOR(mut) ABORT_FAIL(0 == pthread_rwlock_init( & (mut), 0)) 61 #define RW_MUTEX_LOCK_READ(mut) ABORT_FAIL(0 == pthread_rwlock_rdlock( & (mut))) 62 #define RW_MUTEX_UNLOCK_READ(mut) ABORT_FAIL(0 == pthread_rwlock_unlock( & (mut))) 63 #define RW_MUTEX_LOCK_WRITE(mut) ABORT_FAIL(0 == pthread_rwlock_wrlock( & (mut))) 64 #define RW_MUTEX_UNLOCK_WRITE(mut) ABORT_FAIL(0 == pthread_rwlock_unlock( & (mut))) 65 #define RW_MUTEX_DTOR(mut) ABORT_FAIL(0 == pthread_rwlock_destroy( & (mut))) 66 67 68 #else 69 70 #include "AEEstd.h" 71 72 #define RW_MUTEX_T uint32 73 #define RW_MUTEX_CTOR(mut) mut = 0 74 #define RW_MUTEX_LOCK_READ(mut) \ 75 do {\ 76 assert(STD_BIT_TEST(&mut, 1) == 0); \ 77 assert(STD_BIT_TEST(&mut, 2) == 0); \ 78 STD_BIT_SET(&mut, 1); \ 79 } while (0) 80 81 #define RW_MUTEX_UNLOCK_READ(mut) \ 82 do {\ 83 assert(STD_BIT_TEST(&mut, 1)); \ 84 assert(STD_BIT_TEST(&mut, 2) == 0); \ 85 STD_BIT_CLEAR(&mut, 1); \ 86 } while (0) 87 88 #define RW_MUTEX_LOCK_WRITE(mut) \ 89 do {\ 90 assert(STD_BIT_TEST(&mut, 1) == 0); \ 91 assert(STD_BIT_TEST(&mut, 2) == 0); \ 92 STD_BIT_SET(&mut, 2); \ 93 } while (0) 94 95 #define RW_MUTEX_UNLOCK_WRITE(mut) \ 96 do {\ 97 assert(STD_BIT_TEST(&mut, 1) == 0); \ 98 assert(STD_BIT_TEST(&mut, 2)); \ 99 STD_BIT_CLEAR(&mut, 2); \ 100 } while (0) 101 102 #define RW_MUTEX_DTOR(mut) mut = 0 103 104 #endif 105 #endif //MUTEX_H 106