2 #include <linux/futex.h>
7 #define CONFIG_RCU_HAVE_FUTEX 1
8 #include <urcu/futex.h>
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>
17 __thread struct task_struct *current;
19 void __put_task_struct(struct task_struct *t)
21 pthread_join(t->thread, NULL);
25 /* returns true if process was woken up, false if it was already running */
26 int wake_up_process(struct task_struct *p)
28 int ret = p->state != TASK_RUNNING;
30 p->state = TASK_RUNNING;
31 futex(&p->state, FUTEX_WAKE|FUTEX_PRIVATE_FLAG,
32 INT_MAX, NULL, NULL, 0);
40 rcu_quiescent_state();
42 while ((v = current->state) != TASK_RUNNING)
43 futex(¤t->state, FUTEX_WAIT|FUTEX_PRIVATE_FLAG,
47 struct process_timer {
48 struct timer_list timer;
49 struct task_struct *task;
52 static void process_timeout(struct timer_list *t)
54 struct process_timer *timeout =
55 container_of(t, struct process_timer, timer);
57 wake_up_process(timeout->task);
60 long schedule_timeout(long timeout)
62 struct process_timer timer;
67 case MAX_SCHEDULE_TIMEOUT:
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.
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.
86 printk(KERN_ERR "schedule_timeout: wrong timeout "
87 "value %lx\n", timeout);
88 current->state = TASK_RUNNING;
93 expire = timeout + jiffies;
96 timer_setup_on_stack(&timer.timer, process_timeout, 0);
97 mod_timer(&timer.timer, expire);
99 del_timer_sync(&timer.timer);
101 timeout = expire - jiffies;
103 return timeout < 0 ? 0 : timeout;
106 unsigned long __msecs_to_jiffies(const unsigned int m)
109 * Negative value, means infinite timeout:
112 return MAX_JIFFY_OFFSET;
113 return _msecs_to_jiffies(m);
116 u64 nsecs_to_jiffies64(u64 n)
118 #if (NSEC_PER_SEC % HZ) == 0
119 /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
120 return div_u64(n, NSEC_PER_SEC / HZ);
121 #elif (HZ % 512) == 0
122 /* overflow after 292 years if HZ = 1024 */
123 return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
126 * Generic case - optimized for cases where HZ is a multiple of 3.
127 * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
129 return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
133 unsigned long nsecs_to_jiffies(u64 n)
135 return (unsigned long)nsecs_to_jiffies64(n);
138 unsigned int jiffies_to_msecs(const unsigned long j)
140 #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
141 return (MSEC_PER_SEC / HZ) * j;
142 #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
143 return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
145 # if BITS_PER_LONG == 32
146 return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
148 return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
153 unsigned int jiffies_to_usecs(const unsigned long j)
156 * Hz usually doesn't go much further MSEC_PER_SEC.
157 * jiffies_to_usecs() and usecs_to_jiffies() depend on that.
159 BUILD_BUG_ON(HZ > USEC_PER_SEC);
161 #if !(USEC_PER_SEC % HZ)
162 return (USEC_PER_SEC / HZ) * j;
164 # if BITS_PER_LONG == 32
165 return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
167 return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
172 __attribute__((constructor(101)))
173 static void sched_init(void)
175 struct task_struct *p = malloc(sizeof(*p));
177 mlockall(MCL_CURRENT|MCL_FUTURE);
179 memset(p, 0, sizeof(*p));
181 p->state = TASK_RUNNING;
182 atomic_set(&p->usage, 1);
183 init_completion(&p->exited);
188 rcu_register_thread();
191 #ifndef __NR_getrandom
193 #include <sys/stat.h>
194 #include <sys/types.h>
197 __attribute__((constructor(101)))
198 static void rand_init(void)
200 urandom_fd = open("/dev/urandom", O_RDONLY);
201 BUG_ON(urandom_fd < 0);