blob: 216dc1430215735e664370b60ba2adc417f8ef66 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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
Dianne Hackbornc1309d72012-05-08 18:54:22 -070017#define LOG_TAG "misc"
18
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080019//
20// Miscellaneous utility functions.
21//
22#include <utils/misc.h>
Dianne Hackbornc1309d72012-05-08 18:54:22 -070023#include <utils/Log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024
25#include <sys/stat.h>
26#include <string.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027#include <stdio.h>
28
Yabin Cui4a6e5a32015-01-26 19:48:54 -080029#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070030# include <pthread.h>
31#endif
32
33#include <utils/Vector.h>
34
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080035using namespace android;
36
37namespace android {
38
Dianne Hackbornc1309d72012-05-08 18:54:22 -070039struct sysprop_change_callback_info {
40 sysprop_change_callback callback;
41 int priority;
42};
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080043
Yabin Cui4a6e5a32015-01-26 19:48:54 -080044#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070045static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
46static Vector<sysprop_change_callback_info>* gSyspropList = NULL;
47#endif
48
49void add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -080050#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070051 pthread_mutex_lock(&gSyspropMutex);
52 if (gSyspropList == NULL) {
53 gSyspropList = new Vector<sysprop_change_callback_info>();
54 }
55 sysprop_change_callback_info info;
56 info.callback = cb;
57 info.priority = priority;
58 bool added = false;
59 for (size_t i=0; i<gSyspropList->size(); i++) {
60 if (priority >= gSyspropList->itemAt(i).priority) {
61 gSyspropList->insertAt(info, i);
62 added = true;
63 break;
64 }
65 }
66 if (!added) {
67 gSyspropList->add(info);
68 }
69 pthread_mutex_unlock(&gSyspropMutex);
70#endif
71}
72
73void report_sysprop_change() {
Yabin Cui4a6e5a32015-01-26 19:48:54 -080074#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070075 pthread_mutex_lock(&gSyspropMutex);
76 Vector<sysprop_change_callback_info> listeners;
77 if (gSyspropList != NULL) {
78 listeners = *gSyspropList;
79 }
80 pthread_mutex_unlock(&gSyspropMutex);
81
82 //ALOGI("Reporting sysprop change to %d listeners", listeners.size());
83 for (size_t i=0; i<listeners.size(); i++) {
84 listeners[i].callback();
85 }
86#endif
87}
88
89}; // namespace android