/** * gda_worker_fetch_job_result: (skip) * @worker: a #GdaWorker object * @job_id: the ID of the job, as returned by gda_worker_submit_job() * @out_result: (allow-none): a place to store the value returned by the execution of the requested function within the worker thread, or %NULL * @error: (allow-none): a place to store errors, or %NULL * * Fetch the value returned by execution the @job_id job. * * Warning: if an error occurred during the * execution of the requested function within the worker thread, then it will show as @error, while the return value * of this function will be %TRUE. * * Note: if there is a result, it will be stored in @out_result, and it's up to the caller to free * the result, the #GdaWorker object will not do it (ownership of the result is transfered to the caller). * * Returns: %TRUE if the jobs has completed * * Since: 6.0 */ gboolean gda_worker_fetch_job_result (GdaWorker *worker, guint job_id, gpointer *out_result, GError **error) { g_return_val_if_fail (worker, FALSE); if (!worker->jobs_hash) { g_warning ("GdaWorker has been destroyed\n"); return FALSE; } gda_worker_ref (worker); /* increase reference count to having @worker freed while destroying job */ if (out_result) *out_result = NULL; g_rec_mutex_lock (&worker->rmutex); WorkerJob *job; job = g_hash_table_lookup (worker->jobs_hash, &job_id); if (!job) { g_rec_mutex_unlock (& worker->rmutex); g_set_error (error, GDA_WORKER_ERROR, GDA_WORKER_JOB_NOT_FOUND_ERROR, _("Unknown requested job %u"), job_id); gda_worker_unref (worker); return FALSE; } gboolean retval = FALSE; if (job->status & JOB_CANCELLED) g_set_error (error, GDA_WORKER_ERROR, GDA_WORKER_JOB_CANCELLED_ERROR, _("Job %u has been cancelled"), job_id); else if (job->status & JOB_PROCESSED) { retval = TRUE; if (out_result) { *out_result = job->result; job->result = NULL; } if (job->error) { g_propagate_error (error, job->error); job->error = NULL; } g_hash_table_remove (worker->jobs_hash, &job_id); } else if (job->status & JOB_BEING_PROCESSED) g_set_error (error, GDA_WORKER_ERROR, GDA_WORKER_JOB_BEING_PROCESSED_ERROR, _("Job %u is being processed"), job_id); else g_set_error (error, GDA_WORKER_ERROR, GDA_WORKER_JOB_QUEUED_ERROR, _("Job %u has not yet been processed"), job_id); g_rec_mutex_unlock (&worker->rmutex); gda_worker_unref (worker); return retval; }
/* * Reopens a connection after the server has closed it (possibly because of a timeout) */ gboolean gda_ldap_rebind (GdaLdapConnection *cnc, GError **error) { g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE); gda_lockable_lock ((GdaLockable*) cnc); /* CNC LOCK */ LdapConnectionData *cdata; cdata = (LdapConnectionData*) gda_virtual_connection_internal_get_provider_data (GDA_VIRTUAL_CONNECTION (cnc)); if (!cdata) { gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ g_warning ("cdata != NULL failed"); return FALSE; } GdaServerProviderConnectionData *pcdata; pcdata = gda_connection_internal_get_provider_data_error ((GdaConnection*) cnc, NULL); GdaWorker *worker; worker = gda_worker_ref (gda_connection_internal_get_worker (pcdata)); GMainContext *context; context = gda_server_provider_get_real_main_context ((GdaConnection *) cnc); gpointer retval; gda_worker_do_job (worker, context, 0, &retval, NULL, (GdaWorkerFunc) worker_gda_ldap_rebind, (gpointer) cdata, NULL, NULL, error); if (context) g_main_context_unref (context); gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ gda_worker_unref (worker); return retval ? TRUE : FALSE; }
int test10 (void) { g_print ("%s started, main thread is %p\n", __FUNCTION__, g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); GError *error = NULL; g_print ("Waiting for test10_func() to execute\n"); if (gda_worker_wait_job (worker, (GdaWorkerFunc) test10_func_slow, NULL, NULL, &error) != (gpointer) 0x01) { g_print ("gda_worker_wait_job() failed: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed ++; } else { if (gda_worker_wait_job (worker, (GdaWorkerFunc) test10_func_fast, NULL, NULL, &error) != (gpointer) 0x02) { g_print ("gda_worker_wait_job() failed: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed ++; } } g_print ("Unref worker...\n"); gda_worker_unref (worker); g_print ("%s done\n", __FUNCTION__); return nfailed; }
int test9 (void) { g_print ("%s started, main thread is %p\n", __FUNCTION__, g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); #define NB_THREADS 2 GThread *threads[NB_THREADS]; guint i; for (i = 0; i < NB_THREADS; i++) { gchar *name; name = g_strdup_printf ("th%u", i); threads[i] = g_thread_new (name, (GThreadFunc) test9_main_func, worker); g_free (name); } for (i = 0; i < NB_THREADS; i++) { if (g_thread_join (threads[i])) nfailed++; } g_print ("Unref worker...\n"); gda_worker_unref (worker); g_print ("%s done\n", __FUNCTION__); return nfailed; }
int test13 (void) { g_print ("%s started, main thread is %p\n", __FUNCTION__, g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); GError *error = NULL; g_timeout_add (200, (GSourceFunc) test13_time_func, gda_worker_ref (worker)); g_print ("Sumbitting job for test13_func()\n"); gpointer result; if (!gda_worker_do_job (worker, NULL, 0, &result, NULL, (GdaWorkerFunc) test13_func, "1st", NULL, NULL, &error)) { g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed ++; } else if (GPOINTER_TO_UINT (result) != 2) { g_print ("In %s(): expected 2 and got %u\n", __FUNCTION__, GPOINTER_TO_UINT (result)); nfailed ++; } else if (test13_sub_failed) nfailed ++; g_print ("Unref worker...\n"); gda_worker_unref (worker); g_print ("%s done\n", __FUNCTION__); return nfailed; }
int test12 (void) { g_print ("%s started, main thread is %p\n", __FUNCTION__, g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); GError *error = NULL; g_print ("Waiting for test12_func() to execute\n"); gpointer result; if (!gda_worker_do_job (worker, NULL, 0, &result, NULL, (GdaWorkerFunc) test12_func, NULL, NULL, NULL, &error) || (result != (gpointer) 0x02)) { g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed ++; } g_print ("Unref worker...\n"); gda_worker_unref (worker); g_print ("%s done\n", __FUNCTION__); return nfailed; }
/* * Unbinds the connection if possible (i.e. if cdata->keep_bound_count is 0) * This allows to avoid keeping the connection to the LDAP server if unused */ void gda_ldap_may_unbind (GdaLdapConnection *cnc) { gda_lockable_lock ((GdaLockable*) cnc); /* CNC LOCK */ LdapConnectionData *cdata; cdata = (LdapConnectionData*) gda_virtual_connection_internal_get_provider_data (GDA_VIRTUAL_CONNECTION (cnc)); if (!cdata || (cdata->keep_bound_count > 0)) { gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ return; } GdaServerProviderConnectionData *pcdata; pcdata = gda_connection_internal_get_provider_data_error ((GdaConnection*) cnc, NULL); GdaWorker *worker; worker = gda_worker_ref (gda_connection_internal_get_worker (pcdata)); GMainContext *context; context = gda_server_provider_get_real_main_context ((GdaConnection *) cnc); gpointer retval; gda_worker_do_job (worker, context, 0, &retval, NULL, (GdaWorkerFunc) worker_gda_ldap_may_unbind, (gpointer) cdata, NULL, NULL, NULL); if (context) g_main_context_unref (context); gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ gda_worker_unref (worker); }
int test5 (void) { g_print ("Test5 started, main thread is %p\n", g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); GError *error = NULL; GMainLoop *loop; loop = g_main_loop_new (NULL, FALSE); if (! gda_worker_set_callback (worker, NULL, (GdaWorkerCallback) test5_worker_callback, loop, &error)) { g_print ("gda_worker_set_callback() error: %s\n", error && error->message ? error->message : "no detail"); nfailed++; } else { g_assert (g_idle_add ((GSourceFunc) test5_idle_push_jobs, worker) != 0); } g_main_loop_run (loop); g_main_loop_unref (loop); gda_worker_unref (worker); g_print ("Test 5 done\n"); return nfailed; }
/* * Test 1: so basic */ int test1 (void) { g_print ("Test1 started\n"); GdaWorker *worker; worker = gda_worker_new (); gda_worker_unref (worker); return 0; }
int test3 (void) { g_print ("Test3 started, main thread is %p\n", g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); guint jid; GError *error = NULL; Data3 *data; data = g_new (Data3, 1); data->counter = 5; jid = gda_worker_submit_job (worker, NULL, (GdaWorkerFunc) test3_worker_func, data, (GDestroyNotify) data3_free, NULL, &error); if (jid == 0) { g_print ("Error in gda_worker_submit_job(): %s\n", error && error->message ? error->message : "no detail"); g_clear_error (&error); nfailed++; goto out; } while (1) { gint *result; if (! gda_worker_fetch_job_result (worker, jid, (gpointer*) &result, &error)) { g_print ("Still not done, error: %s\n", error && error->message ? error->message : "no detail"); if ((error->domain == GDA_WORKER_ERROR) && (error->code == GDA_WORKER_JOB_NOT_FOUND_ERROR)) { nfailed++; g_clear_error (&error); break; } g_clear_error (&error); } else { if (result) { g_print ("Got result value: %d\n", *result); g_free (result); } else { g_print ("Error: got no result value!\n"); nfailed++; } break; } g_usleep (100000); } out: gda_worker_unref (worker); return nfailed; }
int test6 (void) { g_print ("%s started, main thread is %p\n", __FUNCTION__, g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); GError *error = NULL; guint nticks = 0; guint timer; timer = g_timeout_add (10, (GSourceFunc) test6_timer_cb, &nticks); guint jid; gpointer out; gint wait; wait = 50; if (gda_worker_do_job (worker, NULL, 100, &out, &jid, (GdaWorkerFunc) test6_worker_func, (gpointer) &wait, NULL, (GDestroyNotify) test6_string_free, &error)) { if (!out || strcmp (out, "Test6Done")) { g_print ("Expected out to be [Test6Done] and got [%s]\n", (gchar*) out); nfailed++; } g_free (out); if (jid != 0) { g_print ("Expected JID to be 0 and got %u\n", jid); nfailed++; } } else { g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed ++; } g_source_remove (timer); if (nticks < 3) { g_print ("Tick timer was not called while in gda_worker_do_job()\n"); nfailed++; } g_print ("Unref worker...\n"); gda_worker_unref (worker); g_print ("%s done\n", __FUNCTION__); return nfailed; }
/** * gdaprov_ldap_get_class_info: * @cnc: a #GdaLdapConnection (not %NULL) * @classname: the class name (not %NULL) * * Returns: the #GdaLdapClass for @classname, or %NULL */ GdaLdapClass * gdaprov_ldap_get_class_info (GdaLdapConnection *cnc, const gchar *classname) { g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), NULL); g_return_val_if_fail (classname, NULL); gda_lockable_lock ((GdaLockable*) cnc); /* CNC LOCK */ LdapConnectionData *cdata; cdata = (LdapConnectionData*) gda_virtual_connection_internal_get_provider_data (GDA_VIRTUAL_CONNECTION (cnc)); if (!cdata) { gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ return NULL; } if (cdata->classes_hash) { gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ return g_hash_table_lookup (cdata->classes_hash, classname); } GdaServerProviderConnectionData *pcdata; pcdata = gda_connection_internal_get_provider_data_error ((GdaConnection*) cnc, NULL); GdaWorker *worker; worker = gda_worker_ref (gda_connection_internal_get_worker (pcdata)); GMainContext *context; context = gda_server_provider_get_real_main_context ((GdaConnection *) cnc); WorkerLdapClassInfoData data; data.cnc = cnc; data.cdata = cdata; data.classname = classname; gda_connection_increase_usage ((GdaConnection*) cnc); /* USAGE ++ */ gpointer retval; gda_worker_do_job (worker, context, 0, &retval, NULL, (GdaWorkerFunc) worker_gdaprov_ldap_get_class_info, (gpointer) &data, NULL, NULL, NULL); if (context) g_main_context_unref (context); gda_connection_decrease_usage ((GdaConnection*) cnc); /* USAGE -- */ gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ gda_worker_unref (worker); return (GdaLdapClass*) retval; }
int test8 (void) { g_print ("%s started, main thread is %p\n", __FUNCTION__, g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); GError *error = NULL; guint jid; jid = gda_worker_submit_job (worker, NULL, (GdaWorkerFunc) test8_func, worker, NULL, NULL, &error); if (jid == 0) { g_print ("gda_worker_submit_job() failed: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed ++; } else { while (1) { gpointer out; g_print ("Waiting for result using gda_worker_fetch_job_result()\n"); if (gda_worker_fetch_job_result (worker, jid, &out, &error)) { if (!out || strcmp (out, "test8_sub_func")) { g_print ("Expected out to be [test8_sub_func] and got %s\n", (gchar*) out); nfailed++; } if (error) { g_print ("Got error: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed++; } break; } else g_clear_error (&error); g_usleep (100000); } } g_print ("Unref worker...\n"); gda_worker_unref (worker); g_print ("%s done\n", __FUNCTION__); return nfailed; }
GdaLdapEntry * gdaprov_ldap_describe_entry (GdaLdapConnection *cnc, const gchar *dn, GError **error) { g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), NULL); g_return_val_if_fail (!dn || (dn && *dn), NULL); gda_lockable_lock ((GdaLockable*) cnc); /* CNC LOCK */ LdapConnectionData *cdata; cdata = (LdapConnectionData*) gda_virtual_connection_internal_get_provider_data (GDA_VIRTUAL_CONNECTION (cnc)); if (!cdata) { gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ return NULL; } GdaServerProviderConnectionData *pcdata; pcdata = gda_connection_internal_get_provider_data_error ((GdaConnection*) cnc, NULL); GdaWorker *worker; worker = gda_worker_ref (gda_connection_internal_get_worker (pcdata)); GMainContext *context; context = gda_server_provider_get_real_main_context ((GdaConnection *) cnc); WorkerLdapDescrEntryData data; data.cnc = cnc; data.cdata = cdata; data.dn = dn; gda_connection_increase_usage ((GdaConnection*) cnc); /* USAGE ++ */ gpointer retval; gda_worker_do_job (worker, context, 0, &retval, NULL, (GdaWorkerFunc) worker_gdaprov_ldap_describe_entry, (gpointer) &data, NULL, NULL, error); if (context) g_main_context_unref (context); gda_connection_decrease_usage ((GdaConnection*) cnc); /* USAGE -- */ gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ gda_worker_unref (worker); return (GdaLdapEntry*) retval; }
int test11 (void) { g_print ("%s started, main thread is %p\n", __FUNCTION__, g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); GError *error = NULL; g_print ("Waiting for test11_func() to execute\n"); gpointer result; if (!gda_worker_do_job (worker, NULL, 0, &result, NULL, (GdaWorkerFunc) test11_func, NULL, NULL, NULL, &error) || (result != (gpointer) 0x01)) { g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed ++; } if (!gda_worker_do_job (worker, NULL, 0, &result, NULL, (GdaWorkerFunc) test11_func_e, NULL, NULL, NULL, &error) || result) { g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed ++; } else if (!error || (error->domain != 1) || (error->code != 1)) { g_print ("gda_worker_do_job() failed: unexpected empty error, or wrong error domain or code%s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); nfailed ++; } g_print ("Unref worker...\n"); gda_worker_unref (worker); g_print ("%s done\n", __FUNCTION__); return nfailed; }
gboolean test13_time_func (GdaWorker *worker) { /* this time we should get 3, because the previous call to test13_func() should have returned 2 */ g_print ("Sumbitting another job for test13_func()\n"); gpointer result; GError *error = NULL; if (!gda_worker_do_job (worker, NULL, 0, &result, NULL, (GdaWorkerFunc) test13_func, "2nd", NULL, NULL, &error)) { g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No detail"); g_clear_error (&error); test13_sub_failed = TRUE; } else if (GPOINTER_TO_UINT (result) != 3) { g_print ("In %s(): expected 3 and got %u\n", __FUNCTION__, GPOINTER_TO_UINT (result)); test13_sub_failed = TRUE; } gda_worker_unref (worker); return G_SOURCE_REMOVE; }
/** * gda_ldap_get_attr_info: * @cnc: a #GdaLdapConnection * @cdata: * @attribute: * * Returns: (transfer none): the #LdapAttribute for @attribute, or %NULL */ LdapAttribute * gda_ldap_get_attr_info (GdaLdapConnection *cnc, LdapConnectionData *cdata, const gchar *attribute) { g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL); if (! attribute || !cdata) return NULL; gda_lockable_lock ((GdaLockable*) cnc); /* CNC LOCK */ GdaServerProviderConnectionData *pcdata; pcdata = gda_connection_internal_get_provider_data_error ((GdaConnection*) cnc, NULL); GdaWorker *worker; worker = gda_worker_ref (gda_connection_internal_get_worker (pcdata)); GMainContext *context; context = gda_server_provider_get_real_main_context ((GdaConnection *) cnc); WorkerLdapAttrInfoData data; data.cnc = cnc; data.cdata = cdata; data.attribute = attribute; gda_connection_increase_usage ((GdaConnection*) cnc); /* USAGE ++ */ gpointer retval; gda_worker_do_job (worker, context, 0, &retval, NULL, (GdaWorkerFunc) worker_gda_ldap_get_attr_info, (gpointer) &data, NULL, NULL, NULL); if (context) g_main_context_unref (context); gda_connection_decrease_usage ((GdaConnection*) cnc); /* USAGE -- */ gda_lockable_unlock ((GdaLockable*) cnc); /* CNC UNLOCK */ gda_worker_unref (worker); return (LdapAttribute*) retval; }
int test4 (void) { g_print ("Test4 started, main thread is %p\n", g_thread_self ()); GdaWorker *worker; gint nfailed = 0; worker = gda_worker_new (); guint jid1, jid2; GError *error = NULL; jid1 = gda_worker_submit_job (worker, NULL, (GdaWorkerFunc) test4_worker_func, NULL, NULL, NULL, &error); if (jid1 == 0) { g_print ("Error in gda_worker_submit_job(): %s\n", error && error->message ? error->message : "no detail"); g_clear_error (&error); nfailed++; goto out; } jid2 = gda_worker_submit_job (worker, NULL, (GdaWorkerFunc) test4_worker_func, NULL, NULL, NULL, &error); if (jid2 == 0) { g_print ("Error in gda_worker_submit_job(): %s\n", error && error->message ? error->message : "no detail"); g_clear_error (&error); nfailed++; goto out; } if (! gda_worker_cancel_job (worker, jid2, &error)) { g_print ("Error in gda_worker_cancel_job(): %s\n", error && error->message ? error->message : "no detail"); g_clear_error (&error); nfailed++; goto out; } if (! gda_worker_cancel_job (worker, jid2, &error)) { g_print ("Error in gda_worker_cancel_job(): %s\n", error && error->message ? error->message : "no detail"); g_clear_error (&error); nfailed++; goto out; } if (gda_worker_cancel_job (worker, 10, NULL)) { g_print ("Error in gda_worker_cancel_job(): should have failed!\n"); nfailed++; goto out; } while (1) { gint *result; if (! gda_worker_fetch_job_result (worker, jid1, (gpointer*) &result, &error)) { g_print ("Still not done, error: %s\n", error && error->message ? error->message : "no detail"); if ((error->domain == GDA_WORKER_ERROR) && (error->code == GDA_WORKER_JOB_NOT_FOUND_ERROR)) { nfailed++; g_clear_error (&error); break; } g_clear_error (&error); } else { if (result) { g_print ("Error: got result value when expected none!\n"); nfailed++; } break; } g_usleep (100000); } out: gda_worker_unref (worker); return nfailed; }