comparison druntime/import/core/thread.di @ 760:6f33b427bfd1

Seems like hg ignores .di files, so I missed a bunch of stuff. complete druntime should be there now :)
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Wed, 12 Nov 2008 00:19:18 +0100
parents
children
comparison
equal deleted inserted replaced
759:d3eb054172f9 760:6f33b427bfd1
1 // D import file generated from 'core/thread.d'
2 module core.thread;
3 version = StackGrowsDown;
4 class ThreadException : Exception
5 {
6 this(string msg)
7 {
8 super(msg);
9 }
10 }
11 class FiberException : Exception
12 {
13 this(string msg)
14 {
15 super(msg);
16 }
17 }
18 private
19 {
20 extern (C)
21 {
22 void* rt_stackBottom();
23 }
24 extern (C)
25 {
26 void* rt_stackTop();
27 }
28 void* getStackBottom()
29 {
30 return rt_stackBottom();
31 }
32 void* getStackTop();
33 }
34 version (Windows)
35 {
36 private
37 {
38 import stdc.stdint;
39 import sys.windows.windows;
40 const
41 {
42 DWORD TLS_OUT_OF_INDEXES = -1u;
43 }
44 extern (Windows)
45 {
46 alias uint function(void*) btex_fptr;
47 }
48 extern (C)
49 {
50 uintptr_t _beginthreadex(void*, uint, btex_fptr, void*, uint, uint*);
51 }
52 extern (Windows)
53 {
54 uint thread_entryPoint(void* arg);
55 }
56 HANDLE GetCurrentThreadHandle()
57 {
58 const uint DUPLICATE_SAME_ACCESS = 2;
59 HANDLE curr = GetCurrentThread();
60 HANDLE proc = GetCurrentProcess();
61 HANDLE hndl;
62 DuplicateHandle(proc,curr,proc,&hndl,0,TRUE,DUPLICATE_SAME_ACCESS);
63 return hndl;
64 }
65 }
66 }
67 else
68 {
69 version (Posix)
70 {
71 private
72 {
73 import stdc.posix.semaphore;
74 import stdc.posix.pthread;
75 import stdc.posix.signal;
76 import stdc.posix.time;
77 import stdc.errno;
78 extern (C)
79 {
80 int getErrno();
81 }
82 version (GNU)
83 {
84 import gcc.builtins;
85 }
86 extern (C)
87 {
88 void* thread_entryPoint(void* arg);
89 }
90 sem_t suspendCount;
91 extern (C)
92 {
93 void thread_suspendHandler(int sig);
94 }
95 extern (C)
96 {
97 void thread_resumeHandler(int sig)
98 in
99 {
100 assert(sig == SIGUSR2);
101 }
102 body
103 {
104 }
105 }
106 }
107 }
108 else
109 {
110 static assert(false,"Unknown threading implementation.");
111 }
112 }
113 class Thread
114 {
115 this(void function() fn, size_t sz = 0)
116 in
117 {
118 assert(fn);
119 }
120 body
121 {
122 m_fn = fn;
123 m_sz = sz;
124 m_call = Call.FN;
125 m_curr = &m_main;
126 }
127 this(void delegate() dg, size_t sz = 0)
128 in
129 {
130 assert(dg);
131 }
132 body
133 {
134 m_dg = dg;
135 m_sz = sz;
136 m_call = Call.DG;
137 m_curr = &m_main;
138 }
139 final
140 {
141 void start();
142 }
143 final
144 {
145 Object join(bool rethrow = true);
146 }
147 final
148 {
149 char[] name();
150 }
151 final
152 {
153 void name(char[] val);
154 }
155 final
156 {
157 bool isDaemon();
158 }
159 final
160 {
161 void isDaemon(bool val);
162 }
163 final
164 {
165 bool isRunning();
166 }
167 static const
168 {
169 int PRIORITY_MIN;
170 }
171 static const
172 {
173 int PRIORITY_MAX;
174 }
175 final
176 {
177 int priority();
178 }
179 final
180 {
181 void priority(int val);
182 }
183 static
184 {
185 void sleep(long period);
186 }
187 static
188 {
189 void yield();
190 }
191 static
192 {
193 Thread getThis();
194 }
195 static
196 {
197 Thread[] getAll();
198 }
199 static
200 {
201 int opApply(int delegate(ref Thread) dg);
202 }
203 static const
204 {
205 uint LOCAL_MAX = 64;
206 }
207 static
208 {
209 uint createLocal();
210 }
211 static
212 {
213 void deleteLocal(uint key);
214 }
215 static
216 {
217 void* getLocal(uint key)
218 {
219 return getThis().m_local[key];
220 }
221 }
222 static
223 {
224 void* setLocal(uint key, void* val)
225 {
226 return getThis().m_local[key] = val;
227 }
228 }
229 static this();
230 private
231 {
232 this()
233 {
234 m_call = Call.NO;
235 m_curr = &m_main;
236 }
237 final
238 {
239 void run();
240 }
241 private
242 {
243 enum Call
244 {
245 NO,
246 FN,
247 DG,
248 }
249 version (Win32)
250 {
251 alias uint TLSKey;
252 alias uint ThreadAddr;
253 }
254 else
255 {
256 version (Posix)
257 {
258 alias pthread_key_t TLSKey;
259 alias pthread_t ThreadAddr;
260 }
261 }
262 static
263 {
264 bool[LOCAL_MAX] sm_local;
265 }
266 static
267 {
268 TLSKey sm_this;
269 }
270 void*[LOCAL_MAX] m_local;
271 version (Win32)
272 {
273 HANDLE m_hndl;
274 }
275 ThreadAddr m_addr;
276 Call m_call;
277 char[] m_name;
278 union
279 {
280 void function() m_fn;
281 void delegate() m_dg;
282 }
283 size_t m_sz;
284 version (Posix)
285 {
286 bool m_isRunning;
287 }
288 bool m_isDaemon;
289 Object m_unhandled;
290 private
291 {
292 static
293 {
294 void setThis(Thread t);
295 }
296 private
297 {
298 final
299 {
300 void pushContext(Context* c)
301 in
302 {
303 assert(!c.within);
304 }
305 body
306 {
307 c.within = m_curr;
308 m_curr = c;
309 }
310 }
311 final
312 {
313 void popContext()
314 in
315 {
316 assert(m_curr && m_curr.within);
317 }
318 body
319 {
320 Context* c = m_curr;
321 m_curr = c.within;
322 c.within = null;
323 }
324 }
325 final
326 {
327 Context* topContext()
328 in
329 {
330 assert(m_curr);
331 }
332 body
333 {
334 return m_curr;
335 }
336 }
337 static
338 {
339 struct Context
340 {
341 void* bstack;
342 void* tstack;
343 Context* within;
344 Context* next;
345 Context* prev;
346 }
347 }
348 Context m_main;
349 Context* m_curr;
350 bool m_lock;
351 version (Win32)
352 {
353 uint[8] m_reg;
354 }
355 private
356 {
357 static
358 {
359 Object slock()
360 {
361 return Thread.classinfo;
362 }
363 }
364 static
365 {
366 Context* sm_cbeg;
367 }
368 static
369 {
370 size_t sm_clen;
371 }
372 static
373 {
374 Thread sm_tbeg;
375 }
376 static
377 {
378 size_t sm_tlen;
379 }
380 Thread prev;
381 Thread next;
382 static
383 {
384 void add(Context* c);
385 }
386 static
387 {
388 void remove(Context* c);
389 }
390 static
391 {
392 void add(Thread t);
393 }
394 static
395 {
396 void remove(Thread t);
397 }
398 }
399 }
400 }
401 }
402 }
403 }
404 extern (C)
405 {
406 void thread_init();
407 }
408 extern (C)
409 {
410 void thread_attachThis();
411 }
412 extern (C)
413 {
414 void thread_detachThis()
415 {
416 Thread.remove(Thread.getThis());
417 }
418 }
419 extern (C)
420 {
421 void thread_joinAll();
422 }
423 private
424 {
425 bool multiThreadedFlag = false;
426 }
427 extern (C)
428 {
429 bool thread_needLock()
430 {
431 return multiThreadedFlag;
432 }
433 }
434 private
435 {
436 uint suspendDepth = 0;
437 }
438 extern (C)
439 {
440 void thread_suspendAll();
441 }
442 extern (C)
443 {
444 void thread_resumeAll();
445 }
446 private
447 {
448 alias void delegate(void*, void*) scanAllThreadsFn;
449 }
450 extern (C)
451 {
452 void thread_scanAll(scanAllThreadsFn scan, void* curStackTop = null);
453 }
454 template ThreadLocal(T)
455 {
456 class ThreadLocal
457 {
458 this(T def = T.init)
459 {
460 m_def = def;
461 m_key = Thread.createLocal();
462 }
463 T val()
464 {
465 Wrap* wrap = cast(Wrap*)Thread.getLocal(m_key);
466 return wrap ? wrap.val : m_def;
467 }
468 T val(T newval)
469 {
470 Wrap* wrap = cast(Wrap*)Thread.getLocal(m_key);
471 if (wrap is null)
472 {
473 wrap = new Wrap;
474 Thread.setLocal(m_key,wrap);
475 }
476 wrap.val = newval;
477 return newval;
478 }
479 private
480 {
481 struct Wrap
482 {
483 T val;
484 }
485 T m_def;
486 uint m_key;
487 }
488 }
489 }
490 class ThreadGroup
491 {
492 final
493 {
494 Thread create(void function() fn);
495 }
496 final
497 {
498 Thread create(void delegate() dg);
499 }
500 final
501 {
502 void add(Thread t);
503 }
504 final
505 {
506 void remove(Thread t);
507 }
508 final
509 {
510 int opApply(int delegate(ref Thread) dg);
511 }
512 final
513 {
514 void joinAll(bool rethrow = true);
515 }
516 private
517 {
518 Thread[Thread] m_all;
519 }
520 }
521 private
522 {
523 version (D_InlineAsm_X86)
524 {
525 version (X86_64)
526 {
527 }
528 else
529 {
530 version (Win32)
531 {
532 version = AsmX86_Win32;
533 }
534 else
535 {
536 version (Posix)
537 {
538 version = AsmX86_Posix;
539 }
540 }
541 }
542 }
543 else
544 {
545 version (PPC)
546 {
547 version (Posix)
548 {
549 version = AsmPPC_Posix;
550 }
551 }
552 }
553 version (LLVM_InlineAsm_X86)
554 {
555 version (Win32)
556 {
557 version = LLVM_AsmX86_Win32;
558 }
559 else
560 {
561 version (Posix)
562 {
563 version = LLVM_AsmX86_Posix;
564 }
565 }
566 }
567 else
568 {
569 version (LLVM_InlineAsm_X86_64)
570 {
571 version (Posix)
572 {
573 version = LLVM_AsmX86_64_Posix;
574 }
575 }
576 }
577 version (Posix)
578 {
579 import stdc.posix.unistd;
580 import stdc.posix.sys.mman;
581 import stdc.posix.stdlib;
582 version (AsmX86_Win32)
583 {
584 }
585 else
586 {
587 version (AsmX86_Posix)
588 {
589 }
590 else
591 {
592 version (AsmPPC_Posix)
593 {
594 }
595 else
596 {
597 version (LLVM_AsmX86_Win32)
598 {
599 }
600 else
601 {
602 version (LLVM_AsmX86_Posix)
603 {
604 }
605 else
606 {
607 import stdc.posix.ucontext;
608 }
609 }
610 }
611 }
612 }
613 }
614 const
615 {
616 size_t PAGESIZE;
617 }
618 }
619 static this();
620 private
621 {
622 extern (C)
623 {
624 void fiber_entryPoint();
625 }
626 version (AsmPPC_Posix)
627 {
628 extern (C)
629 {
630 void fiber_switchContext(void** oldp, void* newp);
631 }
632 }
633 else
634 {
635 extern (C)
636 {
637 void fiber_switchContext(void** oldp, void* newp);
638 }
639 }
640 }
641 class Fiber
642 {
643 this(void function() fn, size_t sz = PAGESIZE)
644 in
645 {
646 assert(fn);
647 }
648 body
649 {
650 m_fn = fn;
651 m_call = Call.FN;
652 m_state = State.HOLD;
653 allocStack(sz);
654 initStack();
655 }
656 this(void delegate() dg, size_t sz = PAGESIZE)
657 in
658 {
659 assert(dg);
660 }
661 body
662 {
663 m_dg = dg;
664 m_call = Call.DG;
665 m_state = State.HOLD;
666 allocStack(sz);
667 initStack();
668 }
669 final
670 {
671 Object call(bool rethrow = true);
672 }
673 final
674 {
675 void reset()
676 in
677 {
678 assert(m_state == State.TERM);
679 assert(m_ctxt.tstack == m_ctxt.bstack);
680 }
681 body
682 {
683 m_state = State.HOLD;
684 initStack();
685 m_unhandled = null;
686 }
687 }
688 enum State
689 {
690 HOLD,
691 EXEC,
692 TERM,
693 }
694 final
695 {
696 State state()
697 {
698 return m_state;
699 }
700 }
701 static
702 {
703 void yield();
704 }
705 static
706 {
707 void yieldAndThrow(Object obj);
708 }
709 static
710 {
711 Fiber getThis();
712 }
713 static this();
714 private
715 {
716 this()
717 {
718 m_call = Call.NO;
719 }
720 final
721 {
722 void run();
723 }
724 private
725 {
726 enum Call
727 {
728 NO,
729 FN,
730 DG,
731 }
732 Call m_call;
733 union
734 {
735 void function() m_fn;
736 void delegate() m_dg;
737 }
738 bool m_isRunning;
739 Object m_unhandled;
740 State m_state;
741 private
742 {
743 final
744 {
745 void allocStack(size_t sz);
746 }
747 final
748 {
749 void freeStack();
750 }
751 final
752 {
753 void initStack();
754 }
755 Thread.Context* m_ctxt;
756 size_t m_size;
757 void* m_pmem;
758 static if(is(ucontext_t))
759 {
760 static
761 {
762 ucontext_t sm_utxt = void;
763 }
764 ucontext_t m_utxt = void;
765 ucontext_t* m_ucur = null;
766 }
767 private
768 {
769 static
770 {
771 void setThis(Fiber f);
772 }
773 static
774 {
775 Thread.TLSKey sm_this;
776 }
777 private
778 {
779 final
780 {
781 void switchIn();
782 }
783 final
784 {
785 void switchOut();
786 }
787 }
788 }
789 }
790 }
791 }
792 }