libubox
C utility functions for OpenWrt.
test-runqueue.c File Reference
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "uloop.h"
#include "runqueue.h"

Go to the source code of this file.

Data Structures

struct  sleeper
 

Functions

static void q_empty (struct runqueue *q)
 
static const char * sleeper_type (struct sleeper *s)
 
static void q_sleep_run (struct runqueue *q, struct runqueue_task *t)
 
static void q_sleep_cancel (struct runqueue *q, struct runqueue_task *t, int type)
 
static void q_sleep_complete (struct runqueue *q, struct runqueue_task *p)
 
static void my_runqueue_process_kill_cb (struct runqueue *q, struct runqueue_task *p)
 
static void timer_cb (struct uloop_timeout *t)
 
static struct sleepercreate_sleeper (int val, const struct runqueue_task_type *type, bool kill)
 
static void add_sleeper (int val)
 
int main (int argc, char **argv)
 

Variables

static struct runqueue q
 

Function Documentation

◆ add_sleeper()

static void add_sleeper ( int  val)
static

Definition at line 117 of file test-runqueue.c.

118 {
119  static const struct runqueue_task_type sleeper_type = {
120  .run = q_sleep_run,
121  .cancel = q_sleep_cancel,
122  .kill = runqueue_process_kill_cb,
123  };
124 
125  static const struct runqueue_task_type killer_type = {
126  .run = q_sleep_run,
127  .cancel = q_sleep_cancel,
129  };
130 
131  struct sleeper *k = create_sleeper(val, &killer_type, true);
132  uloop_timeout_set(&k->t, 10);
133  uloop_timeout_add(&k->t);
134  runqueue_task_add(&q, &k->proc.task, false);
135 
136  struct sleeper *s = create_sleeper(val, &sleeper_type, false);
137  runqueue_task_add(&q, &s->proc.task, false);
138 }
void runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running)
Definition: runqueue.c:181
void runqueue_process_kill_cb(struct runqueue *q, struct runqueue_task *t)
Definition: runqueue.c:255
struct runqueue_task task
Definition: runqueue.h:89
void(* run)(struct runqueue *q, struct runqueue_task *t)
Definition: runqueue.h:53
struct runqueue_process proc
Definition: test-runqueue.c:33
struct uloop_timeout t
Definition: test-runqueue.c:32
static struct sleeper * create_sleeper(int val, const struct runqueue_task_type *type, bool kill)
static void q_sleep_run(struct runqueue *q, struct runqueue_task *t)
Definition: test-runqueue.c:47
static struct runqueue q
Definition: test-runqueue.c:27
static void my_runqueue_process_kill_cb(struct runqueue *q, struct runqueue_task *p)
Definition: test-runqueue.c:88
static void q_sleep_cancel(struct runqueue *q, struct runqueue_task *t, int type)
Definition: test-runqueue.c:70
static const char * sleeper_type(struct sleeper *s)
Definition: test-runqueue.c:42
int uloop_timeout_add(struct uloop_timeout *timeout)
Definition: uloop.c:298
int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
Definition: uloop.c:328
Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_sleeper()

static struct sleeper* create_sleeper ( int  val,
const struct runqueue_task_type type,
bool  kill 
)
static

Definition at line 104 of file test-runqueue.c.

105 {
106  struct sleeper *s = calloc(1, sizeof(*s));
107  s->kill = kill;
108  s->t.cb = timer_cb;
109  s->proc.task.type = type;
110  s->proc.task.run_timeout = 500;
112  s->val = val;
113 
114  return s;
115 }
const struct runqueue_task_type * type
Definition: runqueue.h:73
void(* complete)(struct runqueue *q, struct runqueue_task *t)
Definition: runqueue.h:76
int run_timeout
Definition: runqueue.h:79
bool kill
Definition: test-runqueue.c:31
uloop_timeout_handler cb
Definition: uloop.h:77
static void timer_cb(struct uloop_timeout *t)
Definition: test-runqueue.c:97
static void q_sleep_complete(struct runqueue *q, struct runqueue_task *p)
Definition: test-runqueue.c:79
uint8_t type
Definition: udebug-proto.h:0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 140 of file test-runqueue.c.

141 {
142  uloop_init();
143 
144  runqueue_init(&q);
145  q.empty_cb = q_empty;
146  q.max_running_tasks = 1;
147 
148  if (argc > 1)
149  q.max_running_tasks = atoi(argv[1]);
150 
151  add_sleeper(1);
152  add_sleeper(1);
153  add_sleeper(1);
154 
155  uloop_run();
156  uloop_done();
157 
158  return 0;
159 }
void runqueue_init(struct runqueue *q)
Definition: runqueue.c:31
int max_running_tasks
Definition: runqueue.h:36
void(* empty_cb)(struct runqueue *q)
Definition: runqueue.h:41
static void q_empty(struct runqueue *q)
Definition: test-runqueue.c:36
static void add_sleeper(int val)
void uloop_done(void)
Definition: uloop.c:686
int uloop_init(void)
Definition: uloop.c:150
static int uloop_run(void)
Definition: uloop.h:147
Here is the call graph for this function:

◆ my_runqueue_process_kill_cb()

static void my_runqueue_process_kill_cb ( struct runqueue q,
struct runqueue_task p 
)
static

Definition at line 88 of file test-runqueue.c.

89 {
90  struct sleeper *s = container_of(p, struct sleeper, proc.task);
91 
92  fprintf(stderr, "[%d/%d] killing process (%s)\n", q->running_tasks,
95 }
#define container_of(ptr, type, member)
Definition: list.h:38
int running_tasks
Definition: runqueue.h:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ q_empty()

static void q_empty ( struct runqueue q)
static

Definition at line 36 of file test-runqueue.c.

37 {
38  fprintf(stderr, "All done!\n");
39  uloop_end();
40 }
static void uloop_end(void)
Definition: uloop.h:140
Here is the call graph for this function:
Here is the caller graph for this function:

◆ q_sleep_cancel()

static void q_sleep_cancel ( struct runqueue q,
struct runqueue_task t,
int  type 
)
static

Definition at line 70 of file test-runqueue.c.

71 {
72  struct sleeper *s = container_of(t, struct sleeper, proc.task);
73 
74  fprintf(stderr, "[%d/%d] cancel 'sleep %d' (%s)\n", q->running_tasks,
77 }
void runqueue_process_cancel_cb(struct runqueue *q, struct runqueue_task *t, int type)
Definition: runqueue.c:245
Here is the call graph for this function:
Here is the caller graph for this function:

◆ q_sleep_complete()

static void q_sleep_complete ( struct runqueue q,
struct runqueue_task p 
)
static

Definition at line 79 of file test-runqueue.c.

80 {
81  struct sleeper *s = container_of(p, struct sleeper, proc.task);
82 
83  fprintf(stderr, "[%d/%d] finish 'sleep %d' (%s) \n", q->running_tasks,
85  free(s);
86 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ q_sleep_run()

static void q_sleep_run ( struct runqueue q,
struct runqueue_task t 
)
static

Definition at line 47 of file test-runqueue.c.

48 {
49  struct sleeper *s = container_of(t, struct sleeper, proc.task);
50  char str[32];
51  pid_t pid;
52 
53  fprintf(stderr, "[%d/%d] start 'sleep %d' (%s)\n", q->running_tasks,
55 
56  pid = fork();
57  if (pid < 0)
58  return;
59 
60  if (pid) {
61  runqueue_process_add(q, &s->proc, pid);
62  return;
63  }
64 
65  sprintf(str, "%d", s->val);
66  execlp("sleep", "sleep", str, NULL);
67  exit(1);
68 }
void runqueue_process_add(struct runqueue *q, struct runqueue_process *p, pid_t pid)
Definition: runqueue.c:269
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sleeper_type()

static const char* sleeper_type ( struct sleeper s)
static

Definition at line 42 of file test-runqueue.c.

43 {
44  return s->kill ? "killer" : "sleeper";
45 }
Here is the caller graph for this function:

◆ timer_cb()

static void timer_cb ( struct uloop_timeout t)
static

Definition at line 97 of file test-runqueue.c.

98 {
99  struct sleeper *s = container_of(t, struct sleeper, t);
100  if (s->kill)
102 }
void runqueue_task_kill(struct runqueue_task *t)
Definition: runqueue.c:191
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ q

struct runqueue q
static

Definition at line 1 of file test-runqueue.c.