Subversion Repositories Kolibri OS

Rev

Rev 485 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 halyavin 1
;
2
;   THREAD EXAMPLE
3
;
4
;   Compile with FASM for Menuet
5
;
6
 
7
  use32
8
  org    0x0
9
 
10
  db     'MENUET01'             ; 8 byte id
11
  dd     0x01                   ; header version
12
  dd     START                  ; start of code
13
  dd     I_END                  ; size of image
485 heavyiron 14
  dd     0x2000                ; memory for app
15
  dd     0x2000                ; esp
31 halyavin 16
  dd     0x0 , 0x0              ; I_Param , I_Icon
17
 
18
include 'lang.inc'
485 heavyiron 19
include '..\..\..\..\macros.inc'
31 halyavin 20
 
21
 
22
START:                          ; start of execution
23
 
485 heavyiron 24
red:                            ; redraw
31 halyavin 25
    call draw_window            ; at first, draw the window
26
 
27
still:
28
 
29
    mov  eax,10                 ; wait here for event
485 heavyiron 30
    mcall
31 halyavin 31
 
485 heavyiron 32
    dec  eax                    ; redraw request ?
31 halyavin 33
    je   red
485 heavyiron 34
    dec  eax                    ; key in buffer ?
35
    jne  button
31 halyavin 36
 
37
  key:                          ; key
38
    mov  eax,2                  ; just read it and ignore
485 heavyiron 39
    mcall
31 halyavin 40
    jmp  still
41
 
42
  button:                       ; button
43
    mov  eax,17                 ; get id
485 heavyiron 44
    mcall
31 halyavin 45
 
46
    cmp  ah,1                   ; button id=1 ?
47
    jne  noclose
485 heavyiron 48
    or   eax,-1                 ; close this program (thread)
49
    mcall
31 halyavin 50
  noclose:
51
 
52
    cmp  ah,2
53
    jne  no_thread
54
 
55
    cmp  [thread_stack],0x1f00
56
    jge  no_thread
57
 
58
    add  [thread_stack],0x100
59
    mov  eax,51
60
    mov  ebx,1
61
    mov  ecx,START
62
    mov  edx,[thread_stack]
485 heavyiron 63
    mcall
31 halyavin 64
 
65
    jmp  still
66
 
67
  no_thread:
68
 
69
    jmp  still
70
 
485 heavyiron 71
 
31 halyavin 72
thread_stack dd 0x1000
73
 
74
 
75
;   *********************************************
76
;   *******  WINDOW DEFINITIONS AND DRAW ********
77
;   *********************************************
78
 
79
 
80
draw_window:
81
 
82
    mov  eax,12                    ; function 12:tell os about windowdraw
83
    mov  ebx,1                     ; 1, start of draw
485 heavyiron 84
    mcall
31 halyavin 85
 
86
                                   ; DRAW WINDOW
485 heavyiron 87
    xor  eax,eax                   ; function 0 : define and draw window
31 halyavin 88
    mov  ebx,10*65536+290         ; [x start] *65536 + [x size]
89
    mov  ecx,10*65536+130         ; [y start] *65536 + [y size]
90
    mov  esi,[thread_stack]
91
    sub  esi,0x1000
92
    shr  esi,4
93
    shl  esi,16
94
    add  ebx,esi
95
    add  ecx,esi
551 spraid 96
    mov  edx,0x34ffffff            ; color of work area RRGGBB,8->color gl
485 heavyiron 97
    mov  edi,title               ; WINDOW LABEL
98
    mcall
31 halyavin 99
 
485 heavyiron 100
 
31 halyavin 101
    mov  eax,8                     ; NEW THREAD BUTTON
485 heavyiron 102
    mov  ebx,20*65536+128
103
    mov  ecx,63*65536+20
31 halyavin 104
    mov  edx,2
105
    mov  esi,0x90b0d0 ;0x5577cc
485 heavyiron 106
    mcall
31 halyavin 107
 
485 heavyiron 108
    mov  eax,4
109
    mov  ebx,20*65536+10           ; draw info text with function 4
31 halyavin 110
    mov  ecx,0x224466
111
    mov  edx,text
112
    mov  esi,40
113
  newline:
485 heavyiron 114
    mcall
31 halyavin 115
    add  ebx,10
116
    add  edx,40
117
    cmp  [edx],byte 'x'
118
    jne  newline
119
 
120
 
121
    mov  eax,12                    ; function 12:tell os about windowdraw
122
    mov  ebx,2                     ; 2, end of draw
485 heavyiron 123
    mcall
31 halyavin 124
 
125
    ret
126
 
127
 
128
; DATA AREA
129
 
130
if lang eq ru
131
  text:
485 heavyiron 132
      db 'Эта программа создает потоки, запуская  '
133
      db 'один и тот же код много раз. Нам нужно  '
134
      db 'только позаботиться об отдельном стэке  '
135
      db 'для каждого потока.                     '
136
      db 'Память для всех потоков общая.          '
31 halyavin 137
      db '                                        '
138
      db ' СОЗДАТЬ НОВЫЙ ПОТОК                    '
139
      db 'x' ; <- END MARKER, DONT DELETE
140
 
485 heavyiron 141
  title   db   'Пример использования потоков',0
142
 
31 halyavin 143
else
144
  text:
145
      db 'THIS EXAMPLE CREATES THREADS BY RUNNING '
146
      db 'THE SAME CODE MULTIPLE TIMES. ALL WE    '
147
      db 'NEED IS A NEW STACK FOR EACH THREAD.    '
148
      db 'ALL THREADS SHARE THE SAME MEMORY.      '
149
      db '                                        '
150
      db '                                        '
151
      db '  CREATE NEW THREAD                     '
152
      db 'x' ; <- END MARKER, DONT DELETE
153
 
485 heavyiron 154
  title   db   'THREAD EXAMPLE',0
155
 
31 halyavin 156
end if
157
I_END: