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
|
2003-10-05 16:00:30 +00:00
|
|
|
Copyright (C) 2003 Gerhard Jaeger
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
Added pthread support (as found in hp-handle.c) - Gerhard Jaeger
|
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>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/** for init issues - here only for the debug output
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sanei_thread_init( void )
|
|
|
|
{
|
|
|
|
DBG_INIT();
|
|
|
|
}
|
|
|
|
|
2000-03-05 13:57:25 +00:00
|
|
|
#ifdef HAVE_OS2_H
|
2001-08-11 09:46:53 +00:00
|
|
|
|
2000-03-05 13:57:25 +00:00
|
|
|
#define INCL_DOSPROCESS
|
|
|
|
#include <os2.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
*
|
|
|
|
*/
|
|
|
|
int
|
2003-10-07 07:38:37 +00:00
|
|
|
sanei_thread_begin( void (*start)(void *arg), void* args )
|
2000-03-05 13:57:25 +00:00
|
|
|
{
|
2003-10-07 07:38:37 +00:00
|
|
|
return _beginthread( start, NULL, 1024*1024, args );
|
2000-03-05 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sanei_thread_kill( int pid, int sig)
|
|
|
|
{
|
|
|
|
return DosKillThread( pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sanei_thread_waitpid( int pid, int *stat_loc, int options)
|
|
|
|
{
|
2002-02-05 22:04:26 +00:00
|
|
|
if (stat_loc)
|
|
|
|
*stat_loc = 0;
|
|
|
|
return pid; /* DosWaitThread( (TID*) &pid, DCWW_WAIT);*/
|
2000-03-05 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sanei_thread_wait( int *stat_loc)
|
|
|
|
{
|
|
|
|
*stat_loc = 0;
|
|
|
|
return -1; /* return error because I don't know child pid */
|
|
|
|
}
|
|
|
|
|
2003-10-07 07:38:37 +00:00
|
|
|
#else /* HAVE_OS2_H */
|
2003-10-05 16:00:30 +00:00
|
|
|
|
2003-10-07 11:28:03 +00:00
|
|
|
#ifdef USE_PTHREAD
|
2003-10-07 07:38:37 +00:00
|
|
|
# include <pthread.h>
|
|
|
|
#else
|
|
|
|
# include <sys/wait.h>
|
|
|
|
#endif
|
2003-10-05 16:00:30 +00:00
|
|
|
|
|
|
|
int
|
2003-10-07 07:38:37 +00:00
|
|
|
sanei_thread_begin( void (*start)(void *arg), void* args )
|
2003-10-05 16:00:30 +00:00
|
|
|
{
|
2003-10-07 07:38:37 +00:00
|
|
|
int pid;
|
2003-10-07 11:28:03 +00:00
|
|
|
#ifdef USE_PTHREAD
|
2003-10-05 16:00:30 +00:00
|
|
|
pthread_t thread;
|
|
|
|
|
2003-10-07 07:38:37 +00:00
|
|
|
pid = pthread_create( &thread, NULL, start, args );
|
2003-10-05 16:00:30 +00:00
|
|
|
|
2003-10-07 07:38:37 +00:00
|
|
|
if ( pid != 0 ) {
|
|
|
|
DBG( 1, "pthread_create() failed with %d\n", pid );
|
2003-10-05 16:00:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG( 2, "pthread_create() created thread %d\n", (int)thread );
|
|
|
|
return (int)thread;
|
2003-10-07 07:38:37 +00:00
|
|
|
#else
|
|
|
|
pid = fork();
|
|
|
|
|
|
|
|
if( pid < 0 ) {
|
|
|
|
DBG( 1, "fork() failed\n" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pid == 0 ) {
|
|
|
|
|
|
|
|
/* run in child context... */
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
start( args );
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sanei_thread_kill( int pid, int sig)
|
|
|
|
{
|
2003-10-07 07:38:37 +00:00
|
|
|
DBG(2, "sanei_thread_kill() will kill %d\n", (int)pid);
|
2003-10-07 11:28:03 +00:00
|
|
|
#ifdef USE_PTHREAD
|
2003-10-05 16:00:30 +00:00
|
|
|
return pthread_kill((pthread_t)pid, sig );
|
2003-10-07 07:38:37 +00:00
|
|
|
#else
|
|
|
|
return kill( pid, sig );
|
|
|
|
#endif
|
2003-10-05 16:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sanei_thread_waitpid( int pid, int *stat_loc, int options )
|
|
|
|
{
|
|
|
|
if (stat_loc)
|
|
|
|
*stat_loc = 0;
|
|
|
|
|
2003-10-07 11:28:03 +00:00
|
|
|
#ifdef USE_PTHREAD
|
2003-10-07 07:38:37 +00:00
|
|
|
|
|
|
|
_VAR_NOT_USED( options );
|
|
|
|
|
2003-10-05 16:00:30 +00:00
|
|
|
if( 0 == pthread_join((pthread_t)pid, (void*)stat_loc ))
|
|
|
|
pthread_detach((pthread_t)pid );
|
|
|
|
return pid;
|
2003-10-07 07:38:37 +00:00
|
|
|
#else
|
|
|
|
return waitpid( pid, stat_loc, options );
|
|
|
|
#endif
|
2003-10-05 16:00:30 +00:00
|
|
|
}
|
|
|
|
|
2003-10-07 07:38:37 +00:00
|
|
|
SANE_Bool
|
|
|
|
sanei_thread_is_forked( void )
|
|
|
|
{
|
2003-10-07 11:28:03 +00:00
|
|
|
#ifdef USE_PTHREAD
|
2003-10-07 07:38:37 +00:00
|
|
|
return SANE_FALSE;
|
|
|
|
#else
|
|
|
|
return SANE_TRUE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_OS2_H */
|
|
|
|
|