Skip to content

File syn_pt_sem.h

FileList > pt > syn_pt_sem.h

Go to the source code of this file

Lightweight counting semaphores for protothreads. More...

  • #include "../port/syn_port_system.h"
  • #include "syn_pt.h"

Classes

Type Name
struct SYN_PT_Sem
Counting semaphore for protothreads.

Public Static Functions

Type Name
void pt_sem_signal (SYN_PT_Sem * sem)
Increment the semaphore count, unblocking a waiting protothread.
int pt_sem_trywait (SYN_PT_Sem * sem)
Try to acquire the semaphore without blocking.

Macros

Type Name
define PT_SEM_BLOCK (pt, task, sem) /* multi line expression */
Block task execution (SYN_TASK_BLOCKED) until semaphore count is > 0, then decrement it.
define PT_SEM_COUNT (sem) ((sem)->count)
define PT_SEM_INIT (sem, initial) ((sem)->count = (int16\_t)(initial))
Initialize a semaphore with the given count.
define PT_SEM_SIGNAL (sem) [**pt\_sem\_signal**](syn__pt__sem_8h.md#function-pt_sem_signal)(sem)
Macro wrapper for pt_sem_signal.
define PT_SEM_TRYWAIT (sem) [**pt\_sem\_trywait**](syn__pt__sem_8h.md#function-pt_sem_trywait)(sem)
Macro wrapper for pt_sem_trywait.
define PT_SEM_WAIT (pt, sem) /* multi line expression */
Block the protothread until the semaphore count is > 0, then decrement it.

Detailed Description

Provides inter-protothread synchronization with zero RAM overhead beyond the 2-byte counter itself. All operations are macro/inline — no .c file is needed.

** **

static SYN_PT_Sem data_ready;
PT_SEM_INIT(&data_ready, 0);

// Producer (can be called from ISR):
PT_SEM_SIGNAL(&data_ready);

// Consumer (inside a protothread):
PT_SEM_WAIT(pt, &data_ready);
// ... data is now available ...

Public Static Functions Documentation

function pt_sem_signal

Increment the semaphore count, unblocking a waiting protothread.

static inline void pt_sem_signal (
    SYN_PT_Sem * sem
) 

Safe to call from ISR context. The increment runs inside a critical section: count++ on a volatile int16_t is a load-modify-store, and a preempting ISR signaling the same semaphore would otherwise be overwritten by the stale store (lost signal).

Parameters:

  • sem Semaphore to signal.

function pt_sem_trywait

Try to acquire the semaphore without blocking.

static inline int pt_sem_trywait (
    SYN_PT_Sem * sem
) 

Parameters:

  • sem Semaphore to try.

Returns:

1 if acquired (count was > 0 and was decremented), 0 otherwise.


Macro Definition Documentation

define PT_SEM_BLOCK

Block task execution (SYN_TASK_BLOCKED) until semaphore count is > 0, then decrement it.

#define PT_SEM_BLOCK (
    pt,
    task,
    sem
) `/* multi line expression */`

Parameters:

  • pt Protothread.
  • task Pointer to SYN_Task.
  • sem Semaphore to wait on.

define PT_SEM_COUNT

#define PT_SEM_COUNT (
    sem
) `((sem)->count)`

Return the current count.


define PT_SEM_INIT

Initialize a semaphore with the given count.

#define PT_SEM_INIT (
    sem,
    initial
) `((sem)->count = (int16_t)(initial))`

Parameters:

  • sem Pointer to the semaphore.
  • initial Initial count (typically 0 or 1).

define PT_SEM_SIGNAL

Macro wrapper for pt_sem_signal.

#define PT_SEM_SIGNAL (
    sem
) `pt_sem_signal (sem)`


define PT_SEM_TRYWAIT

Macro wrapper for pt_sem_trywait.

#define PT_SEM_TRYWAIT (
    sem
) `pt_sem_trywait (sem)`


define PT_SEM_WAIT

Block the protothread until the semaphore count is > 0, then decrement it.

#define PT_SEM_WAIT (
    pt,
    sem
) `/* multi line expression */`

Must be called from within a protothread (between PT_BEGIN/PT_END).

Parameters:

  • pt Protothread.
  • sem Semaphore to wait on.


The documentation for this class was generated from the following file src/syntropic/pt/syn_pt_sem.h