zpp
Zephyr C++20 Framework
main.cpp File Reference
#include <zephyr/kernel.h>
#include <zephyr/arch/cpu.h>
#include <zephyr/sys/arch_interface.h>
#include <zpp.hpp>
#include <chrono>
Include dependency graph for main.cpp:

Go to the source code of this file.

Macros

#define NUM_THREADS   3
 
#define TCOUNT   10
 
#define COUNT_LIMIT   12
 
#define STACK_SIZE   (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
 

Functions

void inc_count (int my_id) noexcept
 
void watch_count (int my_id) noexcept
 
int main (void)
 

Macro Definition Documentation

◆ COUNT_LIMIT

#define COUNT_LIMIT   12

Definition at line 17 of file main.cpp.

◆ NUM_THREADS

#define NUM_THREADS   3

Copyright (c) 2021 Erwin Rol erwin.nosp@m.@erw.nosp@m.inrol.nosp@m..com Copyright (c) 2020 Intel Corporation

SPDX-License-Identifier: Apache-2.0

Definition at line 15 of file main.cpp.

◆ STACK_SIZE

#define STACK_SIZE   (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)

Definition at line 19 of file main.cpp.

◆ TCOUNT

#define TCOUNT   10

Definition at line 16 of file main.cpp.

Function Documentation

◆ inc_count()

void inc_count ( int  my_id)
noexcept

Definition at line 34 of file main.cpp.

35 {
36  for (int i = 0; i < TCOUNT; i++) {
37  {
38  zpp::lock_guard<zpp::mutex> lg(count_mutex);
39  count++;
40 
41  //
42  // Check the value of count and signal waiting thread when
43  // condition is reached. Note that this occurs while mutex is
44  // locked.
45  //
46 
47  if (count == COUNT_LIMIT) {
48  printk("%s: thread %d, count = %d Threshold reached.",
49  __func__, my_id, count);
50  count_threshold_cv.notify_one();
51  printk("Just sent signal.\n");
52  }
53 
54  printk("%s: thread %d, count = %d, unlocking mutex\n",
55  __func__, my_id, count);
56  }
57 
58  // Sleep so threads can alternate on mutex lock
59  zpp::this_thread::sleep_for(std::chrono::milliseconds(500));
60  }
61 }
lock_guard using zpp::mutex as a lock.
Definition: lock_guard.hpp:23
auto sleep_for(const std::chrono::duration< T_Rep, T_Period > &sleep_duration)
Suspend the current thread for a specified time duration.
Definition: thread.hpp:77
#define TCOUNT
Definition: main.cpp:16
#define COUNT_LIMIT
Definition: main.cpp:17

References COUNT_LIMIT, zpp::this_thread::sleep_for(), and TCOUNT.

Referenced by main().

◆ main()

int main ( void  )

Definition at line 86 of file main.cpp.

87 {
88  const zpp::thread_attr attrs(
92  );
93 
94  t[0] = zpp::thread(tcb[0], tstack(0), attrs, watch_count, 1);
95 
96  t[1] = zpp::thread(tcb[1], tstack(1), attrs, inc_count, 2);
97  t[2] = zpp::thread(tcb[2], tstack(2), attrs, inc_count, 3);
98 
99  // Wait for all threads to complete
100  for (int i = 0; i < NUM_THREADS; i++) {
101  t[i].join();
102  }
103 
104  printk("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n",
105  NUM_THREADS, count);
106 
107  return 0;
108 }
Thread creation attributes.
Definition: thread_attr.hpp:35
static constexpr thread_prio preempt(int prio) noexcept
Create a preemptive priority value.
The class thread repecents a single Zephyr thread.
Definition: thread.hpp:147
void inc_count(int my_id) noexcept
Definition: main.cpp:34
#define NUM_THREADS
Definition: main.cpp:15
void watch_count(int my_id) noexcept
Definition: main.cpp:63

References inc_count(), zpp::no, NUM_THREADS, zpp::thread_prio::preempt(), and watch_count().

◆ watch_count()

void watch_count ( int  my_id)
noexcept

Definition at line 63 of file main.cpp.

64 {
65  printk("Starting %s: thread %d\n", __func__, my_id);
66 
67  zpp::unique_lock lk(count_mutex);
68 
69  while (count < COUNT_LIMIT) {
70  printk("%s: thread %d Count= %d. Going into wait...\n",
71  __func__, my_id, count);
72 
73  count_threshold_cv.wait(lk);
74 
75  printk("%s: thread %d Condition signal received. Count= %d\n",
76  __func__, my_id, count);
77  }
78 
79  printk("%s: thread %d Updating the value of count...\n",
80  __func__, my_id);
81  count += 125;
82  printk("%s: thread %d count now = %d.\n", __func__, my_id, count);
83  printk("%s: thread %d Unlocking mutex.\n", __func__, my_id);
84 }
zpp::unique_lock using zpp::mutex as a lock.
Definition: unique_lock.hpp:24

References COUNT_LIMIT.

Referenced by main().