blob: 2608a65751e0e802e4784da9185825257ea828fd [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
Yabin Cui4a6e5a32015-01-26 19:48:54 -080025#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070026#endif
27
28#include <utils/Vector.h>
29
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080030using namespace android;
31
32namespace android {
33
Dianne Hackbornc1309d72012-05-08 18:54:22 -070034struct sysprop_change_callback_info {
35 sysprop_change_callback callback;
36 int priority;
37};
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080038
Yabin Cui4a6e5a32015-01-26 19:48:54 -080039#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070040static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
41static Vector<sysprop_change_callback_info>* gSyspropList = NULL;
42#endif
43
44void add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -080045#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070046 pthread_mutex_lock(&gSyspropMutex);
47 if (gSyspropList == NULL) {
48 gSyspropList = new Vector<sysprop_change_callback_info>();
49 }
50 sysprop_change_callback_info info;
51 info.callback = cb;
52 info.priority = priority;
53 bool added = false;
54 for (size_t i=0; i<gSyspropList->size(); i++) {
55 if (priority >= gSyspropList->itemAt(i).priority) {
56 gSyspropList->insertAt(info, i);
57 added = true;
58 break;
59 }
60 }
61 if (!added) {
62 gSyspropList->add(info);
63 }
64 pthread_mutex_unlock(&gSyspropMutex);
65#endif
66}
67
68void report_sysprop_change() {
Yabin Cui4a6e5a32015-01-26 19:48:54 -080069#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070070 pthread_mutex_lock(&gSyspropMutex);
71 Vector<sysprop_change_callback_info> listeners;
72 if (gSyspropList != NULL) {
73 listeners = *gSyspropList;
74 }
75 pthread_mutex_unlock(&gSyspropMutex);
76
77 //ALOGI("Reporting sysprop change to %d listeners", listeners.size());
78 for (size_t i=0; i<listeners.size(); i++) {
79 listeners[i].callback();
80 }
81#endif
82}
83
84}; // namespace android