Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / state_trackers / glx / dri / dri_lock.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "pipe/p_thread.h"
30 #include "dri_context.h"
31 #include "xf86drm.h"
32
33 pipe_static_mutex( lockMutex );
34
35 static void
36 dri_contended_lock(struct dri_context *ctx)
37 {
38 __DRIdrawablePrivate *dPriv = ctx->dPriv;
39 __DRIcontextPrivate *cPriv = ctx->cPriv;
40 __DRIscreenPrivate *sPriv = cPriv->driScreenPriv;
41
42 drmGetLock(sPriv->fd, cPriv->hHWContext, 0);
43
44 /* Perform round trip communication with server (including dropping
45 * and retaking the above lock) to update window dimensions:
46 */
47 if (dPriv)
48 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
49 }
50
51
52 /* Lock the hardware and validate our state.
53 */
54 void dri_lock_hardware( struct dri_context *ctx )
55 {
56 __DRIcontextPrivate *cPriv = ctx->cPriv;
57 __DRIscreenPrivate *sPriv = cPriv->driScreenPriv;
58 char __ret = 0;
59
60 pipe_mutex_lock(lockMutex);
61 assert(!ctx->locked);
62
63 DRM_CAS((drmLock *) &sPriv->pSAREA->lock,
64 cPriv->hHWContext,
65 (DRM_LOCK_HELD | cPriv->hHWContext),
66 __ret);
67
68 if (__ret)
69 dri_contended_lock( ctx );
70
71 ctx->locked = TRUE;
72 }
73
74
75 /* Unlock the hardware using the global current context
76 */
77 void dri_unlock_hardware( struct dri_context *ctx )
78 {
79 __DRIcontextPrivate *cPriv = ctx->cPriv;
80 __DRIscreenPrivate *sPriv = cPriv->driScreenPriv;
81
82 assert(ctx->locked);
83 ctx->locked = FALSE;
84
85 DRM_UNLOCK(sPriv->fd,
86 (drmLock *) &sPriv->pSAREA->lock,
87 cPriv->hHWContext);
88
89 pipe_mutex_unlock(lockMutex);
90 }