2007-05-20 20:30:31 +00:00
|
|
|
/*
|
|
|
|
* Cross Platform Thread/Mutex abstraction
|
|
|
|
* Copyright(C) 2007 Michael Jerris
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so.
|
|
|
|
*
|
|
|
|
* This work is provided under this license on an "as is" basis, without warranty of any kind,
|
|
|
|
* either expressed or implied, including, without limitation, warranties that the covered code
|
|
|
|
* is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
|
|
|
|
* risk as to the quality and performance of the covered code is with you. Should any covered
|
|
|
|
* code prove defective in any respect, you (not the initial developer or any other contributor)
|
|
|
|
* assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
|
|
|
|
* constitutes an essential part of this license. No use of any covered code is authorized hereunder
|
|
|
|
* except under this disclaimer.
|
|
|
|
*
|
2009-11-16 15:21:08 +00:00
|
|
|
* Contributors:
|
|
|
|
*
|
|
|
|
* Moises Silva <moy@sangoma.com>
|
|
|
|
*
|
2007-05-20 20:30:31 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-21 02:40:06 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
/* required for TryEnterCriticalSection definition. Must be defined before windows.h include */
|
|
|
|
#define _WIN32_WINNT 0x0400
|
|
|
|
#endif
|
2007-05-20 20:30:31 +00:00
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
#include "freetdm.h"
|
|
|
|
#include "ftdm_threadmutex.h"
|
2007-05-20 20:30:31 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2007-05-21 02:40:06 +00:00
|
|
|
#include <process.h>
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
#define FTDM_THREAD_CALLING_CONVENTION __stdcall
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
struct ftdm_mutex {
|
2007-05-20 20:30:31 +00:00
|
|
|
CRITICAL_SECTION mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
2010-03-12 18:27:24 +00:00
|
|
|
#include <poll.h>
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
#define FTDM_THREAD_CALLING_CONVENTION
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
struct ftdm_mutex {
|
2007-05-21 02:41:36 +00:00
|
|
|
pthread_mutex_t mutex;
|
2007-05-21 02:40:06 +00:00
|
|
|
};
|
|
|
|
|
2009-12-09 20:35:53 +00:00
|
|
|
#endif
|
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
struct ftdm_interrupt {
|
|
|
|
ftdm_socket_t device;
|
2009-12-09 20:35:53 +00:00
|
|
|
#ifdef WIN32
|
2010-03-12 18:27:24 +00:00
|
|
|
/* for generic interruption */
|
|
|
|
HANDLE event;
|
2009-12-09 20:35:53 +00:00
|
|
|
#else
|
2010-03-12 18:27:24 +00:00
|
|
|
/* for generic interruption */
|
|
|
|
int readfd;
|
|
|
|
int writefd;
|
2009-12-09 20:35:53 +00:00
|
|
|
#endif
|
2009-11-14 02:04:38 +00:00
|
|
|
};
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
struct ftdm_thread {
|
2007-05-21 02:40:06 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
void *handle;
|
|
|
|
#else
|
|
|
|
pthread_t handle;
|
|
|
|
#endif
|
|
|
|
void *private_data;
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_thread_function_t function;
|
|
|
|
ftdm_size_t stack_size;
|
2007-05-21 02:40:06 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
pthread_attr_t attribute;
|
|
|
|
#endif
|
2007-05-20 20:30:31 +00:00
|
|
|
};
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_size_t thread_default_stacksize = 0;
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
FT_DECLARE(void) ftdm_thread_override_default_stacksize(ftdm_size_t size)
|
2007-05-21 02:40:06 +00:00
|
|
|
{
|
|
|
|
thread_default_stacksize = size;
|
|
|
|
}
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
static void * FTDM_THREAD_CALLING_CONVENTION thread_launch(void *args)
|
2007-05-21 02:40:06 +00:00
|
|
|
{
|
|
|
|
void *exit_val;
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_thread_t *thread = (ftdm_thread_t *)args;
|
2007-05-21 02:40:06 +00:00
|
|
|
exit_val = thread->function(thread, thread->private_data);
|
|
|
|
#ifndef WIN32
|
|
|
|
pthread_attr_destroy(&thread->attribute);
|
|
|
|
#endif
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_safe_free(thread);
|
2007-05-21 02:40:06 +00:00
|
|
|
|
|
|
|
return exit_val;
|
|
|
|
}
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) ftdm_thread_create_detached(ftdm_thread_function_t func, void *data)
|
2007-05-21 02:40:06 +00:00
|
|
|
{
|
2010-01-15 19:22:49 +00:00
|
|
|
return ftdm_thread_create_detached_ex(func, data, thread_default_stacksize);
|
2007-05-21 02:40:06 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) ftdm_thread_create_detached_ex(ftdm_thread_function_t func, void *data, ftdm_size_t stack_size)
|
2007-05-21 02:40:06 +00:00
|
|
|
{
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_thread_t *thread = NULL;
|
|
|
|
ftdm_status_t status = FTDM_FAIL;
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
if (!func || !(thread = (ftdm_thread_t *)ftdm_malloc(sizeof(ftdm_thread_t)))) {
|
2007-05-21 02:40:06 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->private_data = data;
|
|
|
|
thread->function = func;
|
|
|
|
thread->stack_size = stack_size;
|
|
|
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
thread->handle = (void *)_beginthreadex(NULL, (unsigned)thread->stack_size, (unsigned int (__stdcall *)(void *))thread_launch, thread, 0, NULL);
|
|
|
|
if (!thread->handle) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
CloseHandle(thread->handle);
|
2007-05-21 15:38:35 +00:00
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
status = FTDM_SUCCESS;
|
2007-05-21 15:38:35 +00:00
|
|
|
goto done;
|
2007-05-21 02:40:06 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
if (pthread_attr_init(&thread->attribute) != 0) goto fail;
|
|
|
|
|
2007-05-21 15:38:35 +00:00
|
|
|
if (pthread_attr_setdetachstate(&thread->attribute, PTHREAD_CREATE_DETACHED) != 0) goto failpthread;
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2007-05-21 15:38:35 +00:00
|
|
|
if (thread->stack_size && pthread_attr_setstacksize(&thread->attribute, thread->stack_size) != 0) goto failpthread;
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2007-05-21 15:38:35 +00:00
|
|
|
if (pthread_create(&thread->handle, &thread->attribute, thread_launch, thread) != 0) goto failpthread;
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
status = FTDM_SUCCESS;
|
2007-05-21 02:40:06 +00:00
|
|
|
goto done;
|
2007-06-04 14:53:12 +00:00
|
|
|
failpthread:
|
2007-05-21 15:38:35 +00:00
|
|
|
pthread_attr_destroy(&thread->attribute);
|
|
|
|
#endif
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
fail:
|
2007-05-21 02:40:06 +00:00
|
|
|
if (thread) {
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_safe_free(thread);
|
2007-05-21 02:40:06 +00:00
|
|
|
}
|
2007-06-04 14:53:12 +00:00
|
|
|
done:
|
2007-05-21 02:40:06 +00:00
|
|
|
return status;
|
|
|
|
}
|
2007-05-20 20:30:31 +00:00
|
|
|
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) ftdm_mutex_create(ftdm_mutex_t **mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_status_t status = FTDM_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
#endif
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_mutex_t *check = NULL;
|
2007-05-20 20:30:31 +00:00
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
check = (ftdm_mutex_t *)ftdm_malloc(sizeof(**mutex));
|
2007-05-20 20:30:31 +00:00
|
|
|
if (!check)
|
|
|
|
goto done;
|
|
|
|
#ifdef WIN32
|
|
|
|
InitializeCriticalSection(&check->mutex);
|
|
|
|
#else
|
|
|
|
if (pthread_mutexattr_init(&attr))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (pthread_mutex_init(&check->mutex, &attr))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
goto success;
|
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
fail:
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
goto done;
|
2007-05-20 20:30:31 +00:00
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
success:
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
|
|
|
*mutex = check;
|
2010-01-15 19:22:49 +00:00
|
|
|
status = FTDM_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
done:
|
2007-05-20 20:30:31 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) ftdm_mutex_destroy(ftdm_mutex_t **mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_mutex_t *mp = *mutex;
|
2007-05-21 17:48:13 +00:00
|
|
|
*mutex = NULL;
|
2008-03-19 00:26:27 +00:00
|
|
|
if (!mp) {
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2008-03-19 00:26:27 +00:00
|
|
|
}
|
2007-05-20 20:30:31 +00:00
|
|
|
#ifdef WIN32
|
2007-05-21 17:48:13 +00:00
|
|
|
DeleteCriticalSection(&mp->mutex);
|
2007-05-20 20:30:31 +00:00
|
|
|
#else
|
2007-05-21 17:48:13 +00:00
|
|
|
if (pthread_mutex_destroy(&mp->mutex))
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_safe_free(mp);
|
|
|
|
return FTDM_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) _ftdm_mutex_lock(ftdm_mutex_t *mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
EnterCriticalSection(&mutex->mutex);
|
|
|
|
#else
|
2009-11-20 22:57:24 +00:00
|
|
|
int err;
|
|
|
|
if ((err = pthread_mutex_lock(&mutex->mutex))) {
|
2010-01-15 19:22:49 +00:00
|
|
|
ftdm_log(FTDM_LOG_ERROR, "Failed to lock mutex %d:%s\n", err, strerror(err));
|
|
|
|
return FTDM_FAIL;
|
2009-11-20 22:57:24 +00:00
|
|
|
}
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) _ftdm_mutex_trylock(ftdm_mutex_t *mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
if (!TryEnterCriticalSection(&mutex->mutex))
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#else
|
2007-05-21 02:47:32 +00:00
|
|
|
if (pthread_mutex_trylock(&mutex->mutex))
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) _ftdm_mutex_unlock(ftdm_mutex_t *mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
LeaveCriticalSection(&mutex->mutex);
|
|
|
|
#else
|
|
|
|
if (pthread_mutex_unlock(&mutex->mutex))
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
}
|
2007-06-04 14:53:12 +00:00
|
|
|
|
2008-02-18 16:15:24 +00:00
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) ftdm_interrupt_create(ftdm_interrupt_t **ininterrupt, ftdm_socket_t device)
|
2009-11-14 02:04:38 +00:00
|
|
|
{
|
2010-03-12 18:27:24 +00:00
|
|
|
ftdm_interrupt_t *interrupt = NULL;
|
|
|
|
#ifndef WIN32
|
|
|
|
int fds[2];
|
|
|
|
#endif
|
2009-11-14 02:04:38 +00:00
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
ftdm_assert_return(ininterrupt != NULL, FTDM_FAIL, "interrupt double pointer is null!\n");
|
2009-11-14 02:04:38 +00:00
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
interrupt = ftdm_calloc(1, sizeof(*interrupt));
|
|
|
|
if (!interrupt) {
|
|
|
|
ftdm_log(FTDM_LOG_ERROR, "Failed to allocate interrupt memory\n");
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
interrupt->device = device;
|
2009-11-16 15:52:52 +00:00
|
|
|
#ifdef WIN32
|
2010-04-06 16:04:46 +00:00
|
|
|
interrupt->event = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!interrupt->event) {
|
2010-03-12 18:27:24 +00:00
|
|
|
ftdm_log(FTDM_LOG_ERROR, "Failed to allocate interrupt event\n");
|
2009-11-16 15:52:52 +00:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
#else
|
2010-03-12 18:27:24 +00:00
|
|
|
if (pipe(fds)) {
|
|
|
|
ftdm_log(FTDM_LOG_ERROR, "Failed to allocate interrupt pipe: %s\n", strerror(errno));
|
2009-11-14 02:04:38 +00:00
|
|
|
goto failed;
|
|
|
|
}
|
2010-03-12 18:27:24 +00:00
|
|
|
interrupt->readfd = fds[0];
|
|
|
|
interrupt->writefd = fds[1];
|
2009-11-16 15:52:52 +00:00
|
|
|
#endif
|
2009-11-14 02:04:38 +00:00
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
*ininterrupt = interrupt;
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_SUCCESS;
|
2009-11-14 02:04:38 +00:00
|
|
|
|
|
|
|
failed:
|
2010-03-12 18:27:24 +00:00
|
|
|
if (interrupt) {
|
|
|
|
#ifndef WIN32
|
|
|
|
if (interrupt->readfd) {
|
|
|
|
close(interrupt->readfd);
|
|
|
|
close(interrupt->writefd);
|
|
|
|
interrupt->readfd = -1;
|
|
|
|
interrupt->writefd = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ftdm_safe_free(interrupt);
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
2009-12-09 21:45:09 +00:00
|
|
|
#define ONE_BILLION 1000000000
|
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) ftdm_interrupt_wait(ftdm_interrupt_t *interrupt, int ms)
|
2009-11-14 02:04:38 +00:00
|
|
|
{
|
2010-03-12 18:27:24 +00:00
|
|
|
int num = 1;
|
2009-11-14 02:04:38 +00:00
|
|
|
#ifdef WIN32
|
2009-11-16 15:52:52 +00:00
|
|
|
DWORD res = 0;
|
2010-03-12 18:27:24 +00:00
|
|
|
HANDLE ints[2];
|
|
|
|
#else
|
|
|
|
int res = 0;
|
|
|
|
struct pollfd ints[2];
|
|
|
|
char pipebuf[255];
|
2009-11-26 16:01:55 +00:00
|
|
|
#endif
|
2010-03-12 18:27:24 +00:00
|
|
|
|
|
|
|
ftdm_assert_return(interrupt != NULL, FTDM_FAIL, "Condition is null!\n");
|
|
|
|
|
|
|
|
|
|
|
|
/* start implementation */
|
2009-11-26 16:01:55 +00:00
|
|
|
#ifdef WIN32
|
2010-03-12 18:27:24 +00:00
|
|
|
ints[0] = interrupt->event;
|
|
|
|
if (interrupt->device != FTDM_INVALID_SOCKET) {
|
|
|
|
num++;
|
|
|
|
ints[1] = interrupt->device;
|
|
|
|
}
|
|
|
|
res = WaitForMultipleObjects(num, &ints, FALSE, ms >= 0 ? ms : INFINITE);
|
2009-11-16 15:52:52 +00:00
|
|
|
switch (res) {
|
|
|
|
case WAIT_TIMEOUT:
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_TIMEOUT;
|
2009-11-16 15:52:52 +00:00
|
|
|
case WAIT_FAILED:
|
2010-03-12 18:27:24 +00:00
|
|
|
case WAIT_ABANDONED: /* is it right to fail with abandoned? */
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2009-11-26 16:01:55 +00:00
|
|
|
default:
|
2010-03-12 18:27:24 +00:00
|
|
|
if (res >= (sizeof(ints)/sizeof(ints[0]))) {
|
|
|
|
ftdm_log(FTDM_LOG_ERROR, "Error waiting for freetdm interrupt event (WaitForSingleObject returned %d)\n", res);
|
|
|
|
return FTDM_FAIL;
|
|
|
|
}
|
|
|
|
return FTDM_SUCCESS;
|
2009-11-16 15:52:52 +00:00
|
|
|
}
|
2009-11-14 02:04:38 +00:00
|
|
|
#else
|
2010-03-12 18:27:24 +00:00
|
|
|
ints[0].fd = interrupt->readfd;
|
|
|
|
ints[0].events = POLLIN;
|
|
|
|
ints[0].revents = 0;
|
|
|
|
|
|
|
|
if (interrupt->device != FTDM_INVALID_SOCKET) {
|
|
|
|
num++;
|
|
|
|
ints[1].fd = interrupt->device;
|
|
|
|
ints[1].events = POLLIN;
|
|
|
|
ints[1].revents = 0;
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
2010-01-12 17:52:17 +00:00
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
res = poll(ints, num, ms);
|
|
|
|
|
|
|
|
if (res == -1) {
|
|
|
|
ftdm_log(FTDM_LOG_CRIT, "interrupt poll failed (%s)\n", strerror(errno));
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
2010-03-12 18:27:24 +00:00
|
|
|
|
|
|
|
if (res == 0) {
|
|
|
|
return FTDM_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ints[0].revents & POLLIN) {
|
|
|
|
res = read(ints[0].fd, pipebuf, sizeof(pipebuf));
|
|
|
|
if (res == -1) {
|
|
|
|
ftdm_log(FTDM_LOG_CRIT, "reading interrupt descriptor failed (%s)\n", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_SUCCESS;
|
2009-12-02 16:36:42 +00:00
|
|
|
#endif
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
2008-02-18 16:15:24 +00:00
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) ftdm_interrupt_signal(ftdm_interrupt_t *interrupt)
|
2009-11-14 02:04:38 +00:00
|
|
|
{
|
2010-03-12 18:27:24 +00:00
|
|
|
ftdm_assert_return(interrupt != NULL, FTDM_FAIL, "Interrupt is null!\n");
|
2009-11-14 02:04:38 +00:00
|
|
|
#ifdef WIN32
|
2010-03-12 18:27:24 +00:00
|
|
|
if (!SetEvent(interrupt->interrupt)) {
|
|
|
|
ftdm_log(FTDM_LOG_ERROR, "Failed to signal interrupt\n");
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2009-11-16 15:52:52 +00:00
|
|
|
}
|
2009-11-14 02:04:38 +00:00
|
|
|
#else
|
2009-11-20 22:57:24 +00:00
|
|
|
int err;
|
2010-03-12 18:27:24 +00:00
|
|
|
if ((err = write(interrupt->writefd, "w", 1)) != 1) {
|
|
|
|
ftdm_log(FTDM_LOG_ERROR, "Failed to signal interrupt: %s\n", errno, strerror(errno));
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
|
|
|
#endif
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_SUCCESS;
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
2008-02-18 16:15:24 +00:00
|
|
|
|
2010-03-12 18:27:24 +00:00
|
|
|
FT_DECLARE(ftdm_status_t) ftdm_interrupt_destroy(ftdm_interrupt_t **ininterrupt)
|
2009-11-14 02:04:38 +00:00
|
|
|
{
|
2010-03-12 18:27:24 +00:00
|
|
|
ftdm_interrupt_t *interrupt = NULL;
|
|
|
|
ftdm_assert_return(ininterrupt != NULL, FTDM_FAIL, "Interrupt null when destroying!\n");
|
|
|
|
interrupt = *ininterrupt;
|
2009-11-14 02:04:38 +00:00
|
|
|
#ifdef WIN32
|
2010-04-06 16:04:46 +00:00
|
|
|
CloseHandle(interrupt->event);
|
2009-11-14 02:04:38 +00:00
|
|
|
#else
|
2010-03-12 18:27:24 +00:00
|
|
|
close(interrupt->readfd);
|
|
|
|
close(interrupt->writefd);
|
|
|
|
interrupt->readfd = -1;
|
|
|
|
interrupt->writefd = -1;
|
|
|
|
#endif
|
|
|
|
ftdm_safe_free(interrupt);
|
|
|
|
*ininterrupt = NULL;
|
|
|
|
return FTDM_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_DECLARE(ftdm_status_t) ftdm_interrupt_multiple_wait(ftdm_interrupt_t *interrupts[], ftdm_size_t size, int ms)
|
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
int i;
|
|
|
|
int res = 0;
|
|
|
|
int numdevices = 0;
|
|
|
|
char pipebuf[255];
|
|
|
|
struct pollfd ints[size*2];
|
|
|
|
|
|
|
|
memset(&ints, 0, sizeof(ints));
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
ints[i].events = POLLIN;
|
|
|
|
ints[i].revents = 0;
|
|
|
|
ints[i].fd = interrupts[i]->readfd;
|
|
|
|
if (interrupts[i]->device != FTDM_INVALID_SOCKET) {
|
|
|
|
ints[i+numdevices].events = POLLIN;
|
|
|
|
ints[i+numdevices].revents = 0;
|
|
|
|
ints[i+numdevices].fd = interrupts[i]->device;
|
|
|
|
numdevices++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = poll(ints, size + numdevices, ms);
|
|
|
|
|
|
|
|
if (res == -1) {
|
|
|
|
ftdm_log(FTDM_LOG_CRIT, "interrupt poll failed (%s)\n", strerror(errno));
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_FAIL;
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
2010-03-12 18:27:24 +00:00
|
|
|
|
|
|
|
if (res == 0) {
|
|
|
|
return FTDM_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2010-03-26 15:08:54 +00:00
|
|
|
for (i = 0; i < size; i++) {
|
2010-03-12 18:27:24 +00:00
|
|
|
if (ints[i].revents & POLLIN) {
|
2010-03-26 15:08:54 +00:00
|
|
|
res = read(ints[i].fd, pipebuf, sizeof(pipebuf));
|
2010-03-12 18:27:24 +00:00
|
|
|
if (res == -1) {
|
|
|
|
ftdm_log(FTDM_LOG_CRIT, "reading interrupt descriptor failed (%s)\n", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-14 02:04:38 +00:00
|
|
|
#endif
|
2010-01-15 19:22:49 +00:00
|
|
|
return FTDM_SUCCESS;
|
2009-11-14 02:04:38 +00:00
|
|
|
}
|
2008-02-18 16:15:24 +00:00
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* indent-tabs-mode:t
|
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2009-09-14 22:06:20 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
2007-06-04 14:53:12 +00:00
|
|
|
*/
|