lin
2025-08-20 171e08343a9b6df6c31197f5b4800e8004800f5b
1
2
3
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/* DynamicInterfaceManagement implementation */
 
#include "sles_allinclusive.h"
 
 
// Called by a worker thread to handle an asynchronous AddInterface.
// Parameter self is the DynamicInterface, and MPH specifies which interface to add.
 
static void HandleAdd(void *self, void *ignored, int MPH)
{
 
    // validate input parameters
    IDynamicInterfaceManagement *thiz = (IDynamicInterfaceManagement *) self;
    assert(NULL != thiz);
    IObject *thisObject = InterfaceToIObject(thiz);
    assert(NULL != thisObject);
    assert(0 <= MPH && MPH < MPH_MAX);
    const ClassTable *clazz = thisObject->mClass;
    assert(NULL != clazz);
    int index = clazz->mMPH_to_index[MPH];
    assert(0 <= index && index < (int) clazz->mInterfaceCount);
    SLuint8 *interfaceStateP = &thisObject->mInterfaceStates[index];
    SLresult result;
 
    // check interface state
    object_lock_exclusive(thisObject);
    SLuint8 state = *interfaceStateP;
    switch (state) {
 
    case INTERFACE_ADDING_1:    // normal case
        {
        // change state to indicate we are now adding the interface
        *interfaceStateP = INTERFACE_ADDING_2;
        object_unlock_exclusive(thisObject);
 
        // this section runs with mutex unlocked
        const struct iid_vtable *x = &clazz->mInterfaces[index];
        size_t offset = x->mOffset;
        void *thisItf = (char *) thisObject + offset;
        BoolHook expose = MPH_init_table[MPH].mExpose;
        // call the optional expose hook
        if ((NULL == expose) || (*expose)(thisItf)) {
            result = SL_RESULT_SUCCESS;
        } else {
            result = SL_RESULT_FEATURE_UNSUPPORTED;
        }
 
        // re-lock mutex to update state
        object_lock_exclusive(thisObject);
        assert(INTERFACE_ADDING_2 == *interfaceStateP);
        if (SL_RESULT_SUCCESS == result) {
            ((size_t *) thisItf)[0] ^= ~0;
            state = INTERFACE_ADDED;
        } else {
            state = INTERFACE_INITIALIZED;
        }
        }
        break;
 
    case INTERFACE_ADDING_1A:   // operation was aborted while on work queue
        result = SL_RESULT_OPERATION_ABORTED;
        state = INTERFACE_INITIALIZED;
        break;
 
    default:                    // impossible
        assert(SL_BOOLEAN_FALSE);
        result = SL_RESULT_INTERNAL_ERROR;
        break;
 
    }
 
    // mutex is locked, update state
    *interfaceStateP = state;
 
    // Make a copy of these, so we can call the callback with mutex unlocked
    slDynamicInterfaceManagementCallback callback = thiz->mCallback;
    void *context = thiz->mContext;
    object_unlock_exclusive(thisObject);
 
    // Note that the mutex is unlocked during the callback
    if (NULL != callback) {
        const SLInterfaceID iid = &SL_IID_array[MPH]; // equal but not == to the original IID
        (*callback)(&thiz->mItf, context, SL_DYNAMIC_ITF_EVENT_ASYNC_TERMINATION, result, iid);
    }
 
}
 
 
static SLresult IDynamicInterfaceManagement_AddInterface(SLDynamicInterfaceManagementItf self,
    const SLInterfaceID iid, SLboolean async)
{
    SL_ENTER_INTERFACE
 
    // validate input parameters
    if (NULL == iid) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        IDynamicInterfaceManagement *thiz = (IDynamicInterfaceManagement *) self;
        IObject *thisObject = InterfaceToIObject(thiz);
        const ClassTable *clazz = thisObject->mClass;
        int MPH, index;
        if ((0 > (MPH = IID_to_MPH(iid))) ||
                // no need to check for an initialization hook
                // (NULL == MPH_init_table[MPH].mInit) ||
                (0 > (index = clazz->mMPH_to_index[MPH]))) {
            result = SL_RESULT_FEATURE_UNSUPPORTED;
        } else {
            assert(index < (int) clazz->mInterfaceCount);
            SLuint8 *interfaceStateP = &thisObject->mInterfaceStates[index];
 
            // check interface state
            object_lock_exclusive(thisObject);
            switch (*interfaceStateP) {
 
            case INTERFACE_INITIALIZED: // normal case
                if (async) {
                    // Asynchronous: mark operation pending and cancellable
                    *interfaceStateP = INTERFACE_ADDING_1;
                    object_unlock_exclusive(thisObject);
 
                    // this section runs with mutex unlocked
                    result = ThreadPool_add_ppi(&thisObject->mEngine->mThreadPool, HandleAdd, thiz,
                        NULL, MPH);
                    if (SL_RESULT_SUCCESS != result) {
                        // Engine was destroyed during add, or insufficient memory,
                        // so restore mInterfaceStates state to prior value
                        object_lock_exclusive(thisObject);
                        switch (*interfaceStateP) {
                        case INTERFACE_ADDING_1:    // normal
                        case INTERFACE_ADDING_1A:   // operation aborted while mutex unlocked
                            *interfaceStateP = INTERFACE_INITIALIZED;
                            break;
                        default:                    // unexpected
                            // leave state alone
                            break;
                        }
                    }
 
                } else {
                    // Synchronous: mark operation pending to prevent duplication
                    *interfaceStateP = INTERFACE_ADDING_2;
                    object_unlock_exclusive(thisObject);
 
                    // this section runs with mutex unlocked
                    const struct iid_vtable *x = &clazz->mInterfaces[index];
                    size_t offset = x->mOffset;
                    void *thisItf = (char *) thisObject + offset;
                    // call the optional expose hook
                    BoolHook expose = MPH_init_table[MPH].mExpose;
                    if ((NULL == expose) || (*expose)(thisItf)) {
                        result = SL_RESULT_SUCCESS;
                    } else {
                        result = SL_RESULT_FEATURE_UNSUPPORTED;
                    }
 
                    // re-lock mutex to update state
                    object_lock_exclusive(thisObject);
                    assert(INTERFACE_ADDING_2 == *interfaceStateP);
                    if (SL_RESULT_SUCCESS == result) {
                        *interfaceStateP = INTERFACE_ADDED;
                    } else {
                        *interfaceStateP = INTERFACE_INITIALIZED;
                    }
                }
 
                // mutex is still locked
                break;
 
            default:    // disallow adding of (partially) initialized interfaces
                result = SL_RESULT_PRECONDITIONS_VIOLATED;
                break;
 
            }
 
            object_unlock_exclusive(thisObject);
 
        }
    }
 
    SL_LEAVE_INTERFACE
}
 
 
static SLresult IDynamicInterfaceManagement_RemoveInterface(
    SLDynamicInterfaceManagementItf self, const SLInterfaceID iid)
{
    SL_ENTER_INTERFACE
 
#if USE_PROFILES & USE_PROFILES_BASE
    // validate input parameters
    if (NULL == iid) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        IDynamicInterfaceManagement *thiz = (IDynamicInterfaceManagement *) self;
        IObject *thisObject = InterfaceToIObject(thiz);
        const ClassTable *clazz = thisObject->mClass;
        int MPH, index;
        if ((0 > (MPH = IID_to_MPH(iid))) ||
                // no need to check for an initialization hook
                // (NULL == MPH_init_table[MPH].mInit) ||
                (0 > (index = clazz->mMPH_to_index[MPH]))) {
            result = SL_RESULT_PRECONDITIONS_VIOLATED;
        } else {
            SLuint8 *interfaceStateP = &thisObject->mInterfaceStates[index];
 
            // check interface state
            object_lock_exclusive(thisObject);
            switch (*interfaceStateP) {
 
            case INTERFACE_ADDED:       // normal cases
            case INTERFACE_SUSPENDED:
                {
                // Compute address of the interface
                const struct iid_vtable *x = &clazz->mInterfaces[index];
                size_t offset = x->mOffset;
                void *thisItf = (char *) thisObject + offset;
 
                // Mark operation pending (not necessary; remove is synchronous with mutex locked)
                *interfaceStateP = INTERFACE_REMOVING;
 
                // Check if application ever called Object::GetInterface
                unsigned mask = 1 << index;
                if (thisObject->mGottenMask & mask) {
                    thisObject->mGottenMask &= ~mask;
                    // This trickery invalidates the v-table
                    ((size_t *) thisItf)[0] ^= ~0;
                }
 
                // The remove hook is called with mutex locked
                VoidHook remove = MPH_init_table[MPH].mRemove;
                if (NULL != remove) {
                    (*remove)(thisItf);
                }
                result = SL_RESULT_SUCCESS;
 
                assert(INTERFACE_REMOVING == *interfaceStateP);
                *interfaceStateP = INTERFACE_INITIALIZED;
                }
 
                // mutex is still locked
                break;
 
            default:
                // disallow removal of non-dynamic interfaces, or interfaces which are
                // currently being resumed (will not auto-cancel an asynchronous resume)
                result = SL_RESULT_PRECONDITIONS_VIOLATED;
                break;
 
            }
 
            object_unlock_exclusive(thisObject);
        }
    }
#else
    result = SL_RESULT_FEATURE_UNSUPPORTED;
#endif
 
    SL_LEAVE_INTERFACE
}
 
 
// Called by a worker thread to handle an asynchronous ResumeInterface.
// Parameter self is the DynamicInterface, and MPH specifies which interface to resume.
 
static void HandleResume(void *self, void *ignored, int MPH)
{
 
    // validate input parameters
    IDynamicInterfaceManagement *thiz = (IDynamicInterfaceManagement *) self;
    assert(NULL != thiz);
    IObject *thisObject = InterfaceToIObject(thiz);
    assert(NULL != thisObject);
    assert(0 <= MPH && MPH < MPH_MAX);
    const ClassTable *clazz = thisObject->mClass;
    assert(NULL != clazz);
    int index = clazz->mMPH_to_index[MPH];
    assert(0 <= index && index < (int) clazz->mInterfaceCount);
    SLuint8 *interfaceStateP = &thisObject->mInterfaceStates[index];
    SLresult result;
 
    // check interface state
    object_lock_exclusive(thisObject);
    SLuint8 state = *interfaceStateP;
    switch (state) {
 
    case INTERFACE_RESUMING_1:      // normal case
        {
        // change state to indicate we are now resuming the interface
        *interfaceStateP = INTERFACE_RESUMING_2;
        object_unlock_exclusive(thisObject);
 
        // this section runs with mutex unlocked
        const struct iid_vtable *x = &clazz->mInterfaces[index];
        size_t offset = x->mOffset;
        void *thisItf = (char *) thisObject + offset;
        VoidHook resume = MPH_init_table[MPH].mResume;
        if (NULL != resume) {
            (*resume)(thisItf);
        }
        result = SL_RESULT_SUCCESS;
 
        // re-lock mutex to update state
        object_lock_exclusive(thisObject);
        assert(INTERFACE_RESUMING_2 == *interfaceStateP);
        state = INTERFACE_ADDED;
        }
        break;
 
    case INTERFACE_RESUMING_1A:     // operation was aborted while on work queue
        result = SL_RESULT_OPERATION_ABORTED;
        state = INTERFACE_SUSPENDED;
        break;
 
    default:                        // impossible
        assert(SL_BOOLEAN_FALSE);
        result = SL_RESULT_INTERNAL_ERROR;
        break;
 
    }
 
    // mutex is locked, update state
    *interfaceStateP = state;
 
    // Make a copy of these, so we can call the callback with mutex unlocked
    slDynamicInterfaceManagementCallback callback = thiz->mCallback;
    void *context = thiz->mContext;
    object_unlock_exclusive(thisObject);
 
    // Note that the mutex is unlocked during the callback
    if (NULL != callback) {
        const SLInterfaceID iid = &SL_IID_array[MPH]; // equal but not == to the original IID
        (*callback)(&thiz->mItf, context, SL_DYNAMIC_ITF_EVENT_ASYNC_TERMINATION, result, iid);
    }
}
 
 
static SLresult IDynamicInterfaceManagement_ResumeInterface(SLDynamicInterfaceManagementItf self,
    const SLInterfaceID iid, SLboolean async)
{
    SL_ENTER_INTERFACE
 
    // validate input parameters
    if (NULL == iid) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        IDynamicInterfaceManagement *thiz = (IDynamicInterfaceManagement *) self;
        IObject *thisObject = InterfaceToIObject(thiz);
        const ClassTable *clazz = thisObject->mClass;
        int MPH, index;
        if ((0 > (MPH = IID_to_MPH(iid))) ||
                // no need to check for an initialization hook
                // (NULL == MPH_init_table[MPH].mInit) ||
                (0 > (index = clazz->mMPH_to_index[MPH]))) {
            result = SL_RESULT_PRECONDITIONS_VIOLATED;
        } else {
            assert(index < (int) clazz->mInterfaceCount);
            SLuint8 *interfaceStateP = &thisObject->mInterfaceStates[index];
 
            // check interface state
            object_lock_exclusive(thisObject);
            switch (*interfaceStateP) {
 
            case INTERFACE_SUSPENDED:   // normal case
                if (async) {
                    // Asynchronous: mark operation pending and cancellable
                    *interfaceStateP = INTERFACE_RESUMING_1;
                    object_unlock_exclusive(thisObject);
 
                    // this section runs with mutex unlocked
                    result = ThreadPool_add_ppi(&thisObject->mEngine->mThreadPool, HandleResume,
                        thiz, NULL, MPH);
                    if (SL_RESULT_SUCCESS != result) {
                        // Engine was destroyed during resume, or insufficient memory,
                        // so restore mInterfaceStates state to prior value
                        object_lock_exclusive(thisObject);
                        switch (*interfaceStateP) {
                        case INTERFACE_RESUMING_1:  // normal
                        case INTERFACE_RESUMING_1A: // operation aborted while mutex unlocked
                            *interfaceStateP = INTERFACE_SUSPENDED;
                            break;
                        default:                    // unexpected
                            // leave state alone
                            break;
                        }
                    }
 
                } else {
                    // Synchronous: mark operation pending to prevent duplication
                    *interfaceStateP = INTERFACE_RESUMING_2;
                    object_unlock_exclusive(thisObject);
 
                    // this section runs with mutex unlocked
                    const struct iid_vtable *x = &clazz->mInterfaces[index];
                    size_t offset = x->mOffset;
                    void *thisItf = (char *) thiz + offset;
                    VoidHook resume = MPH_init_table[MPH].mResume;
                    if (NULL != resume) {
                        (*resume)(thisItf);
                    }
                    result = SL_RESULT_SUCCESS;
 
                    // re-lock mutex to update state
                    object_lock_exclusive(thisObject);
                    assert(INTERFACE_RESUMING_2 == *interfaceStateP);
                    *interfaceStateP = INTERFACE_ADDED;
                }
 
                // mutex is now locked
                break;
 
            default:    // disallow resumption of non-suspended interfaces
                result = SL_RESULT_PRECONDITIONS_VIOLATED;
                break;
            }
 
            object_unlock_exclusive(thisObject);
        }
    }
 
    SL_LEAVE_INTERFACE
}
 
 
static SLresult IDynamicInterfaceManagement_RegisterCallback(SLDynamicInterfaceManagementItf self,
    slDynamicInterfaceManagementCallback callback, void *pContext)
{
    SL_ENTER_INTERFACE
 
    IDynamicInterfaceManagement *thiz = (IDynamicInterfaceManagement *) self;
    IObject *thisObject = InterfaceToIObject(thiz);
    object_lock_exclusive(thisObject);
    thiz->mCallback = callback;
    thiz->mContext = pContext;
    object_unlock_exclusive(thisObject);
    result = SL_RESULT_SUCCESS;
 
    SL_LEAVE_INTERFACE
}
 
 
static const struct SLDynamicInterfaceManagementItf_ IDynamicInterfaceManagement_Itf = {
    IDynamicInterfaceManagement_AddInterface,
    IDynamicInterfaceManagement_RemoveInterface,
    IDynamicInterfaceManagement_ResumeInterface,
    IDynamicInterfaceManagement_RegisterCallback
};
 
void IDynamicInterfaceManagement_init(void *self)
{
    IDynamicInterfaceManagement *thiz = (IDynamicInterfaceManagement *) self;
    thiz->mItf = &IDynamicInterfaceManagement_Itf;
    thiz->mCallback = NULL;
    thiz->mContext = NULL;
}