4 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 static struct runqueue q
;
32 struct uloop_timeout t
;
33 struct runqueue_process proc
;
36 static void q_empty(struct runqueue
*q
)
38 fprintf(stderr
, "All done!\n");
42 static const char* sleeper_type(struct sleeper
*s
)
44 return s
->kill
? "killer" : "sleeper";
47 static void q_sleep_run(struct runqueue
*q
, struct runqueue_task
*t
)
49 struct sleeper
*s
= container_of(t
, struct sleeper
, proc
.task
);
53 fprintf(stderr
, "[%d/%d] start 'sleep %d' (%s)\n", q
->running_tasks
,
54 q
->max_running_tasks
, s
->val
, sleeper_type(s
));
61 runqueue_process_add(q
, &s
->proc
, pid
);
65 sprintf(str
, "%d", s
->val
);
66 execlp("sleep", "sleep", str
, NULL
);
70 static void q_sleep_cancel(struct runqueue
*q
, struct runqueue_task
*t
, int type
)
72 struct sleeper
*s
= container_of(t
, struct sleeper
, proc
.task
);
74 fprintf(stderr
, "[%d/%d] cancel 'sleep %d' (%s)\n", q
->running_tasks
,
75 q
->max_running_tasks
, s
->val
, sleeper_type(s
));
76 runqueue_process_cancel_cb(q
, t
, type
);
79 static void q_sleep_complete(struct runqueue
*q
, struct runqueue_task
*p
)
81 struct sleeper
*s
= container_of(p
, struct sleeper
, proc
.task
);
83 fprintf(stderr
, "[%d/%d] finish 'sleep %d' (%s) \n", q
->running_tasks
,
84 q
->max_running_tasks
, s
->val
, sleeper_type(s
));
88 static void my_runqueue_process_kill_cb(struct runqueue
*q
, struct runqueue_task
*p
)
90 struct sleeper
*s
= container_of(p
, struct sleeper
, proc
.task
);
92 fprintf(stderr
, "[%d/%d] killing process (%s)\n", q
->running_tasks
,
93 q
->max_running_tasks
, sleeper_type(s
));
94 runqueue_process_kill_cb(q
, p
);
97 static void timer_cb(struct uloop_timeout
*t
)
99 struct sleeper
*s
= container_of(t
, struct sleeper
, t
);
101 runqueue_task_kill(&s
->proc
.task
);
104 static struct sleeper
* create_sleeper(int val
, const struct runqueue_task_type
*type
, bool kill
)
106 struct sleeper
*s
= calloc(1, sizeof(*s
));
109 s
->proc
.task
.type
= type
;
110 s
->proc
.task
.run_timeout
= 500;
111 s
->proc
.task
.complete
= q_sleep_complete
;
117 static void add_sleeper(int val
)
119 static const struct runqueue_task_type sleeper_type
= {
121 .cancel
= q_sleep_cancel
,
122 .kill
= runqueue_process_kill_cb
,
125 static const struct runqueue_task_type killer_type
= {
127 .cancel
= q_sleep_cancel
,
128 .kill
= my_runqueue_process_kill_cb
,
131 struct sleeper
*k
= create_sleeper(val
, &killer_type
, true);
132 uloop_timeout_set(&k
->t
, 100);
133 uloop_timeout_add(&k
->t
);
134 runqueue_task_add(&q
, &k
->proc
.task
, false);
136 struct sleeper
*s
= create_sleeper(val
, &sleeper_type
, false);
137 runqueue_task_add(&q
, &s
->proc
.task
, false);
140 int main(int argc
, char **argv
)
145 q
.empty_cb
= q_empty
;
146 q
.max_running_tasks
= 1;
149 q
.max_running_tasks
= atoi(argv
[1]);