e40d689873ecaa8c06ce5e91aee07c2e2019768b
[bertos.git] / bertos / mware / resource.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \brief TODO:
34  *
35  * \version $Id$
36  *
37  * \author Bernardo Innocenti <bernie@develer.com>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  */
40
41 #ifndef MWARE_RESOURCE_H
42 #define MWARE_RESOURCE_H
43
44 #include <drv/timer.h> // time_t
45 #include <kern/sem.h>
46
47 #warning FIXME:Revise me!
48
49 /*
50  * Abstract locking primitives used by host OS.
51  */
52 typedef Semaphore ResourceLock;
53 #define ResMan_sleep()         timer_delay(1)
54 #define ResMan_time_t          mtime_t
55
56
57
58 // Forward decl
59 struct Observer;
60
61 /**
62  * Hold context information for a resource such as an audio channel.
63  *
64  * Each driver registers one or more Resource instances with the
65  * ResMan using ResMan_Register().
66  *
67  * Clients can then allocate the resource through ResMan_Alloc()
68  * providing a desired priority and an Observer for asynchronous
69  * notification.
70  *
71  * Allocated resources can be stolen by other clients asking for a
72  * higher priority.  ResMan notifies a preemption request by invoking
73  * the Observer of the current owner.
74  *
75  * The Observer callback must take whatever action is needed to
76  * release the resource as soon as possible to avoid blocking the
77  * new owner.
78  */
79 typedef struct Resource
80 {
81 //Private
82         /// Control access to fields below.
83         Semaphore lock;
84
85         /// Pointer to current owner's observer.  NULL if resource is free.
86         struct Observer *owner;
87
88         /// Priority of current owner (higher values mean higher priority).
89         int pri;
90
91         /// Queue of processes waiting to obtain the resource.
92         List queue;
93 } Resource;
94
95 /// Event sent by ResMan to owners when to request resource release.
96 enum { EVENT_RELEASE = 1 };
97
98 /// Try to allocate a resource \a res with priority \a pri for at most \a timeout ticks.
99 bool ResMan_Alloc(Resource *res, int pri, ResMan_time_t timeout, struct Observer *releaseRequest);
100
101 /// Free resource \a res.  Will eventually wake-up other queued owners.
102 void ResMan_Free(Resource *res);
103
104 void ResMan_Init(Resource *res);
105
106 #endif /* MWARE_RESOURCE_H */