2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* Copyright (C) 1994 Cazamar Systems, Inc. */
12 #ifndef _OSIBASEL_H_ENV_
13 #define _OSIBASEL_H_ENV_ 1
15 /* flags for osi_mutex_t and osi_rwlock_t flags fields. Some bits
16 * are used only in one structure or another.
18 #define OSI_LOCKFLAG_EXCL 1 /* exclusive locked (rwlock only) */
20 /* a mutex (pure exclusive lock). This structure has two forms. In the
21 * base type (type == 0), the d field is interpreted as an atomic counter,
22 * and all the other fields are used. In the other types, type specifies
23 * which operations to use (via the global osi_lockOps), and d.privateDatap
24 * points to the real data used by the mutex.
26 * For the base type, flags tells us if the lock is held, and if anyone else
27 * is waiting for the lock. The field d.atomicCount is used to implement a spin
28 * lock using an atomic increment operation.
30 typedef struct osi_mutex {
31 char type; /* for all types; type 0 uses atomic count */
32 char flags; /* flags for base type */
33 unsigned short atomicIndex; /* index of lock for low-level sync */
34 DWORD tid; /* tid of thread that owns the lock */
35 unsigned short waiters; /* waiters */
38 void *privateDatap; /* data pointer for non-zero types */
39 osi_turnstile_t turn; /* turnstile */
43 /* a read/write lock. This structure has two forms. In the
44 * base type (type == 0), the d field is interpreted as an atomic counter,
45 * and all the other fields are used. In the other types, type specifies
46 * which operations to use (via the global osi_lockOps), and d.privateDatap
47 * points to the real data used by the mutex.
49 * For the base type, flags tells us if the lock is held, and if anyone else
50 * is waiting for the lock. The field d.atomicCount is used to implement a spin
51 * lock using an atomic increment operation.
53 * This type of lock has N readers or one writer.
55 typedef struct osi_rwlock {
56 char type; /* for all types; type 0 uses atomic count */
57 char flags; /* flags for base type */
58 unsigned short atomicIndex; /* index into hash table for low-level sync */
59 DWORD tid; /* writer's tid */
60 unsigned short waiters; /* waiters */
61 unsigned short readers; /* readers */
63 void *privateDatap; /* data pointer for non-zero types */
64 osi_turnstile_t turn; /* turnstile */
68 extern void lock_ObtainRead (struct osi_rwlock *);
70 extern void lock_ObtainWrite (struct osi_rwlock *);
72 extern void lock_ReleaseRead (struct osi_rwlock *);
74 extern void lock_ReleaseWrite (struct osi_rwlock *);
76 extern void lock_ObtainMutex (struct osi_mutex *);
78 extern void lock_ReleaseMutex (struct osi_mutex *);
80 extern int lock_TryRead (struct osi_rwlock *);
82 extern int lock_TryWrite (struct osi_rwlock *);
84 extern int lock_TryMutex (struct osi_mutex *);
86 extern void osi_SleepR (LONG_PTR, struct osi_rwlock *);
88 extern void osi_SleepW (LONG_PTR, struct osi_rwlock *);
90 extern void osi_SleepM (LONG_PTR, struct osi_mutex *);
92 extern void osi_Sleep (LONG_PTR);
94 extern void osi_Wakeup (LONG_PTR);
96 extern void lock_FinalizeRWLock(struct osi_rwlock *);
98 extern void lock_FinalizeMutex(struct osi_mutex *);
100 extern CRITICAL_SECTION osi_baseAtomicCS[];
102 /* and define the functions that create basic locks and mutexes */
104 extern void lock_InitializeRWLock(struct osi_rwlock *, char *);
106 extern void lock_InitializeMutex(struct osi_mutex *, char *);
108 extern void osi_Init (void);
110 extern void lock_ConvertWToR(struct osi_rwlock *);
112 /* and stat functions */
114 extern int lock_GetRWLockState(struct osi_rwlock *);
116 extern int lock_GetMutexState(struct osi_mutex *);
120 extern void osi_BaseInit(void);
122 /* and friendly macros */
124 #define lock_AssertRead(x) osi_assert(lock_GetRWLockState(x) & OSI_RWLOCK_READHELD)
126 #define lock_AssertWrite(x) osi_assert(lock_GetRWLockState(x) & OSI_RWLOCK_WRITEHELD)
128 #define lock_AssertAny(x) osi_assert(lock_GetRWLockState(x) != 0)
130 #define lock_AssertMutex(x) osi_assert(lock_GetMutexState(x) & OSI_MUTEX_HELD)
132 #endif /*_OSIBASEL_H_ENV_ */