~ubuntu-branches/ubuntu/trusty/gdis/trusty

« back to all changes in this revision

Viewing changes to task.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Leidert (dale)
  • Date: 2009-04-06 17:12:18 UTC
  • mfrom: (3.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090406171218-uizoe126jrq09ytt
Tags: 0.90-1
* New upstream release 0.90.
* Acknowledge NMU (closes: #492994). Thanks to Neil Williams.

* makefile.debian: Upstream doesn't provide a Makefile to edit - so created
  our own.
* debian/compat: Added and set to be 5.
* debian/control: Added Homepage, Vcs* and DM-Upload-Allowed fields.
  (Maintainer): Set to the debichem team with approval by Noèl.
  (Uploaders): Added myself.
  (Build-Depends): Increased debhelper version to 5. Removed glutg3-dev.
  Added dpatch.
  (Standards-Version): Bumped to 3.8.1.
  (Description): Removed homepage. Fixed a typo.
* debian/copyright: Updated, completed and adjusted.
* debian/dirs: Dropped useless file.
* debian/docs: Renamed to debian/gdis.docs.
* debian/menu: Renamed to debian/gdis.menu.
  (section): Fixed accordingly to policy.
* debian/gdis.1: Just some formatting changes.
* debian/gdis.desktop: Added file (with small fixes) provided by Phill Bull
  (LP: #111353).
* debian/gdis.install: Added.
* debian/rules: Cleaned. Installation is now done by dh_install. Make sure,
  the CVS directory is not copied. Added dh_desktop call.
* debian/source.lintian-overrides: makefile.debian is created for Debian but
  lives outside debian/.
* debian/watch: Added.
* debian/README.build: Dropped.
* debian/README.source: Added to be compliant to the policy v3.8.
* debian/patches/Debian_make.dpatch: Added.
  - gdis.h (ELEM_FILE): Moved fix for gdis.elemts path (#399132) to this
    patch.
* debian/patches/00list: Added.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (C) 2000 by Sean David Fleming
3
 
 
4
 
sean@ivec.org
5
 
 
6
 
This program is free software; you can redistribute it and/or
7
 
modify it under the terms of the GNU General Public License
8
 
as published by the Free Software Foundation; either version 2
9
 
of the License, or (at your option) any later version.
10
 
 
11
 
This program is distributed in the hope that it will be useful,
12
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
GNU General Public License for more details.
15
 
 
16
 
You should have received a copy of the GNU General Public License
17
 
along with this program; if not, write to the Free Software
18
 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
 
 
20
 
The GNU GPL can also be found at http://www.gnu.org
21
 
*/
22
 
 
23
 
/* irix */
24
 
#define _BSD_SIGNALS 1
25
 
 
26
 
#include <stdio.h>
27
 
#include <stdlib.h>
28
 
#include <string.h>
29
 
#include <unistd.h>
30
 
 
31
 
#include "gdis.h"
32
 
#include "file.h"
33
 
#include "task.h"
34
 
#include "interface.h"
35
 
 
36
 
/* top level data structure */
37
 
extern struct sysenv_pak sysenv;
38
 
 
39
 
#ifndef __WIN32
40
 
#include <sys/wait.h>
41
 
#endif
42
 
 
43
 
/**********************************************/
44
 
/* execute task in thread created by the pool */
45
 
/**********************************************/
46
 
void task_process(struct task_pak *task, gpointer data)
47
 
{
48
 
/* checks */
49
 
if (!task)
50
 
  return;
51
 
if (task->status != QUEUED)
52
 
  return;
53
 
 
54
 
/* setup for current task */
55
 
task->pid = getpid();
56
 
/* TODO - should be mutex locking this */
57
 
task->status = RUNNING;
58
 
 
59
 
/* execute the primary task */
60
 
task->primary(task->ptr1, task);
61
 
 
62
 
/* execute the cleanup task */
63
 
/* TODO - can we run this after (ie in the main process to avoid thread lock) */
64
 
gdk_threads_enter();
65
 
 
66
 
if (task->status != KILLED)
67
 
  {
68
 
  if (task->cleanup)
69
 
    task->cleanup(task->ptr2);
70
 
  task->status = COMPLETED;
71
 
  }
72
 
 
73
 
gdk_flush();
74
 
gdk_threads_leave();
75
 
 
76
 
/* job completion notification */
77
 
gdk_beep();
78
 
}
79
 
 
80
 
/***************************************************/
81
 
/* set up the thread pool to process task requests */
82
 
/***************************************************/
83
 
void task_queue_init(void)
84
 
{
85
 
#ifdef G_THREADS_ENABLED
86
 
g_thread_init(NULL);
87
 
gdk_threads_init();
88
 
if (!g_thread_supported())
89
 
  {
90
 
/* TODO - disallow queueing of background tasks if this happens */
91
 
  gui_text_show(ERROR, "Task queue initialization failed.\n");
92
 
  }
93
 
else
94
 
  sysenv.thread_pool = g_thread_pool_new((GFunc) task_process, NULL,
95
 
                                         sysenv.max_threads,
96
 
                                         FALSE, NULL);
97
 
#endif
98
 
}
99
 
 
100
 
/*****************************/
101
 
/* terminate the thread pool */
102
 
/*****************************/
103
 
void task_queue_free(void)
104
 
{
105
 
g_thread_pool_free(sysenv.thread_pool, TRUE, FALSE);
106
 
}
107
 
 
108
 
/*************************/
109
 
/* free a task structure */
110
 
/*************************/
111
 
void task_free(gpointer data)
112
 
{
113
 
struct task_pak *task = data;
114
 
 
115
 
g_assert(task != NULL);
116
 
 
117
 
g_free(task->label);
118
 
g_free(task->time);
119
 
g_free(task->message);
120
 
g_free(task->status_file);
121
 
 
122
 
if (task->status_fp)
123
 
  fclose(task->status_fp);
124
 
 
125
 
g_string_free(task->status_text, TRUE);
126
 
 
127
 
g_free(task);
128
 
}
129
 
 
130
 
/****************************/
131
 
/* submit a background task */
132
 
/****************************/
133
 
void task_new(const gchar *label,
134
 
              gpointer func1, gpointer arg1,
135
 
              gpointer func2, gpointer arg2,
136
 
              gpointer model)
137
 
{
138
 
struct task_pak *task;
139
 
 
140
 
/* duplicate the task data */
141
 
task = g_malloc(sizeof(struct task_pak));
142
 
sysenv.task_list = g_slist_prepend(sysenv.task_list, task);
143
 
task->pid = -1;
144
 
task->status = QUEUED;
145
 
task->time = NULL;
146
 
task->message = NULL;
147
 
task->pcpu = 0.0;
148
 
task->pmem = 0.0;
149
 
task->progress = 0.0;
150
 
task->locked_model = model;
151
 
 
152
 
task->status_file = NULL;
153
 
task->status_fp = NULL;
154
 
task->status_index = -1;
155
 
task->status_text = g_string_new(NULL);
156
 
 
157
 
task->label = g_strdup(label);
158
 
task->primary = func1;
159
 
task->cleanup = func2;
160
 
task->ptr1 = arg1;
161
 
task->ptr2 = arg2;
162
 
/*
163
 
if (model)
164
 
  ((struct model_pak *) model)->locked = TRUE;
165
 
*/
166
 
 
167
 
/* queue the task */
168
 
g_thread_pool_push(sysenv.thread_pool, task, NULL);
169
 
}
170
 
 
171
 
/**************************************/
172
 
/* platform independant task spawning */
173
 
/**************************************/
174
 
#define DEBUG_TASK_SYNC 0
175
 
gint task_sync(const gchar *command) 
176
 
{
177
 
gint status;
178
 
gchar **argv;
179
 
GError *error=NULL;
180
 
 
181
 
/* checks */
182
 
if (!command)
183
 
  return(1);
184
 
 
185
 
#if _WIN32
186
 
status = g_spawn_command_line_sync(command, NULL, NULL, NULL, &error);
187
 
#else
188
 
/* setup the command vector */
189
 
argv = g_malloc(4 * sizeof(gchar *));
190
 
*(argv) = g_strdup("/bin/sh");
191
 
*(argv+1) = g_strdup("-c");
192
 
*(argv+2) = g_strdup(command);
193
 
*(argv+3) = NULL;
194
 
status = g_spawn_sync(sysenv.cwd, argv, NULL, 0, NULL, NULL, NULL, NULL, NULL, &error);
195
 
g_strfreev(argv);
196
 
#endif
197
 
 
198
 
if (!status)
199
 
  printf("Error: %s\n", error->message);
200
 
 
201
 
return(status);
202
 
}
203
 
 
204
 
/********************************************/
205
 
/* filter out unwanted lines in status file */
206
 
/********************************************/
207
 
gint task_status_keep(gint type, const gchar *line)
208
 
{
209
 
switch (type)
210
 
  {
211
 
  case GULP:
212
 
    if (strstr(line, "CPU"))
213
 
      return(1);
214
 
    if (strstr(line, " **"))
215
 
      return(1);
216
 
/*
217
 
    if (strstr(line, "="))
218
 
      if (strstr(line, "energy"))
219
 
        return(1);
220
 
*/
221
 
    break;
222
 
 
223
 
  default:
224
 
    return(1);
225
 
  }
226
 
return(0);
227
 
}
228
 
 
229
 
/**************************************************/
230
 
/* create descriptive string from the status file */
231
 
/**************************************************/
232
 
void task_status_update(struct task_pak *task)
233
 
{
234
 
/*gint filter;*/
235
 
gchar *line;
236
 
 
237
 
g_assert(task != NULL);
238
 
 
239
 
/* setup any status file filtering */
240
 
/*
241
 
if (g_ascii_strncasecmp("gulp", task->label, 4) == 0)
242
 
  filter = GULP;
243
 
else
244
 
  filter = 0;
245
 
*/
246
 
 
247
 
/* read in the status file */
248
 
if (task->status_file)
249
 
  {
250
 
  if (!task->status_fp)
251
 
    {
252
 
    task->status_index = 0;
253
 
/* exit if we've read in the file and closed it (due to completion) */
254
 
    if (strlen((task->status_text)->str))
255
 
      return;
256
 
    task->status_fp = fopen(task->status_file, "rt");
257
 
    }
258
 
 
259
 
  line = file_read_line(task->status_fp);
260
 
  while (line)
261
 
    {
262
 
/*
263
 
    if (task_status_keep(filter, line))
264
 
*/
265
 
      g_string_append(task->status_text, line);
266
 
 
267
 
    g_free(line);
268
 
    line = file_read_line(task->status_fp);
269
 
    }
270
 
 
271
 
  if (task->status == COMPLETED || task->status == KILLED)
272
 
    {
273
 
    fclose(task->status_fp);
274
 
    task->status_fp = NULL;
275
 
    }
276
 
  }
277
 
}
278
 
 
 
1
/*
 
2
Copyright (C) 2000 by Sean David Fleming
 
3
 
 
4
sean@ivec.org
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
The GNU GPL can also be found at http://www.gnu.org
 
21
*/
 
22
 
 
23
/* irix */
 
24
#define _BSD_SIGNALS 1
 
25
 
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
#include <unistd.h>
 
30
 
 
31
#include "gdis.h"
 
32
#include "file.h"
 
33
#include "task.h"
 
34
#include "job.h"
 
35
#include "grid.h"
 
36
#include "interface.h"
 
37
 
 
38
/* top level data structure */
 
39
extern struct sysenv_pak sysenv;
 
40
 
 
41
#ifndef __WIN32
 
42
#include <sys/wait.h>
 
43
#endif
 
44
 
 
45
/**********************************************/
 
46
/* execute task in thread created by the pool */
 
47
/**********************************************/
 
48
void task_process(struct task_pak *task, gpointer data)
 
49
{
 
50
/* checks */
 
51
if (!task)
 
52
  return;
 
53
if (task->status != QUEUED)
 
54
  return;
 
55
 
 
56
/* setup for current task */
 
57
task->pid = getpid();
 
58
/* TODO - should be mutex locking this */
 
59
task->status = RUNNING;
 
60
 
 
61
/* NB: the primary task needs to not do anything that could */
 
62
/* cause problems eg update GUI elements since this can cause conflicts */
 
63
 
 
64
/* execute the primary task */
 
65
task->primary(task->ptr1, task);
 
66
 
 
67
/* NB: GUI updates should ALL be done in the cleanup task, since we */
 
68
/* do the threads enter to avoid problems - this locks the GUI */
 
69
/* until we call the threads leave function at the end */
 
70
 
 
71
gdk_threads_enter();
 
72
 
 
73
if (task->status != KILLED)
 
74
  {
 
75
/* execute the cleanup task */
 
76
  if (task->cleanup)
 
77
    task->cleanup(task->ptr2);
 
78
  task->status = COMPLETED;
 
79
  }
 
80
 
 
81
gdk_flush();
 
82
gdk_threads_leave();
 
83
 
 
84
/* job completion notification */
 
85
gdk_beep();
 
86
}
 
87
 
 
88
/***************************************************/
 
89
/* set up the thread pool to process task requests */
 
90
/***************************************************/
 
91
void task_queue_init(void)
 
92
{
 
93
#ifdef G_THREADS_ENABLED
 
94
g_thread_init(NULL);
 
95
gdk_threads_init();
 
96
if (!g_thread_supported())
 
97
  {
 
98
/* TODO - disallow queueing of background tasks if this happens */
 
99
  gui_text_show(ERROR, "Task queue initialization failed.\n");
 
100
  }
 
101
else
 
102
  sysenv.thread_pool = g_thread_pool_new((GFunc) task_process, NULL,
 
103
                                         sysenv.max_threads,
 
104
                                         FALSE, NULL);
 
105
#endif
 
106
}
 
107
 
 
108
/*****************************/
 
109
/* terminate the thread pool */
 
110
/*****************************/
 
111
void task_queue_free(void)
 
112
{
 
113
g_thread_pool_free(sysenv.thread_pool, TRUE, FALSE);
 
114
}
 
115
 
 
116
/*************************/
 
117
/* free a task structure */
 
118
/*************************/
 
119
void task_free(gpointer data)
 
120
{
 
121
struct task_pak *task = data;
 
122
 
 
123
g_assert(task != NULL);
 
124
 
 
125
g_free(task->label);
 
126
g_free(task->time);
 
127
g_free(task->message);
 
128
g_free(task->status_file);
 
129
 
 
130
if (task->status_fp)
 
131
  fclose(task->status_fp);
 
132
 
 
133
g_string_free(task->status_text, TRUE);
 
134
 
 
135
g_free(task);
 
136
}
 
137
 
 
138
/****************************/
 
139
/* submit a background task */
 
140
/****************************/
 
141
/* TODO - only show certain tasks in the manager, since this */
 
142
/* could be used to do any tasks in the background - some of */
 
143
/* which may be slow GUI tasks we dont want to be cancellable */
 
144
void task_new(const gchar *label,
 
145
              gpointer func1, gpointer arg1,
 
146
              gpointer func2, gpointer arg2,
 
147
              gpointer model)
 
148
{
 
149
struct task_pak *task;
 
150
 
 
151
/* duplicate the task data */
 
152
task = g_malloc(sizeof(struct task_pak));
 
153
sysenv.task_list = g_slist_prepend(sysenv.task_list, task);
 
154
task->pid = -1;
 
155
task->status = QUEUED;
 
156
task->time = NULL;
 
157
task->message = NULL;
 
158
task->pcpu = 0.0;
 
159
task->pmem = 0.0;
 
160
task->progress = 0.0;
 
161
task->locked_model = model;
 
162
 
 
163
task->status_file = NULL;
 
164
task->status_fp = NULL;
 
165
task->status_index = -1;
 
166
task->status_text = g_string_new(NULL);
 
167
 
 
168
task->label = g_strdup(label);
 
169
task->primary = func1;
 
170
task->cleanup = func2;
 
171
task->ptr1 = arg1;
 
172
task->ptr2 = arg2;
 
173
/*
 
174
if (model)
 
175
  ((struct model_pak *) model)->locked = TRUE;
 
176
*/
 
177
 
 
178
/* queue the task */
 
179
g_thread_pool_push(sysenv.thread_pool, task, NULL);
 
180
}
 
181
 
 
182
/**************************************/
 
183
/* platform independant task spawning */
 
184
/**************************************/
 
185
#define DEBUG_TASK_SYNC 0
 
186
gint task_sync(const gchar *command) 
 
187
{
 
188
gint status;
 
189
gchar **argv;
 
190
GError *error=NULL;
 
191
 
 
192
/* checks */
 
193
if (!command)
 
194
  return(1);
 
195
 
 
196
#if _WIN32
 
197
chdir(sysenv.cwd);
 
198
system(command);
 
199
#else
 
200
/* setup the command vector */
 
201
argv = g_malloc(4 * sizeof(gchar *));
 
202
*(argv) = g_strdup("/bin/sh");
 
203
*(argv+1) = g_strdup("-c");
 
204
*(argv+2) = g_strdup(command);
 
205
*(argv+3) = NULL;
 
206
status = g_spawn_sync(sysenv.cwd, argv, NULL, 0, NULL, NULL, NULL, NULL, NULL, &error);
 
207
g_strfreev(argv);
 
208
#endif
 
209
 
 
210
if (!status)
 
211
  printf("task_sync() error: %s\n", error->message);
 
212
 
 
213
return(status);
 
214
}
 
215
 
 
216
/********************************************/
 
217
/* filter out unwanted lines in status file */
 
218
/********************************************/
 
219
gint task_status_keep(gint type, const gchar *line)
 
220
{
 
221
switch (type)
 
222
  {
 
223
  case GULP:
 
224
    if (strstr(line, "CPU"))
 
225
      return(1);
 
226
    if (strstr(line, " **"))
 
227
      return(1);
 
228
/*
 
229
    if (strstr(line, "="))
 
230
      if (strstr(line, "energy"))
 
231
        return(1);
 
232
*/
 
233
    break;
 
234
 
 
235
  default:
 
236
    return(1);
 
237
  }
 
238
return(0);
 
239
}
 
240
 
 
241
/**************************************************/
 
242
/* create descriptive string from the status file */
 
243
/**************************************************/
 
244
void task_status_update(struct task_pak *task)
 
245
{
 
246
/*gint filter;*/
 
247
gchar *line;
 
248
 
 
249
g_assert(task != NULL);
 
250
 
 
251
/* setup any status file filtering */
 
252
/*
 
253
if (g_ascii_strncasecmp("gulp", task->label, 4) == 0)
 
254
  filter = GULP;
 
255
else
 
256
  filter = 0;
 
257
*/
 
258
 
 
259
/* read in the status file */
 
260
if (task->status_file)
 
261
  {
 
262
  if (!task->status_fp)
 
263
    {
 
264
    task->status_index = 0;
 
265
/* exit if we've read in the file and closed it (due to completion) */
 
266
    if (strlen((task->status_text)->str))
 
267
      return;
 
268
    task->status_fp = fopen(task->status_file, "rt");
 
269
    }
 
270
 
 
271
  line = file_read_line(task->status_fp);
 
272
  while (line)
 
273
    {
 
274
/*
 
275
    if (task_status_keep(filter, line))
 
276
*/
 
277
      g_string_append(task->status_text, line);
 
278
 
 
279
    g_free(line);
 
280
    line = file_read_line(task->status_fp);
 
281
    }
 
282
 
 
283
  if (task->status == COMPLETED || task->status == KILLED)
 
284
    {
 
285
    fclose(task->status_fp);
 
286
    task->status_fp = NULL;
 
287
    }
 
288
  }
 
289
}
 
290