My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sw_semaphores.h
Go to the documentation of this file.
1 /*
2  * OpenVirtualization:
3  * For additional details and support contact developer@sierraware.com.
4  * Additional documentation can be found at www.openvirtualization.org
5  *
6  * Copyright (C) 2010-2014 SierraWare
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  */
23 /*
24  * Header for semaphore implementation functions
25  */
26 
27 
28 #ifndef SW_SEMAPHORES_H
29 #define SW_SEMAPHORES_H
30 
31 #include <sw_types.h>
32 #include <sw_cpu_helper.h>
33 
34 #define OTZ_LOCKED 1
35 #define OTZ_FREE 0
36 
37 #define TRUE 1
38 #define FALSE 0
39 
40 #define OTZ_OK 0
41 #define OTZ_INVALID 1
42 #define OTZ_BUSY 2
43 #define OTZ_AGAIN 3
44 
45 #define INIT_SPIN_LOCK(s) ((s)->lock = 0)
46 
52 extern void sw_spinlock_s(void *data);
53 
59 extern void sw_spinunlock_s(void *data);
60 
66 extern void sw_sem_post_s(volatile s32int *data);
67 
73 extern void sw_sem_wait_s(volatile s32int *data);
74 
82 extern u32 sw_trylock_s(void *data);
83 
87 typedef struct {
89  volatile u32 lock_param;
90 } sw_mutex_t;
91 
92 #define OTZ_MUTEX_INIT {TRUE, OTZ_FREE}
93 
94 /* this implementation does not support attributes. This has been kept for
95  * consistency with pthread programming models. */
96 #define sw_mutexattr_t void
97 
111 s32int sw_mutex_init(sw_mutex_t *mutex, const sw_mutexattr_t *attribute);
112 
123 s32int sw_mutex_destroy(sw_mutex_t *mutex);
124 
134 s32int sw_mutex_lock(sw_mutex_t *mutex);
135 
145 s32int sw_mutex_unlock(sw_mutex_t *mutex);
146 
157 s32int sw_mutex_trylock(sw_mutex_t *mutex);
158 
159 
163 typedef struct {
165  volatile u32 counter_param;
166 } sw_sem_t;
167 
179 s32int sw_sem_init(sw_sem_t *sem, s32int shared, u32 value);
180 
191 s32int sw_sem_wait(sw_sem_t *sem);
192 
204 s32int sw_sem_post(sw_sem_t *sem);
205 
217 s32int sw_sem_destroy(sw_sem_t *sem);
218 
231 s32int sw_sem_getvalue(sw_sem_t *sem, s32int *value);
232 
236 struct spinlock{
237  u32 lock;
238 };
239 
246 static inline void spin_lock_irqsave(struct spinlock* slock,
247  irq_flags_t* irq_flags)
248 {
249  *irq_flags = cpu_irq_save();
250  sw_spinlock_s(&slock->lock);
251 
252 }
253 
260 static inline void spin_unlock_irqrestore(struct spinlock* slock,
261  irq_flags_t irq_flags)
262 {
263  sw_spinunlock_s(&slock->lock);
264  cpu_irq_restore(irq_flags);
265 }
266 
267 #endif
268 
volatile u32 counter_param
Definition: sw_semaphores.h:165
s32int sw_mutex_lock(sw_mutex_t *mutex)
this function locks the mutex object referenced. If the mutex is already locked, it blocks till it be...
Definition: sw_semaphores.c:131
void sw_spinlock_s(void *data)
s32int sw_mutex_init(sw_mutex_t *mutex, const sw_mutexattr_t *attribute)
dymanic initialization of the mutex. The attribute parameter is kept for consistency and is not used...
Definition: sw_semaphores.c:93
void sw_sem_wait_s(volatile s32int *data)
volatile u32 lock_param
Definition: sw_semaphores.h:89
s32int sw_sem_getvalue(sw_sem_t *sem, s32int *value)
Get the current value of the semaphore. It is copied into the argument supplied by the caller...
Definition: sw_semaphores.c:262
u32 lock
Definition: sw_semaphores.h:237
s32int sw_sem_init(sw_sem_t *sem, s32int shared, u32 value)
This function initializes the semaphore, and sets is counters to the value passed to it...
Definition: sw_semaphores.c:182
s32int sw_mutex_unlock(sw_mutex_t *mutex)
this function unlocks the mutex object referenced. Attempts to unlock a mutex that is already locked ...
Definition: sw_semaphores.c:147
Definition: sw_semaphores.h:87
s32int sw_mutex_destroy(sw_mutex_t *mutex)
this function destroys the mutex object. Attempting to destroy a mutex that has already been destroye...
Definition: sw_semaphores.c:112
s32int sw_sem_wait(sw_sem_t *sem)
This function locks the semaphore referenced by the sem value. An atomic decrement is performed...
Definition: sw_semaphores.c:202
void sw_spinunlock_s(void *data)
u8 * data
Definition: fat32.c:40
void cpu_irq_restore(irq_flags_t flags)
restores the interrupt request
Definition: cpu.c:139
s32int sw_sem_post(sw_sem_t *sem)
This function unlocks the semaphore referenced by sem. The value is incremented atomically. If there are any threads waiting on this semaphore, only one of them would be released from sem_wait.
Definition: sw_semaphores.c:222
u32 init_param
Definition: sw_semaphores.h:88
s32int sw_sem_destroy(sw_sem_t *sem)
Delete the semaphore pointed to by sem variable The semaphore cannot be used after deletion...
Definition: sw_semaphores.c:241
irq_flags_t cpu_irq_save(void)
saves interrupt request
Definition: cpu.c:120
#define sw_mutexattr_t
Definition: sw_semaphores.h:96
Definition: sw_semaphores.h:163
s32int sw_mutex_trylock(sw_mutex_t *mutex)
Try to lock the mutex. This function is same as the lock function, except that it returns if the mute...
Definition: sw_semaphores.c:162
u32 init_param
Definition: sw_semaphores.h:164
Definition: sw_semaphores.h:236
void sw_sem_post_s(volatile s32int *data)
u32 sw_trylock_s(void *data)