#include "namespace.h"
#include <sys/types.h>
#include <machine/atomic.h>
#include <errno.h>
#include <pthread.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "un-namespace.h"
#include "libc_private.h"
static constraint_handler_t *_ch = NULL;
static pthread_mutex_t ch_lock = PTHREAD_MUTEX_INITIALIZER;
constraint_handler_t
set_constraint_handler_s(constraint_handler_t handler)
{
constraint_handler_t *new, *old, ret;
new = malloc(sizeof(constraint_handler_t));
if (new == NULL)
return (NULL);
*new = handler;
if (__isthreaded)
_pthread_mutex_lock(&ch_lock);
old = _ch;
_ch = new;
if (__isthreaded)
_pthread_mutex_unlock(&ch_lock);
if (old == NULL) {
ret = NULL;
} else {
ret = *old;
free(old);
}
return (ret);
}
void
__throw_constraint_handler_s(const char * restrict msg, errno_t error)
{
constraint_handler_t ch;
if (__isthreaded)
_pthread_mutex_lock(&ch_lock);
ch = _ch != NULL ? *_ch : NULL;
if (__isthreaded)
_pthread_mutex_unlock(&ch_lock);
if (ch != NULL)
ch(msg, NULL, error);
}
void
abort_handler_s(const char * restrict msg, void * restrict ptr __unused,
errno_t error __unused)
{
static const char ahs[] = "abort_handler_s : ";
(void) _write(STDERR_FILENO, ahs, sizeof(ahs) - 1);
(void) _write(STDERR_FILENO, msg, strlen(msg));
(void) _write(STDERR_FILENO, "\n", 1);
abort();
}
void
ignore_handler_s(const char * restrict msg __unused,
void * restrict ptr __unused, errno_t error __unused)
{
}