Preemptive Scheduling Coroutine Programming Language
Melang is new scripting language of coroutine preemptive scheduling that is based on time slices. In this article, learn about its features and usage scenarios.
Join the DZone community and get the full member experience.Join For Free
The concept of coroutine must be familiar to everyone now. After all, not only LUA, Go, and Kotlin but also C++ all support coroutines.
But different languages have different implementations. Such as LUA, coroutines will be scheduled when someone called
A new language introduced to you today is a scripting language of coroutine preemptive scheduling based on time slices. That is, each coroutine does not need to explicitly call certain functions to give up control, but according to the weight of execution time to control whether to be suspended or run.
Its name is Melang.
We will discuss the following:
- Usage scenarios
A language must have flow control, array, dict, structure, class, etc. This is no different in Melang. We will focus on the following:
Coroutine Preemptive Scheduling
In Melang, each script task is an independent coroutine, and coroutines' execution environments are isolated. But they can communicate with each other by the message queue functions.
Let's look at two examples:
1. Running Multi-Tasks by Melang Execution File
There are two script files:
Then I execute the following command:
After execution, you will see the screen print a and b alternately, as follows:
top command will find out that only one process (and also only one thread) is performing these two tasks. Melang scheduling does not occur in the function
mln_print, but will try to switch during the processing of each statement.
2. Start Up Multi-Tasks in a Melang Task
We create a new script file named
launcher.mln to start up
b.mln by calling function
mln_eval. And then execute:
The output is the same as previous example.
“Reflection means that the class to be operated is known at runtime, and the complete structure of the class can be obtained at runtime and the corresponding method can be called.” - Quoted from a netizen
In Melang, the concept of class is called Set, but it is more similar to the structure in C but also supports function definitions. Let's look at a simple example:
Here, we define a Set named Human, which has two member variables:
age, and a method
init. The general instantiation code is as follows:
This instantiates a
Human object named
me. However, we can also instantiate it like this:
The result is the same.
Then we can call the method of the object:
In Melang, not only Set can be obtained by a string but also functions and object methods can be used in this way.
Examples for function:
Here, we define a function, and we call it indirectly by a string variable.
Example for object method:
As you can see in this example, we call the method of an object by a string member variable.
For an instantiated object, we can still add (or inject) some members and methods to it, for example:
The output is 20. The member variable
age was successfully added to the object
Note: The injection is defined for the object, not Set. Therefore, if you instantiate Human again, there is still no
The above example is the simplest injection. Melang also provides function
get similar to other scripts.
Let's look at an example of method injection:
This example will still output 20, but this time it is output by
mln_print called in the injected method
As we can see in web front-end programming, a variable will be monitored by a set of functions. When the value changes, the callback function will be called. (Note: In Melang, even if the new value is the same as the old one, it also will be called)
n will be tracked by calling the function
mln_watch. When the value of
n changes, the
eventHandler function wil be called.
We will see the following output:
Synchronous Code, Executed Asynchronously
In Melang, code is written in a synchronized form. The advantage is logical coherence.
In traditional languages, synchronous writing also means synchronous processing, so the performance is low. But in Melang, the interpreter will execute completely asynchronously.
These two scripts use the message queue functions to send and receive messages from each other and print them out. For each task, its logic is sequential. For example,
a.mln must send a message first, then receive the message, and then print. If
b.mln does not send a message to
a.mln will be suspended while receiving the message.
The above is just a simple example, and it is the same when it comes to network I/O. When data arrives on the socket, the interpreter will receive it, then return to the script layer, and change the script task from the suspended state to the running state.
The current usage scenario of Melang is mainly embedded in other applications, which is basically the same as LUA. The Melang project on Github is a launcher. The launcher will call the script-related functions in the core library for initialization and execution. And of course, the core library is also open source.
Melang's core library provides many interfaces such as algorithms, data structures, event mechanisms, multi-process and multi-threaded frameworks. Based on this library, not only can obtain melang script, but also reduce development costs.
In addition, if an application uses the event processing interface provided by the core library, not only events can be processed, but scripts can also be executed in the same thread.
Melang provides a basic function library. Besides string, file system and network IO operations, it also provides functions for accessing MySQL8 and a series of functions such as encryption, decryption and communication.
Although the core library has been updated and upgraded for several years, the language is still in its infancy. And we still need to make a lot of efforts on it. Looking forward to your attention to this project. Thanks for reading.
Opinions expressed by DZone contributors are their own.