Path: blob/main/cddl/contrib/opensolaris/head/synch.h
39483 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2014 Garrett D'Amore <[email protected]>23* Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.24*/2526#ifndef _SYNCH_H27#define _SYNCH_H2829/*30* synch.h:31* definitions needed to use the thread synchronization interface32*/3334#ifndef _ASM35#include <sys/machlock.h>36#include <sys/time_impl.h>37#include <sys/synch.h>38#endif /* _ASM */3940#ifdef __cplusplus41extern "C" {42#endif4344#ifndef _ASM4546/*47* Semaphores48*/49typedef struct _sema {50/* this structure must be the same as sem_t in <semaphore.h> */51uint32_t count; /* semaphore count */52uint16_t type;53uint16_t magic;54upad64_t pad1[3]; /* reserved for a mutex_t */55upad64_t pad2[2]; /* reserved for a cond_t */56} sema_t;5758/*59* POSIX.1c Note:60* POSIX.1c requires that <pthread.h> define the structures pthread_mutex_t61* and pthread_cond_t. These structures are identical to mutex_t (lwp_mutex_t)62* and cond_t (lwp_cond_t) which are defined in <synch.h>. A nested included63* of <synch.h> (to allow a "#typedef mutex_t pthread_mutex_t") would pull in64* non-posix symbols/constants violating the namespace restrictions. Hence,65* pthread_mutex_t/pthread_cond_t have been redefined in <pthread.h> (actually66* in <sys/types.h>). Any modifications done to mutex_t/lwp_mutex_t or67* cond_t/lwp_cond_t should also be done to pthread_mutex_t/pthread_cond_t.68*/69typedef lwp_mutex_t mutex_t;70typedef lwp_cond_t cond_t;7172/*73* Readers/writer locks74*75* NOTE: The layout of this structure should be kept in sync with the layout76* of the correponding structure of pthread_rwlock_t in sys/types.h.77* Also, there is an identical structure for lwp_rwlock_t in <sys/synch.h>.78* Because we have to deal with C++, we cannot redefine this one as that one.79*/80typedef struct _rwlock {81int32_t readers; /* rwstate word */82uint16_t type;83uint16_t magic;84mutex_t mutex; /* used with process-shared rwlocks */85cond_t readercv; /* used only to indicate ownership */86cond_t writercv; /* used only to indicate ownership */87} rwlock_t;8889int _lwp_mutex_lock(lwp_mutex_t *);90int _lwp_mutex_unlock(lwp_mutex_t *);91int _lwp_mutex_trylock(lwp_mutex_t *);92int _lwp_cond_wait(lwp_cond_t *, lwp_mutex_t *);93int _lwp_cond_timedwait(lwp_cond_t *, lwp_mutex_t *, timespec_t *);94int _lwp_cond_reltimedwait(lwp_cond_t *, lwp_mutex_t *, timespec_t *);95int _lwp_cond_signal(lwp_cond_t *);96int _lwp_cond_broadcast(lwp_cond_t *);97int _lwp_sema_init(lwp_sema_t *, int);98int _lwp_sema_wait(lwp_sema_t *);99int _lwp_sema_trywait(lwp_sema_t *);100int _lwp_sema_post(lwp_sema_t *);101int cond_init(cond_t *, int, void *);102int cond_destroy(cond_t *);103int cond_wait(cond_t *, mutex_t *);104int cond_timedwait(cond_t *, mutex_t *, const timespec_t *);105int cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);106int cond_signal(cond_t *);107int cond_broadcast(cond_t *);108int mutex_init(mutex_t *, int, void *);109int mutex_destroy(mutex_t *);110int mutex_consistent(mutex_t *);111int mutex_lock(mutex_t *);112int mutex_trylock(mutex_t *);113int mutex_unlock(mutex_t *);114int rwlock_init(rwlock_t *, int, void *);115int rwlock_destroy(rwlock_t *);116int rw_rdlock(rwlock_t *);117int rw_wrlock(rwlock_t *);118int rw_unlock(rwlock_t *);119int rw_tryrdlock(rwlock_t *);120int rw_trywrlock(rwlock_t *);121int sema_init(sema_t *, unsigned int, int, void *);122int sema_destroy(sema_t *);123int sema_wait(sema_t *);124int sema_timedwait(sema_t *, const timespec_t *);125int sema_reltimedwait(sema_t *, const timespec_t *);126int sema_post(sema_t *);127int sema_trywait(sema_t *);128129#endif /* _ASM */130131/* "Magic numbers" tagging synchronization object types */132#define MUTEX_MAGIC _MUTEX_MAGIC133#define SEMA_MAGIC _SEMA_MAGIC134#define COND_MAGIC _COND_MAGIC135#define RWL_MAGIC _RWL_MAGIC136137/*138* POSIX.1c Note:139* DEFAULTMUTEX is defined same as PTHREAD_MUTEX_INITIALIZER in <pthread.h>.140* DEFAULTCV is defined same as PTHREAD_COND_INITIALIZER in <pthread.h>.141* DEFAULTRWLOCK is defined same as PTHREAD_RWLOCK_INITIALIZER in <pthread.h>.142* Any changes to these macros should be reflected in <pthread.h>143*/144#define DEFAULTMUTEX \145{{0, 0, 0, {USYNC_THREAD}, MUTEX_MAGIC}, \146{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}147#define SHAREDMUTEX \148{{0, 0, 0, {USYNC_PROCESS}, MUTEX_MAGIC}, \149{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}150#define RECURSIVEMUTEX \151{{0, 0, 0, {USYNC_THREAD|LOCK_RECURSIVE}, MUTEX_MAGIC}, \152{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}153#define ERRORCHECKMUTEX \154{{0, 0, 0, {USYNC_THREAD|LOCK_ERRORCHECK}, MUTEX_MAGIC}, \155{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}156#define RECURSIVE_ERRORCHECKMUTEX \157{{0, 0, 0, {USYNC_THREAD|LOCK_RECURSIVE|LOCK_ERRORCHECK}, \158MUTEX_MAGIC}, {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}159#define DEFAULTCV \160{{{0, 0, 0, 0}, USYNC_THREAD, COND_MAGIC}, 0}161#define SHAREDCV \162{{{0, 0, 0, 0}, USYNC_PROCESS, COND_MAGIC}, 0}163#define DEFAULTSEMA \164{0, USYNC_THREAD, SEMA_MAGIC, {0, 0, 0}, {0, 0}}165#define SHAREDSEMA \166{0, USYNC_PROCESS, SEMA_MAGIC, {0, 0, 0}, {0, 0}}167#define DEFAULTRWLOCK \168{0, USYNC_THREAD, RWL_MAGIC, DEFAULTMUTEX, DEFAULTCV, DEFAULTCV}169#define SHAREDRWLOCK \170{0, USYNC_PROCESS, RWL_MAGIC, SHAREDMUTEX, SHAREDCV, SHAREDCV}171172/*173* Tests on lock states.174*/175#define SEMA_HELD(x) _sema_held(x)176#define RW_READ_HELD(x) _rw_read_held(x)177#define RW_WRITE_HELD(x) _rw_write_held(x)178#define RW_LOCK_HELD(x) (RW_READ_HELD(x) || RW_WRITE_HELD(x))179#define MUTEX_HELD(x) _mutex_held(x)180181/*182* The following definitions are for assertions which can be checked183* statically by tools like lock_lint. You can also define your own184* run-time test for each. If you don't, we define them to 1 so that185* such assertions simply pass.186*/187#ifndef NO_LOCKS_HELD188#define NO_LOCKS_HELD 1189#endif190#ifndef NO_COMPETING_THREADS191#define NO_COMPETING_THREADS 1192#endif193194#ifndef _ASM195196/*197* The *_held() functions apply equally well to Solaris threads198* and to Posix threads synchronization objects, but the formal199* type declarations are different, so we just declare the argument200* to each *_held() function to be a void *, expecting that they will201* be called with the proper type of argument in each case.202*/203int _sema_held(void *); /* sema_t or sem_t */204int _rw_read_held(void *); /* rwlock_t or pthread_rwlock_t */205int _rw_write_held(void *); /* rwlock_t or pthread_rwlock_t */206int _mutex_held(void *); /* mutex_t or pthread_mutex_t */207208/* Pause API */209void smt_pause(void);210211#endif /* _ASM */212213#ifdef __cplusplus214}215#endif216217#endif /* _SYNCH_H */218219220