locking.c 15.8 KB
Newer Older
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
Copyright 1992, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/

/*
 * Author: Stephen Gildea, MIT X Consortium
 *
 * locking.c - multi-thread locking routines implemented in C Threads
 */

33 34 35
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
36 37 38 39 40 41 42 43
#include "Xlibint.h"
#undef _XLockMutex
#undef _XUnlockMutex
#undef _XCreateMutex
#undef _XFreeMutex

#ifdef XTHREADS

44 45 46 47
#ifdef __UNIXWARE__
#include <dlfcn.h>
#endif

48
#include "Xprivate.h"
49 50 51 52 53
#include "locking.h"
#ifdef XTHREADS_WARN
#include <stdio.h>		/* for warn/debug stuff */
#endif

54 55 56 57 58 59 60 61 62 63 64
/* Additional arguments for source code location lock call was made from */
#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
# define XTHREADS_FILE_LINE_ARGS \
    ,								\
    char* file,			/* source file, from macro */	\
    int line
#else
# define XTHREADS_FILE_LINE_ARGS /* None */
#endif


65 66 67 68 69 70 71 72
#define NUM_FREE_CVLS 4

/* in lcWrap.c */
extern LockInfoPtr _Xi18n_lock;

#ifdef WIN32
static DWORD _X_TlsIndex = (DWORD)-1;

73
void _Xthread_init(void)
74 75 76 77 78 79
{
    if (_X_TlsIndex == (DWORD)-1)
	_X_TlsIndex = TlsAlloc();
}

struct _xthread_waiter *
80
_Xthread_waiter(void)
81 82 83 84
{
    struct _xthread_waiter *me;

    if (!(me = TlsGetValue(_X_TlsIndex))) {
85
	me = xmalloc(sizeof(struct _xthread_waiter));
86 87 88 89 90 91 92 93
	me->sem = CreateSemaphore(NULL, 0, 1, NULL);
	me->next = NULL;
	TlsSetValue(_X_TlsIndex, me);
    }
    return me;
}
#endif /* WIN32 */

94
static xthread_t _Xthread_self(void)
95 96 97 98 99 100 101
{
    return xthread_self();
}

static LockInfoRec global_lock;
static LockInfoRec i18n_lock;

102
static void _XLockMutex(
103 104 105
    LockInfoPtr lip
    XTHREADS_FILE_LINE_ARGS
    )
106 107 108 109
{
    xmutex_lock(lip->lock);
}

110
static void _XUnlockMutex(
111 112 113
    LockInfoPtr lip
    XTHREADS_FILE_LINE_ARGS
    )
114 115 116 117
{
    xmutex_unlock(lip->lock);
}

118 119
static void _XCreateMutex(
    LockInfoPtr lip)
120 121 122 123 124 125 126 127
{
    lip->lock = xmutex_malloc();
    if (lip->lock) {
	xmutex_init(lip->lock);
	xmutex_set_name(lip->lock, "Xlib");
    }
}

128 129
static void _XFreeMutex(
    LockInfoPtr lip)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
    xmutex_clear(lip->lock);
    xmutex_free(lip->lock);
}

#ifdef XTHREADS_WARN
static char *locking_file;
static int locking_line;
static xthread_t locking_thread;
static Bool xlibint_unlock = False; /* XlibInt.c may Unlock and re-Lock */

/* history that is useful to examine in a debugger */
#define LOCK_HIST_SIZE 21

static struct {
    Bool lockp;			/* True for lock, False for unlock */
    xthread_t thread;
    char *file;
    int line;
} locking_history[LOCK_HIST_SIZE];

int lock_hist_loc = 0;		/* next slot to fill */

153 154 155 156
static void _XLockDisplayWarn(
    Display *dpy,
    char *file,			/* source file, from macro */
    int line)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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
{
    xthread_t self;
    xthread_t old_locker;

    self = xthread_self();
    old_locker = locking_thread;
    if (xthread_have_id(old_locker)) {
	if (xthread_equal(old_locker, self))
	    printf("Xlib ERROR: %s line %d thread %x: locking display already locked at %s line %d\n",
		   file, line, self, locking_file, locking_line);
#ifdef XTHREADS_DEBUG
	else
	    printf("%s line %d: thread %x waiting on lock held by %s line %d thread %x\n",
		   file, line, self,
		   locking_file, locking_line, old_locker);
#endif /* XTHREADS_DEBUG */
    }

    xmutex_lock(dpy->lock->mutex);

    if (strcmp(file, "XlibInt.c") == 0) {
	if (!xlibint_unlock)
	    printf("Xlib ERROR: XlibInt.c line %d thread %x locking display it did not unlock\n",
		   line, self);
	xlibint_unlock = False;
    }

#ifdef XTHREADS_DEBUG
    /* if (old_locker  &&  old_locker != self) */
    if (strcmp("XClearArea.c", file) && strcmp("XDrSegs.c", file)) /* ico */
	printf("%s line %d: thread %x got display lock\n", file, line, self);
#endif /* XTHREADS_DEBUG */

    locking_thread = self;
    if (strcmp(file, "XlibInt.c") != 0) {
	locking_file = file;
	locking_line = line;
    }
    locking_history[lock_hist_loc].file = file;
    locking_history[lock_hist_loc].line = line;
    locking_history[lock_hist_loc].thread = self;
    locking_history[lock_hist_loc].lockp = True;
    lock_hist_loc++;
    if (lock_hist_loc >= LOCK_HIST_SIZE)
	lock_hist_loc = 0;
}
#endif /* XTHREADS_WARN */

205
static void _XUnlockDisplay(
206 207 208
    Display *dpy
    XTHREADS_FILE_LINE_ARGS
    )
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
{
#ifdef XTHREADS_WARN
    xthread_t self = xthread_self();

#ifdef XTHREADS_DEBUG
    if (strcmp("XClearArea.c", file) && strcmp("XDrSegs.c", file)) /* ico */
	printf("%s line %d: thread %x unlocking display\n", file, line, self);
#endif /* XTHREADS_DEBUG */

    if (!xthread_have_id(locking_thread))
	printf("Xlib ERROR: %s line %d thread %x: unlocking display that is not locked\n",
	       file, line, self);
    else if (strcmp(file, "XlibInt.c") == 0)
	xlibint_unlock = True;
#ifdef XTHREADS_DEBUG
    else if (strcmp(file, locking_file) != 0)
	/* not always an error because locking_file is not per-thread */
	printf("%s line %d: unlocking display locked from %s line %d (probably okay)\n",
	       file, line, locking_file, locking_line);
#endif /* XTHREADS_DEBUG */
    xthread_clear_id(locking_thread);

    locking_history[lock_hist_loc].file = file;
    locking_history[lock_hist_loc].line = line;
    locking_history[lock_hist_loc].thread = self;
    locking_history[lock_hist_loc].lockp = False;
    lock_hist_loc++;
    if (lock_hist_loc >= LOCK_HIST_SIZE)
	lock_hist_loc = 0;
#endif /* XTHREADS_WARN */
    xmutex_unlock(dpy->lock->mutex);
}


243 244
static struct _XCVList *_XCreateCVL(
    Display *dpy)
245 246 247 248 249 250 251
{
    struct _XCVList *cvl;

    if ((cvl = dpy->lock->free_cvls) != NULL) {
	dpy->lock->free_cvls = cvl->next;
	dpy->lock->num_free_cvls--;
    } else {
252
	cvl = Xmalloc(sizeof(struct _XCVList));
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	if (!cvl)
	    return NULL;
	cvl->cv = xcondition_malloc();
	if (!cvl->cv) {
	    Xfree(cvl);
	    return NULL;
	}
	xcondition_init(cvl->cv);
	xcondition_set_name(cvl->cv, "Xlib read queue");
    }
    cvl->next = NULL;
    return cvl;
}

/* Put ourselves on the queue to read the connection.
   Allocates and returns a queue element. */

static struct _XCVList *
271 272 273
_XPushReader(
    Display *dpy,
    struct _XCVList ***tail)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
{
    struct _XCVList *cvl;

    cvl = _XCreateCVL(dpy);
#ifdef XTHREADS_DEBUG
    printf("_XPushReader called in thread %x, pushing %x\n",
	   xthread_self(), cvl);
#endif
    **tail = cvl;
    *tail = &cvl->next;
    return cvl;
}

/* signal the next thread waiting to read the connection */

289 290 291 292
static void _XPopReader(
    Display *dpy,
    struct _XCVList **list,
    struct _XCVList ***tail)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
{
    register struct _XCVList *front = *list;

#ifdef XTHREADS_DEBUG
    printf("_XPopReader called in thread %x, popping %x\n",
	   xthread_self(), front);
#endif

    if (dpy->flags & XlibDisplayProcConni)
	/* we never added ourself in the first place */
	return;

    if (front) {		/* check "front" for paranoia */
	*list = front->next;
	if (*tail == &front->next)	/* did we free the last elt? */
	    *tail = list;
	if (dpy->lock->num_free_cvls < NUM_FREE_CVLS) {
	    front->next = dpy->lock->free_cvls;
	    dpy->lock->free_cvls = front;
	    dpy->lock->num_free_cvls++;
	} else {
	    xcondition_clear(front->cv);
315 316
	    Xfree(front->cv);
	    Xfree(front);
317 318 319 320 321 322 323 324 325 326 327
	}
    }

    /* signal new front after it is in place */
    if ((dpy->lock->reply_first = (dpy->lock->reply_awaiters != NULL))) {
	ConditionSignal(dpy, dpy->lock->reply_awaiters->cv);
    } else if (dpy->lock->event_awaiters) {
	ConditionSignal(dpy, dpy->lock->event_awaiters->cv);
    }
}

328 329
static void _XConditionWait(
    xcondition_t cv,
330 331 332
    xmutex_t mutex
    XTHREADS_FILE_LINE_ARGS
    )
333 334 335 336 337
{
#ifdef XTHREADS_WARN
    xthread_t self = xthread_self();
    char *old_file = locking_file;
    int old_line = locking_line;
338

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
#ifdef XTHREADS_DEBUG
    printf("line %d thread %x in condition wait\n", line, self);
#endif
    xthread_clear_id(locking_thread);

    locking_history[lock_hist_loc].file = file;
    locking_history[lock_hist_loc].line = line;
    locking_history[lock_hist_loc].thread = self;
    locking_history[lock_hist_loc].lockp = False;
    lock_hist_loc++;
    if (lock_hist_loc >= LOCK_HIST_SIZE)
	lock_hist_loc = 0;
#endif /* XTHREADS_WARN */

    xcondition_wait(cv, mutex);

#ifdef XTHREADS_WARN
    locking_thread = self;
    locking_file = old_file;
    locking_line = old_line;

    locking_history[lock_hist_loc].file = file;
    locking_history[lock_hist_loc].line = line;
    locking_history[lock_hist_loc].thread = self;
    locking_history[lock_hist_loc].lockp = True;
    lock_hist_loc++;
    if (lock_hist_loc >= LOCK_HIST_SIZE)
	lock_hist_loc = 0;
#ifdef XTHREADS_DEBUG
    printf("line %d thread %x was signaled\n", line, self);
#endif /* XTHREADS_DEBUG */
#endif /* XTHREADS_WARN */
}

373
static void _XConditionSignal(
374 375 376
    xcondition_t cv
    XTHREADS_FILE_LINE_ARGS
    )
377 378 379 380 381 382 383 384
{
#ifdef XTHREADS_WARN
#ifdef XTHREADS_DEBUG
    printf("line %d thread %x is signalling\n", line, xthread_self());
#endif
#endif
    xcondition_signal(cv);
}
385

386

387
static void _XConditionBroadcast(
388 389 390
    xcondition_t cv
    XTHREADS_FILE_LINE_ARGS
    )
391 392 393 394 395 396 397 398
{
#ifdef XTHREADS_WARN
#ifdef XTHREADS_DEBUG
    printf("line %d thread %x is broadcasting\n", line, xthread_self());
#endif
#endif
    xcondition_broadcast(cv);
}
399

400

401 402
static void _XFreeDisplayLock(
    Display *dpy)
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
{
    struct _XCVList *cvl;

    if (dpy->lock != NULL) {
	if (dpy->lock->mutex != NULL) {
	    xmutex_clear(dpy->lock->mutex);
	    xmutex_free(dpy->lock->mutex);
	}
	if (dpy->lock->cv != NULL) {
	    xcondition_clear(dpy->lock->cv);
	    xcondition_free(dpy->lock->cv);
	}
	if (dpy->lock->writers != NULL) {
	    xcondition_clear(dpy->lock->writers);
	    xcondition_free(dpy->lock->writers);
	}
	while ((cvl = dpy->lock->free_cvls)) {
	    dpy->lock->free_cvls = cvl->next;
	    xcondition_clear(cvl->cv);
422 423
	    Xfree(cvl->cv);
	    Xfree(cvl);
424
	}
425
	Xfree(dpy->lock);
426 427 428
	dpy->lock = NULL;
    }
    if (dpy->lock_fns != NULL) {
429
	Xfree(dpy->lock_fns);
430 431 432 433 434 435 436 437
	dpy->lock_fns = NULL;
    }
}

/*
 * wait for thread with user-level display lock to release it.
 */

438 439
static void _XDisplayLockWait(
    Display *dpy)
440 441 442 443 444 445 446 447 448 449 450
{
    xthread_t self;

    while (dpy->lock->locking_level > 0) {
	self = xthread_self();
	if (xthread_equal(dpy->lock->locking_thread, self))
	    break;
	ConditionWait(dpy, dpy->lock->cv);
    }
}

451
static void _XLockDisplay(
452 453 454
    Display *dpy
    XTHREADS_FILE_LINE_ARGS
    )
455 456 457 458 459 460 461 462
{
#ifdef XTHREADS_WARN
    _XLockDisplayWarn(dpy, file, line);
#else
    xmutex_lock(dpy->lock->mutex);
#endif
    if (dpy->lock->locking_level > 0)
	_XDisplayLockWait(dpy);
463 464
    _XIDHandler(dpy);
    _XSeqSyncFunction(dpy);
465 466 467 468 469 470
}

/*
 * _XReply is allowed to exit from select/poll and clean up even if a
 * user-level lock is in force, so it uses this instead of _XFancyLockDisplay.
 */
471 472
static void _XInternalLockDisplay(
    Display *dpy,
473 474 475
    Bool wskip
    XTHREADS_FILE_LINE_ARGS
    )
476 477 478 479 480 481 482 483 484 485 486 487 488
{
#ifdef XTHREADS_WARN
    _XLockDisplayWarn(dpy, file, line);
#else
    xmutex_lock(dpy->lock->mutex);
#endif
    if (!wskip && dpy->lock->locking_level > 0)
	_XDisplayLockWait(dpy);
}

static void _XUserLockDisplay(
    register Display* dpy)
{
489 490
    _XDisplayLockWait(dpy);

491 492 493 494 495 496
    if (++dpy->lock->locking_level == 1) {
	dpy->lock->lock_wait = _XDisplayLockWait;
	dpy->lock->locking_thread = xthread_self();
    }
}

497
static
498 499 500 501 502 503 504 505 506 507 508 509 510 511
void _XUserUnlockDisplay(
    register Display* dpy)
{
    if (dpy->lock->locking_level > 0 && --dpy->lock->locking_level == 0) {
	/* signal other threads that might be waiting in XLockDisplay */
	ConditionBroadcast(dpy, dpy->lock->cv);
	dpy->lock->lock_wait = NULL;
	xthread_clear_id(dpy->lock->locking_thread);
    }
}

/* returns 0 if initialized ok, -1 if unable to allocate
   a mutex or other memory */

512 513
static int _XInitDisplayLock(
    Display *dpy)
514
{
515
    dpy->lock_fns = Xmalloc(sizeof(struct _XLockPtrs));
516 517
    if (dpy->lock_fns == NULL)
	return -1;
518
    dpy->lock = Xmalloc(sizeof(struct _XLockInfo));
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
    if (dpy->lock == NULL) {
	_XFreeDisplayLock(dpy);
	return -1;
    }
    dpy->lock->cv = xcondition_malloc();
    dpy->lock->mutex = xmutex_malloc();
    dpy->lock->writers = xcondition_malloc();
    if (!dpy->lock->cv || !dpy->lock->mutex || !dpy->lock->writers) {
	_XFreeDisplayLock(dpy);
	return -1;
    }

    dpy->lock->reply_bytes_left = 0;
    dpy->lock->reply_was_read = False;
    dpy->lock->reply_awaiters = NULL;
    dpy->lock->reply_awaiters_tail = &dpy->lock->reply_awaiters;
    dpy->lock->event_awaiters = NULL;
    dpy->lock->event_awaiters_tail = &dpy->lock->event_awaiters;
    dpy->lock->reply_first = False;
    dpy->lock->locking_level = 0;
    dpy->lock->num_free_cvls = 0;
    dpy->lock->free_cvls = NULL;
    xthread_clear_id(dpy->lock->locking_thread);
    xthread_clear_id(dpy->lock->reading_thread);
    xthread_clear_id(dpy->lock->conni_thread);
    xmutex_init(dpy->lock->mutex);
    xmutex_set_name(dpy->lock->mutex, "Xlib Display");
    xcondition_init(dpy->lock->cv);
    xcondition_set_name(dpy->lock->cv, "XLockDisplay");
    xcondition_init(dpy->lock->writers);
    xcondition_set_name(dpy->lock->writers, "Xlib wait for writable");
    dpy->lock_fns->lock_display = _XLockDisplay;
    dpy->lock->internal_lock_display = _XInternalLockDisplay;
    dpy->lock_fns->unlock_display = _XUnlockDisplay;
    dpy->lock->user_lock_display = _XUserLockDisplay;
    dpy->lock->user_unlock_display = _XUserUnlockDisplay;
    dpy->lock->pop_reader = _XPopReader;
    dpy->lock->push_reader = _XPushReader;
    dpy->lock->condition_wait = _XConditionWait;
    dpy->lock->condition_signal = _XConditionSignal;
    dpy->lock->condition_broadcast = _XConditionBroadcast;
    dpy->lock->create_cvl = _XCreateCVL;
    dpy->lock->lock_wait = NULL; /* filled in by XLockDisplay() */

    return 0;
}

566 567 568 569 570
#ifdef __UNIXWARE__
xthread_t __x11_thr_self() { return 0; }
xthread_t (*_x11_thr_self)() = __x11_thr_self;
#endif

571

572
Status XInitThreads(void)
573 574 575
{
    if (_Xglobal_lock)
	return 1;
576 577 578 579 580 581 582 583 584 585 586
#ifdef __UNIXWARE__
    else {
       void *dl_handle = dlopen(NULL, RTLD_LAZY);
       if (!dl_handle ||
         ((_x11_thr_self = (xthread_t(*)())dlsym(dl_handle,"thr_self")) == 0)) {
	       _x11_thr_self = __x11_thr_self;
	       (void) fprintf (stderr,
	"XInitThreads called, but no libthread in the calling program!\n" );
       }
    }
#endif /* __UNIXWARE__ */
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
#ifdef xthread_init
    xthread_init();		/* return value? */
#endif
    if (!(global_lock.lock = xmutex_malloc()))
	return 0;
    if (!(i18n_lock.lock = xmutex_malloc())) {
	xmutex_free(global_lock.lock);
	global_lock.lock = NULL;
	return 0;
    }
    _Xglobal_lock = &global_lock;
    xmutex_init(_Xglobal_lock->lock);
    xmutex_set_name(_Xglobal_lock->lock, "Xlib global");
    _Xi18n_lock = &i18n_lock;
    xmutex_init(_Xi18n_lock->lock);
    xmutex_set_name(_Xi18n_lock->lock, "Xlib i18n");
    _XLockMutex_fn = _XLockMutex;
    _XUnlockMutex_fn = _XUnlockMutex;
    _XCreateMutex_fn = _XCreateMutex;
    _XFreeMutex_fn = _XFreeMutex;
    _XInitDisplayLock_fn = _XInitDisplayLock;
    _XFreeDisplayLock_fn = _XFreeDisplayLock;
    _Xthread_self_fn = _Xthread_self;

#ifdef XTHREADS_WARN
#ifdef XTHREADS_DEBUG
    setlinebuf(stdout);		/* for debugging messages */
#endif
#endif

    return 1;
}

#else /* XTHREADS */
621
Status XInitThreads(void)
622 623 624 625
{
    return 0;
}
#endif /* XTHREADS */