• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #define KOALA_INTEROP_MODULE InteropNativeModule
17 #include "common-interop.h"
18 #include "interop-types.h"
19 #include "callback-resource.h"
20 #include <deque>
21 #include <unordered_map>
22 
23 
24 static bool needReleaseFront = false;
25 static std::deque<CallbackEventKind> callbackEventsQueue;
26 static std::deque<CallbackBuffer> callbackCallSubqueue;
27 static std::deque<InteropInt32> callbackResourceSubqueue;
28 
enqueueCallback(const CallbackBuffer * event)29 void enqueueCallback(const CallbackBuffer* event)
30 {
31     callbackEventsQueue.push_back(Event_CallCallback);
32     callbackCallSubqueue.push_back(*event);
33 }
34 
holdManagedCallbackResource(InteropInt32 resourceId)35 void holdManagedCallbackResource(InteropInt32 resourceId)
36 {
37     callbackEventsQueue.push_back(Event_HoldManagedResource);
38     callbackResourceSubqueue.push_back(resourceId);
39 }
40 
releaseManagedCallbackResource(InteropInt32 resourceId)41 void releaseManagedCallbackResource(InteropInt32 resourceId)
42 {
43     callbackEventsQueue.push_back(Event_ReleaseManagedResource);
44     callbackResourceSubqueue.push_back(resourceId);
45 }
46 
impl_CheckCallbackEvent(KByte * buffer,KInt size)47 KInt impl_CheckCallbackEvent(KByte* buffer, KInt size)
48 {
49     KByte* result = (KByte*)buffer;
50     if (needReleaseFront) {
51         switch (callbackEventsQueue.front()) {
52             case Event_CallCallback:
53                 callbackCallSubqueue.front().resourceHolder.release();
54                 callbackCallSubqueue.pop_front();
55                 break;
56             case Event_HoldManagedResource:
57             case Event_ReleaseManagedResource:
58                 callbackResourceSubqueue.pop_front();
59                 break;
60             default:
61                 INTEROP_FATAL("Unknown event kind");
62         }
63         callbackEventsQueue.pop_front();
64         needReleaseFront = false;
65     }
66     if (callbackEventsQueue.empty()) {
67         return 0;
68     }
69     const CallbackEventKind frontEventKind = callbackEventsQueue.front();
70 #ifdef __STDC_LIB_EXT1__
71     errno_t res = memcpy_s(result, size, &frontEventKind, 4);
72     if (res != EOK) {
73         return 0;
74     }
75 #else
76     memcpy(result, &frontEventKind, 4);
77 #endif
78 
79     switch (frontEventKind) {
80         case Event_CallCallback:
81 #ifdef __STDC_LIB_EXT1__
82             errno_t res = memcpy_s(result + 4, size, callbackCallSubqueue.front().buffer,
83                 sizeof(CallbackBuffer::buffer));
84             if (res != EOK) {
85                 return 0;
86             }
87 #else
88             memcpy(result + 4, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer));
89 #endif
90             break;
91         case Event_HoldManagedResource:
92         case Event_ReleaseManagedResource: {
93             const InteropInt32 resourceId = callbackResourceSubqueue.front();
94 #ifdef __STDC_LIB_EXT1__
95             errno_t res = memcpy_s(result + 4, size, &frontEventKind, 4);
96             if (res != EOK) {
97                 return 0;
98             }
99 #else
100             memcpy(result + 4, &resourceId, 4);
101 #endif
102             break;
103         }
104         default:
105             INTEROP_FATAL("Unknown event kind");
106     }
107     needReleaseFront = true;
108     return 1;
109 }
KOALA_INTEROP_2(CheckCallbackEvent,KInt,KByte *,KInt)110 KOALA_INTEROP_2(CheckCallbackEvent, KInt, KByte*, KInt)
111 
112 void impl_ReleaseCallbackResource(InteropInt32 resourceId)
113 {
114     releaseManagedCallbackResource(resourceId);
115 }
KOALA_INTEROP_V1(ReleaseCallbackResource,KInt)116 KOALA_INTEROP_V1(ReleaseCallbackResource, KInt)
117 
118 void impl_HoldCallbackResource(InteropInt32 resourceId)
119 {
120     holdManagedCallbackResource(resourceId);
121 }
122 KOALA_INTEROP_V1(HoldCallbackResource, KInt)
123