sememu.c File Reference

#include "semaphore.h"
#include "Carbon/Carbon.h"

Classes

struct  sememu_t

Defines

#define MAX_SEMAPHORES   256
#define GetSemaphorePtr(idx)   &(semaphores[(idx)])
#define SemaphoreIsAvail(idx)   !(used_semaphores[(idx)])
#define SemaphoreSetAvail(idx, b)   used_semaphores[(idx)] = !(b)
#define SemaphoreValid(sem)

Functions

void OSStatus2Errno (OSStatus err)
int sem_init (sem_t *sem, int local, unsigned int value)
int sem_destroy (sem_t *sem)
int sem_wait (sem_t *sem)
int sem_trywait (sem_t *sem)
int sem_post (sem_t *sem)
int sem_getvalue (sem_t *sem, int *sval)
sem_t * sem_open (const char *name, int oflag,...)
int sem_close (sem_t *sem)

Detailed Description

This file includes funtions that emulate POSIX semaphores on a Mac OS X system using the "native" Carbon semaphores. Named semaphores are currently not implemented (i.e. sem_open() and sem_close() always fails with errno set to ENOSYS).

Define Documentation

#define SemaphoreValid sem   ) 
 

Value:

!((sem) < 0 || (sem) > MAX_SEMAPHORES || \
               SemaphoreIsAvail((sem)))


Function Documentation

void OSStatus2Errno OSStatus  err  ) 
 

Set errno to the closes equivalent of an OSStatus.

int sem_close sem_t *  sem  ) 
 

sem_close is not implemented. This function always returnes with errno set to ENOSYS and return value SEM_FAILED.

Returns:
SEM_FAILED

int sem_destroy sem_t *  sem  ) 
 

sem_destroy destroys a semaphore object, freeing the resources it might hold. No threads should be waiting on the semaphore at the time sem_destroy is called.

sem_destroy can set errno to one of the following errors:

[EINVAL] - The sem argument is not a valid semaphore.

[EBUSY] - There are currently processes blocked on the semaphore.

Parameters:
sem A pointer to the semaphore object to destroy.
Returns:
0 upon successful completion, -1 otherwise. If -1 is returned errno is set to the appropriate error.

int sem_getvalue sem_t *  sem,
int *  sval
 

sem_getvalue stores in the location pointed to by sval the current count of the semaphore sem.

sem_getvalue can set errno to one of the following errors:

[EINVAL] - The sem argument is not a valid semaphore.

Parameters:
sem A pointer to the semaphore object to get the value of.
sval A pointer to a location to store the value of sem.
Returns:
0 upon successful completion, -1 otherwise. If -1 is returned errno is set to the appropriate error.

int sem_init sem_t *  sem,
int  local,
unsigned int  value
 

sem_init initializes the semaphore object pointed to by sem. The count associated with the semaphore is set ini- tially to value. The pshared argument indicates whether the semaphore is local to the current process ( pshared is zero) or is to be shared between several processes ( pshared is not zero). Sememu currently does not sup- port process-shared semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.

sem_init can set errno to one of the following errors:

[EINVAL] - The value argument exceeds SEM_VALUE_MAX.

[ENOSPC] - A resource required to initialize the semaphore has been exhausted, or the limit on semaphores (MAX_SEMAPHORES) has been reached.

Parameters:
sem A pointer to the semaphore object to initialize.
local Should always be 0!
value The initial value of the semaphore (usually 1).
Returns:
0 upon successful completion, -1 otherwise. If -1 is returned errno is set to the appropriate error.

sem_t* sem_open const char *  name,
int  oflag,
... 
 

sem_open is not implemented. This function always returnes with errno set to ENOSYS and return value SEM_FAILED.

Returns:
SEM_FAILED

int sem_post sem_t *  sem  ) 
 

sem_post atomically increases the count of the semaphore pointed to by sem. This function never blocks and can safely be used in asynchronous signal handlers.

sem_post can set errno to one of the following errors:

[EINVAL] - The sem argument is not a valid semaphore.

Parameters:
sem A pointer to the semaphore object to unlock.
Returns:
0 upon successful completion, -1 otherwise. If -1 is returned errno is set to the appropriate error.

int sem_trywait sem_t *  sem  ) 
 

sem_trywait is a non-blocking variant of sem_wait. If the semaphore pointed to by sem has non-zero count, the count is atomically decreased and sem_trywait immediately returns 0. If the semaphore count is zero, sem_trywait immediately returns with error EAGAIN.

sem_trywait can set errno to one of the following errors:

[EINVAL] - The sem argument is not a valid semaphore.

[EAGAIN] - The semaphore was already locked, so it cannot be immediately locked by the sem_trywait() operation.

Parameters:
sem A pointer to the semaphore object to wait on.
Returns:
0 upon successful completion, -1 otherwise. If -1 is returned errno is set to the appropriate error.

int sem_wait sem_t *  sem  ) 
 

sem_wait suspends the calling thread until the semaphore pointed to by sem has non-zero count. It then atomically decreases the semaphore count.

sem_wait can set errno to one of the following errors:

[EINVAL] - The sem argument is not a valid semaphore.

Parameters:
sem A pointer to the semaphore object to wait on.
Returns:
0 upon successful completion, -1 otherwise. If -1 is returned errno is set to the appropriate error.