среда, 25 августа 2021 г.

Producer/Consumer

https://www.lua.org/pil/9.2.html

9.2 – Pipes and Filters

One of the most paradigmatic examples of coroutines is in the producer-consumer problem. Let us suppose that we have a function that continually produces values (e.g., reading them from a file) and another function that continually consumes these values (e.g., writing them to another file). Typically, these two functions look like this:

    function producer ()
      while true do
        local x = io.read()     -- produce new value
        send(x)                 -- send to consumer
      end
    end
    
    function consumer ()
      while true do
        local x = receive()        -- receive from producer
        io.write(x, "\n")          -- consume new value
      end
    end

(In that implementation, both the producer and the consumer run forever. It is an easy task to change them to stop when there is no more data to be handled.) The problem here is how to match send with receive. It is a typical case of a who-has-the-main-loop problem. Both the producer and the consumer are active, both have their own main loops, and both assume that the other is a callable service. For this particular example, it is easy to change the structure of one of the functions, unrolling its loop and making it a passive agent. However, this change of structure may be far from easy in other real scenarios.

Coroutines provide an ideal tool to match producers and consumers, because a resume-yield pair turns upside-down the typical relationship between caller and callee. When a coroutine calls yield, it does not enter into a new function; instead, it returns a pending call (to resume). Similarly, a call to resume does not start a new function, but returns a call to yield. This property is exactly what we need to match a send with a receive in such a way that each one acts as if it were the master and the other the slave. So, receive resumes the producer so that it can produce a new value; and send yields the new value back to the consumer:

    function receive ()
      local status, value = coroutine.resume(producer)
      return value
    end
    
    function send (x)
      coroutine.yield(x)
    end

Of course, the producer must now be a coroutine:

    producer = coroutine.create(
      function ()
        while true do
        local x = io.read()     -- produce new value
          send(x)
        end
      end)

In this design, the program starts calling the consumer. When the consumer needs an item, it resumes the producer, which runs until it has an item to give to the consumer, and then stops until the consumer restarts it again. Therefore, we have what we call a consumer-driven design.

We can extend this design with filters, which are tasks that sit between the producer and the consumer doing some kind of transformation in the data. A filter is a consumer and a producer at the same time, so it resumes a producer to get new values and yields the transformed values to a consumer. As a trivial example, we can add to our previous code a filter that inserts a line number at the beginning of each line. The complete code would be like this:

    function receive (prod)
      local status, value = coroutine.resume(prod)
      return value
    end
    
    function send (x)
      coroutine.yield(x)
    end
    
    function producer ()
      return coroutine.create(function ()
        while true do
          local x = io.read()     -- produce new value
          send(x)
        end
      end)
    end
    
    function filter (prod)
      return coroutine.create(function ()
        local line = 1
        while true do
          local x = receive(prod)   -- get new value
          x = string.format("%5d %s", line, x)
          send(x)      -- send it to consumer
          line = line + 1
        end
      end)
    end
    
    function consumer (prod)
      while true do
        local x = receive(prod)   -- get new value
        io.write(x, "\n")          -- consume new value
      end
    end

The final bit simply creates the components it needs, connects them, and starts the final consumer:

    p = producer()
    f = filter(p)
    consumer(f)

Or better yet:

    consumer(filter(producer()))

If you thought about Unix pipes after reading the previous example, you are not alone. After all, coroutines are a kind of (non-preemptive) multithreading. While in pipes each task runs in a separate process, with coroutines each task runs in a separate coroutine. Pipes provide a buffer between the writer (producer) and the reader (consumer) so there is some freedom in their relative speeds. This is important in the context of pipes, because the cost of switching between processes is high. With coroutines, the cost of switching between tasks is much smaller (roughly the same cost of a function call), so the writer and the reader can go hand in hand.

среда, 18 августа 2021 г.

вторник, 17 августа 2021 г.

Lua, Cpp, Architecture

Lua, Socket, Install, Quik

https://forum.quik.ru/forum10/topic5986/

  • качаем https://download.zerobrane.com/misc/luasec-0.6-openssl-1.0.2o-luasocket-3.0-win64.zip
  • забираем из архива socket/core.dll
  • заменяем его в нашем lua/clibs/socket/core.dll
  • берем из папки установленного квика lua5.1.dll
  • вставляем lua5.1.dll в папку lua/clibs/
  • удаляем старый (или переименовываем) и переименовываем новый в lua51.dll


@buybackoff,
core.dll: https://download.zerobrane.com/misc/luasec-0.6-openssl-1.0.2o-luasocket-3.0-win64.zip.
был еще один вариант из другого источника (уже не помню откуда), но он оказался полностью идентичен.
lua51.dll: Переименованный lua5.1.dll из папки нового квика (8.2). Попытки применения других сборок готового lua51.dll, из различных источников (и версий Lua), желаемого результата не принесли.

В общем, текущий вариант пока остается единственным доступным из работоспособных.

Lua, Sockets, Doc

https://w3.impa.br/~diego/software/luasocket/

File Serialization

I love Lua

Manual (Rus):


понедельник, 16 августа 2021 г.

Lua, Cpp, Chat, Example, Embedding

https://www.youtube.com/watch?v=QwfL72yHZEY

https://github.com/jamespascoe/LuaChat/tree/master/src

https://jamespascoe.github.io/cpponsea2020

-------------------------------------------------------

https://www.youtube.com/watch?v=4l5HdmPoynw

https://github.com/OneLoneCoder/videos/blob/master/OneLoneCoder_EmbeddingLua_Part1.cpp

https://www.youtube.com/watch?v=E42Lyv2Ra1c

James Pascoe SWIG, SOL3

https://www.youtube.com/watch?v=ojph4RZvqqQ

https://github.com/jamespascoe/luachat

https://github.com/onelonecoder

---------------------------------------------------------------------------

https://www.youtube.com/watch?v=kDHGfHxwymI

https://github.com/rhymu8354/LuaCppTutorial

----------------------------------------------------------------------------

Lua -> Cpp --> Python

https://github.com/eSKond/QuikMultiBridge

Quik, Lua, Python

https://github.com/eSKond/QuikMultiBridge


суббота, 7 августа 2021 г.

Lua, Video, Install, Compile

http://luabinaries.sourceforge.net/download.html

install

https://www.youtube.com/watch?v=XCwXWOe6VcU

Кодировка

https://www.youtube.com/watch?v=iHNq8uOgI_I

Compile

https://www.youtube.com/watch?v=TALXtup2CjI

Настройка
https://www.youtube.com/watch?v=X6BqMZyzAs4

Configuration

https://www.youtube.com/watch?v=QwKd4PwQAgQ

Embedding in C++

https://www.youtube.com/watch?v=4l5HdmPoynw

Debugging

https://www.youtube.com/watch?v=zqUvW_uWjhc

Practic

https://www.youtube.com/watch?v=SS4Xr6yRHp8

Protocol wireshark

https://www.youtube.com/watch?v=T8yRoh2Wp5A

https://www.youtube.com/watch?v=gc4WT5KyhlA

Beginners

https://www.youtube.com/watch?v=HDAE9OR28gY

Lua Quick Start Guide

https://github.com/search?q=Lua-Quick-Start-Guide

https://github.com/PacktPublishing/Lua-Quick-Start-Guide

https://github.com/gszauer/LuaQuickStartGuide

https://github.com/lackluster-games/lua-quick-start-guide

https://github.com/yongot/learning-lua-quick-start-guide


четверг, 5 августа 2021 г.

Lua, Rus, Doc, Coroutines

https://lua.org.ru/

https://lua.org.ru/main.html#documentation

https://lua.org.ru/contents_ru.html

http://lua-users.org/wiki/TutorialDirectory

http://lua-users.org/wiki/ThreadsTutorial

https://www.lua.org/source/

Coroutines

https://www.lua.org/pil/9.1.html

Lua, Content, Code

https://www.lua.org/pil/contents.html

https://www.lua.org/pil/contents.html#contents

http://www.inf.puc-rio.br/~roberto/book/code.html

https://www.lua.org/manual/5.0/

http://www.inf.puc-rio.br/~roberto/book/errata1.html

https://www.lua.org/pil/

https://www.lua.org/pil/9.4.html


Lua, Tables

https://habr.com/ru/company/mailru/blog/493642/

[1] The basics and design of Lua table (или то же самое на slideshare.net).


[2] Programming in Lua — Tables


[3] GitHub — LuaJIT/LuaJIT — lj_obj.h


[4] GitHub — rosik/luajit:habr-luajit-tables


[5] Википедия — Хеш-таблица — Открытая адресация


[5a] GitHub — LuaJIT/LuaJIT — Issue #494.


[6] Lua 5.2 Reference manual — The Length Operator


[7] GitHub — LuaJIT/LuaJIT lj_tab.c


[8] Lua 5.1 Reference manual — Basic Functions — unpack


[9] Lua 5.1 Reference manual — Basic Functions — select


[10] Lua 5.1 Reference manual — The Stack


[11] GitHub — LuaJIT/LuaJIT — ext_ffi_semantics.html


[12] Tarantool » 2.2 » Reference » Built-in modules reference » Module uuid


[Bonus] Learn X in Y Minutes, where X=Lua


[Bonus] Хабр — Lua. Краткое введение в метатаблицы для чайников

Теги:luajittarantoollua
Хабы:Блог компании Mail.ru GroupПрограммированиеСовершенный кодПроектирование и рефакторингLua

Lua, FFI

https://luajit.org/ext_ffi.html

https://luajit.org/ext_ffi_tutorial.html

http://mad.web.cern.ch/mad/releases/old/V5.00/releases/madng/madng-git/lib/luajit/doc/ext_ffi_api.html

https://enqueuezero.com/ffi.html#context

Lua, Percentile

https://habr.com/ru/company/mailru/blog/529456/

https://en.wikipedia.org/wiki/Percentile

Lua, LuaBridge

https://habr.com/ru/post/237503/

https://eliasdaler.wordpress.com/2014/07/18/using-lua-with-cpp-luabridge/

https://habr.com/ru/post/197300/


Lua, Quik, CSahrp, C#

http://luaq.ru/

http://luaq.ru/OnParam.html

https://quikluacsharp.ru/


Lua, Python, Bridge, Quik

https://github.com/eSKond/QuikMultiBridge

Lua, Parse, CSV

http://lua-users.org/wiki/LuaCsv


Lua, DoubleQueue, Queue,

http://www.lua.org/pil/11.4.html

Lua, tasks.json, Book

 https://www.lua.org/download.html

http://luabinaries.sourceforge.net/download.html

https://www.youtube.com/watch?v=XCwXWOe6VcU

Programming in Lua, Fourth Edition

Roberto Ierusalimschy

Copyright © 2016, 2003 Roberto Ierusalimschy

tasks.json

{
    "version""2.0.0",
    "tasks": [
        {
            "label""Run Lua",
            "type""shell",
            "command""lua54.exe",
            "args": ["${file}"],
            "group": {
                "kind""build",
                "isDefault"true
            }
        }
    ]
}

Lua, C#, CSharp

https://habr.com/ru/post/197262/

Lua, Sockets

https://ilovelua.wordpress.com/%D0%BF%D0%B5%D1%80%D0%B5%D0%B4%D0%B0%D1%87%D0%B0-%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D1%85-%D0%BF%D0%BE-%D1%81%D0%B5%D1%82%D0%B8-%D1%81-luasocket/

https://stackoverflow.com/questions/42783263/lua-tcp-ip-simple-client-server-connection

http://w3.impa.br/~diego/software/luasocket/introduction.html

https://github.com/diegonehab/luasocket/tree/master/samples

https://www.lua.org/pil/9.4.html

https://coronalabs.com/blog/2014/09/23/tutorial-local-multiplayer-with-udptcp/

https://realtimelogic.com/ba/doc/en/lua/SockLib.html

http://lua-users.org/lists/lua-l/2011-03/msg00184.html

Video
https://www.youtube.com/watch?v=qVWOaRYpli0

https://www.youtube.com/watch?v=JtuwmPC0PdE

https://www.youtube.com/watch?v=jREbAopWqKU