1/*
2 * Copyright (C) 2005 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#define LOG_TAG "misc"
18
19//
20// Miscellaneous utility functions.
21//
22#include <utils/misc.h>
23#include <utils/Log.h>
24
25#include <sys/stat.h>
26#include <string.h>
27#include <errno.h>
28#include <stdio.h>
29
30#if !defined(_WIN32)
31# include <pthread.h>
32#endif
33
34#include <utils/Vector.h>
35
36using namespace android;
37
38namespace android {
39
40struct sysprop_change_callback_info {
41    sysprop_change_callback callback;
42    int priority;
43};
44
45#if !defined(_WIN32)
46static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
47static Vector<sysprop_change_callback_info>* gSyspropList = NULL;
48#endif
49
50void add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
51#if !defined(_WIN32)
52    pthread_mutex_lock(&gSyspropMutex);
53    if (gSyspropList == NULL) {
54        gSyspropList = new Vector<sysprop_change_callback_info>();
55    }
56    sysprop_change_callback_info info;
57    info.callback = cb;
58    info.priority = priority;
59    bool added = false;
60    for (size_t i=0; i<gSyspropList->size(); i++) {
61        if (priority >= gSyspropList->itemAt(i).priority) {
62            gSyspropList->insertAt(info, i);
63            added = true;
64            break;
65        }
66    }
67    if (!added) {
68        gSyspropList->add(info);
69    }
70    pthread_mutex_unlock(&gSyspropMutex);
71#endif
72}
73
74void report_sysprop_change() {
75#if !defined(_WIN32)
76    pthread_mutex_lock(&gSyspropMutex);
77    Vector<sysprop_change_callback_info> listeners;
78    if (gSyspropList != NULL) {
79        listeners = *gSyspropList;
80    }
81    pthread_mutex_unlock(&gSyspropMutex);
82
83    //ALOGI("Reporting sysprop change to %d listeners", listeners.size());
84    for (size_t i=0; i<listeners.size(); i++) {
85        listeners[i].callback();
86    }
87#endif
88}
89
90}; // namespace android
91