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

wm_jobs.c « intern « windowmanager « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 422cc42e10b7e5f1b8ce0197f3ad87860b30d5c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
 * $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, update, free */
	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;
	
	/* 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 (*update)(void  *))
{
	steve->startjob= startjob;
	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;
	
}