los_event.c 7.5 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
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 31
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

A
arvinzzz 已提交
32 33
#include "los_event.h"
#include "los_hook.h"
L
likailong 已提交
34
#include "los_interrupt.h"
A
arvinzzz 已提交
35
#include "los_task.h"
星e雨's avatar
星e雨 已提交
36
#include "los_sched.h"
A
arvinzzz 已提交
37

W
wenjun 已提交
38 39 40 41 42 43 44 45

LITE_OS_SEC_TEXT_INIT UINT32 LOS_EventInit(PEVENT_CB_S eventCB)
{
    if (eventCB == NULL) {
        return LOS_ERRNO_EVENT_PTR_NULL;
    }
    eventCB->uwEventID = 0;
    LOS_ListInit(&eventCB->stEventList);
46
    OsHookCall(LOS_HOOK_TYPE_EVENT_INIT, eventCB);
W
wenjun 已提交
47 48 49 50 51 52
    return LOS_OK;
}

LITE_OS_SEC_TEXT UINT32 LOS_EventPoll(UINT32 *eventID, UINT32 eventMask, UINT32 mode)
{
    UINT32 ret = 0;
53
    UINT32 intSave;
W
wenjun 已提交
54

L
likailong 已提交
55 56 57
    if (eventID == NULL) {
        return LOS_ERRNO_EVENT_PTR_NULL;
    }
W
wenjun 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    intSave = LOS_IntLock();
    if (mode & LOS_WAITMODE_OR) {
        if ((*eventID & eventMask) != 0) {
            ret = *eventID & eventMask;
        }
    } else {
        if ((eventMask != 0) && (eventMask == (*eventID & eventMask))) {
            ret = *eventID & eventMask;
        }
    }
    if (ret && (mode & LOS_WAITMODE_CLR)) {
        *eventID = *eventID & ~(ret);
    }
    LOS_IntRestore(intSave);
    return ret;
}

LITE_OS_SEC_TEXT STATIC_INLINE UINT32 OsEventReadParamCheck(PEVENT_CB_S eventCB, UINT32 eventMask, UINT32 mode)
{
    if (eventCB == NULL) {
        return LOS_ERRNO_EVENT_PTR_NULL;
    }
    if ((eventCB->stEventList.pstNext == NULL) || (eventCB->stEventList.pstPrev == NULL)) {
        return LOS_ERRNO_EVENT_NOT_INITIALIZED;
    }
    if (eventMask == 0) {
        return LOS_ERRNO_EVENT_EVENTMASK_INVALID;
    }
    if (eventMask & LOS_ERRTYPE_ERROR) {
        return LOS_ERRNO_EVENT_SETBIT_INVALID;
    }
    if (((mode & LOS_WAITMODE_OR) && (mode & LOS_WAITMODE_AND)) ||
        (mode & ~(LOS_WAITMODE_OR | LOS_WAITMODE_AND | LOS_WAITMODE_CLR)) ||
        !(mode & (LOS_WAITMODE_OR | LOS_WAITMODE_AND))) {
        return LOS_ERRNO_EVENT_FLAGS_INVALID;
    }
    return LOS_OK;
}

LITE_OS_SEC_TEXT UINT32 LOS_EventRead(PEVENT_CB_S eventCB, UINT32 eventMask, UINT32 mode, UINT32 timeOut)
{
    UINT32 ret;
100
    UINT32 intSave;
W
wenjun 已提交
101 102 103 104 105 106 107 108 109 110
    LosTaskCB *runTsk = NULL;

    ret = OsEventReadParamCheck(eventCB, eventMask, mode);
    if (ret != LOS_OK) {
        return ret;
    }

    if (OS_INT_ACTIVE) {
        return LOS_ERRNO_EVENT_READ_IN_INTERRUPT;
    }
111 112 113
    if (g_losTask.runTask->taskStatus & OS_TASK_FLAG_SYSTEM_TASK) {
        return LOS_ERRNO_EVENT_READ_IN_SYSTEM_TASK;
    }
W
wenjun 已提交
114 115
    intSave = LOS_IntLock();
    ret = LOS_EventPoll(&(eventCB->uwEventID), eventMask, mode);
L
LiteOS2021 已提交
116
    OsHookCall(LOS_HOOK_TYPE_EVENT_READ, eventCB, eventMask, mode, timeOut);
W
wenjun 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129
    if (ret == 0) {
        if (timeOut == 0) {
            LOS_IntRestore(intSave);
            return ret;
        }

        if (g_losTaskLock) {
            LOS_IntRestore(intSave);
            return LOS_ERRNO_EVENT_READ_IN_LOCK;
        }
        runTsk = g_losTask.runTask;
        runTsk->eventMask = eventMask;
        runTsk->eventMode = mode;
星e雨's avatar
星e雨 已提交
130
        OsSchedTaskWait(&eventCB->stEventList, timeOut);
W
wenjun 已提交
131 132 133
        LOS_IntRestore(intSave);
        LOS_Schedule();

星e雨's avatar
星e雨 已提交
134
        intSave = LOS_IntLock();
W
wenjun 已提交
135
        if (runTsk->taskStatus & OS_TASK_STATUS_TIMEOUT) {
星e雨's avatar
星e雨 已提交
136
            runTsk->taskStatus &= ~OS_TASK_STATUS_TIMEOUT;
W
wenjun 已提交
137 138 139
            LOS_IntRestore(intSave);
            return LOS_ERRNO_EVENT_READ_TIMEOUT;
        }
星e雨's avatar
星e雨 已提交
140

W
wenjun 已提交
141 142
        ret = LOS_EventPoll(&eventCB->uwEventID, eventMask, mode);
    }
星e雨's avatar
星e雨 已提交
143 144

    LOS_IntRestore(intSave);
W
wenjun 已提交
145 146 147 148 149 150 151
    return ret;
}

LITE_OS_SEC_TEXT UINT32 LOS_EventWrite(PEVENT_CB_S eventCB, UINT32 events)
{
    LosTaskCB *resumedTask = NULL;
    LosTaskCB *nextTask = (LosTaskCB *)NULL;
152
    UINT32 intSave;
W
wenjun 已提交
153 154 155 156 157 158 159 160 161 162 163
    UINT8 exitFlag = 0;
    if (eventCB == NULL) {
        return LOS_ERRNO_EVENT_PTR_NULL;
    }
    if ((eventCB->stEventList.pstNext == NULL) || (eventCB->stEventList.pstPrev == NULL)) {
        return LOS_ERRNO_EVENT_NOT_INITIALIZED;
    }
    if (events & LOS_ERRTYPE_ERROR) {
        return LOS_ERRNO_EVENT_SETBIT_INVALID;
    }
    intSave = LOS_IntLock();
L
LiteOS2021 已提交
164
    OsHookCall(LOS_HOOK_TYPE_EVENT_WRITE, eventCB, events);
W
wenjun 已提交
165 166 167 168 169 170 171 172 173 174 175
    eventCB->uwEventID |= events;
    if (!LOS_ListEmpty(&eventCB->stEventList)) {
        for (resumedTask = LOS_DL_LIST_ENTRY((&eventCB->stEventList)->pstNext, LosTaskCB, pendList);
             &resumedTask->pendList != (&eventCB->stEventList);) {
            nextTask = LOS_DL_LIST_ENTRY(resumedTask->pendList.pstNext, LosTaskCB, pendList);

            if (((resumedTask->eventMode & LOS_WAITMODE_OR) && (resumedTask->eventMask & events) != 0) ||
                ((resumedTask->eventMode & LOS_WAITMODE_AND) &&
                 ((resumedTask->eventMask & eventCB->uwEventID) == resumedTask->eventMask))) {
                exitFlag = 1;

星e雨's avatar
星e雨 已提交
176
                OsSchedTaskWake(resumedTask);
W
wenjun 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
            }
            resumedTask = nextTask;
        }

        if (exitFlag == 1) {
            LOS_IntRestore(intSave);
            LOS_Schedule();
            return LOS_OK;
        }
    }

    LOS_IntRestore(intSave);
    return LOS_OK;
}

LITE_OS_SEC_TEXT_INIT UINT32 LOS_EventDestroy(PEVENT_CB_S eventCB)
{
194
    UINT32 intSave;
W
wenjun 已提交
195 196 197 198 199 200 201
    if (eventCB == NULL) {
        return LOS_ERRNO_EVENT_PTR_NULL;
    }
    intSave = LOS_IntLock();

    if (!LOS_ListEmpty(&eventCB->stEventList)) {
        LOS_IntRestore(intSave);
zhushy_'s avatar
zhushy_ 已提交
202
        return LOS_ERRNO_EVENT_SHOULD_NOT_DESTROYED;
W
wenjun 已提交
203 204 205 206
    }
    eventCB->stEventList.pstNext = (LOS_DL_LIST *)NULL;
    eventCB->stEventList.pstPrev = (LOS_DL_LIST *)NULL;
    LOS_IntRestore(intSave);
207
    OsHookCall(LOS_HOOK_TYPE_EVENT_DESTROY, eventCB);
W
wenjun 已提交
208 209
    return LOS_OK;
}
210

G
Guangyao Ma 已提交
211
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_EventClear(PEVENT_CB_S eventCB, UINT32 eventMask)
W
wenjun 已提交
212
{
213
    UINT32 intSave;
W
wenjun 已提交
214 215 216
    if (eventCB == NULL) {
        return LOS_ERRNO_EVENT_PTR_NULL;
    }
L
LiteOS2021 已提交
217
    OsHookCall(LOS_HOOK_TYPE_EVENT_CLEAR, eventCB, eventMask);
W
wenjun 已提交
218
    intSave = LOS_IntLock();
G
Guangyao Ma 已提交
219
    eventCB->uwEventID &= eventMask;
W
wenjun 已提交
220 221 222
    LOS_IntRestore(intSave);
    return LOS_OK;
}