2 #include <linux/futex.h>
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>
13 __thread struct task_struct *current;
15 void __put_task_struct(struct task_struct *t)
17 pthread_join(t->thread, NULL);
21 /* returns true if process was woken up, false if it was already running */
22 int wake_up_process(struct task_struct *p)
24 int ret = p->state != TASK_RUNNING;
26 p->state = TASK_RUNNING;
27 futex(&p->state, FUTEX_WAKE|FUTEX_PRIVATE_FLAG,
28 INT_MAX, NULL, NULL, 0);
36 rcu_quiescent_state();
38 while ((v = current->state) != TASK_RUNNING)
39 futex(¤t->state, FUTEX_WAIT|FUTEX_PRIVATE_FLAG,
43 static void process_timeout(unsigned long __data)
45 wake_up_process((struct task_struct *)__data);
48 long schedule_timeout(long timeout)
50 struct timer_list timer;
55 case MAX_SCHEDULE_TIMEOUT:
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.
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.
74 printk(KERN_ERR "schedule_timeout: wrong timeout "
75 "value %lx\n", timeout);
76 current->state = TASK_RUNNING;
81 expire = timeout + jiffies;
83 setup_timer(&timer, process_timeout, (unsigned long)current);
84 mod_timer(&timer, expire);
86 del_timer_sync(&timer);
88 timeout = expire - jiffies;
90 return timeout < 0 ? 0 : timeout;
93 unsigned long __msecs_to_jiffies(const unsigned int m)
96 * Negative value, means infinite timeout:
99 return MAX_JIFFY_OFFSET;
100 return _msecs_to_jiffies(m);
103 u64 nsecs_to_jiffies64(u64 n)
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);
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.
116 return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
120 unsigned long nsecs_to_jiffies(u64 n)
122 return (unsigned long)nsecs_to_jiffies64(n);
125 unsigned int jiffies_to_msecs(const unsigned long j)
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);
132 # if BITS_PER_LONG == 32
133 return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
135 return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
140 unsigned int jiffies_to_usecs(const unsigned long j)
143 * Hz usually doesn't go much further MSEC_PER_SEC.
144 * jiffies_to_usecs() and usecs_to_jiffies() depend on that.
146 BUILD_BUG_ON(HZ > USEC_PER_SEC);
148 #if !(USEC_PER_SEC % HZ)
149 return (USEC_PER_SEC / HZ) * j;
151 # if BITS_PER_LONG == 32
152 return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
154 return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
159 __attribute__((constructor(101)))
160 static void sched_init(void)
162 struct task_struct *p = malloc(sizeof(*p));
164 mlockall(MCL_CURRENT|MCL_FUTURE);
166 memset(p, 0, sizeof(*p));
168 p->state = TASK_RUNNING;
169 atomic_set(&p->usage, 1);
170 init_completion(&p->exited);
175 rcu_register_thread();
178 #ifndef __NR_getrandom
180 #include <sys/stat.h>
181 #include <sys/types.h>
184 __attribute__((constructor(101)))
185 static void rand_init(void)
187 urandom_fd = open("/dev/urandom", O_RDONLY);
188 BUG_ON(urandom_fd < 0);