0

I create a workqueue with create_workqueue, and then I want to print the name of the workqueue. The name should be the same name that I've used in create_workqueue as an argument. However, when I try to use my_workqueue->name it says that this is an invalid use of the struct. The item name exists in the structure.

static int __init my_init(void)
{
    struct workqueue_struct *my_workqueue = create_workqueue("myworkqueue");
    pr_info("%s\n", my_workqueue->name);

    return 0;
}
  • Aside: please see [What does double underscore ( __const) mean in C?](https://stackoverflow.com/questions/1449181/what-does-double-underscore-const-mean-in-c) *In C, symbols starting with an underscore followed by either an upper-case letter or another underscore are reserved for the implementation.* – Weather Vane Mar 16 '21 at 17:55
  • 1
    `struct workqueue_struct` is not completely defined by `#include `. `struct workqueue_struct *` should be treated as an opaque pointer type. – Ian Abbott Mar 16 '21 at 17:56
  • 1
    @WeatherVane This is proper usage of `__init` in Linux kernel code. (It expands to some GCC section attribute, allowing the code section to be discarded later.) – Ian Abbott Mar 16 '21 at 17:58
  • @Ian Abbott oh ok, thanks. I missed the space. – Weather Vane Mar 16 '21 at 17:59
  • @IanAbbott Can you elobrate please? Also, can you please explain what is the difference between `work_struct` and `workqueue_struct`? Thanks – Hassan Nasrallah Mar 16 '21 at 18:01
  • Does this answer your question? [C: "invalid use of undefined type ‘struct X’ & dereferencing pointer to incomplete type" errors](https://stackoverflow.com/questions/5943251/c-invalid-use-of-undefined-type-struct-x-dereferencing-pointer-to-incomple). In short: You cannot access fields of the struct `workqueue_struct` which is **defined** in (other) **source** file `kernel/workqueue.c`. – Tsyvarev Mar 16 '21 at 18:03
  • @Tsyvarev Thanks. But when I use and access data from [`struct work_struct workq;`](https://elixir.bootlin.com/linux/latest/source/include/linux/workqueue.h#L102), everything works fine, seems like it is defined in `linux/workqueue.h`. – Hassan Nasrallah Mar 16 '21 at 18:10
  • Yes, the struct `work_struct` is **defined** in the **header** file `linux/workqueue.h`. That is why you can access its fields. That is the core of the difference: you can `#include` a **header** file into your code and get all definitions from it. But you cannot (shouldn't) `#include` a **source** file, so its definitions are not accessible. – Tsyvarev Mar 16 '21 at 18:14
  • @Tsyvarev Ohh I get you. Yeah I didn't realize it is a source and not a header file. – Hassan Nasrallah Mar 16 '21 at 18:16

0 Answers0