Add 10_counting_semaphore
This commit is contained in:
1 parent
9aa20464c0
commit
024153c14c
3 files changed
+188
No files matched your search
@@ -0,0 +1,7 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20.0)
|
||||||
|
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
|
||||||
|
project(10_counting_semaphore)
|
||||||
|
|
||||||
|
target_sources(app PRIVATE src/main.c)
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
CONFIG_SERIAL=y
|
||||||
|
CONFIG_CONSOLE=y
|
||||||
|
CONFIG_PRINTK=y
|
||||||
@@ -0,0 +1,178 @@
|
|||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/sys/printk.h>
|
||||||
|
#include <zephyr/sys/atomic.h>
|
||||||
|
#include <stdint.h> // uintptr_t
|
||||||
|
#include <stdio.h> // snprintf()
|
||||||
|
|
||||||
|
#define STACK_SZ 1024
|
||||||
|
#define PRIO 5
|
||||||
|
|
||||||
|
#define CAPACITY 3 // Maximum number of simultaneous parked cars
|
||||||
|
#define NUM_CARS 5 // Total number of independent car
|
||||||
|
#define TAKE_TIMEOUT_MS 150 // Maximum time to wait for an available parking space
|
||||||
|
|
||||||
|
// Time each car occupies a parking space in ms
|
||||||
|
static const int park_time_ms[NUM_CARS] = {
|
||||||
|
200, 280, 240, 300, 260
|
||||||
|
};
|
||||||
|
|
||||||
|
// Time each car spends driving before requesting another space
|
||||||
|
static const int cruise_time_ms[NUM_CARS] = {
|
||||||
|
100, 80, 120, 90, 110
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Statically create a counting semaphore
|
||||||
|
*
|
||||||
|
* K_SEM_DEFINE(name, initial_count, count_limit)
|
||||||
|
*
|
||||||
|
* k_sem_take() consumes one token
|
||||||
|
* k_sem_give() returns one token
|
||||||
|
*/
|
||||||
|
K_SEM_DEFINE(parking_sem, CAPACITY, CAPACITY);
|
||||||
|
|
||||||
|
/* Shared atomic counter for occupied parking spaces
|
||||||
|
*
|
||||||
|
* Unlike the semaphore, this counter tracks how many
|
||||||
|
* cars have entered the simulated parking area
|
||||||
|
*
|
||||||
|
* It is used for logging and verification
|
||||||
|
* The semaphore itself enforces the capacity limit
|
||||||
|
*/
|
||||||
|
static atomic_t in_room = ATOMIC_INIT(0);
|
||||||
|
|
||||||
|
/* =====================================================
|
||||||
|
* Thread Objects and Stacks
|
||||||
|
* ===================================================== */
|
||||||
|
|
||||||
|
/* Thread Control Blocks (TCBs)
|
||||||
|
*
|
||||||
|
* Each k_thread object stores kernel-managed information
|
||||||
|
* about one thread, including its state and scheduling data
|
||||||
|
*/
|
||||||
|
static struct k_thread car_tcb[NUM_CARS];
|
||||||
|
|
||||||
|
/* Allocate a separate stack for every car thread
|
||||||
|
*
|
||||||
|
* NUM_CARS = 5
|
||||||
|
* STACK_SZ = 1024 bytes requested per thread
|
||||||
|
*/
|
||||||
|
K_THREAD_STACK_ARRAY_DEFINE(car_stacks, NUM_CARS, STACK_SZ);
|
||||||
|
|
||||||
|
/* Entry function executed independently by every car thread
|
||||||
|
*
|
||||||
|
* Zephyr thread entry functions receive three arguments:
|
||||||
|
* p1, p2, and p3
|
||||||
|
*
|
||||||
|
* Here, p1 carries the car ID
|
||||||
|
*/
|
||||||
|
static void car_entry(void *p1, void *p2, void *p3) {
|
||||||
|
|
||||||
|
// Prevent compiler warnings about unused arguments
|
||||||
|
ARG_UNUSED(p2);
|
||||||
|
ARG_UNUSED(p3);
|
||||||
|
|
||||||
|
// Recover the car ID supplied by k_thread_create()
|
||||||
|
const int car_id = (int)(uintptr_t)p1;
|
||||||
|
|
||||||
|
// Select the timing configuration for this car
|
||||||
|
const int park_ms = park_time_ms[car_id];
|
||||||
|
const int cruise_ms = cruise_time_ms[car_id];
|
||||||
|
|
||||||
|
printk("[CAR %d] Ready: Park=%d ms, Cruise=%d ms, Timeout=%d ms\n",
|
||||||
|
car_id, park_ms, cruise_ms, TAKE_TIMEOUT_MS);
|
||||||
|
|
||||||
|
// Each car repeatedly attempts to park
|
||||||
|
while (1) {
|
||||||
|
|
||||||
|
// Try to take one slot; timeout if lot is full for too long
|
||||||
|
int rc = k_sem_take(&parking_sem, K_MSEC(TAKE_TIMEOUT_MS));
|
||||||
|
|
||||||
|
if (rc == 0) {
|
||||||
|
|
||||||
|
// ENTER critical section: obtained a slot
|
||||||
|
int prior = atomic_inc(&in_room);
|
||||||
|
|
||||||
|
// prior is the old value; new in_room is prior+1
|
||||||
|
if (prior + 1 > CAPACITY) {
|
||||||
|
// This should never happen if the semaphore is respected
|
||||||
|
printk("[CAR %d] ERROR: over-capacity! in_room=%d\n",
|
||||||
|
car_id, prior + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
printk("[CAR %d] PARK (in_room=%d/%d)\n",
|
||||||
|
car_id, prior + 1, CAPACITY);
|
||||||
|
|
||||||
|
// Simulate parking (resource usage)
|
||||||
|
k_msleep(park_ms);
|
||||||
|
|
||||||
|
// EXIT: free the slot and leave
|
||||||
|
atomic_dec(&in_room);
|
||||||
|
k_sem_give(&parking_sem);
|
||||||
|
|
||||||
|
printk("[CAR %d] LEAVE (in_room=%ld/%d)\n",
|
||||||
|
car_id, atomic_get(&in_room), CAPACITY);
|
||||||
|
|
||||||
|
// Drive around before trying again (outside critical section)
|
||||||
|
k_msleep(cruise_ms);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Timed out — lot is full right now; try again soon
|
||||||
|
printk("[CAR %d] LOT FULL (timeout after %d ms)\n",
|
||||||
|
car_id, TAKE_TIMEOUT_MS);
|
||||||
|
|
||||||
|
// Small delay before the next parking attempt
|
||||||
|
k_msleep(30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
|
||||||
|
printk("\n[MAIN] Counting semaphore: CAPACITY=%d, NUM_CARS=%d\n", CAPACITY, NUM_CARS);
|
||||||
|
printk("[MAIN] One token == one free slot; k_sem_give() returns a slot\n");
|
||||||
|
printk("[MAIN] Up to %d cars can PARK concurrently; the rest wait/timeout\n", CAPACITY);
|
||||||
|
|
||||||
|
/* Create five independent threads.
|
||||||
|
*
|
||||||
|
* Each thread executes the same car_entry() function,
|
||||||
|
* but receives a unique car ID through argument p1.
|
||||||
|
*/
|
||||||
|
for (int i = 0; i < NUM_CARS; ++i) {
|
||||||
|
|
||||||
|
k_tid_t tid = k_thread_create(
|
||||||
|
&car_tcb[i], // Thread Control Block
|
||||||
|
car_stacks[i], // Stack assigned to this thread
|
||||||
|
STACK_SZ, // Requested stack size
|
||||||
|
car_entry, // Entry function
|
||||||
|
(void *)(uintptr_t)i, // p1: car ID
|
||||||
|
NULL, // p2: unused
|
||||||
|
NULL, // p3: unused
|
||||||
|
PRIO, // Scheduling priority
|
||||||
|
0, // No special thread options
|
||||||
|
K_NO_WAIT // Make thread ready immediately
|
||||||
|
);
|
||||||
|
|
||||||
|
/* Construct a readable thread name:
|
||||||
|
* CAR_0, CAR_1, CAR_2, CAR_3, CAR_4
|
||||||
|
*/
|
||||||
|
char name[12];
|
||||||
|
(void)snprintf(name, sizeof(name), "CAR_%d", i);
|
||||||
|
|
||||||
|
// Assign the name to the thread
|
||||||
|
k_thread_name_set(tid, name);
|
||||||
|
|
||||||
|
/* Delay creation of the next car thread by 10 ms.
|
||||||
|
*
|
||||||
|
* This staggers the starting times and helps make
|
||||||
|
* the console output easier to observe.
|
||||||
|
*/
|
||||||
|
k_msleep(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
k_sleep(K_FOREVER);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user