Multi-tasking & Interfacing with C

Tram Ho

The final set of tools at the Ada language syntax level that we will learn about are the parallel task definition syntax and the syntax that supports direct integration with the C language.

Multi-tasking

A task is essentially another Main program, which will be run in parallel with the original Main program’s code execution. So we can expect that the code that defines the task will be a procedure . However, sometimes managing task by logic code is also necessary, and so the task needs to be a data type. And so we have the concept of task type .

Here we are declaring a simple task type named Extra_Task . The special point here is that the Extra_Task itself is defined with the begin ... end Extra_Task; is similar to a procedure , but is declared and defined like a package containing a procedure Start .

As such, we can expect that external code will both be able to view Extra_Task as a type for declaring task variables, and also be able to, each of these variable names will be the same as a package reference name and can point to the procedure Start to launch.

The operation result describes that the execution of Main ‘s code continues even after the T task is triggered on the T.Start (V); . In addition, the code defined inside the procedure Start of Extra_Task is run synchronously between the main process of running the main code task Main and the task T : Extra_Task .

After the code defined inside the procedure Start finishes, the delay 1.2; is used to slow down T ‘s code by 1.2s and show that task T is running asynchronously from the main task Main .

Both the task Main and T : Extra_Task are procedure that start the processes running the code and can be seen as the starting point of different programs, which can declare their own resource variables like V : Integer and In_Task : Integer to store information to be worked on during each task ‘s execution.

Importing C from Ada

As introduced earlier in the introductory article of this Sub-Series, we will be able to easily integrate C code into an project Ada . We can even write parallel logic on C files and then compile all the project ‘s C and Ada code files with gprbuild .

To use multiple languages ​​in an project Ada , we need to list the names of the languages ​​we want to use in the project ‘s configuration file. Languages ​​other than C eg Fortran , Assembler , etc. may require additional compiler information to be declared so that gprbuild can handle parallel compilation of the code files. Ada .

Then we can create C code files in the project ‘s src folder.

Here we have a header.h file that defines a struct and declares a triple function defined in the body.c file. To use these elements in Ada ‘s code , we need to generate the corresponding declaration code to refer to the elements defined in C code .

Exporting Ada to C

In the opposite direction, when we want to call a sub-program written in Ada code in a C code file of the same project , the action to be performed is Export through Interfaces.C .



So in any C code file, we will be able to see that the add function has been defined in the global scope and declared with the extern keyword before using it.
Share the news now

Source : Viblo