sync.c revision 0b595cc18d82e41dfab0c686e9e63c30a86e8c80
1/*
2 * Copyright (C) 2010 The Android Open Source Project
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/* sync */
18
19#include "sles_allinclusive.h"
20
21
22/** Sync thread.
23 *   The sync thread runs periodically to synchronize audio state between
24 *   the application and platform-specific device driver; for best results
25 *   it should run about every graphics frame (e.g. 20 Hz to 50 Hz).
26 */
27
28void *sync_start(void *arg)
29{
30    CEngine *this = (CEngine *) arg;
31    for (;;) {
32
33        usleep(20000*5);
34
35        object_lock_exclusive(&this->mObject);
36        if (this->mEngine.mShutdown) {
37            this->mEngine.mShutdownAck = SL_BOOLEAN_TRUE;
38            pthread_cond_signal(&this->mEngine.mShutdownCond);
39            object_unlock_exclusive(&this->mObject);
40            break;
41        }
42        if (this->m3DCommit.mWaiting) {
43            this->m3DCommit.mWaiting = 0;
44            ++this->m3DCommit.mGeneration;
45            // There might be more than one thread blocked in Commit, so wake them all
46            object_cond_broadcast(&this->mObject);
47            // here is where we would process the enqueued 3D commands
48        }
49        unsigned instanceMask = this->mEngine.mInstanceMask;
50        unsigned changedMask = this->mEngine.mChangedMask;
51        this->mEngine.mChangedMask = 0;
52        object_unlock_exclusive(&this->mObject);
53
54        // now we know which objects exist, and which of those have changes
55
56        unsigned combinedMask = changedMask | instanceMask;
57        while (combinedMask) {
58            unsigned i = ctz(combinedMask);
59            assert(MAX_INSTANCE > i);
60            combinedMask &= ~(1 << i);
61            IObject *instance = (IObject *) this->mEngine.mInstances[i];
62            // Could be NULL during construct or destroy
63            if (NULL == instance)
64                continue;
65
66            // FIXME race condition here - object could be destroyed by now, better do destroy here
67
68            object_lock_exclusive(instance);
69            //unsigned attributesMask = instance->mAttributesMask;
70            instance->mAttributesMask = 0;
71
72            switch (IObjectToObjectID(instance)) {
73            case SL_OBJECTID_AUDIOPLAYER:
74                {
75                //CAudioPlayer *audioPlayer = (CAudioPlayer *) instance;
76                // do something here
77                object_unlock_exclusive(instance);
78                }
79                break;
80
81            default:
82                object_unlock_exclusive(instance);
83                break;
84            }
85        }
86    }
87    return NULL;
88}
89