ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/vendor/libpeak-0.1.2/peak/arch/m68k/spinlock.h
Revision: 3251
Committed: Wed Apr 2 16:58:30 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-chdr
File size: 1699 byte(s)
Log Message:
- Imported libpeak-0.1.2

File Contents

# Content
1 /*
2 * m68k spinlock operations
3 *
4 * No SMP with m68k, so these functions only implement simple locks that
5 * always depress with sched_yield().
6 *
7 * $Id: spinlock.h.in,v 1.1 2007/05/24 12:54:09 mbuna Exp $
8 */
9 #ifndef INCLUDED_PEAK_SPINLOCK_H_
10 #define INCLUDED_PEAK_SPINLOCK_H_
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15 #include <pthread.h>
16 #include <unistd.h>
17 #ifdef HAVE_SCHED_H
18 #include <sched.h>
19 #endif
20
21 #if defined(__cplusplus)
22 extern "C" {
23 #endif
24
25 /* PEAK INTERNAL SPIN LOCK
26 *
27 * _peak_spinlock_lock(lockp)
28 * _peak_spinlock_lock_try(lockp) - returns 0 (busy) or 1 (got the lock)
29 * _peak_spinlock_unlock(lockp)
30 *
31 */
32
33 extern int _peak_is_threaded;
34
35 typedef volatile unsigned char peak_spinlock_t;
36 #define PEAK_SPINLOCK_INITIALIZER (0)
37
38 /* set *spinlock to 1 and returns previous value */
39 static inline unsigned
40 __peak_spinlock_testandset(register peak_spinlock_t *lockp)
41 {
42 int ret;
43
44 __asm__ __volatile__("tas %1; "
45 "seq %0"
46 : "=dm" (ret), "=m" (*lockp)
47 : "1" (*lockp));
48 return ret & 1;
49 }
50
51 static inline void
52 _peak_spinlock_lock(peak_spinlock_t *lockp)
53 {
54 if (!_peak_is_threaded) /* set only if peak uses several threads */
55 return;
56
57 while (__peak_spinlock_testandset(lockp))
58 sched_yield();
59 }
60
61 static inline int
62 _peak_spinlock_lock_try(peak_spinlock_t *lockp)
63 {
64 if (!_peak_is_threaded)
65 return 1; /* always succeed */
66
67 return !__peak_spinlock_testandset(lockp);
68 }
69
70 static inline void
71 _peak_spinlock_unlock(peak_spinlock_t *lockp)
72 {
73 if (!_peak_is_threaded)
74 return;
75
76 *lockp = 0;
77 }
78
79 #if defined(__cplusplus)
80 }
81 #endif
82
83 #endif /* INCLUDED_PEAK_SPINLOCK_H_ */