Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'source/blender/windowmanager/intern/wm_jobs.c')
-rw-r--r--source/blender/windowmanager/intern/wm_jobs.c270
1 files changed, 270 insertions, 0 deletions
diff --git a/source/blender/windowmanager/intern/wm_jobs.c b/source/blender/windowmanager/intern/wm_jobs.c
new file mode 100644
index 00000000000..1f320df7587
--- /dev/null
+++ b/source/blender/windowmanager/intern/wm_jobs.c
@@ -0,0 +1,270 @@
+/**
+ * $Id:
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2009 Blender Foundation.
+ * All rights reserved.
+ *
+ *
+ * Contributor(s): Blender Foundation
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "DNA_windowmanager_types.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_threads.h"
+
+#include "BKE_blender.h"
+#include "BKE_context.h"
+#include "BKE_idprop.h"
+#include "BKE_library.h"
+#include "BKE_main.h"
+#include "BKE_report.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+#include "wm_window.h"
+#include "wm_event_system.h"
+#include "wm_event_types.h"
+#include "wm.h"
+
+#include "ED_screen.h"
+
+#include "RNA_types.h"
+
+/* ********************** Threaded Jobs Manager ****************************** */
+
+/*
+Add new job
+- register in WM
+- configure callbacks
+
+Start or re-run job
+- if job running
+ - signal job to end
+ - add timer notifier to verify when it has ended, to start it
+- else
+ - start job
+ - add timer notifier to handle progress
+
+Stop job
+ - signal job to end
+ on end, job will tag itself as sleeping
+
+Remove job
+- signal job to end
+ on end, job will remove itself
+
+When job is done:
+- it puts timer to sleep (or removes?)
+
+ */
+
+struct wmJob {
+ struct wmJob *next, *prev;
+
+ /* job originating from, keep track of this when deleting windows */
+ wmWindow *win;
+
+ /* should store entire own context, for start, free or listeners */
+ void *customdata;
+ void (*startjob)(void *, short *stop, short *do_update);
+ void (*free)(void *);
+
+ /* running jobs each have own timer */
+ double timestep;
+ wmTimer *wt;
+ /* the notifier event timers should send */
+ unsigned int note;
+
+ /* managing */
+ void (*listener)(struct wmJob *, struct wmNotifier *);
+
+ /* update gets called if thread defines so, and max once per timerstep */
+ /* no drawing, send notifiers! */
+ void (*update)(void *);
+
+/* internal */
+ void *owner;
+ short running, ready, do_update, stop;
+
+ /* once running, we store this separately */
+ void *run_customdata;
+ void (*run_free)(void *);
+
+ /* we use BLI_threads api, but per job only 1 thread runs */
+ ListBase threads;
+
+};
+
+/* ******************* public API ***************** */
+
+/* returns current or adds new job, but doesnt run it */
+wmJob *WM_jobs_get(wmWindowManager *wm, wmWindow *win, void *owner)
+{
+ wmJob *steve;
+
+ for(steve= wm->jobs.first; steve; steve= steve->next)
+ if(steve->owner==owner)
+ break;
+
+ if(steve==NULL) {
+ steve= MEM_callocN(sizeof(wmJob), "new job");
+
+ BLI_addtail(&wm->jobs, steve);
+ steve->win= win;
+ steve->owner= owner;
+ }
+
+ return steve;
+}
+
+void WM_jobs_customdata(wmJob *steve, void *customdata, void (*free)(void *))
+{
+ /* pending job? just free */
+ if(steve->customdata)
+ steve->free(steve->customdata);
+
+ steve->customdata= customdata;
+ steve->free= free;
+
+ if(steve->running) {
+ /* signal job to end */
+ steve->stop= 1;
+ }
+}
+
+void WM_jobs_timer(wmJob *steve, double timestep, unsigned int note)
+{
+ steve->timestep = timestep;
+ steve->note = note;
+}
+
+void WM_jobs_callbacks(wmJob *steve,
+ void (*startjob)(void *, short *, short *),
+ void (*listener)(struct wmJob *, struct wmNotifier *),
+ void (*update)(void *))
+{
+ steve->startjob= startjob;
+ steve->listener= listener;
+ steve->update= update;
+}
+
+static void *do_job_thread(void *job_v)
+{
+ wmJob *steve= job_v;
+
+ steve->stop= steve->ready= 0;
+ steve->startjob(steve->run_customdata, &steve->stop, &steve->do_update);
+ steve->ready= 1;
+
+ return NULL;
+}
+
+void WM_jobs_start(wmJob *steve)
+{
+ if(steve->running) {
+ /* signal job to end and restart */
+ steve->stop= 1;
+ }
+ else {
+ if(steve->customdata && steve->startjob) {
+
+ /* copy to ensure proper free in end */
+ steve->run_customdata= steve->customdata;
+ steve->run_free= steve->free;
+ steve->free= NULL;
+ steve->customdata= NULL;
+ steve->running= 1;
+
+ BLI_init_threads(&steve->threads, do_job_thread, 1);
+ BLI_insert_thread(&steve->threads, steve);
+
+ /* restarted job has timer already */
+ if(steve->wt==NULL)
+ steve->wt= WM_event_add_window_timer(steve->win, TIMERJOBS, steve->timestep);
+ }
+ else printf("job fails, not initialized\n");
+ }
+}
+
+/* hardcoded to event TIMERJOBS */
+static int wm_jobs_timer(bContext *C, wmOperator *op, wmEvent *evt)
+{
+ wmWindowManager *wm= CTX_wm_manager(C);
+ wmJob *steve= wm->jobs.first;
+
+ for(; steve; steve= steve->next) {
+
+ if(evt->customdata==steve->wt) {
+ /* running threads */
+ if(steve->threads.first) {
+
+ if(steve->do_update) {
+ if(steve->update)
+ steve->update(steve->customdata);
+ if(steve->note)
+ WM_event_add_notifier(C, steve->note, NULL);
+ steve->do_update= 0;
+ }
+
+ if(steve->ready) {
+ /* free own data */
+ steve->run_free(steve->run_customdata);
+ steve->run_customdata= NULL;
+ steve->run_free= NULL;
+
+ steve->running= 0;
+ BLI_end_threads(&steve->threads);
+
+ /* new job added for steve? */
+ if(steve->customdata) {
+ WM_jobs_start(steve);
+ }
+ else {
+ WM_event_remove_window_timer(steve->win, steve->wt);
+ steve->wt= NULL;
+
+ /* remove steve */
+ BLI_remlink(&wm->jobs, steve);
+ MEM_freeN(steve);
+ }
+ }
+ }
+ return OPERATOR_FINISHED;
+ }
+ }
+ return OPERATOR_PASS_THROUGH;
+}
+
+void WM_OT_jobs_timer(wmOperatorType *ot)
+{
+ /* identifiers */
+ ot->name= "Jobs timer";
+ ot->idname= "WM_OT_jobs_timer";
+
+ /* api callbacks */
+ ot->invoke= wm_jobs_timer;
+
+ ot->poll= ED_operator_screenactive;
+
+}