]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/sched.c
use futex instead of pthread cond variable for schedule
[bcachefs-tools-debian] / linux / sched.c
1
2 #include <linux/futex.h>
3 #include <string.h>
4 #include <sys/mman.h>
5
6 #include <linux/math64.h>
7 #include <linux/printk.h>
8 #include <linux/rcupdate.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/timer.h>
12
13 __thread struct task_struct *current;
14
15 void __put_task_struct(struct task_struct *t)
16 {
17         pthread_join(t->thread, NULL);
18         free(t);
19 }
20
21 /* returns true if process was woken up, false if it was already running */
22 int wake_up_process(struct task_struct *p)
23 {
24         int ret = p->state != TASK_RUNNING;
25
26         p->state = TASK_RUNNING;
27         futex(&p->state, FUTEX_WAKE|FUTEX_PRIVATE_FLAG,
28               INT_MAX, NULL, NULL, 0);
29         return ret;
30 }
31
32 void schedule(void)
33 {
34         int v;
35
36         rcu_quiescent_state();
37
38         while ((v = current->state) != TASK_RUNNING)
39                 futex(&current->state, FUTEX_WAIT|FUTEX_PRIVATE_FLAG,
40                       v, NULL, NULL, 0);
41 }
42
43 static void process_timeout(unsigned long __data)
44 {
45         wake_up_process((struct task_struct *)__data);
46 }
47
48 long schedule_timeout(long timeout)
49 {
50         struct timer_list timer;
51         unsigned long expire;
52
53         switch (timeout)
54         {
55         case MAX_SCHEDULE_TIMEOUT:
56                 /*
57                  * These two special cases are useful to be comfortable
58                  * in the caller. Nothing more. We could take
59                  * MAX_SCHEDULE_TIMEOUT from one of the negative value
60                  * but I' d like to return a valid offset (>=0) to allow
61                  * the caller to do everything it want with the retval.
62                  */
63                 schedule();
64                 goto out;
65         default:
66                 /*
67                  * Another bit of PARANOID. Note that the retval will be
68                  * 0 since no piece of kernel is supposed to do a check
69                  * for a negative retval of schedule_timeout() (since it
70                  * should never happens anyway). You just have the printk()
71                  * that will tell you if something is gone wrong and where.
72                  */
73                 if (timeout < 0) {
74                         printk(KERN_ERR "schedule_timeout: wrong timeout "
75                                 "value %lx\n", timeout);
76                         current->state = TASK_RUNNING;
77                         goto out;
78                 }
79         }
80
81         expire = timeout + jiffies;
82
83         setup_timer(&timer, process_timeout, (unsigned long)current);
84         mod_timer(&timer, expire);
85         schedule();
86         del_timer_sync(&timer);
87
88         timeout = expire - jiffies;
89 out:
90         return timeout < 0 ? 0 : timeout;
91 }
92
93 unsigned long __msecs_to_jiffies(const unsigned int m)
94 {
95         /*
96          * Negative value, means infinite timeout:
97          */
98         if ((int)m < 0)
99                 return MAX_JIFFY_OFFSET;
100         return _msecs_to_jiffies(m);
101 }
102
103 u64 nsecs_to_jiffies64(u64 n)
104 {
105 #if (NSEC_PER_SEC % HZ) == 0
106         /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
107         return div_u64(n, NSEC_PER_SEC / HZ);
108 #elif (HZ % 512) == 0
109         /* overflow after 292 years if HZ = 1024 */
110         return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
111 #else
112         /*
113          * Generic case - optimized for cases where HZ is a multiple of 3.
114          * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
115          */
116         return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
117 #endif
118 }
119
120 unsigned long nsecs_to_jiffies(u64 n)
121 {
122         return (unsigned long)nsecs_to_jiffies64(n);
123 }
124
125 unsigned int jiffies_to_msecs(const unsigned long j)
126 {
127 #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
128         return (MSEC_PER_SEC / HZ) * j;
129 #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
130         return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
131 #else
132 # if BITS_PER_LONG == 32
133         return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
134 # else
135         return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
136 # endif
137 #endif
138 }
139
140 unsigned int jiffies_to_usecs(const unsigned long j)
141 {
142         /*
143          * Hz usually doesn't go much further MSEC_PER_SEC.
144          * jiffies_to_usecs() and usecs_to_jiffies() depend on that.
145          */
146         BUILD_BUG_ON(HZ > USEC_PER_SEC);
147
148 #if !(USEC_PER_SEC % HZ)
149         return (USEC_PER_SEC / HZ) * j;
150 #else
151 # if BITS_PER_LONG == 32
152         return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
153 # else
154         return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
155 # endif
156 #endif
157 }
158
159 __attribute__((constructor(101)))
160 static void sched_init(void)
161 {
162         struct task_struct *p = malloc(sizeof(*p));
163
164         mlockall(MCL_CURRENT|MCL_FUTURE);
165
166         memset(p, 0, sizeof(*p));
167
168         p->state        = TASK_RUNNING;
169         atomic_set(&p->usage, 1);
170         init_completion(&p->exited);
171
172         current = p;
173
174         rcu_init();
175         rcu_register_thread();
176 }
177
178 #ifndef __NR_getrandom
179 #include <fcntl.h>
180 #include <sys/stat.h>
181 #include <sys/types.h>
182 int urandom_fd;
183
184 __attribute__((constructor(101)))
185 static void rand_init(void)
186 {
187         urandom_fd = open("/dev/urandom", O_RDONLY);
188         BUG_ON(urandom_fd < 0);
189 }
190 #endif