Skip to content

emilianobilli/libchannel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 

Repository files navigation

Introduction to Go-style Channels in C. libchannel

Go-style channels are a powerful and flexible synchronization mechanism built into the Go programming language. They allow multiple goroutines (Go's equivalent to threads) to communicate and synchronize their execution. A goroutine can send a value into a channel and another goroutine can receive that value from the other end of the channel. This channel-based communication enables goroutines to work together to complete tasks, and it is one of the reasons why Go is known for its simplicity in handling concurrency.

Inspired by this, the library we are discussing brings similar channel-style mechanisms to the C programming language. This library provides a Go-like way to communicate and synchronize between threads in a C program. It's particularly suited for programs where many threads need to interact or synchronize their work in a controlled way.

Here are some key concepts:

  • Channel: In Go, a channel is a communication medium that allows one goroutine to send values to another goroutine. In this C library, a similar concept is used. A channel is a medium through which different threads can send and receive values. Channels can be used to safely pass data between threads.

  • Select: In Go, the select statement is used to choose from multiple send/receive channel operations. It blocks until one of the operations is ready, and if multiple are ready at the same time, it randomly selects one. In this C library, a similar mechanism exists where a thread can wait on multiple channel operations and proceed when one is ready.

  • Goroutine: While C does not have direct support for goroutines, the functionality of goroutines can be approximated with threads.

This library is not a direct clone of Go's concurrency model but it brings many of its benefits to C. It enables C programs to leverage the benefits of Go-style channels, and provides a model for structuring programs that is similar to what you might find in Go.

Create a Channel

In Go, creating a channel is as simple as using the make built-in function. In our C library, we have a similar function make_chan(size_t len) to create a channel.

Here is how you can use it:

int chan_descriptor = make_chan(10);

This function takes one argument which is the size of the channel's buffer, len. In this case, the size of the channel's buffer is 10. The function returns an integer which acts as a descriptor for the created channel.

The any_t Structure

The any_t structure is a way to store various types of data in a type-safe way. This is done through the use of a union, which can store different types of data in the same memory space. The actual type of the data is tracked using an int field named type.

Here is the any_t structure definition:

#define VAR_INT8         0x00
#define VAR_INT16        0x01
#define VAR_INT32        0x02
#define VAR_INT64        0x03
#define VAR_FLOAT        0x04
#define VAR_DOUBLE       0x05
#define VAR_POINTER      0x06

typedef struct {
    int type;
    union {
        int32_t int32_val;
        int64_t int64_val;
        int16_t int16_val;
        int8_t  int8_val;
        float   float_val;
        double  double_val;
        void    *pointer_val;
    } value;
} any_t;

The any_t structure has two main components:

  1. type: An integer that represents the type of the data stored in the value field. It can have one of the following values:
  • VAR_INT8: Indicates that value holds an 8-bit integer.
  • VAR_INT16: Indicates that value holds a 16-bit integer.
  • VAR_INT32: Indicates that value holds a 32-bit integer.
  • VAR_INT64: Indicates that value holds a 64-bit integer.
  • VAR_FLOAT: Indicates that value holds a single-precision floating-point number.
  • VAR_DOUBLE: Indicates that value holds a double-precision floating-point number.
  • VAR_POINTER: Indicates that value holds a pointer.
  1. value: A union that can hold different types of data. The actual type of data it holds is specified by the type field.

Here's an example of how to use any_t to store a 32-bit integer:

any_t my_var;
my_var.type = VAR_INT32;
my_var.value.int32_val = 12345;

And here's an example of how to use any_t to store a pointer:

any_t my_var;
my_var.type = VAR_POINTER;
my_var.value.pointer_val = &some_variable;

When retrieving the data from an any_t variable, make sure to use the correct field in the value union, as indicated by the type field:

if (my_var.type == VAR_INT32) {
    int32_t my_int = my_var.value.int32_val;
    // Use my_int
} else if (my_var.type == VAR_POINTER) {
    void *my_pointer = my_var.value.pointer_val;
    // Use my_pointer
}

Using the select_chan Function

The select_chan function is used to concurrently select and perform operations on multiple channels. This function takes three arguments:

int select_chan(select_set_t *set, size_t n, int should_block);
  1. An array of select_set_t structures, where each structure represents an operation (send or receive) on a specific channel.
  2. The size of the array (i.e., the number of operations to perform).
  3. A third argument that indicates whether the function should block if none of the operations can be immediately performed.

Here is an example of how to use select_chan:

any_t v;
int a = make_chan(1);
int b = make_chan(5);

/* ... create pthreads to send values to the channels */

for (;;) {
    int n;
    select_set_t op[] = {
        {a, OP_RECV, NULL, &v},  
        {b, OP_RECV, NULL, &v}
    };
    n = select_chan(op, 2, SELECT_BLOCK);
    if ( n == a ) {
        // Received from a.
    } else if (n == b) {
        // Received from b.
    }
}

In this example, select_chan will attempt to receive data from channels a and b. The function will block if there is no data available for receiving on any of the channels. When data is received on one of the channels, the function will store the data in the variable v.

The select_set_t Structure

typedef struct {
    int cd;
    int op_type;
    any_t *send;
    any_t *recv;
} select_set_t;

The select_set_t structure is used to specify an operation on a specific channel. This structure has four fields:

  • cd: The descriptor of the channel on which the operation will be performed.
  • op_type: The type of operation to perform. Can be OP_SEND to send data or OP_RECV to receive data.
  • send: A pointer to the data to send. This field is ignored if op_type is OP_RECV.
  • recv: A pointer to a location to store received data. This field is ignored if op_type is OP_SEND.

In the above example, both a and b are channel descriptors, OP_RECV indicates that the operation to perform is a receive, and &v is a pointer to a location to store received data.

About

Golang style channels in C

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published