Skip to content

Commit

Permalink
Build fixes, mostly for old systems and compilers.
Browse files Browse the repository at this point in the history
- Add a config check for ((constructor)) and ((destructor)) funciton
  attributes.
- Add missing GL/glxmd.h header.
- For sufficiently old gcc versions, revert to inline asm
  implementations of Atomic{Increment,Swap,CompareAndSwap}()
- Add a local implementation of asprintf.
- entry_x86_tsd.h: Add missing cast to (char *) in entry_get_public
- glvnd_pthread.[ch]: Only use pthread_rwlock_t if it's available, and fall
  back to pthread_mutex_t otherwise.
- trace.c: Add missing _GNU_SOURCE define
- uthash.h: Fix a -Wcast-qual warning
- x11glvndclient.c: Fix a -Wcast-qual warning by using a writable
  array for storing the XGLV_EXTENSION_NAME

Based on a patch by Brian Nguyen.
  • Loading branch information
kbrenneman committed Mar 27, 2015
1 parent 6d27984 commit 0d990b6
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 14 deletions.
45 changes: 45 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,51 @@ AS_IF([test "x$GLX_USE_TLS" = "xyes"],
[AC_DEFINE([GLX_USE_TLS], 1,
[Define to 1 if ELF TLS with initial-exec addressing is available.])])

AC_MSG_CHECKING([for constructor attributes])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
void __attribute__ ((constructor)) foo(void)
{
}
void __attribute__ ((destructor)) bar(void)
{
}
])],
[USE_ATTRIBUTE_CONSTRUCTOR=yes],[USE_ATTRIBUTE_CONSTRUCTOR=no])
AC_MSG_RESULT($USE_ATTRIBUTE_CONSTRUCTOR)
AS_IF([test "x$USE_ATTRIBUTE_CONSTRUCTOR" = "xyes"],
[AC_DEFINE([USE_ATTRIBUTE_CONSTRUCTOR], 1,
[Define to 1 if the compiler constructor attributes.])])

AC_MSG_CHECKING([for pthreads rwlocks])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <pthread.h>
void foo(void)
{
pthread_rwlock_t lock;
pthread_rwlock_init(&lock, NULL);
}
])],
[HAVE_PTHREAD_RWLOCK_T=yes],[HAVE_PTHREAD_RWLOCK_T=no])
AC_MSG_RESULT($HAVE_PTHREAD_RWLOCK_T)
AS_IF([test "x$HAVE_PTHREAD_RWLOCK_T" = "xyes"],
[AC_DEFINE([HAVE_PTHREAD_RWLOCK_T], 1,
[Define to 1 if the compiler supports pthreads rwlocks.])])

AC_MSG_CHECKING([for sync intrinsics])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
int foo(int volatile *val, int oldVal, int newVal)
{
return __sync_add_and_fetch(val, 1);
return __sync_lock_test_and_set(val, newVal);
return __sync_val_compare_and_swap(val, oldVal, newVal);
}
])],
[HAVE_SYNC_INTRINSICS=yes],[HAVE_SYNC_INTRINSICS=no])
AC_MSG_RESULT($HAVE_SYNC_INTRINSICS)
AS_IF([test "x$HAVE_SYNC_INTRINSICS" = "xyes"],
[AC_DEFINE([HAVE_SYNC_INTRINSICS], 1,
[Define to 1 if the compiler supports pthreads rwlocks.])])

dnl default CFLAGS
CFLAGS="$CFLAGS -Wall -Werror -std=gnu99 -include config.h -fvisibility=hidden $DEFINES"

Expand Down
54 changes: 54 additions & 0 deletions include/GL/glxmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef _GLX_glxmd_h_
#define _GLX_glxmd_h_


/*
* SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
* Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice including the dates of first publication and
* either this permission notice or a reference to
* http://oss.sgi.com/projects/FreeB/
* 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
* SILICON GRAPHICS, INC. 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 Silicon Graphics, Inc.
* shall not be used in advertising or otherwise to promote the sale, use or
* other dealings in this Software without prior written authorization from
* Silicon Graphics, Inc.
*/

/*
** Machine dependent declarations.
*/

/*
** Define floating point wire types. These are in IEEE format on the wire.
*/
typedef float FLOAT32;
typedef double FLOAT64;

/*
** Like B32, but this is used to store floats in a request.
**
** NOTE: Machines that have a native 32-bit IEEE float can define this as
** nothing. Machines that don't might mimic the float with an integer,
** and then define this to :32.
*/
#define F32

#endif /* _GLX_glxmd_h_ */
8 changes: 8 additions & 0 deletions src/GL/libgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
#include "GLdispatch.h"

// Initialize GLX imports
#if defined(USE_ATTRIBUTE_CONSTRUCTOR)
void __attribute__((constructor)) __libGLInit(void)
#else
void _init(void)
#endif
{
// Fix up the static GL entrypoints, if necessary
entry_init_public();
Expand All @@ -52,7 +56,11 @@ void __attribute__((constructor)) __libGLInit(void)
__glXWrapperInit(__glXGetCachedProcAddress);
}

#if defined(USE_ATTRIBUTE_CONSTRUCTOR)
void __attribute__((destructor)) __libGLFini(void)
#else
void _fini(void)
#endif
{
// Unregister the GLdispatch entrypoints
__glDispatchUnregisterStubCallbacks(stub_get_offsets, stub_restore);
Expand Down
39 changes: 39 additions & 0 deletions src/GLX/libglx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1424,17 +1424,48 @@ PUBLIC __GLXextFuncPtr glXGetProcAddress(const GLubyte *procName)

int AtomicIncrement(int volatile *val)
{
#if defined(HAVE_SYNC_INTRINSICS)
return __sync_add_and_fetch(val, 1);
#else
int result;
int delta = 1;

__asm __volatile__ ("lock; xaddl %0, %1"
: "=r" (result), "=m" (*val)
: "0" (delta), "m" (*val));

return result + delta;
#endif
}

int AtomicSwap(int volatile *val, int newVal)
{
#if defined(HAVE_SYNC_INTRINSICS)
return __sync_lock_test_and_set(val, newVal);
#else
int result;

__asm __volatile__ ("xchgl %0, %1"
: "=r" (result), "=m" (*val)
: "0" (newVal), "m" (*val));

return result;
#endif
}

int AtomicCompareAndSwap(int volatile *val, int oldVal, int newVal)
{
#if defined(HAVE_SYNC_INTRINSICS)
return __sync_val_compare_and_swap(val, oldVal, newVal);
#else
int result;

__asm __volatile__ ("lock; cmpxchgl %2, %1"
: "=a" (result), "=m" (*val)
: "r" (newVal), "m" (*val), "0" (oldVal));

return result;
#endif
}

int AtomicDecrementClampAtZero(int volatile *val)
Expand Down Expand Up @@ -1548,7 +1579,11 @@ static void __glXResetOnFork(void)
__glDispatchReset();
}

#if defined(USE_ATTRIBUTE_CONSTRUCTOR)
void __attribute__ ((constructor)) __glXInit(void)
#else
void _init(void)
#endif
{

/* Initialize GLdispatch; this will also initialize our pthreads imports */
Expand All @@ -1575,7 +1610,11 @@ void __attribute__ ((constructor)) __glXInit(void)

}

#if defined(USE_ATTRIBUTE_CONSTRUCTOR)
void __attribute__ ((destructor)) __glXFini(void)
#else
void _fini(void)
#endif
{
/* Check for a fork before going further. */
__glXThreadInitialize();
Expand Down
43 changes: 42 additions & 1 deletion src/GLX/libglxmapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,53 @@ static void InitExportsTable(void)

}

/**
* A local implementation of asprintf(3), for systems that don't support it.
*/
int glvnd_asprintf(char **strp, const char *fmt, ...)
{
static const int GLVND_ASPRINTF_BUF_LEN = 256;
char *str = NULL;
int ret = -1;

if (fmt) {
va_list ap;
int len, current_len = GLVND_ASPRINTF_BUF_LEN;

while (1) {
str = malloc(current_len);

va_start(ap, fmt);
len = vsnprintf(str, current_len, fmt, ap);
va_end(ap);

// If the buffer isn't large enough, then vsnprintf will either
// return -1 (for glibc < 2.1) or the number of bytes the buffer
// needs to be (for glibc >= 2.1).
if ((len > -1) && (len < current_len)) {
ret = len;
break;
} else if (len > -1) {
current_len = len + 1;
} else {
current_len += GLVND_ASPRINTF_BUF_LEN;
}

free(str);
}
}

*strp = str;
return ret;
}


static char *ConstructVendorLibraryFilename(const char *vendorName)
{
char *filename;
int ret;

ret = asprintf(&filename, "libGLX_%s.so.0", vendorName);
ret = glvnd_asprintf(&filename, "libGLX_%s.so.0", vendorName);

if (ret < 0) {
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/GLdispatch/vnd-glapi/mapi/entry_x86_tsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ entry_generate_default_code(char *entry, int slot)
mapi_func
entry_get_public(int slot)
{
return (mapi_func) (x86_entry_start + slot * X86_ENTRY_SIZE);
return (mapi_func) ((char *)x86_entry_start + slot * X86_ENTRY_SIZE);
}

#if !defined(STATIC_DISPATCH_ONLY)
Expand Down
45 changes: 38 additions & 7 deletions src/util/glvnd_pthread/glvnd_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ typedef struct GLVNDPthreadRealFuncsRec {
int (*mutex_lock)(pthread_mutex_t *mutex);
int (*mutex_unlock)(pthread_mutex_t *mutex);

#if defined(HAVE_PTHREAD_RWLOCK_T)
int (*rwlock_init)(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
int (*rwlock_destroy)(pthread_rwlock_t *rwlock);
int (*rwlock_rdlock)(pthread_rwlock_t *rwlock);
int (*rwlock_wrlock)(pthread_rwlock_t *rwlock);
int (*rwlock_unlock)(pthread_rwlock_t *rwlock);
#endif

/* Other used functions */
int (*once)(pthread_once_t *once_control, void (*init_routine)(void));
Expand Down Expand Up @@ -85,6 +87,14 @@ static GLVNDPthreadRealFuncs pthreadRealFuncs;
(obj)->func = mt_ ## func; \
} while (0)

#if defined(HAVE_PTHREAD_RWLOCK_T)
# define GET_MT_RWLOCK_FUNC(obj, handle, func) \
GET_MT_FUNC(obj, handle, func)
#else
# define GET_MT_RWLOCK_FUNC(obj, handle, func) \
(obj)->func = mt_ ## func;
#endif

#define GET_ST_FUNC(obj, func) do { \
(obj)->func = st_ ## func; \
} while (0)
Expand Down Expand Up @@ -259,27 +269,47 @@ static int mt_mutex_unlock(glvnd_mutex_t *mutex)

static int mt_rwlock_init(glvnd_rwlock_t *rwlock, const glvnd_rwlockattr_t *attr)
{
#if defined(HAVE_PTHREAD_RWLOCK_T)
return pthreadRealFuncs.rwlock_init(rwlock, attr);
#else
return pthreadRealFuncs.mutex_init(rwlock, attr);
#endif
}

static int mt_rwlock_destroy(glvnd_rwlock_t *rwlock)
{
#if defined(HAVE_PTHREAD_RWLOCK_T)
return pthreadRealFuncs.rwlock_destroy(rwlock);
#else
return pthreadRealFuncs.mutex_destroy(rwlock);
#endif
}

static int mt_rwlock_rdlock(glvnd_rwlock_t *rwlock)
{
#if defined(HAVE_PTHREAD_RWLOCK_T)
return pthreadRealFuncs.rwlock_rdlock(rwlock);
#else
return pthreadRealFuncs.mutex_lock(rwlock);
#endif
}

static int mt_rwlock_wrlock(glvnd_rwlock_t *rwlock)
{
#if defined(HAVE_PTHREAD_RWLOCK_T)
return pthreadRealFuncs.rwlock_wrlock(rwlock);
#else
return pthreadRealFuncs.mutex_lock(rwlock);
#endif
}

static int mt_rwlock_unlock(glvnd_rwlock_t *rwlock)
{
#if defined(HAVE_PTHREAD_RWLOCK_T)
return pthreadRealFuncs.rwlock_unlock(rwlock);
#else
return pthreadRealFuncs.mutex_unlock(rwlock);
#endif
}

static int mt_once(glvnd_once_t *once_control, void (*init_routine)(void))
Expand Down Expand Up @@ -324,13 +354,12 @@ void glvndSetupPthreads(void *dlhandle, GLVNDPthreadFuncs *funcs)
GET_MT_FUNC(funcs, dlhandle, mutex_lock);
GET_MT_FUNC(funcs, dlhandle, mutex_unlock);

// TODO: these can fall back on internal implementations
// if they're not available in pthreads
GET_MT_FUNC(funcs, dlhandle, rwlock_init);
GET_MT_FUNC(funcs, dlhandle, rwlock_destroy);
GET_MT_FUNC(funcs, dlhandle, rwlock_rdlock);
GET_MT_FUNC(funcs, dlhandle, rwlock_wrlock);
GET_MT_FUNC(funcs, dlhandle, rwlock_unlock);
GET_MT_RWLOCK_FUNC(funcs, dlhandle, rwlock_init);
GET_MT_RWLOCK_FUNC(funcs, dlhandle, rwlock_destroy);
GET_MT_RWLOCK_FUNC(funcs, dlhandle, rwlock_rdlock);
GET_MT_RWLOCK_FUNC(funcs, dlhandle, rwlock_wrlock);
GET_MT_RWLOCK_FUNC(funcs, dlhandle, rwlock_unlock);

GET_MT_FUNC(funcs, dlhandle, once);
GET_MT_FUNC(funcs, dlhandle, key_create);
GET_MT_FUNC(funcs, dlhandle, key_delete);
Expand All @@ -355,11 +384,13 @@ void glvndSetupPthreads(void *dlhandle, GLVNDPthreadFuncs *funcs)
GET_ST_FUNC(funcs, mutex_destroy);
GET_ST_FUNC(funcs, mutex_lock);
GET_ST_FUNC(funcs, mutex_unlock);

GET_ST_FUNC(funcs, rwlock_init);
GET_ST_FUNC(funcs, rwlock_destroy);
GET_ST_FUNC(funcs, rwlock_rdlock);
GET_ST_FUNC(funcs, rwlock_wrlock);
GET_ST_FUNC(funcs, rwlock_unlock);

GET_ST_FUNC(funcs, once);
GET_ST_FUNC(funcs, key_create);
GET_ST_FUNC(funcs, key_delete);
Expand Down
Loading

0 comments on commit 0d990b6

Please sign in to comment.