Subversion Repositories Kolibri OS

Rev

Rev 5056 | Rev 5272 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1616 serge 1
/*
2
 * Mutexes: blocking mutual exclusion locks
3
 *
4
 * started by Ingo Molnar:
5
 *
6
 *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar 
7
 *
8
 * This file contains the main data structure and API definitions.
9
 */
10
#ifndef __LINUX_MUTEX_H
11
#define __LINUX_MUTEX_H
12
 
5270 serge 13
#include 
1616 serge 14
#include 
15
#include 
5270 serge 16
#include 
17
#include 
18
#include 
1616 serge 19
 
20
/*
21
 * Simple, straightforward mutexes with strict semantics:
22
 *
23
 * - only one task can hold the mutex at a time
24
 * - only the owner can unlock the mutex
25
 * - multiple unlocks are not permitted
26
 * - recursive locking is not permitted
27
 * - a mutex object must be initialized via the API
28
 * - a mutex object must not be initialized via memset or copying
29
 * - task may not exit with mutex held
30
 * - memory areas where held locks reside must not be freed
31
 * - held mutexes must not be reinitialized
32
 * - mutexes may not be used in hardware or software interrupt
33
 *   contexts such as tasklets and timers
34
 *
35
 * These semantics are fully enforced when DEBUG_MUTEXES is
36
 * enabled. Furthermore, besides enforcing the above rules, the mutex
37
 * debugging code also implements a number of additional features
38
 * that make lock debugging easier and faster:
39
 *
40
 * - uses symbolic names of mutexes, whenever they are printed in debug output
41
 * - point-of-acquire tracking, symbolic lookup of function names
42
 * - list of all locks held in the system, printout of them
43
 * - owner tracking
44
 * - detects self-recursing locks and prints out all relevant info
45
 * - detects multi-task circular deadlocks and prints out all affected
46
 *   locks and tasks (and only those tasks)
47
 */
48
struct mutex {
49
	/* 1: unlocked, 0: locked, negative: locked, possible waiters */
50
	struct list_head	wait_list;
2161 serge 51
    atomic_t            count;
1616 serge 52
};
53
 
54
/*
55
 * This is the control structure for tasks blocked on mutex,
56
 * which resides on the blocked task's kernel stack:
57
 */
58
struct mutex_waiter {
59
	struct list_head	list;
60
    int                *task;
61
};
62
 
1967 serge 63
 
64
#define __MUTEX_INITIALIZER(lockname) \
2161 serge 65
                { .wait_list = LIST_HEAD_INIT(lockname.wait_list), \
66
                  .count = ATOMIC_INIT(1) \
67
                }
1967 serge 68
 
69
#define DEFINE_MUTEX(mutexname) \
70
        struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
71
 
1616 serge 72
void __attribute__ ((fastcall)) __attribute__ ((dllimport))
73
     mutex_init(struct mutex*)__asm__("MutexInit");
74
void __attribute__ ((fastcall)) __attribute__ ((dllimport))
75
     mutex_lock(struct mutex*)__asm__("MutexLock");
76
void __attribute__ ((fastcall)) __attribute__ ((dllimport))
77
     mutex_unlock(struct mutex*)__asm__("MutexUnlock");
78
 
3391 Serge 79
static inline int mutex_lock_interruptible(struct mutex *lock)
80
{
81
    mutex_lock(lock);
82
    return 0;
83
}
84
 
85
# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
86
 
87
 
1616 serge 88
/**
89
 * mutex_is_locked - is the mutex locked
90
 * @lock: the mutex to be queried
91
 *
92
 * Returns 1 if the mutex is locked, 0 if unlocked.
93
 */
94
static inline int mutex_is_locked(struct mutex *lock)
95
{
96
	return atomic_read(&lock->count) != 1;
97
}
98
 
5056 serge 99
static inline int mutex_trylock(struct mutex *lock)
100
{
101
    if (likely(atomic_cmpxchg(&lock->count, 1, 0) == 1))
102
        return 1;
103
    return 0;
104
}
105
 
106
static inline void mutex_destroy(struct mutex *lock)
107
{
108
 
109
};
110
 
1616 serge 111
#endif