Initialization of the work queue (init_work Parameter Problem)

Source: Internet
Author: User

When writing a small example of a work queue

Error: Macro "init_work" passed 3 arguments, but takes just 2

 

Starting from kernel 2.6.20, The init_work macro has changed,

It was originally three parameters, and then changed to two parameters.

 

 

From http://blog.csdn.net/fudan_abc/archive/2007/08/20/1751565.aspx

So let's take a closer look at init_work and init_delayed_work. in fact, the former is a special case of the latter. They involve the legendary work queue. both macros are defined in include/Linux/workqueue. h:

79 # define init_work (_ work, _ FUNC )/

80 do {/

81 (_ WORK)-> DATA = (atomic_long_t) work_data_init ();/

82 init_list_head (& (_ WORK)-> entry );/

83 prepare_work (_ work), (_ FUNC ));/

84} while (0)

85

86 # define init_delayed_work (_ work, _ FUNC )/

87 do {/

88 init_work (& (_ WORK)-> work, (_ FUNC ));/

89 init_timer (& (_ WORK)-> timer );/

90} while (0)

 

Solution:

Http://ubuntuforums.org/showthread.php? T = 441180

Change the following lines:
Static void ppsc_tq_int (void * Data)
Change
Static void ppsc_tq_int (struct work_struct * Data)


Init_work (& PHA-> WQ, ppsc_tq_int, PHA );

Change
Init_work (& PHA-> WQ, ppsc_tq_int );

Successful!

 

Another solution:Container_of(But I have never tried it)

 

Http://www.linuxsir.org/bbs/thread310620.html

=>

Http://rt2x00.serialmonkey.com/phpBB/viewtopic.php? F = 8 & t = 2951 & START = 0 & St = 0 & Sk = T & SD = A & SID = 0681e0000914045dedf06f000034b99131

==>

(About work_struct and init_work)

Http://lwn.net/Articles/213149/

 

Okay.

(1) Any work_struct struct that has one of the following called upon it:

Queue_delayed_work ()
Queue_delayed_work_on ()
Schedule_delayed_work ()
Schedule_delayed_work_on ()
Cancel_rearming_delayed_work ()
Cancel_rearming_delayed_workqueue ()
Cancel_delayed_work ()

Needs changing into a delayed_work struct.

Note that cancel_delayed_work () is often called where it'll be ineffective
-I think people misunderstand what it does.

(2) A delayed_work struct must be initialised:

_ Delayed_work_initializer
Declare_delayed_work
Init_delayed_work

Rather:

_ Work_initializer
Declare_work
Init_work

Those only apply to work_struct (non-delayable work ).

(3) the initialisation functions no longer take a data argument, and this
Shocould be deleted.

(4) anywhere one of the following is called on a delayed_work struct:

Queue_work ()
Queue_work_on ()
Schedule_work ()
Schedule_work_on ()

It must be converted to the equivalent one:

Queue_delayed_work ()
Queue_delayed_work_on ()
Schedule_delayed_work ()
Schedule_delayed_work_on ()

And given a 0 timeout argument as an additional argument. This just
Queues the work item and doesn' t set the timer.

(5) anywhere the work item's pending flag is examined directly:

Test_bit (0, & work-> pending)

This shoshould be replaced with the appropriate one:

Work_pending (work)
Delayed_work_pending (work)

(6) The work function _ must _ be changed to conform to the following prototype:

Void foo_work_func (struct work_struct * Work)
{
...
}

This applies to both work_struct and delayed_work handlers.

(A) If the arbitary datum previusly passed to the initialiser was null,
Then the work argument shoshould just be ignored.

(B) If the datum was the address of the structure containing
Work_struct, then something like the following shocould be used:

Struct Foo {
Struct work_struct worker;
...
};

Void foo_work_func (struct work_struct * Work)
{
Struct Foo * Foo = container_of (work, struct Foo, worker );
...
}

If the work_struct can be placed at the beginning of the containing
Structure This will eliminate the subtraction instruction
Container_of () might otherwise require.

(C) If the datum was the address of the structure containing
Delayed_work, then something like the following shocould be used:

Struct Foo {
Struct delayed_work worker;
...
};

Void foo_work_func (struct work_struct * Work)
{
Struct Foo * Foo = container_of (work, struct Foo, worker. Work );
...
}

Note! There's an extra ". Work" in the container_of () because
Work_struct pointed to is embedded within the delayed_work.

(D) If the datum is not a pointer to the iner, but the container is
Guaranteed to exist whilst the work handler runs, then the datum can
Be stored in an extra variable in the container.

The handler wowould then be formed as for (B) or (c), and the extra
Variable accessed after the container_of () line.

Quite often there's a linked pair of structures, with a work_struct in
One being initialised with the address of the other as its datum.
Typical Case is struct net_device and the private data. In this case
Just adding a back pointer from the private data to the net_device
Struct seems to work.

(E) If the auxiliary datum is totally unrelated and can't be stored in
Extra variable because the container might go away, then
Work_struct or delayed_work shoshould be initialised with one of these
Instead:

Declare_work_nar
Declare_delayed_work_nar
Init_work_nar
Init_delayed_work_nar
_ Work_initializer_nar
_ Delayed_work_initializer_nar

These take the same arguments as the normal initialisers, but set
Flag in the work_struct to indicate that the pending flag is not to be
Cleared before the work function is called.

The datum is then stored in an extra variable in the container:

Struct Foo {
Struct work_struct worker;
Void * worker_data;
...
};

And a work item is initialised with something like this:

Void thing (struct Foo * Foo)
{
...
Init_work_nar (& foo-> worker, foo_work_func );
Foo-> worker_data = silly_data;
...
}

And then the work function releases the work item itself when it has
Extracted the auxiliary data:

Void foo_work_func (struct work_struct * Work)
{
Struct Foo * Foo = container_of (work, struct Foo, worker );
Void * silly_data = foo-> worker_data;
Work_release (work );
...
}

As an added bonus, you can have multiple auxiliary data if you so
Desire. You're not limited to a single word.

(7) If the work function was being called directly, then rather than passing
In the auxiliary datum, you have to pass in the address of the work_struct
Instead. So for a work_struct, you 'd change:

Void call_work (struct Foo * Foo)
{
...
-Foo_work_func (FOO );
+ Foo_work_func (& foo-> worker );
...
}

And for a delayed_work, you 'd do:

Void call_work (struct Foo * Foo)
{
...
-Foo_work_func (FOO );
+ Foo_work_func (& foo-> worker. Work );
...
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.