2001-05-23 15:26:42 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: JRD Access Method
|
2003-11-07 09:06:35 +01:00
|
|
|
* MODULE: thd.cpp
|
2001-05-23 15:26:42 +02:00
|
|
|
* DESCRIPTION: Thread support routines
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Interbase Public
|
|
|
|
* License Version 1.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy
|
|
|
|
* of the License at http://www.Inprise.com/IPL.html
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an
|
|
|
|
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Inprise Corporation
|
|
|
|
* and its predecessors. Portions created by Inprise Corporation are
|
|
|
|
* Copyright (C) Inprise Corporation.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
2002-10-29 03:45:09 +01:00
|
|
|
*
|
|
|
|
* 2002.10.28 Sean Leyne - Completed removal of obsolete "DGUX" port
|
|
|
|
*
|
2002-10-30 07:40:58 +01:00
|
|
|
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
|
|
|
|
*
|
2001-05-23 15:26:42 +02:00
|
|
|
*/
|
|
|
|
|
2001-07-29 19:42:23 +02:00
|
|
|
#include "firebird.h"
|
2004-04-29 00:43:34 +02:00
|
|
|
#include <stdio.h>
|
2001-05-23 15:26:42 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include "../jrd/common.h"
|
|
|
|
#include "../jrd/thd.h"
|
|
|
|
#include "../jrd/isc.h"
|
2004-06-08 15:41:08 +02:00
|
|
|
#include "../jrd/os/thd_priority.h"
|
2001-05-23 15:26:42 +02:00
|
|
|
#include "../jrd/gds_proto.h"
|
|
|
|
#include "../jrd/isc_s_proto.h"
|
|
|
|
#include "../jrd/gdsassert.h"
|
2004-07-14 23:56:19 +02:00
|
|
|
#include "../common/classes/fb_tls.h"
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef WIN_NT
|
|
|
|
#include <process.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2002-08-26 14:18:16 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2001-05-23 15:26:42 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-12-24 10:35:48 +01:00
|
|
|
#ifdef SOLARIS_MT
|
2002-11-01 14:21:17 +01:00
|
|
|
#include <thread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
#ifdef SUPERSERVER
|
|
|
|
int THD_rec_mutex_destroy(REC_MUTX_T * rec_mutex)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ r e c _ m u t e x _ d e s t r o y ( S U P E R S E R V E R )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
|
2004-06-08 15:41:08 +02:00
|
|
|
return 0;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int THD_rec_mutex_init(REC_MUTX_T * rec_mutex)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ r e c _ m u t e x _ i n i t ( S U P E R S E R V E R )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
|
|
|
|
rec_mutex->rec_mutx_id = 0;
|
|
|
|
rec_mutex->rec_mutx_count = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int THD_rec_mutex_lock(REC_MUTX_T * rec_mutex)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ r e c _ m u t e x _ l o c k ( S U P E R S E R V E R )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
*
|
|
|
|
**************************************/
|
2004-08-30 20:11:08 +02:00
|
|
|
if (rec_mutex->rec_mutx_id == ThreadData::getId())
|
2006-03-15 18:34:13 +01:00
|
|
|
{
|
2001-05-23 15:26:42 +02:00
|
|
|
rec_mutex->rec_mutx_count++;
|
2006-03-15 18:34:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
rec_mutex->rec_mutx_mtx->enter();
|
|
|
|
}
|
|
|
|
catch (const Firebird::system_call_failed& e)
|
|
|
|
{
|
|
|
|
return e.getErrorCode();
|
|
|
|
}
|
2004-08-30 20:11:08 +02:00
|
|
|
rec_mutex->rec_mutx_id = ThreadData::getId();
|
2001-05-23 15:26:42 +02:00
|
|
|
rec_mutex->rec_mutx_count = 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int THD_rec_mutex_unlock(REC_MUTX_T * rec_mutex)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ r e c _ m u t e x _ u n l o c k ( S U P E R S E R V E R )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
|
2004-08-30 20:11:08 +02:00
|
|
|
if (rec_mutex->rec_mutx_id != ThreadData::getId())
|
2002-11-14 09:33:08 +01:00
|
|
|
return FB_FAILURE;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
rec_mutex->rec_mutx_count--;
|
|
|
|
|
2006-03-15 18:34:13 +01:00
|
|
|
if (rec_mutex->rec_mutx_count == 0)
|
|
|
|
{
|
2001-05-23 15:26:42 +02:00
|
|
|
rec_mutex->rec_mutx_id = 0;
|
2006-03-15 18:34:13 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
rec_mutex->rec_mutx_mtx->leave();
|
|
|
|
}
|
|
|
|
catch (const Firebird::system_call_failed& e)
|
|
|
|
{
|
|
|
|
return e.getErrorCode();
|
|
|
|
}
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
2006-03-15 18:34:13 +01:00
|
|
|
return 0;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
#endif /* SUPERSERVER */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WIN_NT
|
|
|
|
#define THREAD_SUSPEND_DEFINED
|
|
|
|
int THD_resume(THD_T thread)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ r e s u m e ( W I N _ N T )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Resume execution of a thread that has been
|
|
|
|
* suspended.
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
|
|
|
|
if (ResumeThread(thread) == 0xFFFFFFFF)
|
|
|
|
return GetLastError();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int THD_suspend(THD_T thread)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ s u s p e n d ( W I N _ N T )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Suspend execution of a thread.
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
|
|
|
|
if (SuspendThread(thread) == 0xFFFFFFFF)
|
|
|
|
return GetLastError();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef THREAD_SUSPEND_DEFINED
|
|
|
|
int THD_resume(THD_T thread)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ r e s u m e ( G e n e r i c )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Resume execution of a thread that has been
|
|
|
|
* suspended.
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int THD_suspend(THD_T thread)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ s u s p e n d ( G e n e r i c )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Suspend execution of a thread.
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void THD_sleep(ULONG milliseconds)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ s l e e p
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Thread sleeps for requested number
|
|
|
|
* of milliseconds.
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
#ifdef WIN_NT
|
|
|
|
SleepEx(milliseconds, FALSE);
|
|
|
|
#else
|
2003-03-24 15:41:42 +01:00
|
|
|
|
2006-05-01 06:12:30 +02:00
|
|
|
#ifdef MULTI_THREAD
|
2004-01-03 11:59:52 +01:00
|
|
|
event_t timer;
|
|
|
|
event_t* timer_ptr = &timer;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
ISC_event_init(&timer, 0, 0);
|
2004-01-03 11:59:52 +01:00
|
|
|
SLONG count = ISC_event_clear(&timer);
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-09-01 09:58:04 +02:00
|
|
|
ISC_event_wait(1, &timer_ptr, &count, milliseconds * 1000, NULL, 0);
|
2001-05-23 15:26:42 +02:00
|
|
|
ISC_event_fini(&timer);
|
2006-05-01 06:12:30 +02:00
|
|
|
#else // MULTI_THREAD
|
2001-05-23 15:26:42 +02:00
|
|
|
int seconds;
|
|
|
|
|
|
|
|
/* Insure that process sleeps some amount of time. */
|
|
|
|
|
|
|
|
if (!(seconds = milliseconds / 1000))
|
|
|
|
++seconds;
|
|
|
|
|
|
|
|
/* Feedback unslept time due to premature wakeup from signals. */
|
|
|
|
|
|
|
|
while (seconds = sleep(seconds));
|
|
|
|
|
2006-05-01 06:12:30 +02:00
|
|
|
#endif // MULTI_THREAD
|
2003-03-24 15:41:42 +01:00
|
|
|
|
2006-05-01 06:12:30 +02:00
|
|
|
#endif // WIN_NT
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void THD_yield(void)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* T H D _ y i e l d
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Thread relinquishes the processor.
|
|
|
|
*
|
|
|
|
**************************************/
|
2006-05-01 06:12:30 +02:00
|
|
|
#ifdef MULTI_THREAD
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2004-05-22 04:11:56 +02:00
|
|
|
#ifdef USE_POSIX_THREADS
|
2001-05-23 15:26:42 +02:00
|
|
|
/* use sched_yield() instead of pthread_yield(). Because pthread_yield()
|
|
|
|
is not part of the (final) POSIX 1003.1c standard. Several drafts of
|
|
|
|
the standard contained pthread_yield(), but then the POSIX guys
|
|
|
|
discovered it was redundant with sched_yield() and dropped it.
|
|
|
|
So, just use sched_yield() instead. POSIX systems on which
|
|
|
|
sched_yield() is available define _POSIX_PRIORITY_SCHEDULING
|
2001-07-12 07:46:06 +02:00
|
|
|
in <unistd.h>. Darwin defined _POSIX_THREAD_PRIORITY_SCHEDULING
|
|
|
|
instead of _POSIX_PRIORITY_SCHEDULING.
|
2001-05-23 15:26:42 +02:00
|
|
|
*/
|
2001-07-12 07:46:06 +02:00
|
|
|
#if (defined _POSIX_PRIORITY_SCHEDULING || defined _POSIX_THREAD_PRIORITY_SCHEDULING)
|
2001-05-23 15:26:42 +02:00
|
|
|
sched_yield();
|
|
|
|
#else
|
|
|
|
pthread_yield();
|
|
|
|
#endif /* _POSIX_PRIORITY_SCHEDULING */
|
|
|
|
#endif
|
|
|
|
|
2004-06-15 11:02:14 +02:00
|
|
|
#ifdef SOLARIS_MT
|
2001-05-23 15:26:42 +02:00
|
|
|
thr_yield();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WIN_NT
|
2003-05-14 10:09:47 +02:00
|
|
|
SleepEx(0, FALSE);
|
2001-05-23 15:26:42 +02:00
|
|
|
#endif
|
2006-05-01 06:12:30 +02:00
|
|
|
#endif /* MULTI_THREAD */
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|