Home | History | Annotate | Download | only in libutils
      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 <stdio.h>
     28 
     29 #if !defined(_WIN32)
     30 # include <pthread.h>
     31 #endif
     32 
     33 #include <utils/Vector.h>
     34 
     35 using namespace android;
     36 
     37 namespace android {
     38 
     39 struct sysprop_change_callback_info {
     40     sysprop_change_callback callback;
     41     int priority;
     42 };
     43 
     44 #if !defined(_WIN32)
     45 static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
     46 static Vector<sysprop_change_callback_info>* gSyspropList = NULL;
     47 #endif
     48 
     49 void add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
     50 #if !defined(_WIN32)
     51     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 
     73 void report_sysprop_change() {
     74 #if !defined(_WIN32)
     75     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
     90