10d163575Sopenharmony_ci/* 2 * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. 3 * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 12 * of conditions and the following disclaimer in the documentation and/or other materials 13 * provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 16 * to endorse or promote products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include "los_event.h" 33#include "linux/semaphore.h" 34#include "linux/wait.h" 35#include "sys/types.h" 36#include "poll.h" 37 38 39int _sema_init(losMutexDef_t *sem, unsigned int value) 40{ 41 UINT32 semHandle; 42 UINT32 ret; 43 44 if ((sem == NULL) || (value > OS_SEM_COUNT_MAX)) { 45 return -1; 46 } 47 48 ret = LOS_SemCreate((UINT16)value, &semHandle); 49 if (ret != LOS_OK) { 50 return -1; 51 } 52 53 sem->sem = GET_SEM(semHandle); 54 55 return 0; 56} 57 58int _sema_lock(losMutexDef_t *sem) 59{ 60 UINT32 ret; 61 62 if ((sem == NULL) || (sem->sem == NULL)) { 63 return -1; 64 } 65 66 if ((UINTPTR)sem->sem == UNINIT_VALUE) { 67 if (_sema_init(sem, sem->count) != 0) { 68 return -1; 69 } 70 } 71 72 ret = LOS_SemPend(sem->sem->semID, LOS_WAIT_FOREVER); 73 if (ret == LOS_OK) { 74 return 0; 75 } 76 77 return -1; 78} 79 80int _sema_trylock(losMutexDef_t *sem) 81{ 82 UINT32 ret; 83 84 if ((sem == NULL) || (sem->sem == NULL)) { 85 return -1; 86 } 87 88 ret = LOS_SemPend(sem->sem->semID, LOS_NO_WAIT); 89 if (ret == LOS_OK) { 90 return 0; 91 } 92 93 return -1; 94} 95 96int _sema_unlock(losMutexDef_t *sem) 97{ 98 UINT32 ret; 99 100 if ((sem == NULL) || (sem->sem == NULL)) { 101 return -1; 102 } 103 104 ret = LOS_SemPost(sem->sem->semID); 105 if (ret != LOS_OK) { 106 return -1; 107 } 108 109 return 0; 110} 111 112int _sema_destory(losMutexDef_t *sem) 113{ 114 UINT32 ret; 115 116 if ((sem == NULL) || (sem->sem == NULL)) { 117 return -1; 118 } 119 if ((UINTPTR)sem->sem == UNINIT_VALUE) { 120 return -1; 121 } 122 ret = LOS_SemDelete(sem->sem->semID); 123 if (ret != LOS_OK) { 124 return -1; 125 } 126 127 return 0; 128} 129 130void __init_waitqueue_head(wait_queue_head_t *wait) 131{ 132 if (wait == NULL) { 133 return; 134 } 135 (VOID)LOS_EventInit(&wait->stEvent); 136 spin_lock_init(&wait->lock); 137 LOS_ListInit(&wait->poll_queue); 138} 139 140void __wake_up_interruptible(wait_queue_head_t *wait) 141{ 142 if (wait == NULL) { 143 return; 144 } 145 (VOID)LOS_EventWrite(&wait->stEvent, 0x1); 146 notify_poll(wait); 147} 148 149void __wake_up_interruptible_poll(wait_queue_head_t *wait, pollevent_t key) 150{ 151 if (wait == NULL) { 152 return; 153 } 154 (VOID)LOS_EventWrite(&wait->stEvent, 0x1); 155 notify_poll_with_key(wait, key); 156} 157 158