Remove ctdb_set_callback() from the public API and provide the callback as part
authorRonnie Sahlberg <ronniesahlberg@gmail.com>
Tue, 18 May 2010 02:19:12 +0000 (12:19 +1000)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Tue, 18 May 2010 02:19:12 +0000 (12:19 +1000)
of the *_send() signature.

client/ctdb_client.c
include/ctdb.h
libctdb/libctdb.c
libctdb/tst.c

index 6a19510203fa3fb470cb84a3b73e52e806524e16..4d492939d90e1c2d3c128e83a16b945e75659344 100644 (file)
@@ -1112,7 +1112,7 @@ int ctdb_ctrl_getpnn(struct ctdb_context *ctdb, struct timeval timeout, uint32_t
        uint32_t pnn;
        ctdb_handle *handle;
 
-       handle = ctdb_getpnn_send(ctdb, destnode);
+       handle = ctdb_getpnn_send(ctdb, destnode, NULL, NULL);
        if (handle == NULL) {
                DEBUG(DEBUG_ERR, (__location__ " Failed to send getpnn control\n"));
                return -1;
index 86886e5c9018611ad004a629ffaa4de84ee50e7d..6e07a105129a76c44ba2917188c158eee456c438 100644 (file)
@@ -35,7 +35,7 @@
  *    The exception is when called from in the registered callback,
  *    in this case the fucntion is guaranteed not to block.
  *
- * 2, Registering an async callback to be invoked when the call completes.
+ * 2, providing an async callback to be invoked when the call completes.
  *    From inside the callback you use the *_recv() function to extract the
  *    response data.
  *
@@ -77,31 +77,7 @@ int ctdb_service(struct ctdb_context *ctdb);
 typedef void ctdb_handle;
 
 
-/*
- * After issuing a *_send() command, you can use this function to register a
- * a callback function to be automatically called once the call
- * finishes.
- *
- * Once the callback function returns, the handle will be automatically
- * destroyed.
- *
- * If using ctdb_free() to abort a call in flight, you have to take care
- * to avoid the race condition that would exist between the callback and
- * ctdb_free().
- *
- * Possible method could be :
- * * take pthreads mutex
- * * ctdb_set_callback(handle, NULL, NULL)
- * * verify that the callback has not yet been called
- *     (if it has handle is no longer valid)
- * * ctdb_free(handle)
- * * release pthreads mutex
- */
-typedef void (*ctdb_callback)(int32_t status, struct ctdb_context *ctdb, ctdb_handle *, void *private_data);
-
-int ctdb_set_callback(ctdb_handle *handle, ctdb_callback callback, void *private_data);
-
-
+typedef void (*ctdb_generic_callback)(int32_t status, struct ctdb_context *ctdb, ctdb_handle *, void *private_data);
 
 
 /*
@@ -224,7 +200,9 @@ int ctdb_send_message(struct ctdb_context *ctdb, uint32_t pnn, uint64_t srvid, T
  */
 ctdb_handle *
 ctdb_getpnn_send(struct ctdb_context *ctdb,
-                uint32_t destnode);
+                uint32_t destnode,
+                ctdb_generic_callback callback,
+                void *private_data);
 int ctdb_getpnn_recv(struct ctdb_context *ctdb,
                     ctdb_handle *handle,
                     uint32_t *pnn);
@@ -238,11 +216,11 @@ int ctdb_getpnn(struct ctdb_context *ctdb,
 /*
  * functions to read the recovery master of a node
  */
-typedef void (*ctdb_getrecmaster_cb)(int32_t status, int32_t recmaster, void *private_data);
-
 ctdb_handle *
 ctdb_getrecmaster_send(struct ctdb_context *ctdb,
-                      uint32_t destnode);
+                       uint32_t destnode,
+                       ctdb_generic_callback callback,
+                       void *private_data);
 int ctdb_getrecmaster_recv(struct ctdb_context *ctdb,
                        ctdb_handle *handle,
                        uint32_t *recmaster);
@@ -254,7 +232,7 @@ int ctdb_getrecmaster(struct ctdb_context *ctdb,
 
 
 /*
- * cancel a request/call
+ * cancel a request/call or release a resource
  */
 int ctdb_free(ctdb_handle *);
 
index e4a5ea6e6d06fa7eedd2b159bcf41cc48cae8a65..9fe55a9b50602b8650f435150af333657fecc527 100644 (file)
@@ -45,7 +45,7 @@ static void
 ctdb_control_cb(struct ctdb_client_control_state *state)
 {
        struct ctdb_control_cb_data *cb_data = state->async.private_data;
-       ctdb_callback callback = (ctdb_callback)cb_data->callback;
+       ctdb_generic_callback callback = (ctdb_generic_callback)cb_data->callback;
 
        /* dont recurse */
        state->async.fn = NULL;
@@ -63,7 +63,8 @@ ctdb_control_cb(struct ctdb_client_control_state *state)
 /*
  * This function is used to set the callback action for a handle
  */
-int ctdb_set_callback(ctdb_handle *handle, ctdb_callback callback, void *private_data)
+static int
+ctdb_set_generic_callback(ctdb_handle *handle, ctdb_generic_callback callback, void *private_data)
 {
        struct ctdb_client_control_state *control_state = talloc_get_type(handle, struct ctdb_client_control_state);
 
@@ -240,7 +241,9 @@ int ctdb_free(ctdb_handle *handle)
  *************************/
 ctdb_handle *
 ctdb_getpnn_send(struct ctdb_context *ctdb,
-                       uint32_t destnode)
+                       uint32_t destnode,
+                       ctdb_generic_callback callback,
+                       void *private_data)
 {
        struct ctdb_client_control_state *state;
 
@@ -253,6 +256,10 @@ ctdb_getpnn_send(struct ctdb_context *ctdb,
                return NULL;
        }
 
+       if (callback != NULL) {
+               ctdb_set_generic_callback(state, callback, private_data);
+       }
+
        return (ctdb_handle *)state;
 }
 
@@ -278,7 +285,7 @@ int ctdb_getpnn(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *pnn)
 {
        struct ctdb_client_control_state *state;
        
-       state = ctdb_getpnn_send(ctdb, destnode);
+       state = ctdb_getpnn_send(ctdb, destnode, NULL, NULL);
        if (state == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " ctdb_getpnn_send() failed.\n"));
                return -1;
@@ -294,7 +301,9 @@ int ctdb_getpnn(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *pnn)
  ***********************/
 ctdb_handle *
 ctdb_getrecmaster_send(struct ctdb_context *ctdb,
-                       uint32_t destnode)
+                       uint32_t destnode,
+                       ctdb_generic_callback callback,
+                       void *private_data)
 {
        struct ctdb_client_control_state *state;
 
@@ -307,6 +316,10 @@ ctdb_getrecmaster_send(struct ctdb_context *ctdb,
                return NULL;
        }
 
+       if (callback != NULL) {
+               ctdb_set_generic_callback(state, callback, private_data);
+       }
+
        return (ctdb_handle *)state;
 }
 
@@ -332,7 +345,7 @@ int ctdb_getrecmaster(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *re
 {
        struct ctdb_client_control_state *state;
        
-       state = ctdb_getrecmaster_send(ctdb, destnode);
+       state = ctdb_getrecmaster_send(ctdb, destnode, NULL, NULL);
        if (state == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " ctdb_getrecmaster_send() failed.\n"));
                return -1;
index d3db13302704bf9ea13958fcf1d8388c9dc01e19..ca968a4f1bec73780e61bd0957e0cce50f902e0d 100644 (file)
@@ -146,24 +146,18 @@ int main(int argc, char *argv[])
         * ASYNC call with callback to read the recmaster
         * this is the preferred way to use libctdb
         */
-       handle = ctdb_getrecmaster_send(ctdb, CTDB_CURRENT_NODE);
+       handle = ctdb_getrecmaster_send(ctdb, CTDB_CURRENT_NODE, rm_cb, NULL);
        if (handle == NULL) {
                printf("Failed to send get_recmaster control\n");
                exit(10);
        }
-       ret = ctdb_set_callback(handle, rm_cb, NULL);
-       if (ret != 0) {
-               printf("Failed to set callback for get_recmaster\n");
-               ctdb_free(handle);
-               exit(10);
-       }
 
        /*
         * SEMI-SYNC call with callback to read the recmaster
         * calls the blocking *_recv() function.
         * Avoid this mode for performance critical tasks
         */
-       handle = ctdb_getrecmaster_send(ctdb, CTDB_CURRENT_NODE);
+       handle = ctdb_getrecmaster_send(ctdb, CTDB_CURRENT_NODE, NULL, NULL);
        if (handle == NULL) {
                printf("Failed to send get_recmaster control\n");
                exit(10);
@@ -192,17 +186,12 @@ int main(int argc, char *argv[])
 
 
 
-       handle = ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE);
+       handle = ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, pnn_cb, NULL);
        if (handle == NULL) {
                printf("Failed to send get_pnn control\n");
                exit(10);
        }
-       ret = ctdb_set_callback(handle, pnn_cb, NULL);
-       if (ret != 0) {
-               printf("Failed to set callback for getpnn\n");
-               ctdb_free(handle);
-               exit(10);
-       }
+
 
 
        handle = ctdb_readrecordlock_send(ctdb, ctdb_db_context, key, rrl_cb, NULL);