| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- /*
- * Copyright (c) 2006-2022, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2018-08-24 yangjie the first version
- * 2020-10-17 Meco Man translate to English comment
- */
- /*
- * Demo: mutex(es)
- *
- * This demo demonstrates how the mutex manage the shared resource.
- *
- * read more:
- * https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/#mutex
- */
- #include <rtthread.h>
- #define THREAD_PRIORITY 8
- #define THREAD_TIMESLICE 5
- /* mutex handler */
- static rt_mutex_t dynamic_mutex = RT_NULL;
- static rt_uint8_t number1, number2 = 0;
- #ifdef rt_align
- rt_align(RT_ALIGN_SIZE)
- #else
- ALIGN(RT_ALIGN_SIZE)
- #endif
- static char thread1_stack[1024];
- static struct rt_thread thread1;
- static void rt_thread_entry1(void *parameter)
- {
- while (1)
- {
- /* pending the mutex */
- rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
- /* protect and deal with public variables */
- number1++;
- rt_thread_mdelay(10);
- number2++;
- /* release the mutex */
- rt_mutex_release(dynamic_mutex);
- }
- }
- #ifdef rt_align
- rt_align(RT_ALIGN_SIZE)
- #else
- ALIGN(RT_ALIGN_SIZE)
- #endif
- static char thread2_stack[1024];
- static struct rt_thread thread2;
- static void rt_thread_entry2(void *parameter)
- {
- while (1)
- {
- rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
- if (number1 != number2)
- {
- rt_kprintf("not protect.number1 = %d, mumber2 = %d \n", number1, number2);
- }
- else
- {
- rt_kprintf("mutex protect ,number1 = mumber2 is %d\n", number1);
- }
- number1++;
- number2++;
- rt_mutex_release(dynamic_mutex);
- if (number1 >= 50)
- return;
- }
- }
- int mutex_sample(void)
- {
- /* create mutex */
- dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_PRIO);
- if (dynamic_mutex == RT_NULL)
- {
- rt_kprintf("create dynamic mutex failed.\n");
- return -1;
- }
- rt_thread_init(&thread1,
- "thread1",
- rt_thread_entry1,
- RT_NULL,
- &thread1_stack[0],
- sizeof(thread1_stack),
- THREAD_PRIORITY, THREAD_TIMESLICE);
- #ifdef RT_USING_SMP
- /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
- rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
- #endif
- rt_thread_startup(&thread1);
- rt_thread_init(&thread2,
- "thread2",
- rt_thread_entry2,
- RT_NULL,
- &thread2_stack[0],
- sizeof(thread2_stack),
- THREAD_PRIORITY - 1, THREAD_TIMESLICE);
- #ifdef RT_USING_SMP
- /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
- rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
- #endif
- rt_thread_startup(&thread2);
- return 0;
- }
- /* export the msh command */
- MSH_CMD_EXPORT(mutex_sample, mutex sample);
|