From 121a43fe1941f4fe43d9b1972630695e50c3e2bc Mon Sep 17 00:00:00 2001 From: Brian Behlendorf Date: Tue, 9 Dec 2008 16:55:26 -0800 Subject: [PATCH] Add threads compat and sysmacro compat --- zfs/lib/libspl/include/sys/sysmacros.h | 96 +++++++++++++++++++ zfs/lib/libspl/include/sys/thread.h | 66 +++++++++++++ zfs/lib/libspl/include/sys/u8_textprep.h | 2 - zfs/lib/libspl/include/sys/u8_textprep_data.h | 2 - zfs/lib/libspl/include/unistd.h | 51 ++++++++++ 5 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 zfs/lib/libspl/include/sys/sysmacros.h create mode 100644 zfs/lib/libspl/include/sys/thread.h create mode 100644 zfs/lib/libspl/include/unistd.h diff --git a/zfs/lib/libspl/include/sys/sysmacros.h b/zfs/lib/libspl/include/sys/sysmacros.h new file mode 100644 index 0000000000..a6c00c88d8 --- /dev/null +++ b/zfs/lib/libspl/include/sys/sysmacros.h @@ -0,0 +1,96 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (the "License"). You may not use this file except in compliance + * with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ +/* + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SOL_SYS_SYSMACROS_H +#define _SOL_SYS_SYSMACROS_H + +#include_next + +/* common macros */ +#ifndef MIN +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#endif +#ifndef MAX +#define MAX(a, b) ((a) < (b) ? (b) : (a)) +#endif +#ifndef ABS +#define ABS(a) ((a) < 0 ? -(a) : (a)) +#endif + +#define makedevice(maj,min) makedev(maj,min) +#define _sysconf(a) sysconf(a) +#define __NORETURN __attribute__ ((noreturn)) + +/* + * Compatibility macros/typedefs needed for Solaris -> Linux port + */ +#define P2ALIGN(x, align) ((x) & -(align)) +#define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1) +#define P2ROUNDUP(x, align) (-(-(x) & -(align))) +#define P2ROUNDUP_TYPED(x, align, type) \ + (-(-(type)(x) & -(type)(align))) +#define P2PHASE(x, align) ((x) & ((align) - 1)) +#define P2NPHASE(x, align) (-(x) & ((align) - 1)) +#define P2NPHASE_TYPED(x, align, type) \ + (-(type)(x) & ((type)(align) - 1)) +#define ISP2(x) (((x) & ((x) - 1)) == 0) +#define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0) + +/* + * Typed version of the P2* macros. These macros should be used to ensure + * that the result is correctly calculated based on the data type of (x), + * which is passed in as the last argument, regardless of the data + * type of the alignment. For example, if (x) is of type uint64_t, + * and we want to round it up to a page boundary using "PAGESIZE" as + * the alignment, we can do either + * P2ROUNDUP(x, (uint64_t)PAGESIZE) + * or + * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t) + */ +#define P2ALIGN_TYPED(x, align, type) \ + ((type)(x) & -(type)(align)) +#define P2PHASE_TYPED(x, align, type) \ + ((type)(x) & ((type)(align) - 1)) +#define P2NPHASE_TYPED(x, align, type) \ + (-(type)(x) & ((type)(align) - 1)) +#define P2ROUNDUP_TYPED(x, align, type) \ + (-(-(type)(x) & -(type)(align))) +#define P2END_TYPED(x, align, type) \ + (-(~(type)(x) & -(type)(align))) +#define P2PHASEUP_TYPED(x, align, phase, type) \ + ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align))) +#define P2CROSS_TYPED(x, y, align, type) \ + (((type)(x) ^ (type)(y)) > (type)(align) - 1) +#define P2SAMEHIGHBIT_TYPED(x, y, type) \ + (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y))) + + +/* avoid any possibility of clashing with version */ +#if defined(_KERNEL) && !defined(_KMEMUSER) && !defined(offsetof) +#define offsetof(s, m) ((size_t)(&(((s *)0)->m))) +#endif + +#endif /* _SOL_SYS_SYSMACROS_H */ diff --git a/zfs/lib/libspl/include/sys/thread.h b/zfs/lib/libspl/include/sys/thread.h new file mode 100644 index 0000000000..ddf4635575 --- /dev/null +++ b/zfs/lib/libspl/include/sys/thread.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2006 OmniTI, Inc. All rights reserved + * This header file distributed under the terms of the CDDL. + * Portions Copyright 2004 Sun Microsystems, Inc. All Rights reserved. + */ +#ifndef _SYS_THREAD_H +#define _SYS_THREAD_H + +#include +#include +#include + +typedef pthread_t thread_t; +typedef pthread_mutex_t mutex_t; +typedef pthread_cond_t cond_t; + +#define THR_BOUND 1 +#define THR_DETACHED 2 +#define THR_DAEMON 4 + +#define thr_self() pthread_self() +#define thr_sigsetmask pthread_sigmask +#define __nthreads() 2 /* XXX: Force multi-thread */ + +static inline int +thr_create(void *stack_base, size_t stack_size, + void *(*start_func)(void *), void *arg, + long flags, thread_t *new_thread_id) +{ + pthread_attr_t attr; + int rc; + + pthread_attr_init(&attr); + + if (flags & THR_DETACHED) + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + rc = pthread_create(new_thread_ID, &attr, start_func, arg); + pthread_attr_destroy(&attr); + + return rc; +} + +#define mutex_init(mp, type, arg) pthread_mutex_init(mp, NULL) +#define mutex_lock(mp) pthread_mutex_lock(mp) +#define mutex_unlock(mp) pthread_mutex_unlock(mp) +#define mutex_destroy(mp) pthread_mutex_destroy(mp) +#define mutex_trylock(mp) pthread_mutex_trylock(mp) +#define DEFAULTMUTEX PTHREAD_MUTEX_INITIALIZER +#define DEFAULTCV PTHREAD_COND_INITIALIZER +#define MUTEX_HELD(mp) 1 /* XXX: Use mutex_trylock() */ + +#define cond_init(c, type, arg) pthread_cond_init(c, NULL) +#define cond_wait(c, m) pthread_cond_wait(c, m) +#define _cond_wait(c, m) pthread_cond_wait(c, m) +#define cond_signal(c) pthread_cond_signal(c) +#define cond_broadcast(c) pthread_cond_broadcast(c) +#define cond_destroy(c) pthread_cond_destroy(c) +#define cond_timedwait pthread_cond_timedwait +#define _cond_timedwait pthread_cond_timedwait + +#ifndef RTLD_FIRST +#define RTLD_FIRST 0 +#endif + +#endif /* _SYS_THREAD_H */ diff --git a/zfs/lib/libspl/include/sys/u8_textprep.h b/zfs/lib/libspl/include/sys/u8_textprep.h index 041ef611ca..5653374d79 100644 --- a/zfs/lib/libspl/include/sys/u8_textprep.h +++ b/zfs/lib/libspl/include/sys/u8_textprep.h @@ -23,8 +23,6 @@ * Use is subject to license terms. */ -#include "zfs_config.h" - #ifdef HAVE_UNICODE #include_next #else diff --git a/zfs/lib/libspl/include/sys/u8_textprep_data.h b/zfs/lib/libspl/include/sys/u8_textprep_data.h index 5980bc5438..6cdb57baa9 100644 --- a/zfs/lib/libspl/include/sys/u8_textprep_data.h +++ b/zfs/lib/libspl/include/sys/u8_textprep_data.h @@ -65,8 +65,6 @@ * This file has been modified by Sun Microsystems, Inc. */ -#include "zfs_config.h" - #ifdef HAVE_UNICODE #include_next #else diff --git a/zfs/lib/libspl/include/unistd.h b/zfs/lib/libspl/include/unistd.h new file mode 100644 index 0000000000..dfb2937459 --- /dev/null +++ b/zfs/lib/libspl/include/unistd.h @@ -0,0 +1,51 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (the "License"). You may not use this file except in compliance + * with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ +/* + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#include_next + +#ifndef _SYS_UNISTD_H +#define _SYS_UNISTD_H + +#ifndef HAVE_ISSETUGID +#include +#define issetugid() (geteuid() == 0 || getegid() == 0) +#endif + +#ifdef HAVE_IOCTL_IN_UNISTD_H +#include +#endif + +#if !defined(__sun__) && !defined(__sun) +/* It seems Solaris only returns positive host ids */ +static inline long fake_gethostid(void) +{ + long id = gethostid(); + return id >= 0 ? id : -id; +} +#define gethostid() fake_gethostid() +#endif + +#endif /* _SYS_UNISTD_H */