From 029675b432cb455e466bca723db0dc90fdbf54b5 Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Tue, 18 May 2010 12:19:12 +1000 Subject: [PATCH] Remove ctdb_set_callback() from the public API and provide the callback as part of the *_send() signature. --- client/ctdb_client.c | 2 +- include/ctdb.h | 40 +++++++++------------------------------- libctdb/libctdb.c | 25 +++++++++++++++++++------ libctdb/tst.c | 19 ++++--------------- 4 files changed, 33 insertions(+), 53 deletions(-) diff --git a/client/ctdb_client.c b/client/ctdb_client.c index 6a195102..4d492939 100644 --- a/client/ctdb_client.c +++ b/client/ctdb_client.c @@ -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; diff --git a/include/ctdb.h b/include/ctdb.h index 86886e5c..6e07a105 100644 --- a/include/ctdb.h +++ b/include/ctdb.h @@ -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 *); diff --git a/libctdb/libctdb.c b/libctdb/libctdb.c index e4a5ea6e..9fe55a9b 100644 --- a/libctdb/libctdb.c +++ b/libctdb/libctdb.c @@ -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; diff --git a/libctdb/tst.c b/libctdb/tst.c index d3db1330..ca968a4f 100644 --- a/libctdb/tst.c +++ b/libctdb/tst.c @@ -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); -- 2.34.1