summaryrefslogtreecommitdiff
path: root/sos-code-article6.5/sos/ksynch.h
blob: 597971ddd11bf7e93c8404d4f83d59e742d20198 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (C) 2004 David Decotigny

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
   USA. 
*/
#ifndef _SOS_KSYNCH_H_
#define _SOS_KSYNCH_H_


/**
 * @file synch.h
 *
 * Common kernel synchronisation primitives.
 */


#include <sos/errno.h>
#include <sos/kwaitq.h>


/* ====================================================================
 * Kernel semaphores, NON-recursive
 */


/**
 * The structure of a (NON-RECURSIVE) kernel Semaphore
 */
struct sos_ksema
{
  int value;
  struct sos_kwaitq kwaitq;
};


/*
 * Initialize a kernel semaphore structure with the given name
 *
 * @param name Name of the semaphore (for debugging purpose only; safe
 * [deep copied])
 *
 * @param initial_value The value of the semaphore before any up/down
 */
sos_ret_t sos_ksema_init(struct sos_ksema *sema, const char *name,
			 int initial_value);


/*
 * De-initialize a kernel semaphore
 *
 * @return -SOS_EBUSY when semaphore could not be de-initialized
 * because at least a thread is in the waitq.
 */
sos_ret_t sos_ksema_dispose(struct sos_ksema *sema);


/*
 * Enters the semaphore
 *
 * @param timeout Maximum time to wait for the semaphore. Or NULL for
 * "no limit". Updated on return to reflect the time remaining (0 when
 * timeout has been triggered)
 *
 * @return -SOS_EINTR when timeout was triggered or when another waitq
 * woke us up.
 *
 * @note This is a BLOCKING FUNCTION
 */
sos_ret_t sos_ksema_down(struct sos_ksema *sema,
			 struct sos_time *timeout);


/*
 * Try to enter the semaphore without blocking.
 *
 * @return -SOS_EBUSY when locking the semaphore would block
 */
sos_ret_t sos_ksema_trydown(struct sos_ksema *sema);


/**
 * Increments the semaphore's value, eventually waking up a thread
 */
sos_ret_t sos_ksema_up(struct sos_ksema *sema);



/* ====================================================================
 * Kernel mutex (ie binary semaphore with strong ownership),
 * NON-recursive !
 */


/**
 * The structure of a (NON-RECURSIVE) kernel Mutex
 */
struct sos_kmutex
{
  struct sos_thread  *owner;
  struct sos_kwaitq  kwaitq;
};


/*
 * Initialize a kernel mutex structure with the given name
 *
 * @param name Name of the mutex (for debugging purpose only; safe
 * [deep copied])
 *
 * @param initial_value The value of the mutex before any up/down
 */
sos_ret_t sos_kmutex_init(struct sos_kmutex *mutex, const char *name);


/*
 * De-initialize a kernel mutex
 *
 * @return -SOS_EBUSY when mutex could not be de-initialized
 * because at least a thread is in the waitq.
 */
sos_ret_t sos_kmutex_dispose(struct sos_kmutex *mutex);


/*
 * Lock the mutex. If the same thread multiply locks the same mutex,
 * it won't hurt (no deadlock, return value = -SOS_EBUSY).
 *
 * @param timeout Maximum time to wait for the mutex. Or NULL for "no
 * limit". Updated on return to reflect the time remaining (0 when
 * timeout has been triggered)
 *
 * @return -SOS_EINTR when timeout was triggered or when another waitq
 * woke us up, -SOS_EBUSY when the thread already owns the mutex.
 *
 * @note This is a BLOCKING FUNCTION
 */
sos_ret_t sos_kmutex_lock(struct sos_kmutex *mutex,
			  struct sos_time *timeout);


/*
 * Try to lock the mutex without blocking.
 *
 * @return -SOS_EBUSY when locking the mutex would block
 */
sos_ret_t sos_kmutex_trylock(struct sos_kmutex *mutex);


/**
 * Unlock the mutex, eventually waking up a thread
 *
 * @return -SOS_EPERM when the calling thread is NOT the owner of the
 * mutex
 */
sos_ret_t sos_kmutex_unlock(struct sos_kmutex *mutex);


#endif /* _SOS_KSYNCH_H_ */