Read/write lockify the devicestate stuff a bit.
[asterisk/asterisk.git] / main / devicestate.c
index 06386aa..78e8a19 100644 (file)
@@ -144,21 +144,21 @@ static const char *devstatestring[] = {
 struct devstate_prov {
        char label[40];
        ast_devstate_prov_cb_type callback;
-       AST_LIST_ENTRY(devstate_prov) list;
+       AST_RWLIST_ENTRY(devstate_prov) list;
 };
 
 /*! \brief A list of providers */
-static AST_LIST_HEAD_STATIC(devstate_provs, devstate_prov);
+static AST_RWLIST_HEAD_STATIC(devstate_provs, devstate_prov);
 
 /*! \brief  A device state watcher (callback) */
 struct devstate_cb {
        void *data;
        ast_devstate_cb_type callback;  /*!< Where to report when state changes */
-       AST_LIST_ENTRY(devstate_cb) list;
+       AST_RWLIST_ENTRY(devstate_cb) list;
 };
 
 /*! \brief A device state watcher list */
-static AST_LIST_HEAD_STATIC(devstate_cbs, devstate_cb);
+static AST_RWLIST_HEAD_STATIC(devstate_cbs, devstate_cb);
 
 struct state_change {
        AST_LIST_ENTRY(state_change) list;
@@ -277,9 +277,9 @@ int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
        devprov->callback = callback;
        ast_copy_string(devprov->label, label, sizeof(devprov->label));
 
-       AST_LIST_LOCK(&devstate_provs);
-       AST_LIST_INSERT_HEAD(&devstate_provs, devprov, list);
-       AST_LIST_UNLOCK(&devstate_provs);
+       AST_RWLIST_WRLOCK(&devstate_provs);
+       AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list);
+       AST_RWLIST_UNLOCK(&devstate_provs);
 
        return 0;
 }
@@ -289,16 +289,16 @@ void ast_devstate_prov_del(const char *label)
 {
        struct devstate_prov *devcb;
 
-       AST_LIST_LOCK(&devstate_provs);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
+       AST_RWLIST_WRLOCK(&devstate_provs);
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
                if (!strcasecmp(devcb->label, label)) {
-                       AST_LIST_REMOVE_CURRENT(&devstate_provs, list);
+                       AST_RWLIST_REMOVE_CURRENT(&devstate_provs, list);
                        free(devcb);
                        break;
                }
        }
-       AST_LIST_TRAVERSE_SAFE_END;
-       AST_LIST_UNLOCK(&devstate_provs);
+       AST_RWLIST_TRAVERSE_SAFE_END;
+       AST_RWLIST_UNLOCK(&devstate_provs);
 }
 
 /*! \brief Get provider device state */
@@ -308,8 +308,8 @@ static int getproviderstate(const char *provider, const char *address)
        int res = AST_DEVICE_INVALID;
 
 
-       AST_LIST_LOCK(&devstate_provs);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devprov, list) {
+       AST_RWLIST_RDLOCK(&devstate_provs);
+       AST_RWLIST_TRAVERSE(&devstate_provs, devprov, list) {
                if (option_debug > 4)
                        ast_log(LOG_DEBUG, "Checking provider %s with %s\n", devprov->label, provider);
 
@@ -318,8 +318,7 @@ static int getproviderstate(const char *provider, const char *address)
                        break;
                }
        }
-       AST_LIST_TRAVERSE_SAFE_END;
-       AST_LIST_UNLOCK(&devstate_provs);
+       AST_RWLIST_UNLOCK(&devstate_provs);
        return res;
 }
 
@@ -334,9 +333,9 @@ int ast_devstate_add(ast_devstate_cb_type callback, void *data)
        devcb->data = data;
        devcb->callback = callback;
 
-       AST_LIST_LOCK(&devstate_cbs);
-       AST_LIST_INSERT_HEAD(&devstate_cbs, devcb, list);
-       AST_LIST_UNLOCK(&devstate_cbs);
+       AST_RWLIST_WRLOCK(&devstate_cbs);
+       AST_RWLIST_INSERT_HEAD(&devstate_cbs, devcb, list);
+       AST_RWLIST_UNLOCK(&devstate_cbs);
 
        return 0;
 }
@@ -346,16 +345,16 @@ void ast_devstate_del(ast_devstate_cb_type callback, void *data)
 {
        struct devstate_cb *devcb;
 
-       AST_LIST_LOCK(&devstate_cbs);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_cbs, devcb, list) {
+       AST_RWLIST_WRLOCK(&devstate_cbs);
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_cbs, devcb, list) {
                if ((devcb->callback == callback) && (devcb->data == data)) {
-                       AST_LIST_REMOVE_CURRENT(&devstate_cbs, list);
+                       AST_RWLIST_REMOVE_CURRENT(&devstate_cbs, list);
                        free(devcb);
                        break;
                }
        }
-       AST_LIST_TRAVERSE_SAFE_END;
-       AST_LIST_UNLOCK(&devstate_cbs);
+       AST_RWLIST_TRAVERSE_SAFE_END;
+       AST_RWLIST_UNLOCK(&devstate_cbs);
 }
 
 /*! \brief Notify callback watchers of change, and notify PBX core for hint updates
@@ -370,10 +369,10 @@ static void do_state_change(const char *device)
        if (option_debug > 2)
                ast_log(LOG_DEBUG, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));
 
-       AST_LIST_LOCK(&devstate_cbs);
-       AST_LIST_TRAVERSE(&devstate_cbs, devcb, list)
+       AST_RWLIST_RDLOCK(&devstate_cbs);
+       AST_RWLIST_TRAVERSE(&devstate_cbs, devcb, list)
                devcb->callback(device, state, devcb->data);
-       AST_LIST_UNLOCK(&devstate_cbs);
+       AST_RWLIST_UNLOCK(&devstate_cbs);
 
        ast_hint_state_changed(device);
 }