xref: /OK3568_Linux_fs/external/mpp/osal/mpp_queue.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2017 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "mpp_err.h"
18 #include "mpp_queue.h"
19 
MppQueue(node_destructor func)20 MppQueue::MppQueue(node_destructor func)
21     : mpp_list(func), mFlushFlag(0)
22 {
23     sem_init(&mQueuePending, 0, 0);
24 }
25 
~MppQueue()26 MppQueue::~MppQueue()
27 {
28     sem_destroy (&mQueuePending);
29 }
30 
push(void * data,RK_S32 size)31 RK_S32 MppQueue::push(void *data, RK_S32 size)
32 {
33     RK_S32 ret = 0;
34 
35     ret = mpp_list::add_at_tail(data, size);
36     mFlushFlag = 0;
37     sem_post(&mQueuePending);
38 
39     return ret;
40 }
41 
pull(void * data,RK_S32 size)42 RK_S32 MppQueue::pull(void *data, RK_S32 size)
43 {
44     if (!mFlushFlag)
45         sem_wait(&mQueuePending);
46     {
47         AutoMutex autoLock(mpp_list::mutex());
48         if (!mpp_list::list_size())
49             return MPP_NOK;
50 
51         return mpp_list::del_at_head(data, size);
52     }
53     return MPP_ERR_INIT;
54 }
55 
flush()56 RK_S32 MppQueue::flush()
57 {
58     if (mFlushFlag)
59         return 0;
60 
61     mFlushFlag = 1;
62     sem_post(&mQueuePending);
63     return mpp_list::flush();
64 }
65