Changeset 152 for MicroThreading


Ignore:
Timestamp:
Jan 27, 2011, 10:30:55 AM (13 years ago)
Author:
george
Message:
  • Fixed: Wrong context restoration after return from micro thread.
  • Add: Option of TMicroThreadScheduler to set use or not use main thread for scheduling.
Location:
MicroThreading
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/Demo.lpi

    r151 r152  
    3939      </Item2>
    4040    </RequiredPackages>
    41     <Units Count="57">
     41    <Units Count="61">
    4242      <Unit0>
    4343        <Filename Value="Demo.lpr"/>
     
    4747        <TopLine Value="1"/>
    4848        <CursorPos X="5" Y="9"/>
    49         <UsageCount Value="62"/>
     49        <UsageCount Value="67"/>
    5050      </Unit0>
    5151      <Unit1>
     
    5757        <EditorIndex Value="0"/>
    5858        <WindowIndex Value="0"/>
    59         <TopLine Value="252"/>
    60         <CursorPos X="1" Y="274"/>
    61         <UsageCount Value="62"/>
     59        <TopLine Value="268"/>
     60        <CursorPos X="1" Y="288"/>
     61        <UsageCount Value="67"/>
    6262        <Loaded Value="True"/>
    6363        <LoadedDesigner Value="True"/>
     
    6767        <UnitName Value="UMicroThreading"/>
    6868        <IsVisibleTab Value="True"/>
    69         <EditorIndex Value="1"/>
    70         <WindowIndex Value="0"/>
    71         <TopLine Value="258"/>
    72         <CursorPos X="1" Y="271"/>
    73         <UsageCount Value="34"/>
     69        <EditorIndex Value="2"/>
     70        <WindowIndex Value="0"/>
     71        <TopLine Value="272"/>
     72        <CursorPos X="23" Y="288"/>
     73        <UsageCount Value="37"/>
    7474        <Loaded Value="True"/>
    7575      </Unit2>
     
    8080        <TopLine Value="55"/>
    8181        <CursorPos X="3" Y="70"/>
    82         <UsageCount Value="10"/>
     82        <UsageCount Value="9"/>
    8383      </Unit3>
    8484      <Unit4>
     
    8787        <TopLine Value="346"/>
    8888        <CursorPos X="24" Y="351"/>
    89         <UsageCount Value="8"/>
     89        <UsageCount Value="7"/>
    9090      </Unit4>
    9191      <Unit5>
     
    9494        <TopLine Value="1"/>
    9595        <CursorPos X="1" Y="1"/>
    96         <UsageCount Value="8"/>
     96        <UsageCount Value="7"/>
    9797        <DefaultSyntaxHighlighter Value="XML"/>
    9898      </Unit5>
     
    103103        <TopLine Value="286"/>
    104104        <CursorPos X="1" Y="1"/>
    105         <UsageCount Value="8"/>
     105        <UsageCount Value="7"/>
    106106      </Unit6>
    107107      <Unit7>
     
    111111        <TopLine Value="1"/>
    112112        <CursorPos X="1" Y="8"/>
    113         <UsageCount Value="10"/>
     113        <UsageCount Value="9"/>
    114114        <DefaultSyntaxHighlighter Value="Delphi"/>
    115115      </Unit7>
     
    120120        <TopLine Value="56"/>
    121121        <CursorPos X="24" Y="77"/>
    122         <UsageCount Value="19"/>
     122        <UsageCount Value="18"/>
    123123        <DefaultSyntaxHighlighter Value="Delphi"/>
    124124      </Unit8>
     
    129129        <TopLine Value="264"/>
    130130        <CursorPos X="37" Y="141"/>
    131         <UsageCount Value="19"/>
     131        <UsageCount Value="18"/>
    132132        <DefaultSyntaxHighlighter Value="Delphi"/>
    133133      </Unit9>
     
    138138        <TopLine Value="302"/>
    139139        <CursorPos X="5" Y="33"/>
    140         <UsageCount Value="8"/>
     140        <UsageCount Value="7"/>
    141141      </Unit10>
    142142      <Unit11>
     
    145145        <TopLine Value="479"/>
    146146        <CursorPos X="3" Y="494"/>
    147         <UsageCount Value="11"/>
     147        <UsageCount Value="10"/>
    148148      </Unit11>
    149149      <Unit12>
     
    152152        <TopLine Value="1046"/>
    153153        <CursorPos X="34" Y="1053"/>
    154         <UsageCount Value="17"/>
     154        <UsageCount Value="16"/>
    155155      </Unit12>
    156156      <Unit13>
     
    159159        <TopLine Value="317"/>
    160160        <CursorPos X="1" Y="341"/>
    161         <UsageCount Value="10"/>
     161        <UsageCount Value="9"/>
    162162      </Unit13>
    163163      <Unit14>
     
    166166        <TopLine Value="45"/>
    167167        <CursorPos X="1" Y="62"/>
    168         <UsageCount Value="10"/>
     168        <UsageCount Value="9"/>
    169169      </Unit14>
    170170      <Unit15>
     
    173173        <TopLine Value="1670"/>
    174174        <CursorPos X="1" Y="1685"/>
    175         <UsageCount Value="11"/>
     175        <UsageCount Value="10"/>
    176176      </Unit15>
    177177      <Unit16>
     
    181181        <TopLine Value="290"/>
    182182        <CursorPos X="36" Y="306"/>
    183         <UsageCount Value="20"/>
     183        <UsageCount Value="19"/>
    184184      </Unit16>
    185185      <Unit17>
     
    188188        <TopLine Value="67"/>
    189189        <CursorPos X="10" Y="91"/>
    190         <UsageCount Value="8"/>
     190        <UsageCount Value="7"/>
    191191      </Unit17>
    192192      <Unit18>
     
    195195        <TopLine Value="1823"/>
    196196        <CursorPos X="3" Y="1838"/>
    197         <UsageCount Value="12"/>
     197        <UsageCount Value="11"/>
    198198      </Unit18>
    199199      <Unit19>
     
    202202        <TopLine Value="1348"/>
    203203        <CursorPos X="1" Y="1363"/>
    204         <UsageCount Value="10"/>
     204        <UsageCount Value="9"/>
    205205      </Unit19>
    206206      <Unit20>
     
    209209        <TopLine Value="32"/>
    210210        <CursorPos X="48" Y="50"/>
    211         <UsageCount Value="9"/>
     211        <UsageCount Value="8"/>
    212212      </Unit20>
    213213      <Unit21>
     
    216216        <TopLine Value="8"/>
    217217        <CursorPos X="11" Y="25"/>
    218         <UsageCount Value="11"/>
     218        <UsageCount Value="10"/>
    219219      </Unit21>
    220220      <Unit22>
     
    224224        <TopLine Value="1"/>
    225225        <CursorPos X="25" Y="9"/>
    226         <UsageCount Value="12"/>
     226        <UsageCount Value="11"/>
    227227      </Unit22>
    228228      <Unit23>
     
    231231        <TopLine Value="38"/>
    232232        <CursorPos X="29" Y="50"/>
    233         <UsageCount Value="16"/>
     233        <UsageCount Value="15"/>
    234234      </Unit23>
    235235      <Unit24>
     
    238238        <TopLine Value="106"/>
    239239        <CursorPos X="10" Y="119"/>
    240         <UsageCount Value="19"/>
     240        <UsageCount Value="18"/>
    241241      </Unit24>
    242242      <Unit25>
     
    245245        <TopLine Value="254"/>
    246246        <CursorPos X="12" Y="258"/>
    247         <UsageCount Value="12"/>
     247        <UsageCount Value="11"/>
    248248      </Unit25>
    249249      <Unit26>
     
    253253        <TopLine Value="581"/>
    254254        <CursorPos X="11" Y="585"/>
    255         <UsageCount Value="12"/>
     255        <UsageCount Value="11"/>
    256256      </Unit26>
    257257      <Unit27>
     
    260260        <TopLine Value="525"/>
    261261        <CursorPos X="11" Y="538"/>
    262         <UsageCount Value="12"/>
     262        <UsageCount Value="11"/>
    263263      </Unit27>
    264264      <Unit28>
     
    268268        <TopLine Value="57"/>
    269269        <CursorPos X="16" Y="70"/>
    270         <UsageCount Value="13"/>
     270        <UsageCount Value="12"/>
    271271      </Unit28>
    272272      <Unit29>
     
    275275        <TopLine Value="18"/>
    276276        <CursorPos X="50" Y="31"/>
    277         <UsageCount Value="13"/>
     277        <UsageCount Value="12"/>
    278278      </Unit29>
    279279      <Unit30>
     
    282282        <TopLine Value="109"/>
    283283        <CursorPos X="3" Y="112"/>
    284         <UsageCount Value="13"/>
     284        <UsageCount Value="12"/>
    285285      </Unit30>
    286286      <Unit31>
     
    289289        <TopLine Value="12"/>
    290290        <CursorPos X="3" Y="25"/>
    291         <UsageCount Value="13"/>
     291        <UsageCount Value="12"/>
    292292      </Unit31>
    293293      <Unit32>
    294294        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classesh.inc"/>
    295         <WindowIndex Value="0"/>
    296         <TopLine Value="1504"/>
    297         <CursorPos X="1" Y="1510"/>
    298         <UsageCount Value="16"/>
     295        <EditorIndex Value="6"/>
     296        <WindowIndex Value="0"/>
     297        <TopLine Value="1466"/>
     298        <CursorPos X="15" Y="1479"/>
     299        <UsageCount Value="18"/>
     300        <Loaded Value="True"/>
    299301      </Unit32>
    300302      <Unit33>
    301303        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/tthread.inc"/>
    302         <WindowIndex Value="0"/>
    303         <TopLine Value="96"/>
    304         <CursorPos X="15" Y="98"/>
    305         <UsageCount Value="13"/>
     304        <EditorIndex Value="7"/>
     305        <WindowIndex Value="0"/>
     306        <TopLine Value="52"/>
     307        <CursorPos X="23" Y="55"/>
     308        <UsageCount Value="15"/>
     309        <Loaded Value="True"/>
    306310      </Unit33>
    307311      <Unit34>
     
    310314        <TopLine Value="130"/>
    311315        <CursorPos X="10" Y="143"/>
    312         <UsageCount Value="13"/>
     316        <UsageCount Value="12"/>
    313317      </Unit34>
    314318      <Unit35>
     
    317321        <TopLine Value="1923"/>
    318322        <CursorPos X="6" Y="1936"/>
    319         <UsageCount Value="13"/>
     323        <UsageCount Value="12"/>
    320324      </Unit35>
    321325      <Unit36>
    322326        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classes.inc"/>
    323         <WindowIndex Value="0"/>
    324         <TopLine Value="109"/>
    325         <CursorPos X="3" Y="111"/>
    326         <UsageCount Value="16"/>
     327        <EditorIndex Value="8"/>
     328        <WindowIndex Value="0"/>
     329        <TopLine Value="85"/>
     330        <CursorPos X="21" Y="107"/>
     331        <UsageCount Value="18"/>
     332        <Loaded Value="True"/>
    327333      </Unit36>
    328334      <Unit37>
     
    331337        <TopLine Value="181"/>
    332338        <CursorPos X="21" Y="194"/>
    333         <UsageCount Value="13"/>
     339        <UsageCount Value="12"/>
    334340      </Unit37>
    335341      <Unit38>
    336342        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/sysutils/osutilsh.inc"/>
     343        <EditorIndex Value="3"/>
    337344        <WindowIndex Value="0"/>
    338345        <TopLine Value="14"/>
    339346        <CursorPos X="11" Y="27"/>
    340347        <UsageCount Value="13"/>
     348        <Loaded Value="True"/>
    341349      </Unit38>
    342350      <Unit39>
     
    345353        <TopLine Value="2275"/>
    346354        <CursorPos X="39" Y="2285"/>
    347         <UsageCount Value="12"/>
     355        <UsageCount Value="11"/>
    348356      </Unit39>
    349357      <Unit40>
     
    352360        <TopLine Value="492"/>
    353361        <CursorPos X="3" Y="504"/>
    354         <UsageCount Value="10"/>
     362        <UsageCount Value="9"/>
    355363      </Unit40>
    356364      <Unit41>
     
    359367        <TopLine Value="197"/>
    360368        <CursorPos X="30" Y="208"/>
    361         <UsageCount Value="10"/>
     369        <UsageCount Value="9"/>
    362370      </Unit41>
    363371      <Unit42>
     
    366374        <TopLine Value="737"/>
    367375        <CursorPos X="21" Y="747"/>
    368         <UsageCount Value="10"/>
     376        <UsageCount Value="9"/>
    369377      </Unit42>
    370378      <Unit43>
     
    373381        <TopLine Value="96"/>
    374382        <CursorPos X="1" Y="1"/>
    375         <UsageCount Value="10"/>
     383        <UsageCount Value="9"/>
    376384      </Unit43>
    377385      <Unit44>
     
    380388        <TopLine Value="221"/>
    381389        <CursorPos X="1" Y="235"/>
    382         <UsageCount Value="13"/>
     390        <UsageCount Value="12"/>
    383391      </Unit44>
    384392      <Unit45>
     
    387395        <TopLine Value="387"/>
    388396        <CursorPos X="1" Y="400"/>
    389         <UsageCount Value="13"/>
     397        <UsageCount Value="12"/>
    390398      </Unit45>
    391399      <Unit46>
     
    394402        <TopLine Value="6081"/>
    395403        <CursorPos X="6" Y="6094"/>
    396         <UsageCount Value="13"/>
     404        <UsageCount Value="12"/>
    397405      </Unit46>
    398406      <Unit47>
     
    401409        <TopLine Value="160"/>
    402410        <CursorPos X="12" Y="163"/>
    403         <UsageCount Value="10"/>
     411        <UsageCount Value="9"/>
    404412      </Unit47>
    405413      <Unit48>
    406414        <Filename Value="../UPlatform.pas"/>
    407415        <UnitName Value="UPlatform"/>
    408         <EditorIndex Value="2"/>
     416        <EditorIndex Value="10"/>
    409417        <WindowIndex Value="0"/>
    410418        <TopLine Value="1"/>
    411419        <CursorPos X="24" Y="9"/>
    412         <UsageCount Value="14"/>
     420        <UsageCount Value="17"/>
    413421        <Loaded Value="True"/>
    414422      </Unit48>
     
    418426        <TopLine Value="9153"/>
    419427        <CursorPos X="8" Y="9166"/>
    420         <UsageCount Value="10"/>
     428        <UsageCount Value="9"/>
    421429      </Unit49>
    422430      <Unit50>
     
    426434        <TopLine Value="1"/>
    427435        <CursorPos X="1" Y="1"/>
    428         <UsageCount Value="10"/>
     436        <UsageCount Value="9"/>
    429437      </Unit50>
    430438      <Unit51>
     
    433441        <TopLine Value="2380"/>
    434442        <CursorPos X="30" Y="2390"/>
    435         <UsageCount Value="10"/>
     443        <UsageCount Value="9"/>
    436444      </Unit51>
    437445      <Unit52>
     
    440448        <TopLine Value="150"/>
    441449        <CursorPos X="55" Y="162"/>
    442         <UsageCount Value="10"/>
     450        <UsageCount Value="9"/>
    443451      </Unit52>
    444452      <Unit53>
     
    447455        <TopLine Value="5227"/>
    448456        <CursorPos X="1" Y="5242"/>
    449         <UsageCount Value="10"/>
     457        <UsageCount Value="9"/>
    450458      </Unit53>
    451459      <Unit54>
     
    455463        <TopLine Value="102"/>
    456464        <CursorPos X="1" Y="121"/>
    457         <UsageCount Value="10"/>
     465        <UsageCount Value="9"/>
    458466      </Unit54>
    459467      <Unit55>
     
    462470        <TopLine Value="3515"/>
    463471        <CursorPos X="1" Y="3531"/>
    464         <UsageCount Value="10"/>
     472        <UsageCount Value="9"/>
    465473      </Unit55>
    466474      <Unit56>
     
    469477        <TopLine Value="1159"/>
    470478        <CursorPos X="1" Y="1174"/>
    471         <UsageCount Value="10"/>
     479        <UsageCount Value="9"/>
    472480      </Unit56>
     481      <Unit57>
     482        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/lists.inc"/>
     483        <EditorIndex Value="9"/>
     484        <WindowIndex Value="0"/>
     485        <TopLine Value="590"/>
     486        <CursorPos X="14" Y="594"/>
     487        <UsageCount Value="12"/>
     488        <Loaded Value="True"/>
     489      </Unit57>
     490      <Unit58>
     491        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32callback.inc"/>
     492        <EditorIndex Value="5"/>
     493        <WindowIndex Value="0"/>
     494        <TopLine Value="1086"/>
     495        <CursorPos X="1" Y="1099"/>
     496        <UsageCount Value="12"/>
     497        <Loaded Value="True"/>
     498      </Unit58>
     499      <Unit59>
     500        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/extctrls.pp"/>
     501        <UnitName Value="ExtCtrls"/>
     502        <EditorIndex Value="1"/>
     503        <WindowIndex Value="0"/>
     504        <TopLine Value="357"/>
     505        <CursorPos X="3" Y="370"/>
     506        <UsageCount Value="12"/>
     507        <Loaded Value="True"/>
     508      </Unit59>
     509      <Unit60>
     510        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/customtimer.pas"/>
     511        <UnitName Value="CustomTimer"/>
     512        <EditorIndex Value="4"/>
     513        <WindowIndex Value="0"/>
     514        <TopLine Value="40"/>
     515        <CursorPos X="17" Y="53"/>
     516        <UsageCount Value="11"/>
     517        <Loaded Value="True"/>
     518      </Unit60>
    473519    </Units>
    474     <JumpHistory Count="30" HistoryIndex="29">
     520    <JumpHistory Count="30" HistoryIndex="28">
    475521      <Position1>
    476522        <Filename Value="../UMicroThreading.pas"/>
    477         <Caret Line="257" Column="1" TopLine="241"/>
     523        <Caret Line="299" Column="1" TopLine="280"/>
    478524      </Position1>
    479525      <Position2>
    480526        <Filename Value="../UMicroThreading.pas"/>
    481         <Caret Line="258" Column="1" TopLine="241"/>
     527        <Caret Line="300" Column="1" TopLine="280"/>
    482528      </Position2>
    483529      <Position3>
    484530        <Filename Value="../UMicroThreading.pas"/>
    485         <Caret Line="261" Column="1" TopLine="241"/>
     531        <Caret Line="301" Column="1" TopLine="280"/>
    486532      </Position3>
    487533      <Position4>
    488534        <Filename Value="../UMicroThreading.pas"/>
    489         <Caret Line="321" Column="13" TopLine="311"/>
     535        <Caret Line="302" Column="1" TopLine="283"/>
    490536      </Position4>
    491537      <Position5>
    492538        <Filename Value="../UMicroThreading.pas"/>
    493         <Caret Line="116" Column="1" TopLine="103"/>
     539        <Caret Line="465" Column="1" TopLine="452"/>
    494540      </Position5>
    495541      <Position6>
    496542        <Filename Value="../UMicroThreading.pas"/>
    497         <Caret Line="326" Column="1" TopLine="307"/>
     543        <Caret Line="468" Column="1" TopLine="452"/>
    498544      </Position6>
    499545      <Position7>
    500546        <Filename Value="../UMicroThreading.pas"/>
    501         <Caret Line="183" Column="3" TopLine="159"/>
     547        <Caret Line="469" Column="1" TopLine="452"/>
    502548      </Position7>
    503549      <Position8>
    504550        <Filename Value="../UMicroThreading.pas"/>
    505         <Caret Line="281" Column="40" TopLine="266"/>
     551        <Caret Line="470" Column="1" TopLine="452"/>
    506552      </Position8>
    507553      <Position9>
    508554        <Filename Value="../UMicroThreading.pas"/>
    509         <Caret Line="183" Column="1" TopLine="172"/>
     555        <Caret Line="226" Column="1" TopLine="213"/>
    510556      </Position9>
    511557      <Position10>
    512558        <Filename Value="../UMicroThreading.pas"/>
    513         <Caret Line="683" Column="3" TopLine="660"/>
     559        <Caret Line="304" Column="1" TopLine="291"/>
    514560      </Position10>
    515561      <Position11>
    516562        <Filename Value="../UMicroThreading.pas"/>
    517         <Caret Line="273" Column="21" TopLine="260"/>
     563        <Caret Line="312" Column="1" TopLine="291"/>
    518564      </Position11>
    519565      <Position12>
    520566        <Filename Value="../UMicroThreading.pas"/>
    521         <Caret Line="281" Column="1" TopLine="269"/>
     567        <Caret Line="329" Column="1" TopLine="316"/>
    522568      </Position12>
    523569      <Position13>
    524570        <Filename Value="../UMicroThreading.pas"/>
    525         <Caret Line="282" Column="40" TopLine="269"/>
     571        <Caret Line="227" Column="1" TopLine="214"/>
    526572      </Position13>
    527573      <Position14>
    528574        <Filename Value="../UMicroThreading.pas"/>
    529         <Caret Line="281" Column="44" TopLine="269"/>
     575        <Caret Line="228" Column="1" TopLine="214"/>
    530576      </Position14>
    531577      <Position15>
    532578        <Filename Value="../UMicroThreading.pas"/>
    533         <Caret Line="282" Column="1" TopLine="269"/>
     579        <Caret Line="351" Column="1" TopLine="338"/>
    534580      </Position15>
    535581      <Position16>
    536582        <Filename Value="../UMicroThreading.pas"/>
    537         <Caret Line="281" Column="3" TopLine="269"/>
     583        <Caret Line="352" Column="1" TopLine="338"/>
    538584      </Position16>
    539585      <Position17>
    540586        <Filename Value="../UMicroThreading.pas"/>
    541         <Caret Line="282" Column="17" TopLine="263"/>
     587        <Caret Line="350" Column="1" TopLine="338"/>
    542588      </Position17>
    543589      <Position18>
    544590        <Filename Value="../UMicroThreading.pas"/>
    545         <Caret Line="402" Column="1" TopLine="389"/>
     591        <Caret Line="351" Column="35" TopLine="338"/>
    546592      </Position18>
    547593      <Position19>
    548594        <Filename Value="../UMicroThreading.pas"/>
    549         <Caret Line="270" Column="48" TopLine="262"/>
     595        <Caret Line="279" Column="19" TopLine="267"/>
    550596      </Position19>
    551597      <Position20>
    552598        <Filename Value="../UMicroThreading.pas"/>
    553         <Caret Line="285" Column="1" TopLine="263"/>
     599        <Caret Line="295" Column="1" TopLine="270"/>
    554600      </Position20>
    555601      <Position21>
    556602        <Filename Value="../UMicroThreading.pas"/>
    557         <Caret Line="282" Column="19" TopLine="269"/>
     603        <Caret Line="289" Column="1" TopLine="270"/>
    558604      </Position21>
    559605      <Position22>
    560606        <Filename Value="../UMicroThreading.pas"/>
    561         <Caret Line="54" Column="26" TopLine="45"/>
     607        <Caret Line="290" Column="1" TopLine="270"/>
    562608      </Position22>
    563609      <Position23>
    564610        <Filename Value="../UMicroThreading.pas"/>
    565         <Caret Line="283" Column="1" TopLine="270"/>
     611        <Caret Line="291" Column="1" TopLine="270"/>
    566612      </Position23>
    567613      <Position24>
    568614        <Filename Value="../UMicroThreading.pas"/>
    569         <Caret Line="394" Column="1" TopLine="381"/>
     615        <Caret Line="292" Column="1" TopLine="270"/>
    570616      </Position24>
    571617      <Position25>
    572618        <Filename Value="../UMicroThreading.pas"/>
    573         <Caret Line="385" Column="1" TopLine="380"/>
     619        <Caret Line="598" Column="50" TopLine="587"/>
    574620      </Position25>
    575621      <Position26>
    576622        <Filename Value="../UMicroThreading.pas"/>
    577         <Caret Line="386" Column="1" TopLine="380"/>
     623        <Caret Line="291" Column="40" TopLine="278"/>
    578624      </Position26>
    579625      <Position27>
    580         <Filename Value="UMainForm.pas"/>
    581         <Caret Line="274" Column="1" TopLine="252"/>
     626        <Filename Value="../UMicroThreading.pas"/>
     627        <Caret Line="280" Column="1" TopLine="272"/>
    582628      </Position27>
    583629      <Position28>
    584630        <Filename Value="../UMicroThreading.pas"/>
    585         <Caret Line="287" Column="43" TopLine="269"/>
     631        <Caret Line="288" Column="26" TopLine="272"/>
    586632      </Position28>
    587633      <Position29>
    588634        <Filename Value="../UMicroThreading.pas"/>
    589         <Caret Line="286" Column="1" TopLine="269"/>
     635        <Caret Line="285" Column="35" TopLine="272"/>
    590636      </Position29>
    591637      <Position30>
    592638        <Filename Value="../UMicroThreading.pas"/>
    593         <Caret Line="181" Column="3" TopLine="160"/>
     639        <Caret Line="397" Column="18" TopLine="393"/>
    594640      </Position30>
    595641    </JumpHistory>
     
    629675  </CompilerOptions>
    630676  <Debugging>
     677    <BreakPoints Count="4">
     678      <Item1>
     679        <Source Value="../UMicroThreading.pas"/>
     680        <Line Value="296"/>
     681      </Item1>
     682      <Item2>
     683        <Source Value="../UMicroThreading.pas"/>
     684        <Line Value="371"/>
     685      </Item2>
     686      <Item3>
     687        <Source Value="../UMicroThreading.pas"/>
     688        <Line Value="471"/>
     689      </Item3>
     690      <Item4>
     691        <Source Value="../UMicroThreading.pas"/>
     692        <Line Value="288"/>
     693      </Item4>
     694    </BreakPoints>
     695    <Watches Count="2">
     696      <Item1>
     697        <Expression Value="TMicroThread(MainForm.Scheduler.FMicroThreads[0]).FStackPointer"/>
     698      </Item1>
     699      <Item2>
     700        <Expression Value="TMicroThread(MainForm.Scheduler.FMicroThreads[0]).FBasePointer"/>
     701      </Item2>
     702    </Watches>
    631703    <Exceptions Count="3">
    632704      <Item1>
  • MicroThreading/Demo/UMainForm.lfm

    r151 r152  
    1717    Top = 8
    1818    Width = 761
    19     ActivePage = TabSheet1
     19    ActivePage = TabSheet2
    2020    Anchors = [akTop, akLeft, akRight, akBottom]
    21     TabIndex = 0
     21    TabIndex = 1
    2222    TabOrder = 0
    2323    object TabSheet1: TTabSheet
     
    5858        MaxValue = 100000
    5959        MinValue = 1
     60        TabStop = False
    6061        TabOrder = 2
    61         Value = 200
     62        Value = 2
    6263      end
    6364      object Memo1: TMemo
     
    9596        OnChange = SpinEdit3Change
    9697        TabOrder = 5
    97         Value = 100000
     98        Value = 1000000
    9899      end
    99100    end
     
    244245          TabOrder = 1
    245246        end
     247        object CheckBoxUseMainThread: TCheckBox
     248          Left = 10
     249          Height = 17
     250          Top = 64
     251          Width = 98
     252          Caption = 'Use main thread'
     253          OnChange = CheckBoxUseMainThreadChange
     254          TabOrder = 2
     255        end
    246256      end
    247257    end
  • MicroThreading/Demo/UMainForm.pas

    r151 r152  
    2121    ButtonSchedulerStartStop: TButton;
    2222    ButtonShowThreadId: TButton;
     23    CheckBoxUseMainThread: TCheckBox;
    2324    GroupBox1: TGroupBox;
    2425    GroupBox2: TGroupBox;
     
    5354    procedure ButtonShowThreadIdClick(Sender: TObject);
    5455    procedure ButtonClearMicroThreadsClick(Sender: TObject);
     56    procedure CheckBoxUseMainThreadChange(Sender: TObject);
    5557    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    5658    procedure FormCreate(Sender: TObject);
     
    165167begin
    166168  //Scheduler.FMicroThreads.Clear;
    167   for I := 0 to SpinEdit1.Value do
     169  for I := 0 to SpinEdit1.Value - 1 do
    168170    Scheduler.AddMethod(Worker);
    169171end;
     
    199201    Scheduler.MicroThreadsLock.Release;
    200202  end;
     203end;
     204
     205procedure TMainForm.CheckBoxUseMainThreadChange(Sender: TObject);
     206begin
     207  Scheduler.UseMainThread := CheckBoxUseMainThread.Checked;
    201208end;
    202209
  • MicroThreading/UMicroThreading.pas

    r151 r152  
    1818  cthreads,
    1919  {$ENDIF}{$ENDIF}
    20   Classes, SysUtils, Contnrs, SyncObjs, DateUtils, Dialogs, Forms, UPlatform;
     20  Classes, ExtCtrls, SysUtils, Contnrs, SyncObjs, DateUtils, Dialogs, Forms, UPlatform;
    2121
    2222const
     
    9898  TMicroThreadThread = class(TThread)
    9999    Manager: TMicroThreadManager;
     100    ExecuteTerminated: Boolean;
    100101    procedure Execute; override;
    101102    constructor Create(CreateSuspended: Boolean;
     
    119120    function Execute(Count: Integer): Integer;
    120121  public
     122    Id: Integer;
    121123    procedure Yield;
    122124    constructor Create;
     
    139141    FLastId: Integer;
    140142    FFrequency: Int64;
    141     FTerminated: Boolean;
     143    FMainThreadTerminated: Boolean;
    142144    FMicroThreads: TObjectList; // TList<TMicroThread>
    143145    FMainThreadManager: TMicroThreadManager;
    144146    FMicroThreadsLock: TCriticalSection;
    145147    FState: TMicroThreadSchedulerState;
     148    FUseMainThread: Boolean;
     149    FMainThreadStarter: TTimer;
    146150    function GetMicroThreadCount: Integer;
    147151    function GetThreadPoolCount: Integer;
     
    150154    procedure SetThreadPoolSize(const AValue: Integer);
    151155    function GetNextMicroThread: TMicroThread;
     156    procedure ReleaseMicroThread(MicroThread: TMicroThread);
     157    procedure SetUseMainThread(const AValue: Boolean);
    152158    procedure Start;
    153159    procedure Stop;
    154160    procedure PoolThreadTerminated(Sender: TObject);
    155161    procedure UpdateThreadPoolSize;
     162    procedure MainThreadStart(Sender: TObject);
    156163  public
    157164    function Add(MicroThread: TMicroThread): Integer;
     
    168175    property MainThreadManager: TMicroThreadManager read FMainThreadManager;
    169176    property Active: Boolean read FActive write SetActive;
     177    property UseMainThread: Boolean read FUseMainThread write SetUseMainThread;
    170178  end;
    171179
     
    222230
    223231procedure TMicroThreadManager.Yield;
    224 var
    225   I: Integer;
    226   CurrentTime: TDateTime;
    227 begin
    228   CurrentTime := NowPrecise;
     232begin
    229233  if Assigned(FCurrentMicroThread) then begin
    230     FCurrentMicroThread.FExecutionEndTime := CurrentTime;
     234    FCurrentMicroThread.FExecutionEndTime := NowPrecise;
    231235    FCurrentMicroThread.FExecutionTime := FCurrentMicroThread.FExecutionTime +
    232236      (FCurrentMicroThread.FExecutionEndTime - FCurrentMicroThread.FExecutionStartTime);
    233     if FCurrentMicroThread.FState = tsRunning then
    234       FCurrentMicroThread.FState := tsWaiting;
    235237    asm
    236238      // Store microthread stack
     
    249251    end;
    250252    FCurrentMicroThread.FManager := nil;
     253    FScheduler.ReleaseMicroThread(FCurrentMicroThread);
    251254    FCurrentMicroThread := nil;
    252255  end;
    253256
    254   FCurrentMicroThread := FScheduler.GetNextMicroThread;
    255 
    256   if Assigned(FCurrentMicroThread) and (FExecutedCount < FExecuteCount) then begin
    257     FCurrentMicroThread.FManager := Self;
    258     Inc(FExecutedCount);
    259     asm
    260       // Store FScheduler stack
    261       mov eax, Self
    262       mov edx, esp
    263       mov [eax].TMicroThreadManager.FStackPointer, edx
    264       mov edx, ebp
    265       mov [eax].TMicroThreadManager.FBasePointer, edx
    266     end;
    267     if not FCurrentMicroThread.FExecuted then begin
    268       FCurrentMicroThread.FExecuted := True;
    269       FCurrentMicroThread.FState := tsRunning;
    270       FCurrentMicroThread.FExecutionStartTime := CurrentTime;
     257  if FExecutedCount < FExecuteCount then begin
     258    FCurrentMicroThread := FScheduler.GetNextMicroThread;
     259
     260    if Assigned(FCurrentMicroThread) then begin
     261      Inc(FExecutedCount);
     262      FCurrentMicroThread.FExecutionStartTime := NowPrecise;
     263      FCurrentMicroThread.FManager := Self;
    271264      asm
    272         // Restore microthread stack
    273         mov ecx, Self
    274         mov eax, [ecx].TMicroThreadManager.FCurrentMicroThread
    275         mov edx, [eax].TMicroThread.FStackPointer
    276         mov ecx, esp
    277         mov esp, edx
    278         push ebp // remember bp on micro thread stack for read back
    279         push ecx
    280         mov edx, [eax].TMicroThread.FBasePointer
    281         mov ebp, edx
    282         // We want to call virtual method Execute
    283         // but virtual methods can be called only statically
    284         // Then static method CallExecute is calling virtual method Execute
    285         call TMicroThread.CallExecute
    286 //      end;
    287 //      StaticMicroThread.Execute;
    288 //      asm
    289         pop edx
    290         pop ebp
    291         mov esp, edx
     265        // Store FScheduler stack
     266        mov eax, Self
     267        mov edx, esp
     268        mov [eax].TMicroThreadManager.FStackPointer, edx
     269        mov edx, ebp
     270        mov [eax].TMicroThreadManager.FBasePointer, edx
    292271      end;
    293       //FSelected.Method(FSelected);
    294   (*    StaticManager := FCurrentMicroThread.FManager;
    295       asm
    296         // Restore FScheduler stack
    297         mov eax, StaticManager // Self is invalid before BP restore
    298         mov edx, [eax].TMicroThreadManager.FStackPointer
    299         mov esp, edx
    300         mov edx, [eax].TMicroThreadManager.FBasePointer
    301         mov ebp, edx
    302       end;
    303 *)
    304       FCurrentMicroThread.FManager := nil;
    305       FCurrentMicroThread.FExecutionEndTime := CurrentTime;
    306       FCurrentMicroThread.FExecutionTime := FCurrentMicroThread.FExecutionTime +
    307        (FCurrentMicroThread.FExecutionEndTime - FCurrentMicroThread.FExecutionStartTime);
    308       FCurrentMicroThread.FFinished := True;
    309       if FCurrentMicroThread.FFreeOnTerminate then begin
    310         // Microthread is finished, remove it from queue
    311         with FScheduler do
    312         try
    313           FMicroThreadsLock.Acquire;
    314           FMicroThreads.Delete(FMicroThreads.IndexOf(FCurrentMicroThread));
    315         finally
    316           FMicroThreadsLock.Release;
     272      if not FCurrentMicroThread.FExecuted then begin
     273        FCurrentMicroThread.FExecuted := True;
     274        asm
     275          // Restore microthread stack
     276          mov ecx, Self
     277          mov eax, [ecx].TMicroThreadManager.FCurrentMicroThread
     278          mov edx, [eax].TMicroThread.FStackPointer
     279          mov esp, edx
     280          mov edx, [eax].TMicroThread.FBasePointer
     281          mov ebp, edx
     282          // We want to call virtual method Execute
     283          // but virtual methods can be called only statically
     284          // Then static method CallExecute is calling virtual method Execute
     285          call TMicroThread.CallExecute
     286
     287          // Restore FScheduler stack
     288          // ecx register is set by CallExecute to running micro thread
     289          mov eax, [ecx].TMicroThread.FManager
     290          mov edx, [eax].TMicroThreadManager.FStackPointer
     291          mov esp, edx
     292          mov edx, [eax].TMicroThreadManager.FBasePointer
     293          mov ebp, edx
     294        end;
     295
     296        FCurrentMicroThread.FExecutionEndTime := NowPrecise;
     297        FCurrentMicroThread.FExecutionTime := FCurrentMicroThread.FExecutionTime +
     298         (FCurrentMicroThread.FExecutionEndTime - FCurrentMicroThread.FExecutionStartTime);
     299        FCurrentMicroThread.FFinished := True;
     300        if FCurrentMicroThread.FFreeOnTerminate then begin
     301          // Microthread is finished, remove it from queue
     302          with FScheduler do
     303          try
     304            FMicroThreadsLock.Acquire;
     305            FMicroThreads.Delete(FMicroThreads.IndexOf(FCurrentMicroThread));
     306          finally
     307            FMicroThreadsLock.Release;
     308          end;
     309        end else begin
     310          FCurrentMicroThread.FManager := nil;
     311          FScheduler.ReleaseMicroThread(FCurrentMicroThread);
     312        end;
     313        //FCurrentMicroThread.FManager := nil;
     314        //FScheduler.ReleaseMicroThread(FCurrentMicroThread);
     315        FCurrentMicroThread := nil;
     316      end else
     317      //if FCurrentMicroThread.State = tsWaiting then
     318      begin
     319        // Execute selected thread
     320        asm
     321          // Restore microthread stack
     322          mov ecx, Self
     323          mov eax, [ecx].TMicroThreadManager.FCurrentMicroThread
     324          mov edx, [eax].TMicroThread.FStackPointer
     325          mov esp, edx
     326          mov edx, [eax].TMicroThread.FBasePointer
     327          mov ebp, edx
    317328        end;
    318329      end;
    319       FCurrentMicroThread := nil;
    320     end else
    321     if FCurrentMicroThread.State = tsWaiting then begin
    322       // Execute selected thread
    323       FCurrentMicroThread.FState := tsRunning;
    324       FCurrentMicroThread.FExecutionStartTime := CurrentTime;
    325       asm
    326         // Restore microthread stack
    327         mov ecx, Self
    328         mov eax, [ecx].TMicroThreadManager.FCurrentMicroThread
    329         mov edx, [eax].TMicroThread.FStackPointer
    330         mov esp, edx
    331         mov edx, [eax].TMicroThread.FBasePointer
    332         mov ebp, edx
    333       end;
    334330    end;
    335   end else begin
    336     FCurrentMicroThread := nil;
    337331  end;
    338332end;
     
    370364begin
    371365  inherited;
     366  ExecuteTerminated := False;
    372367  Manager := TMicroThreadManager.Create;
    373368end;
     
    375370destructor TMicroThreadThread.Destroy;
    376371begin
     372  Terminate;
     373  repeat
     374    Application.ProcessMessages;
     375    Sleep(1);
     376  until ExecuteTerminated;
     377
    377378  Manager.Free;
    378379  inherited Destroy;
     
    393394begin
    394395  Execute;
     396  asm
     397    mov ecx, Self
     398  end;
    395399end;
    396400
     
    513517constructor TMicroThreadScheduler.Create;
    514518begin
    515   FTerminated := True;
     519  FMainThreadStarter := TTimer.Create(nil);
     520  FMainThreadStarter.Enabled := False;
     521  FMainThreadStarter.Interval := 1;
     522  FMainThreadStarter.OnTimer := MainThreadStart;
     523  FMainThreadTerminated := True;
    516524  FMicroThreadsLock := TCriticalSection.Create;
    517525  FMicroThreads := TObjectList.Create;
     
    521529  FMainThreadManager := TMicroThreadManager.Create;
    522530  FMainThreadManager.FScheduler := Self;
     531  UseMainThread := False;
    523532end;
    524533
     
    526535begin
    527536  Active := False;
     537  FMainThreadStarter.Free;
    528538  FMainThreadManager.Free;
    529539  FThreadPool.Free;
     
    534544
    535545procedure TMicroThreadScheduler.Start;
    536 var
    537   Executed: Integer;
    538   I: Integer;
    539 begin
    540   FTerminated := False;
     546begin
     547  FMainThreadTerminated := False;
    541548  UpdateThreadPoolSize;
    542549  FState := ssRunning;
    543   repeat
    544     Executed := FMainThreadManager.Execute(10);
    545     Application.ProcessMessages;
    546     if Executed = 0 then Sleep(1);
    547   until FState <> ssRunning;
    548   FTerminated := True;
    549550end;
    550551
     
    567568    Application.ProcessMessages;
    568569    Sleep(1);
    569   until FTerminated and (ThreadPoolSize = 0);
     570  until FMainThreadTerminated and (ThreadPoolSize = 0);
    570571  FState := ssStopped;
    571572end;
    572573
    573574procedure TMicroThreadScheduler.PoolThreadTerminated(Sender: TObject);
    574 begin
     575var
     576  ThreadIndex: Integer;
     577begin
     578  TMicroThreadThread(Sender).ExecuteTerminated := True;
    575579  try
    576580    FThreadPoolLock.Acquire;
    577581    FThreadPool.OwnsObjects := False;
    578     FThreadPool.Delete(FThreadPool.IndexOf(Sender));
     582    ThreadIndex := FThreadPool.IndexOf(Sender);
     583    if ThreadIndex <> -1 then FThreadPool.Delete(ThreadIndex);
    579584    FThreadPool.OwnsObjects := True;
    580585  finally
     
    594599        NewThread := TMicroThreadThread.Create(True);
    595600        NewThread.Manager.FScheduler := Self;
     601        NewThread.Manager.Id := FThreadPool.Count;
    596602        NewThread.OnTerminate := PoolThreadTerminated;
    597603        ThreadPool.Add(NewThread);
    598604        NewThread.Resume;
    599605      end;
    600     end else
    601     ThreadPool.Count := FThreadPoolSize;
     606    end else begin
     607      while FThreadPool.Count > FThreadPoolSize do begin
     608        FThreadPool.Delete(FThreadPool.Count - 1);
     609      end;
     610    end;
    602611  finally
    603612    FThreadPoolLock.Release;
    604613  end;
     614end;
     615
     616procedure TMicroThreadScheduler.MainThreadStart(Sender: TObject);
     617var
     618  Executed: Integer;
     619begin
     620  FMainThreadStarter.Enabled := False;
     621  repeat
     622    Executed := FMainThreadManager.Execute(1);
     623    Application.ProcessMessages;
     624    if Executed = 0 then Sleep(1);
     625  until (FState <> ssRunning) or (not FUseMainThread);
     626  FMainThreadTerminated := True;
    605627end;
    606628
     
    619641      FRoundRobinIndex := 0;
    620642    while (I < FMicroThreads.Count) and
    621      (TMicroThread(FMicroThreads[FRoundRobinIndex]).State <> tsWaiting) do begin
     643     (TMicroThread(FMicroThreads[FRoundRobinIndex]).FState <> tsWaiting) do begin
    622644      // WakeUp sleeping threads
    623645      if (TMicroThread(FMicroThreads[FRoundRobinIndex]).FState = tsSleeping) and
     
    634656    if I < FMicroThreads.Count then begin
    635657      Result := TMicroThread(FMicroThreads[FRoundRobinIndex]);
     658      Result.FState := tsRunning;
    636659    end;
    637660  finally
    638661    FMicroThreadsLock.Release;
     662  end;
     663end;
     664
     665procedure TMicroThreadScheduler.ReleaseMicroThread(MicroThread: TMicroThread);
     666begin
     667  try
     668    FMicroThreadsLock.Acquire;
     669    if MicroThread.FState = tsRunning then begin
     670      MicroThread.FState := tsWaiting;
     671    end;
     672  finally
     673    FMicroThreadsLock.Release;
     674  end;
     675end;
     676
     677procedure TMicroThreadScheduler.SetUseMainThread(const AValue: Boolean);
     678begin
     679  if FUseMainThread = AValue then Exit;
     680  FUseMainThread := AValue;
     681  if FState = ssRunning then begin
     682    if AValue then FMainThreadStarter.Enabled := True;
    639683  end;
    640684end;
     
    686730
    687731//StaticManagers := TObjectList.Create;
    688 MainScheduler := TMicroThreadScheduler.Create;
     732//MainScheduler := TMicroThreadScheduler.Create;
    689733
    690734finalization
    691735
    692 MainScheduler.Free;
     736//MainScheduler.Free;
    693737//StaticManagers.Free;
    694738
Note: See TracChangeset for help on using the changeset viewer.