File: /usr/src/linux/include/asm-cris/semaphore-helper.h

1     /* $Id: semaphore-helper.h,v 1.3 2001/03/26 15:00:33 orjanf Exp $
2      *
3      * SMP- and interrupt-safe semaphores helper functions. Generic versions, no
4      * optimizations whatsoever... 
5      *
6      */
7     
8     #ifndef _ASM_SEMAPHORE_HELPER_H
9     #define _ASM_SEMAPHORE_HELPER_H
10     
11     #include <asm/atomic.h>
12     
13     #define read(a) ((a)->counter)
14     #define inc(a) (((a)->counter)++)
15     #define dec(a) (((a)->counter)--)
16     
17     #define count_inc(a) ((*(a))++)
18     
19     /*
20      * These two _must_ execute atomically wrt each other.
21      */
22     static inline void wake_one_more(struct semaphore * sem)
23     {
24     	atomic_inc(&sem->waking);
25     }
26     
27     static inline int waking_non_zero(struct semaphore *sem)
28     {
29     	unsigned long flags;
30     	int ret = 0;
31     
32     	save_and_cli(flags);
33     	if (read(&sem->waking) > 0) {
34     		dec(&sem->waking);
35     		ret = 1;
36     	}
37     	restore_flags(flags);
38     	return ret;
39     }
40     
41     static inline int waking_non_zero_interruptible(struct semaphore *sem,
42     						struct task_struct *tsk)
43     {
44     	int ret = 0;
45     	unsigned long flags;
46     
47     	save_and_cli(flags);
48     	if (read(&sem->waking) > 0) {
49     		dec(&sem->waking);
50     		ret = 1;
51     	} else if (signal_pending(tsk)) {
52     		count_inc(&sem->count);
53     		ret = -EINTR;
54     	}
55     	restore_flags(flags);
56     	return ret;
57     }
58     
59     static inline int waking_non_zero_trylock(struct semaphore *sem)
60     {
61             int ret = 1;
62     	unsigned long flags;
63     
64     	save_and_cli(flags);
65     	if (read(&sem->waking) <= 0)
66     		count_inc(&sem->count);
67     	else {
68     		dec(&sem->waking);
69     		ret = 0;
70     	}
71     	restore_flags(flags);
72     	return ret;
73     }
74     
75     #endif /* _ASM_SEMAPHORE_HELPER_H */
76     
77     
78