]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/sched.c
drop dead code
[bcachefs-tools-debian] / linux / sched.c
1
2 #include <linux/futex.h>
3 #include <string.h>
4 #include <sys/mman.h>
5
6 /* hack for mips: */
7 #define CONFIG_RCU_HAVE_FUTEX 1
8 #include <urcu/futex.h>
9
10 #include <linux/math64.h>
11 #include <linux/printk.h>
12 #include <linux/rcupdate.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/timer.h>
16
17 __thread struct task_struct *current;
18
19 void __put_task_struct(struct task_struct *t)
20 {
21         pthread_join(t->thread, NULL);
22         free(t);
23 }
24
25 /* returns true if process was woken up, false if it was already running */
26 int wake_up_process(struct task_struct *p)
27 {
28         int ret = p->state != TASK_RUNNING;
29
30         p->state = TASK_RUNNING;
31         futex(&p->state, FUTEX_WAKE|FUTEX_PRIVATE_FLAG,
32               INT_MAX, NULL, NULL, 0);
33         return ret;
34 }
35
36 void schedule(void)
37 {
38         int v;
39
40         rcu_quiescent_state();
41
42         while ((v = current->state) != TASK_RUNNING)
43                 futex(&current->state, FUTEX_WAIT|FUTEX_PRIVATE_FLAG,
44                       v, NULL, NULL, 0);
45 }
46
47 struct process_timer {
48         struct timer_list timer;
49         struct task_struct *task;
50 };
51
52 static void process_timeout(struct timer_list *t)
53 {
54         struct process_timer *timeout =
55                 container_of(t, struct process_timer, timer);
56
57         wake_up_process(timeout->task);
58 }
59
60 long schedule_timeout(long timeout)
61 {
62         struct process_timer timer;
63         unsigned long expire;
64
65         switch (timeout)
66         {
67         case MAX_SCHEDULE_TIMEOUT:
68                 /*
69                  * These two special cases are useful to be comfortable
70                  * in the caller. Nothing more. We could take
71                  * MAX_SCHEDULE_TIMEOUT from one of the negative value
72                  * but I' d like to return a valid offset (>=0) to allow
73                  * the caller to do everything it want with the retval.
74                  */
75                 schedule();
76                 goto out;
77         default:
78                 /*
79                  * Another bit of PARANOID. Note that the retval will be
80                  * 0 since no piece of kernel is supposed to do a check
81                  * for a negative retval of schedule_timeout() (since it
82                  * should never happens anyway). You just have the printk()
83                  * that will tell you if something is gone wrong and where.
84                  */
85                 if (timeout < 0) {
86                         printk(KERN_ERR "schedule_timeout: wrong timeout "
87                                 "value %lx\n", timeout);
88                         current->state = TASK_RUNNING;
89                         goto out;
90                 }
91         }
92
93         expire = timeout + jiffies;
94
95         timer.task = current;
96         timer_setup_on_stack(&timer.timer, process_timeout, 0);
97         mod_timer(&timer.timer, expire);
98         schedule();
99         del_timer_sync(&timer.timer);
100
101         timeout = expire - jiffies;
102 out:
103         return timeout < 0 ? 0 : timeout;
104 }
105
106 __attribute__((constructor(101)))
107 static void sched_init(void)
108 {
109         struct task_struct *p = malloc(sizeof(*p));
110
111         mlockall(MCL_CURRENT|MCL_FUTURE);
112
113         memset(p, 0, sizeof(*p));
114
115         p->state        = TASK_RUNNING;
116         atomic_set(&p->usage, 1);
117         init_completion(&p->exited);
118
119         current = p;
120
121         rcu_init();
122         rcu_register_thread();
123 }
124
125 #ifndef __NR_getrandom
126 #include <fcntl.h>
127 #include <sys/stat.h>
128 #include <sys/types.h>
129 int urandom_fd;
130
131 __attribute__((constructor(101)))
132 static void rand_init(void)
133 {
134         urandom_fd = open("/dev/urandom", O_RDONLY);
135         BUG_ON(urandom_fd < 0);
136 }
137 #endif