2000-03-05 13:57:25 +00:00
|
|
|
/* sane - Scanner Access Now Easy.
|
2001-08-11 09:46:53 +00:00
|
|
|
Copyright (C) 1998-2001 Yuri Dario
|
2004-01-13 07:40:28 +00:00
|
|
|
Copyright (C) 2003-2004 Gerhard Jaeger (pthread/process support)
|
2000-03-05 13:57:25 +00:00
|
|
|
This file is part of the SANE package.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
|
|
|
|
MA 02111-1307, USA.
|
|
|
|
|
|
|
|
As a special exception, the authors of SANE give permission for
|
|
|
|
additional uses of the libraries contained in this release of SANE.
|
|
|
|
|
|
|
|
The exception is that, if you link a SANE library with other files
|
|
|
|
to produce an executable, this does not by itself cause the
|
|
|
|
resulting executable to be covered by the GNU General Public
|
|
|
|
License. Your use of that executable is in no way restricted on
|
|
|
|
account of linking the SANE library code into it.
|
|
|
|
|
|
|
|
This exception does not, however, invalidate any other reasons why
|
|
|
|
the executable file might be covered by the GNU General Public
|
|
|
|
License.
|
|
|
|
|
|
|
|
If you submit changes to SANE to the maintainers to be included in
|
|
|
|
a subsequent release, you agree by submitting the changes that
|
|
|
|
those changes may be distributed with this exception intact.
|
|
|
|
|
|
|
|
If you write modifications of your own for SANE, it is your choice
|
|
|
|
whether to permit this exception to apply to your modifications.
|
2003-10-07 07:38:37 +00:00
|
|
|
If you do not wish that, delete this exception notice.
|
2001-08-11 09:46:53 +00:00
|
|
|
|
2003-10-08 20:31:15 +00:00
|
|
|
OS/2
|
2001-08-11 09:46:53 +00:00
|
|
|
Helper functions for the OS/2 port (using threads instead of forked
|
|
|
|
processes). Don't use them in the backends, they are used automatically by
|
|
|
|
macros.
|
2003-10-05 16:00:30 +00:00
|
|
|
|
2003-10-08 20:31:15 +00:00
|
|
|
Other OS:
|
|
|
|
use this lib, if you intend to let run your reader function within its own
|
|
|
|
task (thread or process). Depending on the OS and/or the configure settings
|
|
|
|
pthread or fork is used to achieve this goal.
|
2000-03-05 13:57:25 +00:00
|
|
|
*/
|
|
|
|
|
2001-08-11 09:46:53 +00:00
|
|
|
#include "../include/sane/config.h"
|
2000-03-05 13:57:25 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-10-12 15:39:04 +00:00
|
|
|
#include <string.h>
|
2003-10-08 20:31:15 +00:00
|
|
|
#include <errno.h>
|
2003-10-17 12:20:05 +00:00
|
|
|
#include <signal.h>
|
2000-03-05 13:57:25 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2003-10-13 07:16:09 +00:00
|
|
|
#ifdef HAVE_OS2_H
|
|
|
|
# define INCL_DOSPROCESS
|
|
|
|
# include <os2.h>
|
|
|
|
#endif
|
2005-07-15 21:59:25 +00:00
|
|
|
#ifdef __BEOS__
|
|
|
|
# undef USE_PTHREAD /* force */
|
|
|
|
# include <kernel/OS.h>
|
|
|
|
#endif
|
|
|
|
#if !defined USE_PTHREAD && !defined HAVE_OS2_H && !defined __BEOS__
|
2003-10-17 12:20:05 +00:00
|
|
|
# include <sys/wait.h>
|
2003-10-12 15:39:04 +00:00
|
|
|
#endif
|
|
|
|
#if defined USE_PTHREAD
|
2003-10-13 07:16:09 +00:00
|
|
|
# include <pthread.h>
|
2003-10-12 15:39:04 +00:00
|
|
|
#endif
|
2000-03-05 13:57:25 +00:00
|
|
|
|
2003-10-07 07:38:37 +00:00
|
|
|
#define BACKEND_NAME sanei_thread /**< name of this module for debugging */
|
|
|
|
|
|
|
|
#include "../include/sane/sane.h"
|
|
|
|
#include "../include/sane/sanei_debug.h"
|
|
|
|
#include "../include/sane/sanei_thread.h"
|
|
|
|
|
|
|
|
#ifndef _VAR_NOT_USED
|
|
|
|
# define _VAR_NOT_USED(x) ((x)=(x))
|
|
|
|
#endif
|
2003-10-12 15:39:04 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
|
|
|
int (*func)( void* );
|
|
|
|
SANE_Status status;
|
|
|
|
void *func_data;
|
|
|
|
|
|
|
|
} ThreadDataDef, *pThreadDataDef;
|
|
|
|
|
|
|
|
static ThreadDataDef td;
|
|
|
|
|
2003-10-07 07:38:37 +00:00
|
|
|
/** for init issues - here only for the debug output
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sanei_thread_init( void )
|
|
|
|
{
|
|
|
|
DBG_INIT();
|
2003-10-12 15:39:04 +00:00
|
|
|
|
|
|
|
memset( &td, 0, sizeof(ThreadDataDef));
|
|
|
|
td.status = SANE_STATUS_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
SANE_Bool
|
|
|
|
sanei_thread_is_forked( void )
|
|
|
|
{
|
2005-07-15 21:59:25 +00:00
|
|
|
#if defined USE_PTHREAD || defined HAVE_OS2_H || defined __BEOS__
|
2003-10-12 15:39:04 +00:00
|
|
|
return SANE_FALSE;
|
|
|
|
#else
|
|
|
|
return SANE_TRUE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-05-15 12:50:25 +00:00
|
|
|
sanei_thread_kill( SANE_Pid pid )
|
2003-10-12 15:39:04 +00:00
|
|
|
{
|
2008-05-15 12:50:25 +00:00
|
|
|
DBG(2, "sanei_thread_kill() will kill %ld\n", (long) pid);
|
2003-10-12 15:39:04 +00:00
|
|
|
#ifdef USE_PTHREAD
|
2004-02-01 19:14:28 +00:00
|
|
|
#if defined (__APPLE__) && defined (__MACH__)
|
|
|
|
return pthread_kill((pthread_t)pid, SIGUSR2);
|
|
|
|
#else
|
2003-10-12 15:39:04 +00:00
|
|
|
return pthread_cancel((pthread_t)pid);
|
2004-02-01 19:14:28 +00:00
|
|
|
#endif
|
2003-10-12 15:39:04 +00:00
|
|
|
#elif defined HAVE_OS2_H
|
|
|
|
return DosKillThread(pid);
|
|
|
|
#else
|
|
|
|
return kill( pid, SIGTERM );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-03-05 13:57:25 +00:00
|
|
|
#ifdef HAVE_OS2_H
|
2001-08-11 09:46:53 +00:00
|
|
|
|
2003-10-12 15:39:04 +00:00
|
|
|
static void
|
|
|
|
local_thread( void *arg )
|
|
|
|
{
|
|
|
|
pThreadDataDef ltd = (pThreadDataDef)arg;
|
|
|
|
|
|
|
|
DBG( 2, "thread started, calling func() now...\n" );
|
|
|
|
ltd->status = ltd->func( ltd->func_data );
|
|
|
|
|
|
|
|
DBG( 2, "func() done - status = %d\n", ltd->status );
|
2003-10-13 07:16:09 +00:00
|
|
|
_endthread();
|
2003-10-12 15:39:04 +00:00
|
|
|
}
|
|
|
|
|
2000-03-05 13:57:25 +00:00
|
|
|
/*
|
|
|
|
* starts a new thread or process
|
|
|
|
* parameters:
|
2003-10-07 07:38:37 +00:00
|
|
|
* star address of reader function
|
|
|
|
* args pointer to scanner data structure
|
2000-03-05 13:57:25 +00:00
|
|
|
*
|
2003-10-17 11:06:00 +00:00
|
|
|
*/
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid
|
2003-10-10 06:57:36 +00:00
|
|
|
sanei_thread_begin( int (*func)(void *args), void* args )
|
2000-03-05 13:57:25 +00:00
|
|
|
{
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid pid;
|
2003-10-12 15:39:04 +00:00
|
|
|
|
|
|
|
td.func = func;
|
|
|
|
td.func_data = args;
|
|
|
|
|
|
|
|
pid = _beginthread( local_thread, NULL, 1024*1024, (void*)&td );
|
|
|
|
if ( pid == -1 ) {
|
|
|
|
DBG( 1, "_beginthread() failed\n" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG( 2, "_beginthread() created thread %d\n", pid );
|
|
|
|
return pid;
|
2000-03-05 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid
|
|
|
|
sanei_thread_waitpid( SANE_Pid pid, int *status )
|
2000-03-05 13:57:25 +00:00
|
|
|
{
|
2003-10-10 06:57:36 +00:00
|
|
|
if (status)
|
2003-10-08 20:31:15 +00:00
|
|
|
*status = 0;
|
2002-02-05 22:04:26 +00:00
|
|
|
return pid; /* DosWaitThread( (TID*) &pid, DCWW_WAIT);*/
|
2000-03-05 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-05-15 12:50:25 +00:00
|
|
|
sanei_thread_sendsig( SANE_Pid pid, int sig )
|
2000-03-05 13:57:25 +00:00
|
|
|
{
|
2003-10-08 20:31:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-15 21:59:25 +00:00
|
|
|
#elif defined __BEOS__
|
|
|
|
|
|
|
|
static int32
|
|
|
|
local_thread( void *arg )
|
|
|
|
{
|
|
|
|
pThreadDataDef ltd = (pThreadDataDef)arg;
|
|
|
|
|
|
|
|
DBG( 2, "thread started, calling func() now...\n" );
|
|
|
|
ltd->status = ltd->func( ltd->func_data );
|
|
|
|
|
|
|
|
DBG( 2, "func() done - status = %d\n", ltd->status );
|
|
|
|
return ltd->status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* starts a new thread or process
|
|
|
|
* parameters:
|
|
|
|
* star address of reader function
|
|
|
|
* args pointer to scanner data structure
|
|
|
|
*
|
|
|
|
*/
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid
|
2005-07-15 21:59:25 +00:00
|
|
|
sanei_thread_begin( int (*func)(void *args), void* args )
|
|
|
|
{
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid pid;
|
2005-07-15 21:59:25 +00:00
|
|
|
|
|
|
|
td.func = func;
|
|
|
|
td.func_data = args;
|
|
|
|
|
|
|
|
pid = spawn_thread( local_thread, "sane thread (yes they can be)", B_NORMAL_PRIORITY, (void*)&td );
|
|
|
|
if ( pid < B_OK ) {
|
|
|
|
DBG( 1, "spawn_thread() failed\n" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ( resume_thread(pid) < B_OK ) {
|
|
|
|
DBG( 1, "resume_thread() failed\n" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG( 2, "spawn_thread() created thread %d\n", pid );
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid
|
|
|
|
sanei_thread_waitpid( SANE_Pid pid, int *status )
|
2005-07-15 21:59:25 +00:00
|
|
|
{
|
|
|
|
int32 st;
|
|
|
|
if ( wait_for_thread(pid, &st) < B_OK )
|
|
|
|
return -1;
|
|
|
|
if ( status )
|
|
|
|
*status = (int)st;
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-05-15 12:50:25 +00:00
|
|
|
sanei_thread_sendsig( SANE_Pid pid, int sig )
|
2005-07-15 21:59:25 +00:00
|
|
|
{
|
|
|
|
if (sig == SIGKILL)
|
|
|
|
sig = SIGKILLTHR;
|
|
|
|
return kill(pid, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_OS2_H, __BEOS__ */
|
2003-10-05 16:00:30 +00:00
|
|
|
|
2003-10-08 20:31:15 +00:00
|
|
|
#ifdef USE_PTHREAD
|
|
|
|
|
2003-10-10 06:57:36 +00:00
|
|
|
/* seems to be undefined in MacOS X */
|
|
|
|
#ifndef PTHREAD_CANCELED
|
|
|
|
# define PTHREAD_CANCELED ((void *) -1)
|
|
|
|
#endif
|
|
|
|
|
2003-10-17 11:06:00 +00:00
|
|
|
/**
|
|
|
|
*/
|
2004-02-01 19:14:28 +00:00
|
|
|
#if defined (__APPLE__) && defined (__MACH__)
|
|
|
|
static void
|
|
|
|
thread_exit_handler( int signo )
|
|
|
|
{
|
2004-04-14 19:33:56 +00:00
|
|
|
DBG( 2, "signal(%i) caught, calling pthread_exit now...\n", signo );
|
2004-02-01 19:14:28 +00:00
|
|
|
pthread_exit( PTHREAD_CANCELED );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-10-08 20:31:15 +00:00
|
|
|
static void*
|
|
|
|
local_thread( void *arg )
|
|
|
|
{
|
|
|
|
static int status;
|
2003-10-12 15:39:04 +00:00
|
|
|
pThreadDataDef ltd = (pThreadDataDef)arg;
|
2003-10-08 20:31:15 +00:00
|
|
|
|
2004-02-01 19:14:28 +00:00
|
|
|
#if defined (__APPLE__) && defined (__MACH__)
|
|
|
|
struct sigaction act;
|
|
|
|
|
|
|
|
sigemptyset(&(act.sa_mask));
|
|
|
|
act.sa_flags = 0;
|
|
|
|
act.sa_handler = thread_exit_handler;
|
|
|
|
sigaction( SIGUSR2, &act, 0 );
|
|
|
|
#else
|
2004-04-14 19:33:56 +00:00
|
|
|
int old;
|
|
|
|
|
2003-10-08 20:31:15 +00:00
|
|
|
pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &old );
|
2004-01-13 07:40:28 +00:00
|
|
|
pthread_setcanceltype ( PTHREAD_CANCEL_ASYNCHRONOUS, &old );
|
2004-02-01 19:14:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
DBG( 2, "thread started, calling func() now...\n" );
|
|
|
|
|
2003-10-12 15:39:04 +00:00
|
|
|
status = ltd->func( ltd->func_data );
|
2003-10-08 20:31:15 +00:00
|
|
|
|
2004-04-09 12:19:47 +00:00
|
|
|
/* so sanei_thread_get_status() will work correctly... */
|
|
|
|
ltd->status = status;
|
|
|
|
|
2003-10-08 20:31:15 +00:00
|
|
|
DBG( 2, "func() done - status = %d\n", status );
|
|
|
|
|
|
|
|
/* return the status, so pthread_join is able to get it*/
|
|
|
|
pthread_exit((void*)&status );
|
|
|
|
}
|
|
|
|
|
2003-10-17 11:06:00 +00:00
|
|
|
/**
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
restore_sigpipe( void )
|
|
|
|
{
|
|
|
|
struct sigaction act;
|
|
|
|
|
|
|
|
if( sigaction( SIGPIPE, NULL, &act ) == 0 ) {
|
|
|
|
|
|
|
|
if( act.sa_handler == SIG_IGN ) {
|
|
|
|
sigemptyset( &act.sa_mask );
|
|
|
|
act.sa_flags = 0;
|
|
|
|
act.sa_handler = SIG_DFL;
|
|
|
|
|
|
|
|
DBG( 2, "restoring SIGPIPE to SIG_DFL\n" );
|
|
|
|
sigaction( SIGPIPE, &act, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-08 20:31:15 +00:00
|
|
|
#else /* the process stuff */
|
|
|
|
|
|
|
|
static int
|
2008-05-15 12:50:25 +00:00
|
|
|
eval_wp_result( SANE_Pid pid, int wpres, int pf )
|
2003-10-08 20:31:15 +00:00
|
|
|
{
|
|
|
|
int retval = SANE_STATUS_IO_ERROR;
|
|
|
|
|
|
|
|
if( wpres == pid ) {
|
|
|
|
|
|
|
|
if( WIFEXITED(pf)) {
|
|
|
|
retval = WEXITSTATUS(pf);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if( !WIFSIGNALED(pf)) {
|
|
|
|
retval = SANE_STATUS_GOOD;
|
|
|
|
} else {
|
|
|
|
DBG( 1, "Child terminated by signal %d\n", WTERMSIG(pf));
|
|
|
|
if( WTERMSIG(pf) == SIGTERM )
|
|
|
|
retval = SANE_STATUS_GOOD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid
|
2003-10-08 20:31:15 +00:00
|
|
|
sanei_thread_begin( int (func)(void *args), void* args )
|
2003-10-05 16:00:30 +00:00
|
|
|
{
|
2003-10-07 11:28:03 +00:00
|
|
|
#ifdef USE_PTHREAD
|
2003-10-17 11:06:00 +00:00
|
|
|
struct sigaction act;
|
2008-05-15 12:50:25 +00:00
|
|
|
int result;
|
2003-10-12 15:39:04 +00:00
|
|
|
pthread_t thread;
|
2003-10-05 16:00:30 +00:00
|
|
|
|
2003-10-17 11:06:00 +00:00
|
|
|
/* if signal handler for SIGPIPE is SIG_DFL, replace by SIG_IGN */
|
|
|
|
if( sigaction( SIGPIPE, NULL, &act ) == 0 ) {
|
|
|
|
|
|
|
|
if( act.sa_handler == SIG_DFL ) {
|
|
|
|
sigemptyset( &act.sa_mask );
|
|
|
|
act.sa_flags = 0;
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
|
|
|
|
DBG( 2, "setting SIGPIPE to SIG_IGN\n" );
|
|
|
|
sigaction( SIGPIPE, &act, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-01 19:14:28 +00:00
|
|
|
td.func = func;
|
|
|
|
td.func_data = args;
|
2003-10-17 11:06:00 +00:00
|
|
|
|
2008-05-15 12:50:25 +00:00
|
|
|
result = pthread_create( &thread, NULL, local_thread, &td );
|
2004-02-01 19:14:28 +00:00
|
|
|
usleep( 1 );
|
2003-10-05 16:00:30 +00:00
|
|
|
|
2008-05-15 12:50:25 +00:00
|
|
|
if ( result != 0 ) {
|
|
|
|
DBG( 1, "pthread_create() failed with %d\n", result );
|
2003-10-05 16:00:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-15 12:50:25 +00:00
|
|
|
DBG( 2, "pthread_create() created thread %ld\n", (SANE_Pid)thread );
|
|
|
|
return (SANE_Pid)thread;
|
2003-10-07 07:38:37 +00:00
|
|
|
#else
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid pid;
|
2003-10-07 07:38:37 +00:00
|
|
|
pid = fork();
|
|
|
|
if( pid < 0 ) {
|
|
|
|
DBG( 1, "fork() failed\n" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pid == 0 ) {
|
|
|
|
|
|
|
|
/* run in child context... */
|
2008-05-15 12:50:25 +00:00
|
|
|
int status = func( args );
|
2003-10-07 07:38:37 +00:00
|
|
|
|
|
|
|
/* don't use exit() since that would run the atexit() handlers */
|
|
|
|
_exit( status );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parents return */
|
|
|
|
return pid;
|
|
|
|
#endif
|
2003-10-05 16:00:30 +00:00
|
|
|
}
|
|
|
|
|
2003-10-08 20:31:15 +00:00
|
|
|
int
|
2008-05-15 12:50:25 +00:00
|
|
|
sanei_thread_sendsig( SANE_Pid pid, int sig )
|
2003-10-08 20:31:15 +00:00
|
|
|
{
|
|
|
|
#ifdef USE_PTHREAD
|
2008-05-15 12:50:25 +00:00
|
|
|
DBG(2, "sanei_thread_sendsig() %d to thread (id=%ld)\n", sig, pid);
|
|
|
|
return pthread_kill( (pthread_t)pid, sig );
|
2003-10-07 07:38:37 +00:00
|
|
|
#else
|
2003-10-08 20:31:15 +00:00
|
|
|
DBG(2, "sanei_thread_sendsig() %d to process (id=%d)\n", sig, pid);
|
2003-10-07 07:38:37 +00:00
|
|
|
return kill( pid, sig );
|
|
|
|
#endif
|
2003-10-05 16:00:30 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid
|
|
|
|
sanei_thread_waitpid( SANE_Pid pid, int *status )
|
2003-10-05 16:00:30 +00:00
|
|
|
{
|
2003-10-07 11:28:03 +00:00
|
|
|
#ifdef USE_PTHREAD
|
2003-10-08 20:31:15 +00:00
|
|
|
int *ls;
|
|
|
|
#else
|
|
|
|
int ls;
|
|
|
|
#endif
|
2008-05-15 12:50:25 +00:00
|
|
|
SANE_Pid result;
|
|
|
|
int stat;
|
2003-10-10 13:01:46 +00:00
|
|
|
|
|
|
|
stat = 0;
|
2003-10-07 07:38:37 +00:00
|
|
|
|
2008-05-15 12:50:25 +00:00
|
|
|
DBG(2, "sanei_thread_waitpid() - %ld\n", (long) pid);
|
2003-10-08 20:31:15 +00:00
|
|
|
#ifdef USE_PTHREAD
|
2008-05-15 12:50:25 +00:00
|
|
|
result = pthread_join( (pthread_t)pid, (void*)&ls );
|
2003-10-07 07:38:37 +00:00
|
|
|
|
2003-10-08 20:31:15 +00:00
|
|
|
if( 0 == result ) {
|
|
|
|
if( PTHREAD_CANCELED == ls ) {
|
|
|
|
DBG(2, "* thread has been canceled!\n" );
|
2003-10-10 13:01:46 +00:00
|
|
|
stat = SANE_STATUS_GOOD;
|
2003-10-08 20:31:15 +00:00
|
|
|
} else {
|
2003-10-10 13:01:46 +00:00
|
|
|
stat = *ls;
|
2003-10-08 20:31:15 +00:00
|
|
|
}
|
2003-10-10 13:01:46 +00:00
|
|
|
DBG(2, "* result = %d (%p)\n", stat, (void*)status );
|
2004-01-13 07:40:28 +00:00
|
|
|
result = pid;
|
2003-10-08 20:31:15 +00:00
|
|
|
}
|
2004-10-14 06:51:17 +00:00
|
|
|
/* call detach in any case to make sure that the thread resources
|
|
|
|
* will be freed, when the thread has terminated
|
|
|
|
*/
|
2008-05-15 12:50:25 +00:00
|
|
|
DBG(2, "* detaching thread(%ld)\n", pid );
|
2004-10-14 06:51:17 +00:00
|
|
|
pthread_detach((pthread_t)pid);
|
2003-10-10 13:01:46 +00:00
|
|
|
if (status)
|
|
|
|
*status = stat;
|
2003-10-17 11:06:00 +00:00
|
|
|
|
|
|
|
restore_sigpipe();
|
2003-10-07 07:38:37 +00:00
|
|
|
#else
|
2003-10-08 20:31:15 +00:00
|
|
|
result = waitpid( pid, &ls, 0 );
|
|
|
|
if((result < 0) && (errno == ECHILD)) {
|
2003-10-10 13:01:46 +00:00
|
|
|
stat = SANE_STATUS_GOOD;
|
2003-10-08 20:31:15 +00:00
|
|
|
result = pid;
|
|
|
|
} else {
|
2003-10-10 13:01:46 +00:00
|
|
|
stat = eval_wp_result( pid, result, ls );
|
|
|
|
DBG(2, "* result = %d (%p)\n", stat, (void*)status );
|
2003-10-08 20:31:15 +00:00
|
|
|
}
|
2003-10-10 13:01:46 +00:00
|
|
|
if( status )
|
|
|
|
*status = stat;
|
2003-10-08 20:31:15 +00:00
|
|
|
#endif
|
2004-01-13 07:40:28 +00:00
|
|
|
return result;
|
2003-10-08 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
2003-10-12 15:39:04 +00:00
|
|
|
#endif /* HAVE_OS2_H */
|
2003-10-05 16:00:30 +00:00
|
|
|
|
2003-10-17 12:20:05 +00:00
|
|
|
SANE_Status
|
2008-05-15 12:50:25 +00:00
|
|
|
sanei_thread_get_status( SANE_Pid pid )
|
2003-10-17 12:20:05 +00:00
|
|
|
{
|
2005-07-15 21:59:25 +00:00
|
|
|
#if defined USE_PTHREAD || defined HAVE_OS2_H || defined __BEOS__
|
2003-10-17 12:20:05 +00:00
|
|
|
_VAR_NOT_USED( pid );
|
|
|
|
|
|
|
|
return td.status;
|
|
|
|
#else
|
|
|
|
int ls, stat, result;
|
|
|
|
|
|
|
|
stat = SANE_STATUS_IO_ERROR;
|
|
|
|
if( pid > 0 ) {
|
|
|
|
|
|
|
|
result = waitpid( pid, &ls, WNOHANG );
|
|
|
|
|
|
|
|
stat = eval_wp_result( pid, result, ls );
|
|
|
|
}
|
|
|
|
return stat;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-10-13 07:16:09 +00:00
|
|
|
/* END sanei_thread.c .......................................................*/
|