/** * Internally used helper function that creates a status updater thread which * will call the passed callback function when status updates occur. * If status_cb is NULL no thread will be created, but the operation will * run synchronously until it completes or an error occurs. * * @param client The connected installation proxy client * @param status_cb Pointer to a callback function or NULL * @param operation Operation name. Will be passed to the callback function * in async mode or shown in debug messages in sync mode. * @param user_data Callback data passed to status_cb. * * @return INSTPROXY_E_SUCCESS when the thread was created (async mode), or * when the operation completed successfully (sync). * An INSTPROXY_E_* error value is returned if an error occured. */ static instproxy_error_t instproxy_create_status_updater(instproxy_client_t client, instproxy_status_cb_t status_cb, const char *operation, void *user_data) { instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR; if (status_cb) { /* async mode */ struct instproxy_status_data *data = (struct instproxy_status_data*)malloc(sizeof(struct instproxy_status_data)); if (data) { data->client = client; data->cbfunc = status_cb; data->operation = strdup(operation); data->user_data = user_data; #ifdef WIN32 client->status_updater = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)instproxy_status_updater, data, 0, NULL); if (client->status_updater != INVALID_HANDLE_VALUE) { res = INSTPROXY_E_SUCCESS; } else { client->status_updater = NULL; } #else if (pthread_create(&client->status_updater, NULL, instproxy_status_updater, data) == 0) { res = INSTPROXY_E_SUCCESS; } #endif } } else { /* sync mode */ res = instproxy_perform_operation(client, NULL, operation, NULL); } return res; }
/** * Internally used status updater thread function that will call the specified * callback function when status update messages (or error messages) are * received. * * @param arg Pointer to an allocated struct instproxy_status_data that holds * the required data about the connected client and the callback function. * * @return Always NULL. */ static void* instproxy_status_updater(void* arg) { struct instproxy_status_data *data = (struct instproxy_status_data*)arg; /* run until the operation is complete or an error occurs */ (void)instproxy_perform_operation(data->client, data->cbfunc, data->operation, data->user_data); /* cleanup */ instproxy_lock(data->client); debug_info("done, cleaning up."); if (data->operation) { free(data->operation); } data->client->status_updater = (thread_t)NULL; instproxy_unlock(data->client); free(data); return NULL; }
/** * Internally used helper function that creates a status updater thread which * will call the passed callback function when status updates occur. * If status_cb is NULL no thread will be created, but the operation will * run synchronously until it completes or an error occurs. * * @param client The connected installation proxy client * @param status_cb Pointer to a callback function or NULL * @param operation Operation name. Will be passed to the callback function * in async mode or shown in debug messages in sync mode. * @param user_data Callback data passed to status_cb. * * @return INSTPROXY_E_SUCCESS when the thread was created (async mode), or * when the operation completed successfully (sync). * An INSTPROXY_E_* error value is returned if an error occured. */ static instproxy_error_t instproxy_create_status_updater(instproxy_client_t client, instproxy_status_cb_t status_cb, const char *operation, void *user_data) { instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR; if (status_cb) { /* async mode */ struct instproxy_status_data *data = (struct instproxy_status_data*)malloc(sizeof(struct instproxy_status_data)); if (data) { data->client = client; data->cbfunc = status_cb; data->operation = strdup(operation); data->user_data = user_data; if (thread_create(&client->status_updater, instproxy_status_updater, data) == 0) { res = INSTPROXY_E_SUCCESS; } } } else { /* sync mode */ res = instproxy_perform_operation(client, NULL, operation, NULL); } return res; }