File:  [parser3project] / parser3 / src / main / pa_threads.C
Revision 1.14: download - view: text, annotated - select for diffs - revision graph
Thu Oct 29 20:33:33 2026 UTC (3 weeks, 3 days ago) by moko
Branches: MAIN
CVS tags: HEAD
FreeBSD fix

/** @file
	Parser: simple Mutex realization class.

	Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
	Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
*/

#include "pa_threads.h"

volatile const char * IDENT_PA_THREADS_C="$Id: pa_threads.C,v 1.14 2026/10/29 20:33:33 moko Exp $" IDENT_PA_THREADS_H;

Mutex global_mutex;

#ifdef _MSC_VER

uint pa_get_thread_id() {
	return GetCurrentThreadId();
}

Mutex::Mutex() :
	handle(CreateMutex(NULL, FALSE, 0)) {
}

Mutex::~Mutex() {
	CloseHandle(handle);
}

void Mutex::acquire() {
    WaitForSingleObject(handle, INFINITE);
}

void Mutex::release() {
    ReleaseMutex(handle);
}

#else

#include <pthread.h>
#ifdef HAVE_GETTID
#include <sys/syscall.h>
#endif
#ifdef HAVE_THR_SELF
extern "C" { // old FreeBSD requires it
#include <sys/thr.h>
}
#endif

uint pa_get_thread_id() {
#if defined(HAVE_GETTID)
	return syscall(__NR_gettid);
#elif defined(HAVE_THR_SELF)
	long id;
	thr_self(&id);
	return id;
#else
	return getpid();
#endif
}

Mutex::Mutex() {
	pthread_mutex_init(&handle, NULL);
}

Mutex::~Mutex() {
	pthread_mutex_destroy(&handle);
}

void Mutex::acquire() {
	pthread_mutex_lock(&handle);
}

void Mutex::release() {
	pthread_mutex_unlock(&handle);
}

#endif

E-mail: