OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [include/] [asm-or32/] [semaphore.h] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1276 phoenix
/*
2
 * Based on:
3
 * include/asm-cris/semaphore.h
4
 */
5
 
6
#ifndef _OR32_SEMAPHORE_H
7
#define _OR32_SEMAPHORE_H
8
 
9
#define RW_LOCK_BIAS             0x01000000
10
 
11
#include <linux/wait.h>
12
#include <linux/spinlock.h>
13
#include <linux/rwsem.h>
14
 
15
#include <asm/system.h>
16
#include <asm/atomic.h>
17
 
18
int printk(const char *fmt, ...);
19
 
20
struct semaphore {
21
        int count; /* not atomic_t since we do the atomicity here already */
22
        atomic_t waking;
23
        wait_queue_head_t wait;
24
#if WAITQUEUE_DEBUG
25
        long __magic;
26
#endif
27
};
28
 
29
#if WAITQUEUE_DEBUG
30
# define __SEM_DEBUG_INIT(name)         , (long)&(name).__magic
31
#else
32
# define __SEM_DEBUG_INIT(name)
33
#endif
34
 
35
#define __SEMAPHORE_INITIALIZER(name,count)             \
36
        { count, ATOMIC_INIT(0),          \
37
          __WAIT_QUEUE_HEAD_INITIALIZER((name).wait)    \
38
          __SEM_DEBUG_INIT(name) }
39
 
40
#define __MUTEX_INITIALIZER(name) \
41
        __SEMAPHORE_INITIALIZER(name,1)
42
 
43
#define __DECLARE_SEMAPHORE_GENERIC(name,count) \
44
        struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
45
 
46
#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
47
#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
48
 
49
extern inline void sema_init(struct semaphore *sem, int val)
50
{
51
        *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
52
}
53
 
54
static inline void init_MUTEX (struct semaphore *sem)
55
{
56
        sema_init(sem, 1);
57
}
58
 
59
static inline void init_MUTEX_LOCKED (struct semaphore *sem)
60
{
61
        sema_init(sem, 0);
62
}
63
 
64
extern void __down(struct semaphore * sem);
65
extern int __down_interruptible(struct semaphore * sem);
66
extern int __down_trylock(struct semaphore * sem);
67
extern void __up(struct semaphore * sem);
68
 
69
/* notice - we probably can do cli/sti here instead of saving */
70
 
71
extern inline void down(struct semaphore * sem)
72
{
73
        unsigned long flags;
74
        int failed;
75
 
76
#if WAITQUEUE_DEBUG
77
        CHECK_MAGIC(sem->__magic);
78
#endif
79
 
80
        /* atomically decrement the semaphores count, and if its negative, we wait */
81
        save_flags(flags);
82
        cli();
83
        failed = --(sem->count) < 0;
84
        restore_flags(flags);
85
        if(failed) {
86
                __down(sem);
87
        }
88
}
89
 
90
/*
91
 * This version waits in interruptible state so that the waiting
92
 * process can be killed.  The down_interruptible routine
93
 * returns negative for signalled and zero for semaphore acquired.
94
 */
95
 
96
extern inline int down_interruptible(struct semaphore * sem)
97
{
98
        unsigned long flags;
99
        int failed;
100
 
101
#if WAITQUEUE_DEBUG
102
        CHECK_MAGIC(sem->__magic);
103
#endif
104
 
105
        /* atomically decrement the semaphores count, and if its negative, we wait */
106
        save_flags(flags);
107
        cli();
108
        failed = --(sem->count) < 0;
109
        restore_flags(flags);
110
        if(failed)
111
                failed = __down_interruptible(sem);
112
        return(failed);
113
}
114
 
115
extern inline int down_trylock(struct semaphore * sem)
116
{
117
        unsigned long flags;
118
        int failed;
119
 
120
#if WAITQUEUE_DEBUG
121
        CHECK_MAGIC(sem->__magic);
122
#endif
123
 
124
        save_flags(flags);
125
        cli();
126
        failed = --(sem->count) < 0;
127
        restore_flags(flags);
128
        if(failed)
129
                failed = __down_trylock(sem);
130
        return(failed);
131
}
132
 
133
/*
134
 * Note! This is subtle. We jump to wake people up only if
135
 * the semaphore was negative (== somebody was waiting on it).
136
 * The default case (no contention) will result in NO
137
 * jumps for both down() and up().
138
 */
139
extern inline void up(struct semaphore * sem)
140
{
141
        unsigned long flags;
142
        int wakeup;
143
 
144
#if WAITQUEUE_DEBUG
145
        CHECK_MAGIC(sem->__magic);
146
#endif
147
 
148
        /* atomically increment the semaphores count, and if it was negative, we wake people */
149
        save_flags(flags);
150
        cli();
151
        wakeup = ++(sem->count) <= 0;
152
        restore_flags(flags);
153
        if(wakeup) {
154
                __up(sem);
155
        }
156
}
157
 
158
static inline int sem_getcount(struct semaphore *sem)
159
{
160
        return sem->count;
161
}
162
 
163
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.