8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 18:43:03 +01:00
firebird-mirror/src/common/thd.cpp

265 lines
5.4 KiB
C++
Raw Normal View History

2001-05-23 15:26:42 +02:00
/*
* PROGRAM: JRD Access Method
* 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
*/
#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"
2008-01-23 16:54:36 +01:00
#include "../common/thd.h"
2001-05-23 15:26:42 +02:00
#include "../jrd/gdsassert.h"
2008-01-23 16:54:36 +01:00
#include "../common/classes/semaphore.h"
#include "../common/classes/alloc.h"
#include "../common/classes/init.h"
2001-05-23 15:26:42 +02:00
#ifdef WIN_NT
#include <process.h>
#include <windows.h>
#endif
#ifdef HAVE_UNISTD_H
2001-05-23 15:26:42 +02:00
#include <unistd.h>
#endif
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
2001-05-23 15:26:42 +02:00
2008-01-23 16:54:36 +01:00
#ifdef USE_POSIX_THREADS
#include <pthread.h>
#endif
2001-05-23 15:26:42 +02:00
2008-01-16 11:25:04 +01:00
FB_THREAD_ID getThreadId()
2001-05-23 15:26:42 +02:00
{
/**************************************
*
2008-01-16 11:25:04 +01:00
* T H D _ g e t _ t h r e a d _ i d
2001-05-23 15:26:42 +02:00
*
**************************************
*
* Functional description
2008-01-16 11:25:04 +01:00
* Get platform's notion of a thread ID.
2001-05-23 15:26:42 +02:00
*
**************************************/
2008-01-16 11:25:04 +01:00
FB_THREAD_ID id = 1;
#ifdef WIN_NT
id = GetCurrentThreadId();
#endif
2008-01-23 16:54:36 +01:00
#ifdef USE_POSIX_THREADS
2008-01-16 11:25:04 +01:00
id = (FB_THREAD_ID) pthread_self();
2009-04-10 17:36:58 +02:00
#endif
2008-01-16 11:25:04 +01:00
return id;
2001-05-23 15:26:42 +02:00
}
void THD_sleep(ULONG milliseconds)
{
/**************************************
*
* T H D _ s l e e p
*
**************************************
*
* Functional description
* Thread sleeps for requested number
* of milliseconds.
*
**************************************/
#if defined(WIN_NT)
2001-05-23 15:26:42 +02:00
SleepEx(milliseconds, FALSE);
#elif defined(HAVE_NANOSLEEP)
timespec timer, rem;
timer.tv_sec = milliseconds / 1000;
timer.tv_nsec = (milliseconds % 1000) * 1000000;
while (nanosleep(&timer, &rem) != 0)
{
if (errno != EINTR)
{
2009-02-19 16:49:31 +01:00
Firebird::system_call_failed::raise("nanosleep");
}
2009-02-19 16:49:31 +01:00
timer = rem;
}
2001-05-23 15:26:42 +02:00
#else
2008-01-23 16:54:36 +01:00
Firebird::Semaphore timer;
timer.tryEnter(0, milliseconds);
#endif
2001-05-23 15:26:42 +02:00
}
2009-01-14 12:10:48 +01:00
void THD_yield()
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* T H D _ y i e l d
*
**************************************
*
* Functional description
* Thread relinquishes the processor.
*
**************************************/
#ifdef USE_POSIX_THREADS
2008-12-05 02:20:14 +01: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();
2006-05-02 02:25:11 +02:00
#endif // _POSIX_PRIORITY_SCHEDULING
2001-05-23 15:26:42 +02:00
#endif
#ifdef WIN_NT
SleepEx(0, FALSE);
2001-05-23 15:26:42 +02:00
#endif
}
// Cleanup on thread completion
#ifdef USE_POSIX_THREADS
namespace {
pthread_key_t key;
pthread_once_t keyOnce = PTHREAD_ONCE_INIT;
void makeKey()
{
int err = pthread_key_create(&key, ThreadCleanup::destructor);
if (err)
{
Firebird::system_call_failed("pthread_key_create", err);
}
}
void initThreadCleanup()
{
int err = pthread_once(&keyOnce, makeKey);
if (err)
{
Firebird::system_call_failed("pthread_once", err);
}
err = pthread_setspecific(key, &key);
if (err)
{
Firebird::system_call_failed("pthread_setspecific", err);
}
}
ThreadCleanup* chain = NULL;
Firebird::GlobalPtr<Firebird::Mutex> cleanupMutex;
} // anonymous namespace
ThreadCleanup** ThreadCleanup::findCleanup(FPTR_VOID_PTR cleanup, void* arg)
{
for (ThreadCleanup** ptr = &chain; *ptr; ptr = &((*ptr)->next))
{
if ((*ptr)->function == cleanup && (*ptr)->argument == arg)
{
return ptr;
}
}
return NULL;
}
void ThreadCleanup::destructor(void*)
{
Firebird::MutexLockGuard guard(cleanupMutex);
for (ThreadCleanup* ptr = chain; ptr; ptr = ptr->next)
{
ptr->function(ptr->argument);
}
}
void ThreadCleanup::add(FPTR_VOID_PTR cleanup, void* arg)
{
Firebird::MutexLockGuard guard(cleanupMutex);
initThreadCleanup();
if (findCleanup(cleanup, arg))
{
return;
}
chain = FB_NEW(*getDefaultMemoryPool()) ThreadCleanup(cleanup, arg, chain);
}
void ThreadCleanup::remove(FPTR_VOID_PTR cleanup, void* arg)
{
ThreadCleanup** ptr = findCleanup(cleanup, arg);
if (!ptr)
{
return;
}
ThreadCleanup* toDelete = *ptr;
*ptr = toDelete->next;
delete toDelete;
}
2009-09-03 03:28:54 +02:00
#else // USE_POSIX_THREADS
ThreadCleanup** ThreadCleanup::findCleanup(FPTR_VOID_PTR, void*)
{
return NULL;
}
void ThreadCleanup::destructor(void*)
{
}
void ThreadCleanup::add(FPTR_VOID_PTR, void*)
{
}
void ThreadCleanup::remove(FPTR_VOID_PTR, void*)
{
}
2009-09-03 03:28:54 +02:00
#endif // USE_POSIX_THREADS