pthread_xt.cc 18 KB
Newer Older
Paul McCullagh's avatar
Paul McCullagh committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/* Copyright (c) 2005 PrimeBase Technologies GmbH
 *
 * PrimeBase XT
 *
 * 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
 *
 * 2006-03-22	Paul McCullagh
 *
 * H&G2JCtL
 *
 * This file contains windows specific code
 */

#include "xt_config.h"

#ifdef XT_WIN
#include <my_pthread.h>
#else
#include <sys/resource.h>
#endif
#include <errno.h>
#include <limits.h>
#include <string.h>

#include "pthread_xt.h"
#include "thread_xt.h"

#ifdef XT_WIN

Paul McCullagh's avatar
Paul McCullagh committed
42
xtPublic void xt_p_init_threading(void)
Paul McCullagh's avatar
Paul McCullagh committed
43 44 45
{
}

Paul McCullagh's avatar
Paul McCullagh committed
46
xtPublic int xt_p_set_normal_priority(pthread_t thr)
Paul McCullagh's avatar
Paul McCullagh committed
47 48 49 50 51 52
{
	if (!SetThreadPriority (thr, THREAD_PRIORITY_NORMAL))
		return GetLastError();
	return 0;
}

Paul McCullagh's avatar
Paul McCullagh committed
53
xtPublic int xt_p_set_low_priority(pthread_t thr)
Paul McCullagh's avatar
Paul McCullagh committed
54 55 56 57 58 59
{
	if (!SetThreadPriority (thr, THREAD_PRIORITY_LOWEST))
		return GetLastError();
	return 0;
}

Paul McCullagh's avatar
Paul McCullagh committed
60
xtPublic int xt_p_set_high_priority(pthread_t thr)
Paul McCullagh's avatar
Paul McCullagh committed
61 62 63 64 65 66 67 68 69
{
	if (!SetThreadPriority (thr, THREAD_PRIORITY_HIGHEST))
		return GetLastError();
	return 0;
}

#define XT_RWLOCK_MAGIC 0x78AC390E

#ifdef XT_THREAD_LOCK_INFO
Paul McCullagh's avatar
Paul McCullagh committed
70
xtPublic int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr, const char *n)
Paul McCullagh's avatar
Paul McCullagh committed
71
#else
Paul McCullagh's avatar
Paul McCullagh committed
72
xtPublic int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr)
Paul McCullagh's avatar
Paul McCullagh committed
73 74 75 76 77 78 79 80 81 82
#endif
{
	InitializeCriticalSection(&mutex->mt_cs);
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_init(&mutex->mt_lock_info, mutex);
	mutex->mt_name = n;
#endif
	return 0;
}

Paul McCullagh's avatar
Paul McCullagh committed
83
xtPublic int xt_p_mutex_destroy(xt_mutex_type *mutex)
Paul McCullagh's avatar
Paul McCullagh committed
84 85 86 87 88 89 90 91
{
	DeleteCriticalSection(&mutex->mt_cs);
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_free(&mutex->mt_lock_info);
#endif
	return 0;
}

Paul McCullagh's avatar
Paul McCullagh committed
92
xtPublic int xt_p_mutex_lock(xt_mutex_type *mx)
Paul McCullagh's avatar
Paul McCullagh committed
93 94 95 96 97 98 99 100
{
	EnterCriticalSection(&mx->mt_cs);
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_add_owner(&mx->mt_lock_info);
#endif
	return 0;
}

Paul McCullagh's avatar
Paul McCullagh committed
101
xtPublic int xt_p_mutex_unlock(xt_mutex_type *mx)
Paul McCullagh's avatar
Paul McCullagh committed
102 103 104 105 106 107 108 109
{
	LeaveCriticalSection(&mx->mt_cs);
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_release_owner(&mx->mt_lock_info);
#endif
	return 0;
}

Paul McCullagh's avatar
Paul McCullagh committed
110
xtPublic int xt_p_mutex_trylock(xt_mutex_type *mutex)
Paul McCullagh's avatar
Paul McCullagh committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
{
#if(_WIN32_WINNT >= 0x0400)
	/* NOTE: MySQL bug! was using?!
	 * pthread_mutex_trylock(A) (WaitForSingleObject((A), 0) == WAIT_TIMEOUT)
	 */
	if (TryEnterCriticalSection(&mutex->mt_cs)) {
#ifdef XT_THREAD_LOCK_INFO
		xt_thread_lock_info_add_owner(&mutex->mt_lock_info);
#endif
		return 0;
	}
	return WAIT_TIMEOUT;
#else
	EnterCriticalSection(&mutex->mt_cs);
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_add_owner(&mutex->mt_lock_info);
#endif
	return 0;
#endif
}

#ifdef XT_THREAD_LOCK_INFO
Paul McCullagh's avatar
Paul McCullagh committed
133
xtPublic int xt_p_rwlock_init(xt_rwlock_type *rwl, const pthread_condattr_t *attr, const char *n)
Paul McCullagh's avatar
Paul McCullagh committed
134
#else
Paul McCullagh's avatar
Paul McCullagh committed
135
xtPublic int xt_p_rwlock_init(xt_rwlock_type *rwl, const pthread_condattr_t *attr)
Paul McCullagh's avatar
Paul McCullagh committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#endif
{
	int result;

	if (rwl == NULL)
		return ERROR_BAD_ARGUMENTS;

	rwl->rw_sh_count = 0;
	rwl->rw_ex_count = 0;
	rwl->rw_sh_complete_count = 0;

	result = xt_p_mutex_init_with_autoname(&rwl->rw_ex_lock, NULL);
	if (result != 0)
		goto failed;

	result = xt_p_mutex_init_with_autoname(&rwl->rw_sh_lock, NULL);
	if (result != 0)
		goto failed_2;

	result = pthread_cond_init(&rwl->rw_sh_cond, NULL);
	if (result != 0)
		goto failed_3;

	rwl->rw_magic = XT_RWLOCK_MAGIC;
#ifdef XT_THREAD_LOCK_INFO
	rwl->rw_name = n;
	xt_thread_lock_info_init(&rwl->rw_lock_info, rwl);
#endif
	return 0;

	failed_3:
	(void) xt_p_mutex_destroy(&rwl->rw_sh_lock);

	failed_2:
	(void) xt_p_mutex_destroy(&rwl->rw_ex_lock);

	failed:
	return result;
}

Paul McCullagh's avatar
Paul McCullagh committed
176
xtPublic int xt_p_rwlock_destroy(xt_rwlock_type *rwl)
Paul McCullagh's avatar
Paul McCullagh committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
{
	int result = 0, result1 = 0, result2 = 0;

	if (rwl == NULL)
		return ERROR_BAD_ARGUMENTS;

	if (rwl->rw_magic != XT_RWLOCK_MAGIC)
		return ERROR_BAD_ARGUMENTS;

	if ((result = xt_p_mutex_lock(&rwl->rw_ex_lock)) != 0)
		return result;

	if ((result = xt_p_mutex_lock(&rwl->rw_sh_lock)) != 0) {
		(void) xt_p_mutex_unlock(&rwl->rw_ex_lock);
		return result;
	}

	/*
	 * Check whether any threads own/wait for the lock (wait for ex.access);
	 * report "BUSY" if so.
	 */
	if (rwl->rw_ex_count > 0 || rwl->rw_sh_count > rwl->rw_sh_complete_count) {
		result = xt_p_mutex_unlock(&rwl->rw_sh_lock);
		result1 = xt_p_mutex_unlock(&rwl->rw_ex_lock);
		result2 = ERROR_BUSY;
	}
	else {
		rwl->rw_magic = 0;

		if ((result = xt_p_mutex_unlock(&rwl->rw_sh_lock)) != 0)
		{
			xt_p_mutex_unlock(&rwl->rw_ex_lock);
			return result;
		}

		if ((result = xt_p_mutex_unlock(&rwl->rw_ex_lock)) != 0)
			return result;

		result = pthread_cond_destroy(&rwl->rw_sh_cond);
		result1 = xt_p_mutex_destroy(&rwl->rw_sh_lock);
		result2 = xt_p_mutex_destroy(&rwl->rw_ex_lock);
	}

#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_free(&rwl->rw_lock_info);
#endif

	return (result != 0) ? result : ((result1 != 0) ? result1 : result2);
}


Paul McCullagh's avatar
Paul McCullagh committed
228
xtPublic int xt_p_rwlock_rdlock(xt_rwlock_type *rwl)
Paul McCullagh's avatar
Paul McCullagh committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
{
	int result;

	if (rwl == NULL)
		return ERROR_BAD_ARGUMENTS;

	if (rwl->rw_magic != XT_RWLOCK_MAGIC)
		return ERROR_BAD_ARGUMENTS;

	if ((result = xt_p_mutex_lock(&rwl->rw_ex_lock)) != 0)
		return result;

	if (++rwl->rw_sh_count == INT_MAX) {
		if ((result = xt_p_mutex_lock(&rwl->rw_sh_lock)) != 0)
		{
			(void) xt_p_mutex_unlock(&rwl->rw_ex_lock);
			return result;
		}

		rwl->rw_sh_count -= rwl->rw_sh_complete_count;
		rwl->rw_sh_complete_count = 0;

		if ((result = xt_p_mutex_unlock(&rwl->rw_sh_lock)) != 0)
		{
			(void) xt_p_mutex_unlock(&rwl->rw_ex_lock);
			return result;
		}
	}

#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_add_owner(&rwl->rw_lock_info);
#endif

	return (xt_p_mutex_unlock (&(rwl->rw_ex_lock)));
}

Paul McCullagh's avatar
Paul McCullagh committed
265
xtPublic int xt_p_rwlock_wrlock(xt_rwlock_type *rwl)
Paul McCullagh's avatar
Paul McCullagh committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
{
	int result;

	if (rwl == NULL)
		return ERROR_BAD_ARGUMENTS;

	if (rwl->rw_magic != XT_RWLOCK_MAGIC)
		return ERROR_BAD_ARGUMENTS;

	if ((result = xt_p_mutex_lock (&rwl->rw_ex_lock)) != 0)
		return result;

	if ((result = xt_p_mutex_lock (&rwl->rw_sh_lock)) != 0) {
		(void) xt_p_mutex_unlock (&rwl->rw_ex_lock);
		return result;
	}

	if (rwl->rw_ex_count == 0) {
		if (rwl->rw_sh_complete_count > 0) {
			rwl->rw_sh_count -= rwl->rw_sh_complete_count;
			rwl->rw_sh_complete_count = 0;
		}

		if (rwl->rw_sh_count > 0) {
			rwl->rw_sh_complete_count = -rwl->rw_sh_count;

			do {
				result = pthread_cond_wait (&rwl->rw_sh_cond, &rwl->rw_sh_lock.mt_cs);
			}
			while (result == 0 && rwl->rw_sh_complete_count < 0);

			if (result == 0)
				rwl->rw_sh_count = 0;
		}
	}

	if (result == 0)
		rwl->rw_ex_count++;

#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_add_owner(&rwl->rw_lock_info);
#endif

	return result;
}

Paul McCullagh's avatar
Paul McCullagh committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
xtPublic xtBool xt_p_rwlock_try_wrlock(xt_rwlock_type *rwl)
{
	int result;

	if (rwl == NULL)
		return FALSE;

	if (rwl->rw_magic != XT_RWLOCK_MAGIC)
		return FALSE;

	if ((result = xt_p_mutex_trylock(&rwl->rw_ex_lock)) != 0)
		return FALSE;

	if ((result = xt_p_mutex_lock(&rwl->rw_sh_lock)) != 0) {
		(void) xt_p_mutex_unlock(&rwl->rw_ex_lock);
		return FALSE;
	}

	if (rwl->rw_ex_count == 0) {
		if (rwl->rw_sh_complete_count > 0) {
			rwl->rw_sh_count -= rwl->rw_sh_complete_count;
			rwl->rw_sh_complete_count = 0;
		}

		if (rwl->rw_sh_count > 0) {
			rwl->rw_sh_complete_count = -rwl->rw_sh_count;

			do {
				result = pthread_cond_wait (&rwl->rw_sh_cond, &rwl->rw_sh_lock.mt_cs);
			}
			while (result == 0 && rwl->rw_sh_complete_count < 0);

			if (result == 0)
				rwl->rw_sh_count = 0;
		}
	}

	if (result == 0)
		rwl->rw_ex_count++;

#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_add_owner(&rwl->rw_lock_info);
#endif

	return TRUE;
}

xtPublic int xt_p_rwlock_unlock(xt_rwlock_type *rwl)
Paul McCullagh's avatar
Paul McCullagh committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
{
	int result, result1;

	if (rwl == NULL)
		return (ERROR_BAD_ARGUMENTS);

	if (rwl->rw_magic != XT_RWLOCK_MAGIC)
		return ERROR_BAD_ARGUMENTS;

	if (rwl->rw_ex_count == 0) {
		if ((result = xt_p_mutex_lock(&rwl->rw_sh_lock)) != 0)
			return result;

		if (++rwl->rw_sh_complete_count == 0)
			result = pthread_cond_signal(&rwl->rw_sh_cond);

		result1 = xt_p_mutex_unlock(&rwl->rw_sh_lock);
	}
	else {
		rwl->rw_ex_count--;

		result = xt_p_mutex_unlock(&rwl->rw_sh_lock);
		result1 = xt_p_mutex_unlock(&rwl->rw_ex_lock);
	}

#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_release_owner(&rwl->rw_lock_info);
#endif

	return ((result != 0) ? result : result1);
}

Paul McCullagh's avatar
Paul McCullagh committed
392
xtPublic int xt_p_cond_wait(xt_cond_type *cond, xt_mutex_type *mutex)
Paul McCullagh's avatar
Paul McCullagh committed
393 394 395 396
{
	return xt_p_cond_timedwait(cond, mutex, NULL);
}

Paul McCullagh's avatar
Paul McCullagh committed
397
xtPublic int xt_p_cond_timedwait(xt_cond_type *cond, xt_mutex_type *mt, struct timespec *abstime)
Paul McCullagh's avatar
Paul McCullagh committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
{
	pthread_mutex_t	*mutex = &mt->mt_cs;
	int				result;
	long			timeout; 
	union ft64		now;

	if (abstime != NULL) {
		GetSystemTimeAsFileTime(&now.ft);

		timeout = (long)((abstime->tv.i64 - now.i64) / 10000);
		if (timeout < 0)
			timeout = 0L;
		if (timeout > abstime->max_timeout_msec)
			timeout = abstime->max_timeout_msec;
	}
	else
		timeout= INFINITE;

	WaitForSingleObject(cond->broadcast_block_event, INFINITE);

	EnterCriticalSection(&cond->lock_waiting);
	cond->waiting++;
	LeaveCriticalSection(&cond->lock_waiting);

	LeaveCriticalSection(mutex);

	result= WaitForMultipleObjects(2, cond->events, FALSE, timeout);

	EnterCriticalSection(&cond->lock_waiting);
	cond->waiting--;
	
	if (cond->waiting == 0) {
		/* The last waiter must reset the broadcast
		 * state (whther there was a broadcast or not)!
		 */
		ResetEvent(cond->events[xt_cond_type::BROADCAST]);
		SetEvent(cond->broadcast_block_event);
	}
	LeaveCriticalSection(&cond->lock_waiting);
	
	EnterCriticalSection(mutex);

	return result == WAIT_TIMEOUT ? ETIMEDOUT : 0;
}

Paul McCullagh's avatar
Paul McCullagh committed
443
xtPublic int xt_p_join(pthread_t thread, void **value)
Paul McCullagh's avatar
Paul McCullagh committed
444
{
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
	DWORD exitcode;

	while(1) {
		switch (WaitForSingleObject(thread, 10000)) {
			case WAIT_OBJECT_0:
				return 0;
			case WAIT_TIMEOUT:
				/* Don't do this! According to the Win docs:
				 * _endthread automatically closes the thread handle
				 * (whereas _endthreadex does not). Therefore, when using
				 * _beginthread and _endthread, do not explicitly close the
				 * thread handle by calling the Win32 CloseHandle API.
				CloseHandle(thread);
				 */
				/* This is done so that if the thread was not [yet] in the running
				 * state when this function was called we won't deadlock here.
				 */
				if (GetExitCodeThread(thread, &exitcode) && (exitcode == STILL_ACTIVE))
					break;
				return 0;
			case WAIT_FAILED:
				return GetLastError();
		}
Paul McCullagh's avatar
Paul McCullagh committed
468
	}
469

Paul McCullagh's avatar
Paul McCullagh committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
	return 0;
}

#else // XT_WIN

#ifdef __darwin__
#define POLICY			SCHED_RR
#else
#define POLICY			pth_policy
#endif

static int pth_policy;
static int pth_normal_priority;
static int pth_min_priority;
static int pth_max_priority;

/* Return zero if the priority was set OK,
 * else errno.
 */
static int pth_set_priority(pthread_t thread, int priority)
{
	struct sched_param	sp;

	memset(&sp, 0, sizeof(struct sched_param));
	sp.sched_priority = priority;
	return pthread_setschedparam(thread, POLICY, &sp);
}

static void pth_get_priority_limits(void)
{
	XTThreadPtr			self = NULL;
	struct sched_param	sp;
	int					err;
	int					start;

	/* Save original priority: */
	err = pthread_getschedparam(pthread_self(), &pth_policy, &sp);
	if (err) {
		xt_throw_errno(XT_CONTEXT, err);
		return;
	}
	pth_normal_priority = sp.sched_priority;

	start = sp.sched_priority;

#ifdef XT_FREEBSD 
	pth_min_priority = sched_get_priority_min(sched_getscheduler(0));
	pth_max_priority = sched_get_priority_max(sched_getscheduler(0));
#else
	/* Search for the minimum priority: */
	pth_min_priority = start;
	for (;;) {
		/* 2007-03-01: Corrected, pth_set_priority returns the error code
		 * (thanks to Hakan for pointing out this bug!)
		 */
		if (pth_set_priority(pthread_self(), pth_min_priority-1) != 0)
			break;
		pth_min_priority--;
	}

	/* Search for the maximum priority: */
	pth_max_priority = start;
	for (;;) {
		if (pth_set_priority(pthread_self(), pth_max_priority+1) != 0)
			break;
		pth_max_priority++;
	}

	/* Restore original priority: */
	pthread_setschedparam(pthread_self(), pth_policy, &sp);
#endif
}

xtPublic void xt_p_init_threading(void)
{
	pth_get_priority_limits();
}

xtPublic int xt_p_set_low_priority(pthread_t thr)
{
	if (pth_min_priority == pth_max_priority) {
		/* Under Linux the priority of normal (non-runtime)
		 * threads are set using the standard methods
		 * for setting process priority.
		 */

		/* We could set who == 0 because it should have the same affect
		 * as using the PID.
		 */

		/* -20 = highest, 20 = lowest */
		if (setpriority(PRIO_PROCESS, getpid(), 20) == -1)
			return errno;
		return 0;
	}
	return pth_set_priority(thr, pth_min_priority);
}

xtPublic int xt_p_set_normal_priority(pthread_t thr)
{
	if (pth_min_priority == pth_max_priority) {
		if (setpriority(PRIO_PROCESS, getpid(), 0) == -1)
			return errno;
		return 0;
	}
	return pth_set_priority(thr, pth_normal_priority);
}

xtPublic int xt_p_set_high_priority(pthread_t thr)
{
	if (pth_min_priority == pth_max_priority) {
		if (setpriority(PRIO_PROCESS, getpid(), -20) == -1)
			return errno;
		return 0;
	}
	return pth_set_priority(thr, pth_max_priority);
}

#ifdef DEBUG_LOCKING

xtPublic int xt_p_mutex_lock(xt_mutex_type *mutex, u_int line, const char *file)
{
	XTThreadPtr self = xt_get_self();
	int			r;

	ASSERT_NS(mutex->mu_init == 12345);
	r = pthread_mutex_lock(&mutex->mu_plock);
	if (r == 0) {
		if (mutex->mu_trace)
			printf("==LOCK mutex %d %s:%d\n", (int) mutex->mu_trace, file, (int) line);
		ASSERT_NS(!mutex->mu_locker);
		mutex->mu_locker = self;
		mutex->mu_line = line;
		mutex->mu_file = file;
	}
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_add_owner(&mutex->mu_lock_info);
#endif
	return r;
}

xtPublic int xt_p_mutex_unlock(xt_mutex_type *mutex)
{
	XTThreadPtr self = xt_get_self();

	ASSERT_NS(mutex->mu_init == 12345);
	ASSERT_NS(mutex->mu_locker == self);
	mutex->mu_locker = NULL;
	if (mutex->mu_trace)
		printf("UNLOCK mutex %d\n", (int) mutex->mu_trace);
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_release_owner(&mutex->mu_lock_info);
#endif
	return pthread_mutex_unlock(&mutex->mu_plock);
}

xtPublic int xt_p_mutex_destroy(xt_mutex_type *mutex)
{
628 629
	//ASSERT_NS(mutex->mu_init == 12345);
	mutex->mu_init = 11111;
Paul McCullagh's avatar
Paul McCullagh committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_free(&mutex->mu_lock_info);
#endif
	return pthread_mutex_destroy(&mutex->mu_plock);
}

xtPublic int xt_p_mutex_trylock(xt_mutex_type *mutex)
{
	XTThreadPtr self = xt_get_self();
	int			r;

	ASSERT_NS(mutex->mu_init == 12345);
	r = pthread_mutex_trylock(&mutex->mu_plock);
	if (r == 0) {
		ASSERT_NS(!mutex->mu_locker);
		mutex->mu_locker = self;
#ifdef XT_THREAD_LOCK_INFO
		xt_thread_lock_info_add_owner(&mutex->mu_lock_info);
#endif
	}
	return r;
}

#ifdef XT_THREAD_LOCK_INFO
xtPublic int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr, const char *n)
#else
xtPublic int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr)
#endif
{
	mutex->mu_init = 12345;
	mutex->mu_trace = FALSE;
	mutex->mu_locker = NULL;
#ifdef XT_THREAD_LOCK_INFO
	mutex->mu_name = n;
	xt_thread_lock_info_init(&mutex->mu_lock_info, mutex);
#endif
	return pthread_mutex_init(&mutex->mu_plock, attr);
}

xtPublic int xt_p_cond_wait(xt_cond_type *cond, xt_mutex_type *mutex)
{
	XTThreadPtr self = xt_get_self();
	int			r;

	ASSERT_NS(mutex->mu_init == 12345);
	ASSERT_NS(mutex->mu_locker == self);
	mutex->mu_locker = NULL;
	r = pthread_cond_wait(cond, &mutex->mu_plock);
	ASSERT_NS(!mutex->mu_locker);
	mutex->mu_locker = self;
	return r;
}

xtPublic int xt_p_cond_timedwait(xt_cond_type *cond, xt_mutex_type *mutex, const struct timespec *abstime)
{
	XTThreadPtr self = xt_get_self();
	int			r;

	ASSERT_NS(mutex->mu_init == 12345);
	ASSERT_NS(mutex->mu_locker == self);
	mutex->mu_locker = NULL;
	r = pthread_cond_timedwait(cond, &mutex->mu_plock, abstime);
	ASSERT_NS(!mutex->mu_locker);
	mutex->mu_locker = self;
	return r;
}

xtPublic int xt_p_rwlock_rdlock(xt_rwlock_type *rwlock)
{
	int r;

	ASSERT_NS(rwlock->rw_init == 67890);
	r = pthread_rwlock_rdlock(&rwlock->rw_plock);
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_add_owner(&rwlock->rw_lock_info);
#endif
	return r;
}

xtPublic int xt_p_rwlock_wrlock(xt_rwlock_type *rwlock)
{
	XTThreadPtr self = xt_get_self();
	int			r;

	ASSERT_NS(rwlock->rw_init == 67890);
	r = pthread_rwlock_wrlock(&rwlock->rw_plock);
	if (r == 0) {
		ASSERT_NS(!rwlock->rw_locker);
		rwlock->rw_locker = self;
	}
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_add_owner(&rwlock->rw_lock_info);
#endif
	return r;
}

Paul McCullagh's avatar
Paul McCullagh committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
xtPublic xtBool xt_p_rwlock_try_wrlock(xt_rwlock_type *rwlock)
{
	XTThreadPtr self = xt_get_self();
	int			r;

	ASSERT_NS(rwlock->rw_init == 67890);
	r = pthread_rwlock_trywrlock(&rwlock->rw_plock);
	if (r == 0) {
		ASSERT_NS(!rwlock->rw_locker);
		rwlock->rw_locker = self;
#ifdef XT_THREAD_LOCK_INFO
		xt_thread_lock_info_add_owner(&rwlock->rw_lock_info);
#endif
	}
	return r == 0;
}

Paul McCullagh's avatar
Paul McCullagh committed
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
xtPublic int xt_p_rwlock_unlock(xt_rwlock_type *rwlock)
{
	XTThreadPtr self = xt_get_self();

	ASSERT_NS(rwlock->rw_init == 67890);
	if (rwlock->rw_locker) {
		ASSERT_NS(rwlock->rw_locker == self);
		rwlock->rw_locker = NULL;
	}
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_release_owner(&rwlock->rw_lock_info);
#endif
	return pthread_rwlock_unlock(&rwlock->rw_plock);
}

xtPublic int xt_p_rwlock_destroy(xt_rwlock_type *rwlock)
{
	ASSERT_NS(rwlock->rw_init == 67890);
	rwlock->rw_init = 0;
#ifdef XT_THREAD_LOCK_INFO
	xt_thread_lock_info_free(&rwlock->rw_lock_info);
#endif
	return pthread_rwlock_destroy(&rwlock->rw_plock);
}

#ifdef XT_THREAD_LOCK_INFO
xtPublic int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_rwlockattr_t *attr, const char *n)
#else
xtPublic int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_rwlockattr_t *attr)
#endif
{
	rwlock->rw_init = 67890;
	rwlock->rw_readers = 0;
	rwlock->rw_locker = NULL;
#ifdef XT_THREAD_LOCK_INFO
	rwlock->rw_name = n;
	xt_thread_lock_info_init(&rwlock->rw_lock_info, rwlock);
#endif
	return pthread_rwlock_init(&rwlock->rw_plock, attr);
}

#endif // DEBUG_LOCKING

#endif // XT_WIN