1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; UrForth level
1: self
-hosting
32-bit Forth compiler
3 ;; Copyright
(C
) 2020 Ketmar Dark
// Invisible Vector
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 note that new thread has pad
, but not tib
. it means that you cannot use tib
11 also
, don
't try to compile code, or TLOAD files: those things won't work
.
12 temp pool won
't work too (FIXME)
16 also thread definitions
18 ;; you can register your cleanups here
19 ;; WARNING! registering is system-wide!
20 : (thread-cleanup) ... ;
23 : (trd-die) ( code -- )
24 \ endcr ." exiting from the thread " os:get-tid . cr
25 >r ['] (thread
-cleanup
) catch
27 \ pad
-allocate endcr
." !!!: " (pad
-tres
) @
. cr
29 ;;pad
-area @ ?dup
if pad
-area #pad
-area
-resv
- #pad
-area
-resv #pad
-area
+ os
:munmap
. cr
endif
35 : trd
-abort
-cleanup
( -- )
36 ERR
-USER
-ERROR
(trd
-die
)
39 : trd
-error
-cleanup
( code
-- )
44 : (thread
-starter
) ( cfa
-- )
47 ['] trd-abort-cleanup (abort-cleanup-ptr) !
48 ['] trd
-abort
-cleanup
(abort
-ptr
) !
49 ['] trd-error-cleanup (error-ptr) !
50 ['] trd
-error
-cleanup
(fatal
-error
-ptr
) !
52 \
." starting thread...\n"
57 : (tls
-sp0
!) ( value baseaddr
-- sp0
) ['] sp0 uservar-@ofs + ! ;
58 : (tls-rp0!) ( value baseaddr -- rp0 ) ['] rp0 uservar
-@ofs
+ ! ;
59 : (tls
-#sp
!) ( value baseaddr
-- sp0
) ['] #sp uservar-@ofs + ! ;
60 : (tls-#rp!) ( value baseaddr -- rp0 ) ['] #rp uservar
-@ofs
+ ! ;
61 : (tls
-size
!) ( value baseaddr
-- size
) ['] forth:(user-full-size) uservar-@ofs + ! ;
62 : (tls-addr!) ( value baseaddr -- size ) ['] forth
:(user
-base
-addr
) uservar
-@ofs
+ ! ;
64 : (tls
-sp0@
) ( baseaddr
-- sp0
) ['] sp0 uservar-@ofs + @ ;
65 : (tls-rp0@) ( baseaddr -- rp0 ) ['] rp0 uservar
-@ofs
+ @
;
66 : (tls
-size@
) ( baseaddr
-- size
) ['] forth:(user-full-size) uservar-@ofs + @ ;
69 ;; this will allocate userarea and stacks for a new thread
70 ;; it will also copy userarea defaults, set addresses, stack sizes, and stack pointers
71 : (alloc-tls) ( dsize rsize -- baseaddr )
72 ;; for simplicity, we will copy the whole user area, even if it isn't wholly used
73 2dup cells swap cells
+ 8 +cells forth
:(user
-area
-max
-size
) + dup
>r
;; ( dsize rsize bytes | bytes
)
74 os
:prot
-r
/w os
:mmap not
-?abort
" cannot allocate thread memory"
76 ;; init with
default user area
77 forth
:(user
-area
-default) r@ forth
:(user
-area
-max
-size
) 0 max cmove
78 ;; setup basic tls params
84 ;; setup stack pointers
85 r@
(tls
-size@
) 4 -cells r@
+ dup r@
(tls
-sp0
!)
86 swap cells
- r@
(tls
-rp0
!)
90 ;; this will be factored
to "spawn-ex" later
91 : spawn
( cfa
-- tid
)
92 ;; allocate memory
for userarea and stacks
93 forth
:(user
-area
-default) ['] #sp uservar-@ofs + @
94 forth:(user-area-default) ['] #rp uservar
-@ofs
+ @
96 ;; thread stack contains cfa and user
-defined data
(cfa is first
to pop
)
98 0x29a r@
(tls
-sp0@
) ! ;; udata
100 ;; prepare other clone args
101 r@ (tls-sp0@) r@ (tls-rp0@)
103 \ os:CLONE-CHILD-CLEARTID or
104 \ os:CLONE-CHILD-SETTID or
105 os:CLONE-DETACHED or ;; pure and useless compatibility flag that does nothing
109 os:CLONE-PARENT-SETTID or
116 ;; create TLS descriptor
117 r> 16 ralloca swap >r dup
118 forth:(user-tls-entry-index) over ! cell+ ;; entry index
119 r@ over ! cell+ ;; base address
120 r@ (tls-size@) 4095 + 12 rshift over ! cell+ ;; limit in pages
121 0b1_0_1_0_00_1 swap ! ;; flags
125 dup 0xffff_f000 u>= if
127 os:errno-name type cr bye
128 r> dup (tls-size@) os:munmap drop
129 abort" cannot create thread!"