~ubuntu-branches/ubuntu/trusty/cctools/trusty-proposed

« back to all changes in this revision

Viewing changes to dttools/src/process.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-07-14 14:41:37 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110714144137-f7912jbs6esk7ffc
Tags: 3.3.3-1
* New upstream release.
* Updated Debian configuration patches.
* Added build-dependency to python-support to install the workqueue
  extension correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#include <stdlib.h>
13
13
#include <signal.h>
14
14
 
15
 
static struct list * complete_list = 0;
 
15
static struct list *complete_list = 0;
16
16
 
17
 
static void alarm_handler( int sig )
 
17
static void alarm_handler(int sig)
18
18
{
19
19
        // do nothing except interrupt the wait
20
20
}
21
21
 
22
 
static int process_work( int timeout )
 
22
static int process_work(int timeout)
23
23
{
24
24
        void *old_handler = 0;
25
25
        int flags = 0;
26
26
 
27
 
        if(timeout==0) {
 
27
        if(timeout == 0) {
28
28
                flags = WNOHANG;
29
29
        } else {
30
30
                flags = 0;
31
 
                old_handler = signal(SIGALRM,alarm_handler);
 
31
                old_handler = signal(SIGALRM, alarm_handler);
32
32
                alarm(timeout);
33
33
        }
34
34
 
35
35
        struct process_info p;
36
36
 
37
 
        p.pid = wait4(-1,&p.status,flags,&p.rusage);
38
 
        if(p.pid<=0) return 0;
 
37
        p.pid = wait4(-1, &p.status, flags, &p.rusage);
 
38
        if(p.pid <= 0) {
 
39
                signal(SIGALRM, old_handler);
 
40
                return 0;
 
41
        }
39
42
 
40
43
        struct process_info *i = malloc(sizeof(*i));
41
44
        *i = p;
42
45
 
43
 
        list_push_tail(complete_list,i);
 
46
        list_push_tail(complete_list, i);
44
47
 
45
48
        return 1;
46
49
}
47
50
 
48
 
struct process_info * process_wait( int timeout )
 
51
struct process_info *process_wait(int timeout)
49
52
{
50
53
        struct process_info *p;
51
54
 
52
 
        if(!complete_list) complete_list = list_create();
 
55
        if(!complete_list)
 
56
                complete_list = list_create();
53
57
 
54
58
        p = list_pop_head(complete_list);
55
 
        if(p) return p;
 
59
        if(p)
 
60
                return p;
56
61
 
57
62
        process_work(timeout);
58
63
 
59
64
        return list_pop_head(complete_list);
60
65
}
61
66
 
62
 
void process_putback( struct process_info *p )
 
67
void process_putback(struct process_info *p)
63
68
{
64
 
        if(!complete_list) complete_list = list_create();
 
69
        if(!complete_list)
 
70
                complete_list = list_create();
65
71
 
66
 
        list_push_tail(complete_list,p);        
 
72
        list_push_tail(complete_list, p);
67
73
}
68
74
 
69
75
int process_pending()
70
76
{
71
 
        if(!complete_list) complete_list = list_create();
 
77
        if(!complete_list)
 
78
                complete_list = list_create();
72
79
 
73
 
        if(list_size(complete_list)>0) return 1;
 
80
        if(list_size(complete_list) > 0)
 
81
                return 1;
74
82
 
75
83
        return process_work(0);
76
84
}