blob: 873def6f363abf8c7cbf16abc708645d5edd9833 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * include/asm-s390/semaphore.h
3 *
4 * S390 version
5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 *
7 * Derived from "include/asm-i386/semaphore.h"
8 * (C) Copyright 1996 Linus Torvalds
9 */
10
11#ifndef _S390_SEMAPHORE_H
12#define _S390_SEMAPHORE_H
13
14#include <asm/system.h>
15#include <asm/atomic.h>
16#include <linux/wait.h>
17#include <linux/rwsem.h>
18
19struct semaphore {
20 /*
21 * Note that any negative value of count is equivalent to 0,
22 * but additionally indicates that some process(es) might be
23 * sleeping on `wait'.
24 */
25 atomic_t count;
26 wait_queue_head_t wait;
27};
28
29#define __SEMAPHORE_INITIALIZER(name,count) \
30 { ATOMIC_INIT(count), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) }
31
32#define __MUTEX_INITIALIZER(name) \
33 __SEMAPHORE_INITIALIZER(name,1)
34
35#define __DECLARE_SEMAPHORE_GENERIC(name,count) \
36 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
37
38#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
39#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
40
41static inline void sema_init (struct semaphore *sem, int val)
42{
43 *sem = (struct semaphore) __SEMAPHORE_INITIALIZER((*sem),val);
44}
45
46static inline void init_MUTEX (struct semaphore *sem)
47{
48 sema_init(sem, 1);
49}
50
51static inline void init_MUTEX_LOCKED (struct semaphore *sem)
52{
53 sema_init(sem, 0);
54}
55
56asmlinkage void __down(struct semaphore * sem);
57asmlinkage int __down_interruptible(struct semaphore * sem);
58asmlinkage int __down_trylock(struct semaphore * sem);
59asmlinkage void __up(struct semaphore * sem);
60
61static inline void down(struct semaphore * sem)
62{
63 might_sleep();
64 if (atomic_dec_return(&sem->count) < 0)
65 __down(sem);
66}
67
68static inline int down_interruptible(struct semaphore * sem)
69{
70 int ret = 0;
71
72 might_sleep();
73 if (atomic_dec_return(&sem->count) < 0)
74 ret = __down_interruptible(sem);
75 return ret;
76}
77
78static inline int down_trylock(struct semaphore * sem)
79{
80 int old_val, new_val;
81
82 /*
83 * This inline assembly atomically implements the equivalent
84 * to the following C code:
85 * old_val = sem->count.counter;
86 * if ((new_val = old_val) > 0)
87 * sem->count.counter = --new_val;
88 * In the ppc code this is called atomic_dec_if_positive.
89 */
90 __asm__ __volatile__ (
91 " l %0,0(%3)\n"
92 "0: ltr %1,%0\n"
93 " jle 1f\n"
94 " ahi %1,-1\n"
95 " cs %0,%1,0(%3)\n"
96 " jl 0b\n"
97 "1:"
98 : "=&d" (old_val), "=&d" (new_val), "=m" (sem->count.counter)
99 : "a" (&sem->count.counter), "m" (sem->count.counter)
100 : "cc", "memory" );
101 return old_val <= 0;
102}
103
104static inline void up(struct semaphore * sem)
105{
106 if (atomic_inc_return(&sem->count) <= 0)
107 __up(sem);
108}
109
110#endif