UbixOS  2.0
system.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <mpi/mpi.h>
30 #include <lib/kmalloc.h>
31 #include <string.h>
32 #include <ubixos/spinlock.h>
33 #include <sys/sysproto.h>
34 
35 
36 static mpi_mbox_t *mboxList = 0x0;
37 static struct spinLock mpiSpinLock = SPIN_LOCK_INITIALIZER;
38 
39 /*****************************************************************************************
40 
41  Function: static mpiMbox_t * mpiFindMbox(char *name)
42 
43  Description: This function will find a mail box that matches the supplied name
44 
45  Notes: This function is not task-safe! Lock must be done before call.
46 
47  *****************************************************************************************/
48 static mpi_mbox_t * mpi_findMbox(char *name) {
49  mpi_mbox_t *mbox = 0x0;
50 
51  for (mbox = mboxList; mbox; mbox = mbox->next) {
52  if (!strcmp(mbox->name, name)) {
53  return (mbox);
54  }
55  }
56 
57  return (0x0);
58 }
59 
60 /*****************************************************************************************
61 
62  Function: int mpiCreateMbox(char *name)
63 
64  Description: This function will create a new mailbox if it fails it will return -1
65  otherwise it returns 0x0
66 
67  Notes:
68 
69  *****************************************************************************************/
70 int mpi_createMbox(char *name) {
71  mpi_mbox_t *mbox = 0x0;
72 
73  spinLock(&mpiSpinLock);
74 if (mpi_findMbox(name) != 0x0) {
75  spinUnlock(&mpiSpinLock);
76  return (-1);
77  }
78 
79  mbox = (mpi_mbox_t *) kmalloc(sizeof(mpi_mbox_t));
80 
81 sprintf(mbox->name, name);
82  mbox->pid = _current->id;
83 
84  if (mboxList == 0x0) {
85  mbox->prev = 0x0;
86  mbox->next = 0x0;
87  mboxList = mbox;
88  }
89  else {
90  mbox->next = mboxList;
91  mbox->prev = 0x0;
92  mboxList->prev = mbox;
93  mboxList = mbox;
94  }
95 
96 
97  spinUnlock(&mpiSpinLock);
98  return (0x0);
99 }
100 
101 /*****************************************************************************************
102 
103  Function: int mpiSpam(uInt32 type,void *data)
104 
105  Description: This function will send a message to every mailbox
106 
107  Notes:
108 
109  *****************************************************************************************/
110 int mpi_spam(uInt32 type, void *data) {
111  mpi_mbox_t *mbox = 0x0;
112  mpi_message_t *message = 0x0;
113 
114  spinLock(&mpiSpinLock);
115 
116  for (mbox = mboxList; mbox; mbox = mbox->next) {
117  message = (mpi_message_t *) kmalloc(sizeof(mpi_message_t));
118 
119  message->header = type;
120  memcpy(message->data, data, MESSAGE_LENGTH);
121  message->next = 0x0;
122 
123  if (mbox->msg == 0x0) {
124  mbox->msg = message;
125  }
126  else {
127  mbox->msgLast->next = message;
128  mbox->msgLast = message;
129  }
130  }
131 
132  spinUnlock(&mpiSpinLock);
133  return (0x0);
134 }
135 
136 /*****************************************************************************************
137 
138  Function: int mpiPostMessage(char *name,uInt32 type,void *data)
139 
140  Description: This function will post a message to specified mailbox
141 
142  Notes:
143 
144  *****************************************************************************************/
145 int mpi_postMessage(char *name, uint32_t type, mpi_message_t *msg) {
146  mpi_mbox_t *mbox = 0x0;
147  mpi_message_t *message = 0x0;
148 
149  spinLock(&mpiSpinLock);
150 
151  mbox = mpi_findMbox(name);
152 
153  if (mbox == 0x0) {
154  spinUnlock(&mpiSpinLock);
155  return (0x1);
156  }
157 
158  message = (mpi_message_t *) kmalloc(sizeof(mpi_message_t));
159 
160  message->header = msg->header;
161  memcpy(message->data, msg->data, MESSAGE_LENGTH);
162  message->pid = _current->id;
163  message->next = 0x0;
164 
165  if (mbox->msg == 0x0) {
166  mbox->msg = message;
167  }
168  else {
169  mbox->msgLast->next = message;
170  mbox->msgLast = message;
171  }
172 
173  spinUnlock(&mpiSpinLock);
174 
175  if (type == 0x2) {
176  while (mbox->msgLast != 0x0)
177  ;
178  }
179 
180  return (0x0);
181 }
182 
183 /*****************************************************************************************
184 
185  Function: int mpiFetchMessage(char *name,mpiMessage_t *msg)
186 
187  Description: This function will fetch the next message out of the specified mailbox
188 
189  Notes:
190 
191  *****************************************************************************************/
192 int mpi_fetchMessage(char *name, mpi_message_t *msg) {
193  mpi_mbox_t *mbox = 0x0;
194  mpi_message_t *tmpMsg = 0x0;
195 
196  spinLock(&mpiSpinLock);
197 
198  mbox = mpi_findMbox(name);
199 
200  if (mbox == 0x0) {
201  spinUnlock(&mpiSpinLock);
202  return (-1);
203  }
204 
205  if (mbox->msg == 0x0) {
206  spinUnlock(&mpiSpinLock);
207  return (-1);
208  }
209 
210  if (mbox->pid != _current->id) {
211  spinUnlock(&mpiSpinLock);
212  return (-1);
213  }
214 
215  msg->header = mbox->msg->header;
216  memcpy(msg->data, mbox->msg->data, MESSAGE_LENGTH);
217  msg->pid = mbox->msg->pid;
218 
219  tmpMsg = mbox->msg;
220  mbox->msg = mbox->msg->next;
221 
222  kfree(tmpMsg);
223 
224  spinUnlock(&mpiSpinLock);
225  return (0x0);
226 }
227 
228 /*****************************************************************************************
229 
230  Function: int mpiDestroyMbox(char *name)
231 
232  Description: This function will fetch the next message out of the specified mailbox
233 
234  Notes:
235 
236  *****************************************************************************************/
237 int mpi_destroyMbox(char *name) {
238  mpi_mbox_t *mbox = 0x0;
239 
240  spinLock(&mpiSpinLock);
241 
242  for (mbox = mboxList; mbox; mbox = mbox->next) {
243  if (!strcmp(mbox->name, name)) {
244  if (mbox->pid != _current->id) {
245  spinUnlock(&mpiSpinLock);
246  return (-1);
247  }
248  mbox->prev->next = mbox->next;
249  mbox->next->prev = mbox->prev;
250  kfree(mbox);
251  spinUnlock(&mpiSpinLock);
252  return (0x0);
253  }
254  }
255 
256  spinUnlock(&mpiSpinLock);
257  return (-1);
258 }
259 
260 /***
261  END
262  ***/
mpi_mbox
Definition: mpi.h:44
spinlock.h
sysproto.h
mpi_mbox::msg
struct mpi_message * msg
Definition: mpi.h:47
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
mpi_mbox::msgLast
struct mpi_message * msgLast
Definition: mpi.h:48
string.h
mpi_message::header
uInt32 header
Definition: mpi.h:39
kfree
void kfree(void *baseAddr)
Definition: kmalloc.c:342
mpi_postMessage
int mpi_postMessage(char *name, uint32_t type, mpi_message_t *msg)
Definition: system.c:141
strcmp
int strcmp(const char *str1, const char *str2)
spinUnlock
void spinUnlock(spinLock_t *lock)
Definition: spinlock.c:36
SPIN_LOCK_INITIALIZER
#define SPIN_LOCK_INITIALIZER
Definition: spinlock.h:36
memcpy
void * memcpy(const void *dst, const void *src, size_t length)
sprintf
int sprintf(char *buf, const char *fmt,...)
Definition: kprintf.c:278
mpi_message::next
struct mpi_message * next
Definition: mpi.h:41
spinLock
void spinLock(spinLock_t *lock)
Definition: spinlock.c:55
taskStruct::id
pidType id
Definition: sched.h:63
mpi_spam
int mpi_spam(uInt32 type, void *data)
Definition: system.c:107
mpi.h
uint32_t
__uint32_t uint32_t
Definition: types.h:46
_current
kTask_t * _current
Definition: sched.c:50
name
const char * name
Definition: pci.c:37
mpi_mbox::prev
struct mpi_mbox * prev
Definition: mpi.h:46
mpi_createMbox
int mpi_createMbox(char *name)
Definition: system.c:68
mpi_destroyMbox
int mpi_destroyMbox(char *name)
Definition: system.c:231
mpi_message::pid
pidType pid
Definition: mpi.h:40
kmalloc
void * kmalloc(uInt32 len)
Definition: kmalloc.c:241
spinLock
Definition: spinlock.h:41
mpi_message::data
char data[248]
Definition: mpi.h:38
mpi_mbox::pid
pidType pid
Definition: mpi.h:50
mpi_mbox::next
struct mpi_mbox * next
Definition: mpi.h:45
MESSAGE_LENGTH
#define MESSAGE_LENGTH
Definition: mpi.h:35
mpi_message
Definition: mpi.h:37
mpi_mbox::name
char name[64]
Definition: mpi.h:49
mpi_fetchMessage
int mpi_fetchMessage(char *name, mpi_message_t *msg)
Definition: system.c:187
kmalloc.h