int test2 (void) { g_print ("Test2 started\n"); ITSignaler *its; its = itsignaler_new (); GTimer *timer; timer = g_timer_new (); GMainLoop *loop; loop = g_main_loop_new (NULL, FALSE); g_mutex_lock (&mutex); CbData cbdata; cbdata.counter = 0; cbdata.loop = loop; itsignaler_add (its, NULL, (ITSignalerFunc) source_callback, &cbdata, NULL); itsignaler_unref (its); g_mutex_unlock (&mutex); GThread *th; th = g_thread_new ("sub", (GThreadFunc) thread1_start, its); g_main_loop_run (loop); g_main_loop_unref (loop); g_timer_stop (timer); g_thread_join (th); if (cbdata.counter == MAX_ITERATIONS * 2) { gdouble duration; duration = g_timer_elapsed (timer, NULL); g_print ("Test Ok, %0.5f s\n", duration); g_timer_destroy (timer); return 0; } else { g_print ("Test Failed: got %d notification(s) out of %d\n", cbdata.counter, MAX_ITERATIONS * 2); g_timer_destroy (timer); return 1; } }
/** * gda_worker_do_job: (skip) * @worker: a #GdaWorker object * @context: (allow-none): a #GMainContext to execute a main loop in (while waiting), or %NULL * @timeout_ms: the maximum number of milisecons to wait before returning, or %0 for unlimited wait * @out_result: (allow-none): a place to store the result, if any, of @func's execution, or %NULL * @out_job_id: (allow-none): a place to store the ID of the job having been submitted, or %NULL * @func: the function to call from the worker thread * @data: (allow-none): the data to pass to @func, or %NULL * @data_destroy_func: (allow-none): a function to destroy @data, or %NULL * @result_destroy_func: (allow-none): a function to destroy the result, if any, of @func's execution, or %NULL * @error: (allow-none): a place to store errors, or %NULL. * * Request that the worker thread call @func with the @data argument, much like gda_worker_submit_job(), * but waits (starting a #GMainLoop) for a maximum of @timeout_ms miliseconds for @func to be executed. * * If this function is called from within @worker's worker thread, then this function simply calls @func with @data and does not * use @context. * * The following cases are possible if this function is not called from within @worker's worker thread: * <itemizedlist> * <listitem><para>the call to @func took less than @timeout_ms miliseconds: the return value is %TRUE and * @out_result contains the result of the @func's execution, and @out_job_id contains %NULL. Note in this * case that @error may still contain an error code if @func's execution produced an error. Also note that in this case * any setting defined by gda_worker_set_callback() is not applied (as the result is immediately returned)</para></listitem> * <listitem><para>The call to @func takes more then @timeout_ms miliseconds: the return value is %TRUE and * @out_result is %NULL and @out_job_id contains the ID of the job as if it had been submitted using gda_worker_submit_job(). * If @out_job_id is %NULL, and if no setting has been defined using gda_worker_set_callback(), then the job will be discarded * (as if gda_worker_forget_job() had been called). * </para></listitem> * <listitem><para>The call to @func could not be done (some kind of plumbing error for instance): the returned value is %FALSE * and @out_result and @out_job_id are set to %NULL (if they are not %NULL)</para></listitem> * </itemizedlist> * * Notes: * <itemizedlist> * <listitem><para>@result_destroy_func is needed in case @out_result is %NULL (to avoid memory leaks)</para></listitem> * <listitem><para>passing %NULL for @context is similar to passing the result of g_main_context_ref_thread_default()</para></listitem> * </itemizedlist> * * Returns: %TRUE if no error occurred * * Since: 6.0 */ gboolean gda_worker_do_job (GdaWorker *worker, GMainContext *context, gint timeout_ms, gpointer *out_result, guint *out_job_id, GdaWorkerFunc func, gpointer data, GDestroyNotify data_destroy_func, GDestroyNotify result_destroy_func, GError **error) { if (out_result) *out_result = NULL; if (out_job_id) *out_job_id = 0; g_return_val_if_fail (worker, FALSE); g_return_val_if_fail (func, FALSE); if (!worker->callbacks_hash || !worker->jobs_hash) { g_warning ("GdaWorker has been destroyed\n"); return FALSE; } if (gda_worker_thread_is_worker (worker)) { /* we are called from within the worker thread => call the function directly */ gpointer result; result = func (data, error); if (data_destroy_func) data_destroy_func (data); if (out_result) *out_result = result; else if (result && result_destroy_func) result_destroy_func (result); return TRUE; } guint jid, itsid, timer = 0; /* determine which GMainContext to use */ GMainContext *co; gboolean unref_co = FALSE; co = context; if (!co) { co = g_main_context_ref_thread_default (); unref_co = TRUE; } /* prepare main loop */ GMainLoop *loop; loop = g_main_loop_new (co, FALSE); /* prepare ITSignaler to be notified */ ITSignaler *its; its = itsignaler_new (); itsid = itsignaler_add (its, co, (ITSignalerFunc) do_itsignaler_cb, g_main_loop_ref (loop), (GDestroyNotify) g_main_loop_unref); /* push job */ g_rec_mutex_lock (& worker->rmutex); /* required to call _gda_worker_submit_job_with_its() */ jid = _gda_worker_submit_job_with_its (worker, its, func, data, data_destroy_func, result_destroy_func, error); g_rec_mutex_unlock (& worker->rmutex); if (jid == 0) { /* an error occurred */ g_assert (itsignaler_remove (its, co, itsid)); itsignaler_unref (its); g_main_loop_unref (loop); if (unref_co) g_main_context_unref (co); return FALSE; } /* check if result is already here */ WorkerJob *job; job = itsignaler_pop_notification (its, 0); if (!job) { if (timeout_ms > 0) { /* start timer to limit waiting time */ GSource *timer_src; timer_src = g_timeout_source_new (timeout_ms); g_source_set_callback (timer_src, (GSourceFunc) do_timer_cb, loop, NULL); timer = g_source_attach (timer_src, co); g_source_unref (timer_src); } g_main_loop_run (loop); /* either timer has arrived or job has been done */ job = itsignaler_pop_notification (its, 0); } g_main_loop_unref (loop); g_assert (itsignaler_remove (its, co, itsid)); itsignaler_unref (its); if (job) { /* job done before the timer, if any, elapsed */ if (timer > 0) g_assert (g_source_remove (timer)); g_assert (gda_worker_fetch_job_result (worker, jid, out_result, error)); } else { /* timer came first, job is not yet finished */ /* apply settings from gda_worker_set_callback(), if any */ g_rec_mutex_lock (&worker->rmutex); DeclaredCallback *dc; dc = g_hash_table_lookup (worker->callbacks_hash, co); if (dc) { job = g_hash_table_lookup (worker->jobs_hash, &jid); g_assert (job); g_assert (!job->reply_its); job->reply_its = itsignaler_ref (dc->its); } g_rec_mutex_unlock (& worker->rmutex); /* cleanups */ if (out_job_id) *out_job_id = jid; else if (!dc) /* forget all about the job */ gda_worker_forget_job (worker, jid); } if (unref_co) g_main_context_unref (co); return TRUE; }
/** * gda_worker_set_callback: (skip) * @worker: a #GdaWorker object * @context: (allow-none): a #GMainContext, or %NULL * @callback: (allow-none): the function to call when a job submitted from within the calling thread using gda_worker_submit_job() has finished being processed. * @user_data: argument passed to @callback * @error: (allow-none): a place to store errors, or %NULL * * Declare a callback function to be called when a job has been processed. If @callback is %NULL, then any previously * effect of this function is removed. If the same function is called with a different @callback value, then the previous one * is simply replaced. * * Since this function adds a new source of events to the specified #GMainContext (or the default one if @context is %NULL), * * Notes: * <itemizedlist> * <listitem><para>before calling this function, @worker internally gets rid of the job, so the @jib_id passed * to @callback does not actually designate a known job ID, and so calling gda_worker_fetch_job_result() for that * job ID will fail</para></listitem> * <listitem><para>the job's result, if any, has to be freed by @callback (@worker does not do it)</para></listitem> * <listitem><para>any call to this function will only be honored for the jobs submitted _after_ calling it, the ones * submitted before are not affected</para></listitem> * <listitem><para>passing %NULL for @context is similar to passing the result of g_main_context_ref_thread_default()</para></listitem> * </itemizedlist> * * Returns: %TRUE if no error occurred. * * Since: 6.0 */ gboolean gda_worker_set_callback (GdaWorker *worker, GMainContext *context, GdaWorkerCallback callback, gpointer user_data, GError **error) { g_return_val_if_fail (worker, FALSE); g_return_val_if_fail (callback, FALSE); if (!worker->callbacks_hash) { g_warning ("GdaWorker has been destroyed\n"); return FALSE; } GMainContext *co; gboolean unref_co = FALSE; co = context; if (!co) { co = g_main_context_ref_thread_default (); unref_co = TRUE; } #ifdef DEBUG_NOTIFICATION g_print ("[W] %s() GMainContext=%p, thread=%p\n", __FUNCTION__, co, g_thread_self()); #endif g_rec_mutex_lock (& worker->rmutex); gboolean retval = TRUE; DeclaredCallback *dc = NULL; dc = g_hash_table_lookup (worker->callbacks_hash, co); if (dc) { if (callback) { /* change callback's attributes */ dc->callback = callback; dc->user_data = user_data; } else { /* remove callback */ g_hash_table_remove (worker->callbacks_hash, co); } } if (!dc) { if (callback) { /* add callback */ dc = declared_callback_new (worker, co, callback, user_data); if (!dc) { g_set_error (error, GDA_WORKER_ERROR, GDA_WORKER_INTER_THREAD_ERROR, _("Cannot build inter thread communication device")); g_rec_mutex_unlock (& worker->rmutex); return FALSE; } g_hash_table_insert (worker->callbacks_hash, co, dc); dc->source_sig_id = itsignaler_add (dc->its, co, (ITSignalerFunc) dc_callback, dc, NULL); if (dc->source_sig_id == 0) { g_hash_table_remove (worker->callbacks_hash, co); g_set_error (error, GDA_WORKER_ERROR, GDA_WORKER_INTER_THREAD_ERROR, _("GdaWorker internal error")); retval = FALSE; } } else { /* nothing to do */ } } g_rec_mutex_unlock (& worker->rmutex); if (unref_co) g_main_context_unref (co); return retval; }