source: trunk/kernel/kern/kcond_var.h @ 330

Last change on this file since 330 was 1, checked in by alain, 8 years ago

First import

File size: 2.5 KB
Line 
1/*
2 * kern/kcond_var.h - kernel condition variables synchronization
3 *
4 * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
5 * Copyright (c) 2011,2012 UPMC Sorbonne Universites
6 * 
7 * This file is part of ALMOS-kernel.
8 *
9 * ALMOS-kernel is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2.0 of the License.
12 *
13 * ALMOS-kernel is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ALMOS-kernel; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef _KCOND_VAR_H_
24#define _KCOND_VAR_H_
25
26#include <types.h>
27#include <spinlock.h>
28#include <wait_queue.h>
29
30///////////////////////////////////////////////////
31///               Public Section                ///
32///////////////////////////////////////////////////
33
34/** Kernel Condition Variable Sync Type */
35typedef struct wait_queue_s kcv_t;             
36
37/** Initialize CondVar */
38#define kcv_init(cv,name)
39
40/** Wait on CondVar until condition completion */
41#define kcv_wait(cv,lock)
42
43/** Signal (notifiy) CondVar and Wakeup one waiting thread */
44#define kcv_signal(cv)
45
46/** Destroy CondVar */
47#define kcv_destroy(cv)
48
49/** Broadcast CondVar and return the number of notified threads */
50static inline uint_t kcv_broadcast(kcv_t *cv);
51
52
53///////////////////////////////////////////////////
54///               Private Section               ///
55///////////////////////////////////////////////////
56
57#undef kcv_init
58#define kcv_init(_cv,_name)                             \
59        do{                                             \
60                wait_queue_init((_cv), (_name));        \
61        }while(0)
62
63#undef kcv_wait
64#define kcv_wait(_cv,_lock)                             \
65        do{                                             \
66                wait_on((_cv), WAIT_LAST);              \
67                spinlock_unlock_nosched((_lock));       \
68                sched_sleep(CURRENT_THREAD);            \
69                spinlock_lock((_lock));                 \
70        }while(0)
71
72#undef kcv_signal
73#define kcv_signal(_cv)                                 \
74        do{                                             \
75                (void)wakeup_one((_cv), WAIT_ANY);      \
76        }while(0)
77
78static inline uint_t kcv_broadcast(kcv_t *cv)
79{
80        return wakeup_all(cv); 
81}
82
83#undef kcv_destroy
84#define kcv_destroy(_cv)                                                \
85        do{                                                             \
86                if(!(wait_queue_isEmpty((_cv))))                        \
87                        printk(ERROR,"ERROR: %s: kcv [%s] is busy\n", __FUNCTION__, (_cv)->name); \
88        }while(0)
89
90#endif  /* _KCOND_VAR_H_ */
Note: See TracBrowser for help on using the repository browser.