add mailbox system
This commit is contained in:
parent
c2947fbb88
commit
41ab209d83
2 changed files with 217 additions and 0 deletions
142
include/eduos/mailbox.h
Normal file
142
include/eduos/mailbox.h
Normal file
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
* Copyright (c) 2010, Stefan Lankes, RWTH Aachen University
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __MAILBOX_H__
|
||||
#define __MAILBOX_H__
|
||||
|
||||
#include <eduos/string.h>
|
||||
#include <eduos/mailbox_types.h>
|
||||
#include <eduos/tasks.h>
|
||||
#include <eduos/semaphore.h>
|
||||
#include <eduos/errno.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define MAILBOX(name, type) \
|
||||
inline static int mailbox_##name##_init(mailbox_##name##_t* m) { \
|
||||
if (BUILTIN_EXPECT(!m, 0)) \
|
||||
return -EINVAL; \
|
||||
\
|
||||
memset(m->buffer, 0x00, sizeof(type)*MAILBOX_SIZE); \
|
||||
m->wpos = m->rpos = 0; \
|
||||
sem_init(&m->mails, 0); \
|
||||
sem_init(&m->boxes, MAILBOX_SIZE); \
|
||||
spinlock_init(&m->rlock); \
|
||||
spinlock_init(&m->wlock); \
|
||||
\
|
||||
return 0; \
|
||||
}\
|
||||
\
|
||||
inline static int mailbox_##name##_destroy(mailbox_##name##_t* m) { \
|
||||
if (BUILTIN_EXPECT(!m, 0)) \
|
||||
return -EINVAL; \
|
||||
\
|
||||
sem_destroy(&m->mails); \
|
||||
sem_destroy(&m->boxes); \
|
||||
spinlock_destroy(&m->rlock); \
|
||||
spinlock_destroy(&m->wlock); \
|
||||
\
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
inline static int mailbox_##name##_post(mailbox_##name##_t* m, type mail) { \
|
||||
if (BUILTIN_EXPECT(!m, 0)) \
|
||||
return -EINVAL; \
|
||||
\
|
||||
sem_wait(&m->boxes); \
|
||||
spinlock_lock(&m->wlock); \
|
||||
m->buffer[m->wpos] = mail; \
|
||||
m->wpos = (m->wpos+1) % MAILBOX_SIZE; \
|
||||
spinlock_unlock(&m->wlock); \
|
||||
sem_post(&m->mails); \
|
||||
\
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
inline static int mailbox_##name##_trypost(mailbox_##name##_t* m, type mail) { \
|
||||
if (BUILTIN_EXPECT(!m, 0)) \
|
||||
return -EINVAL; \
|
||||
\
|
||||
if (sem_trywait(&m->boxes)) \
|
||||
return -EBUSY; \
|
||||
spinlock_lock(&m->wlock); \
|
||||
m->buffer[m->wpos] = mail; \
|
||||
m->wpos = (m->wpos+1) % MAILBOX_SIZE; \
|
||||
spinlock_unlock(&m->wlock); \
|
||||
sem_post(&m->mails); \
|
||||
\
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
inline static int mailbox_##name##_fetch(mailbox_##name##_t* m, type* mail) { \
|
||||
int err; \
|
||||
\
|
||||
if (BUILTIN_EXPECT(!m || !mail, 0)) \
|
||||
return -EINVAL; \
|
||||
\
|
||||
err = sem_wait(&m->mails); \
|
||||
if (err) return err; \
|
||||
spinlock_lock(&m->rlock); \
|
||||
*mail = m->buffer[m->rpos]; \
|
||||
m->rpos = (m->rpos+1) % MAILBOX_SIZE; \
|
||||
spinlock_unlock(&m->rlock); \
|
||||
sem_post(&m->boxes); \
|
||||
\
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
inline static int mailbox_##name##_tryfetch(mailbox_##name##_t* m, type* mail) { \
|
||||
if (BUILTIN_EXPECT(!m || !mail, 0)) \
|
||||
return -EINVAL; \
|
||||
\
|
||||
if (sem_trywait(&m->mails) != 0) \
|
||||
return -EINVAL; \
|
||||
spinlock_lock(&m->rlock); \
|
||||
*mail = m->buffer[m->rpos]; \
|
||||
m->rpos = (m->rpos+1) % MAILBOX_SIZE; \
|
||||
spinlock_unlock(&m->rlock); \
|
||||
sem_post(&m->boxes); \
|
||||
\
|
||||
return 0; \
|
||||
}\
|
||||
|
||||
MAILBOX(wait_msg, wait_msg_t)
|
||||
MAILBOX(int32, int32_t)
|
||||
MAILBOX(int16, int16_t)
|
||||
MAILBOX(int8, int8_t)
|
||||
MAILBOX(uint32, uint32_t)
|
||||
MAILBOX(uint16, uint16_t)
|
||||
MAILBOX(uint8, uint8_t)
|
||||
MAILBOX(ptr, void*)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
75
include/eduos/mailbox_types.h
Normal file
75
include/eduos/mailbox_types.h
Normal file
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2010, Stefan Lankes, RWTH Aachen University
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @author Stefan Lankes
|
||||
* @file include/metalsvm/mailbox_types.h
|
||||
* @brief Message type structure definitions for various task return types
|
||||
*/
|
||||
|
||||
#ifndef __MAILBOX_TYPES_H__
|
||||
#define __MAILBOX_TYPES_H__
|
||||
|
||||
#include <eduos/semaphore_types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @brief Wait message structure
|
||||
*
|
||||
* This message struct keeps a recipient task id and the message itself */
|
||||
typedef struct {
|
||||
/// The task id of the task which is waiting for this message
|
||||
tid_t id;
|
||||
/// The message payload
|
||||
int32_t result;
|
||||
} wait_msg_t;
|
||||
|
||||
#define MAILBOX_TYPES(name, type) \
|
||||
typedef struct mailbox_##name { \
|
||||
type buffer[MAILBOX_SIZE]; \
|
||||
int wpos, rpos; \
|
||||
sem_t mails; \
|
||||
sem_t boxes; \
|
||||
spinlock_t rlock, wlock; \
|
||||
} mailbox_##name##_t;
|
||||
|
||||
MAILBOX_TYPES(wait_msg, wait_msg_t)
|
||||
MAILBOX_TYPES(int32, int32_t)
|
||||
MAILBOX_TYPES(int16, int16_t)
|
||||
MAILBOX_TYPES(int8, int8_t)
|
||||
MAILBOX_TYPES(uint32, uint32_t)
|
||||
MAILBOX_TYPES(uint16, uint16_t)
|
||||
MAILBOX_TYPES(uint8, uint8_t)
|
||||
MAILBOX_TYPES(ptr, void*)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue