Semaphore

Offers interface for accessing semaphores.  Semaphores are used to signal events between different thread_t or process_t.  If a process executes another program the semaphore is closed.  To prevent this an inherit function would be need and a way to transfer the id and init from id function.

Summary
SemaphoreOffers interface for accessing semaphores.
CopyrightThis program is free software.
Files
C-kern/api/platform/sync/semaphore.hHeader file of Semaphore.
C-kern/platform/Linux/sync/semaphore.cLinux implementation file Semaphore Linuximpl.
Types
struct semaphore_tExport semaphore_t.
Functions
test
unittest_platform_sync_semaphoreTests system semaphore functionality.
semaphore_tDescribes a system semaphore used between threads.
lifetime
semaphore_FREEStatic initializer.
init_semaphoreInitializes a semaphore.
free_semaphoreWakes up any waiting threads and frees the associated resources.
synchronize
signal_semaphoreWakes up signal_count waiters.
wait_semaphoreWaits until a signal is received.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/platform/sync/semaphore.h

Header file of Semaphore.

C-kern/platform/Linux/sync/semaphore.c

Linux implementation file Semaphore Linuximpl.

Types

struct semaphore_t

typedef struct semaphore_t semaphore_t

Export semaphore_t.

Functions

Summary
test
unittest_platform_sync_semaphoreTests system semaphore functionality.

test

unittest_platform_sync_semaphore

int unittest_platform_sync_semaphore(void)

Tests system semaphore functionality.

semaphore_t

struct semaphore_t

Describes a system semaphore used between threads.  This thread safe object allows to wait for or to send a signal to one or more waiters.

Summary
lifetime
semaphore_FREEStatic initializer.
init_semaphoreInitializes a semaphore.
free_semaphoreWakes up any waiting threads and frees the associated resources.
synchronize
signal_semaphoreWakes up signal_count waiters.
wait_semaphoreWaits until a signal is received.

lifetime

semaphore_FREE

#define semaphore_FREE { sys_semaphore_FREE }

Static initializer.

init_semaphore

int init_semaphore(/*out*/semaphore_t *semaobj,
uint16_t init_signal_count)

Initializes a semaphore.  The internal signal counter is set to init_signal_count.  The next init_signal_count calls to wait_semaphore therefore succeed without waiting.

free_semaphore

int free_semaphore(semaphore_t *semaobj)

Wakes up any waiting threads and frees the associated resources.  Make sure that no other thread which is not already waiting on the semaphore accesses it after free_semaphore has been called.

synchronize

signal_semaphore

int signal_semaphore(semaphore_t *semaobj,
uint32_t signal_count)

Wakes up signal_count waiters.  Or the next signal_count calls to wait_semaphore succeed without waiting.  Internally this function increments a signal counter with the number signal_count.  Calling this function signal_count times with a value of 1 has therefore the same effect.  If the internal counter would overflow the signal function waits until at least that many calls to wait_semaphore has been done so that adding signal_count to the counter does no more produce an overflow.  On Linux a 64bit counter is used internally.

wait_semaphore

int wait_semaphore(semaphore_t *semaobj)

Waits until a signal is received.  This functions waits until the internal counter becomes greater zero.  The signal counter is decremented by one and the function returns with success (0).

Offers interface for accessing semaphores.
Implements Semaphore.
typedef struct semaphore_t semaphore_t
Export semaphore_t.
struct semaphore_t
Describes a system semaphore used between threads.
int unittest_platform_sync_semaphore(void)
Tests system semaphore functionality.
#define semaphore_FREE { sys_semaphore_FREE }
Static initializer.
int init_semaphore(/*out*/semaphore_t *semaobj,
uint16_t init_signal_count)
Initializes a semaphore.
int free_semaphore(semaphore_t *semaobj)
Wakes up any waiting threads and frees the associated resources.
int signal_semaphore(semaphore_t *semaobj,
uint32_t signal_count)
Wakes up signal_count waiters.
int wait_semaphore(semaphore_t *semaobj)
Waits until a signal is received.
Close