Skip to content

VietDung2k1/linux-event-driven-with-pthreads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

POS - EVENT DRIVEN WITH POSIX THREADS

Event-driven development with pthreads (POSIX Threads).

image

1 - Benefits of Event-Driven Development

Utilizing pthreads (POSIX Threads) as the foundation for embedded applications offers the advantage of creating dedicated threads for various project functions. These threads can efficiently manage interactions with peripherals, sensors, and other devices, while leveraging the Linux kernel for robust multitasking and concurrency.

Adopting an event-driven model using pthreads (POSIX Threads) in embedded applications offers several key advantages.

  • Optimized Resource Management
  • Enhanced Flexibility
  • Simplified Scalability
  • Improved Responsiveness
  • Better Modularity

2 - The Active Object Pattern

It basically creates a task that continuously waits for events in a private mailbox. The task also has a dispatcher, which is a function that can handle and execute different actions depending on the type of event.

void *task_demo(void *) {

    ...

	while (pos_task_receive_msg(TASK_DEMO_ID, &msg_id, &msg_data, &msg_len)) {
		switch (msg_id) {
		case AC_DEMO_TASK_POST: {
			POS_LOG_INFO(TAG, "AC_DEMO_TASK_POST");
		} break;

        ... Other signal ...

		default:
			break;
		}
		pos_free_msg(msg_data);
	}

	return (void *)0;
}

pos_task_receive_msg: waits until a message is sent to the task. The message can come from another task, from a timer service.

Only tasks that receive events need to create a mailbox for them.

/* shell: debug and config */
pos_create_task(TASK_SHELL_ID);

/* demo */
pos_create_vector(TASK_DEMO_ID);
pos_create_task(TASK_DEMO_ID);

Build and Run the project.

make
make run

Example output

     _/_/_/      _/_/      _/_/_/   
    _/    _/  _/    _/  _/          
   _/_/_/    _/    _/    _/_/       
  _/        _/    _/        _/      
 _/          _/_/    _/_/_/         
Author: VietDung
Build: Nov 25 2024
[15:34:14] [POS] table task size: 2
[15:34:14] [POS] create task name: _shell, id: 0, task: 0x55ca1f7de660
[15:34:14] [POS] create msg handle : _demo, id: 1, task: 0x55ca225c2820
[15:34:14] [POS] wait ...
[15:34:14] [POS] create task name: _demo, id: 1, task: 0x55ca1f7de860
[15:34:14] [POS] POS STARTED
[15:34:14] [POS] wait ...
[15:34:14] [DEMO] STARTED
[15:34:14] [POS] task post id: 1, sig: 10, msg: (nil), len: 0
[15:34:14] [POS] timer set id: 1, sig: 11, msg: 0x55ca1f7df4bf, len: 12
[15:34:14] [POS] timer set copy msg 11 -> 0x7f2338000bb0, len: 12
[15:34:14] [POS] task receive copy msg 10 -> (nil), len: 0
[15:34:14] [DEMO] AC_DEMO_TASK_POST
[15:34:14] [SHELL] STARTED
[15:34:15] [TIMER]  task_id:1 msg_id:11
[15:34:15] [POS] task post id: 1, sig: 11, msg: 0x7f2338000bb0, len: 12
[15:34:15] [POS] task post copy msg 11 -> 0x7f233c000b60, len: 12
[15:34:15] [POS] task receive copy msg 11 -> 0x7f233c000b60, len: 12
[15:34:15] [DEMO] AC_DEMO_TASK_POST_AND_DATA: Hello World!
[15:34:15] [POS] delete msg 0x7f233c000b60

Reference

Topic Link
Blog & Tutorial https://epcb.vn/blogs/ak-embedded-software

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published