blob: ed1ba23923a245e654a3a20091882c36cb61d505 [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>
27#include <errno.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028#include <stdio.h>
29
Yabin Cui4a6e5a32015-01-26 19:48:54 -080030#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070031# include <pthread.h>
32#endif
33
34#include <utils/Vector.h>
35
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080036using namespace android;
37
38namespace android {
39
Dianne Hackbornc1309d72012-05-08 18:54:22 -070040struct sysprop_change_callback_info {
41 sysprop_change_callback callback;
42 int priority;
43};
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080044
Yabin Cui4a6e5a32015-01-26 19:48:54 -080045#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070046static 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) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -080051#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070052 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() {
Yabin Cui4a6e5a32015-01-26 19:48:54 -080075#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070076 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