mars/brick.h

657 lines
19 KiB
C
Raw Normal View History

2010-07-23 11:55:18 +00:00
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
#ifndef BRICK_H
#define BRICK_H
#define msleep msleep_interruptible
2011-02-23 20:48:06 +00:00
#include <linux/list.h>
2010-07-30 05:46:22 +00:00
#include <linux/sched.h>
2011-02-23 20:48:06 +00:00
#include <linux/wait.h>
2010-07-30 05:46:22 +00:00
2011-04-15 10:13:22 +00:00
#include <asm/atomic.h>
#include "brick_locks.h"
2011-07-20 13:11:44 +00:00
#include "meta.h"
2011-08-31 11:42:04 +00:00
#define MAX_BRICK_TYPES 64
#ifdef _STRATEGY
#define _STRATEGY_CODE(X) X
#define _NORMAL_CODE(X) /**/
#else
#define _STRATEGY_CODE(X) /**/
#define _NORMAL_CODE(X) X
#endif
#define SAFE_STR(str) ((str) ? (str) : "NULL")
2010-07-23 11:55:18 +00:00
2011-03-24 16:05:46 +00:00
#define BRICK_FATAL "BRICK_FATAL "
#define BRICK_ERROR "BRICK_ERROR "
#define BRICK_WARNING "BRICK_WARN "
#define BRICK_INFO "BRICK_INFO "
#define BRICK_DEBUG "BRICK_DEBUG "
2011-03-22 14:36:26 +00:00
2011-07-20 13:11:44 +00:00
#define _BRICK_FMT(_fmt) __BASE_FILE__ " %d %s(): " _fmt, __LINE__, __FUNCTION__
2010-07-23 11:55:18 +00:00
2011-07-20 13:11:44 +00:00
#define _BRICK_MSG(_dump, PREFIX, _fmt, _args...) do { say(PREFIX _BRICK_FMT(_fmt), ##_args); if (_dump) brick_dump_stack(); } while (0)
2011-03-22 14:36:26 +00:00
2011-07-20 13:11:44 +00:00
#define BRICK_FAT(_fmt, _args...) _BRICK_MSG(true, BRICK_FATAL, _fmt, ##_args)
#define BRICK_ERR(_fmt, _args...) _BRICK_MSG(true, BRICK_ERROR, _fmt, ##_args)
#define BRICK_WRN(_fmt, _args...) _BRICK_MSG(false, BRICK_WARNING, _fmt, ##_args)
#define BRICK_INF(_fmt, _args...) _BRICK_MSG(false, BRICK_INFO, _fmt, ##_args)
2011-03-22 14:36:26 +00:00
2010-07-23 11:55:18 +00:00
#ifdef BRICK_DEBUGGING
2011-07-20 13:11:44 +00:00
#define BRICK_DBG(_fmt, _args...) _BRICK_MSG(false, BRICK_DEBUG, _fmt, ##_args)
2010-07-23 11:55:18 +00:00
#else
2011-07-20 13:11:44 +00:00
#define BRICK_DBG(_args...) /**/
2010-07-23 11:55:18 +00:00
#endif
2011-03-22 14:36:26 +00:00
#ifdef IO_DEBUGGING
2011-07-20 13:11:44 +00:00
#define BRICK_IO(_fmt, _args...) _BRICK_MSG(false, BRICK_DEBUG, _fmt, ##_args)
2011-03-22 14:36:26 +00:00
#else
2011-07-20 13:11:44 +00:00
#define BRICK_IO(_args...) /*empty*/
2011-03-22 14:36:26 +00:00
#endif
#include "brick_checking.h"
2010-07-23 11:55:18 +00:00
/////////////////////////////////////////////////////////////////////////
2011-03-24 16:05:46 +00:00
// number management helpers
2010-07-23 11:55:18 +00:00
2011-03-24 16:05:46 +00:00
extern int get_nr(void);
extern void put_nr(int nr);
/////////////////////////////////////////////////////////////////////////
// definitions for generic objects with aspects
2010-07-23 11:55:18 +00:00
struct generic_object;
2010-07-23 11:55:18 +00:00
struct generic_aspect;
2010-08-10 17:39:30 +00:00
#define GENERIC_ASPECT_TYPE(TYPE) \
const char *aspect_type_name; \
2010-07-30 05:46:22 +00:00
const struct generic_object_type *object_type; \
2010-08-08 09:03:42 +00:00
int aspect_size; \
int (*init_fn)(struct TYPE##_aspect *ini); \
void (*exit_fn)(struct TYPE##_aspect *ini); \
2010-07-23 11:55:18 +00:00
struct generic_aspect_type {
GENERIC_ASPECT_TYPE(generic);
};
2010-08-10 17:39:30 +00:00
#define GENERIC_OBJECT_TYPE(TYPE) \
const char *object_type_name; \
2010-07-23 11:55:18 +00:00
int default_size; \
int object_type_nr; \
int (*init_fn)(struct TYPE##_object *ini); \
void (*exit_fn)(struct TYPE##_object *ini); \
2010-07-23 11:55:18 +00:00
struct generic_object_type {
GENERIC_OBJECT_TYPE(generic);
};
2010-08-10 17:39:30 +00:00
#define GENERIC_OBJECT_LAYOUT(TYPE) \
int size_hint; \
2010-08-05 15:54:48 +00:00
atomic_t alloc_count; \
2010-08-08 09:03:42 +00:00
atomic_t free_count; \
2010-07-23 11:55:18 +00:00
struct generic_object_layout {
GENERIC_OBJECT_LAYOUT(generic);
};
extern void init_generic_object_layout(struct generic_object_layout *lay, const struct generic_object_type *type);
extern void exit_generic_object_layout(struct generic_object_layout *lay);
2010-08-10 17:39:30 +00:00
#define GENERIC_OBJECT(TYPE) \
const struct generic_object_type *object_type; \
2010-08-10 17:39:30 +00:00
struct TYPE##_object_layout *object_layout; \
struct TYPE##_aspect **aspects; \
int aspect_nr_max; \
int free_offset; \
int max_offset; \
2010-07-23 11:55:18 +00:00
struct generic_object {
GENERIC_OBJECT(generic);
};
2010-08-10 17:39:30 +00:00
#define GENERIC_ASPECT(TYPE) \
struct TYPE##_object *object; \
const struct generic_aspect_type *aspect_type; \
bool shortcut; \
2010-07-23 11:55:18 +00:00
struct generic_aspect {
GENERIC_ASPECT(generic);
};
/////////////////////////////////////////////////////////////////////////
// definitions for asynchronous callback objects
2010-08-10 17:39:30 +00:00
#define GENERIC_CALLBACK(TYPE) \
void (*cb_fn)(struct TYPE##_callback *cb); \
2010-08-08 14:02:54 +00:00
void *cb_private; \
int cb_error; \
struct generic_callback *cb_next; \
2010-08-08 14:02:54 +00:00
struct generic_callback {
GENERIC_CALLBACK(generic);
};
#define CALLBACK_OBJECT(TYPE) \
GENERIC_OBJECT(TYPE); \
struct generic_callback *object_cb; \
struct generic_callback _object_cb; \
struct callback_object {
CALLBACK_OBJECT(generic);
};
/* Initial setup of the callback chain
*/
#define SETUP_CALLBACK(obj,fn,priv) \
(obj)->_object_cb.cb_fn = (fn); \
(obj)->_object_cb.cb_private = (priv); \
(obj)->_object_cb.cb_error = 0; \
(obj)->_object_cb.cb_next = NULL; \
(obj)->object_cb = &(obj)->_object_cb; \
/* Insert a new member into the callback chain
*/
#define INSERT_CALLBACK(obj,new,fn,priv) \
if (!(new)->cb_fn) { \
(new)->cb_fn = (fn); \
(new)->cb_private = (priv); \
(new)->cb_error = 0; \
(new)->cb_next = (obj)->object_cb; \
(obj)->object_cb = (new); \
}
/* Call the first callback in the chain.
*/
#define SIMPLE_CALLBACK(obj,err) \
if (obj) { \
struct generic_callback *__cb = (obj)->object_cb; \
if (__cb) { \
__cb->cb_error = (err); \
__cb->cb_fn(__cb); \
} \
}
#define CHECKED_CALLBACK(obj,err,done) \
{ \
struct generic_callback *__cb; \
CHECK_PTR(obj, done); \
__cb = (obj)->object_cb; \
CHECK_PTR_NULL(__cb, done); \
__cb->cb_error = (err); \
__cb->cb_fn(__cb); \
}
/* An intermediate callback handler must call this
* to continue the callback chain.
*/
#define NEXT_CHECKED_CALLBACK(cb,done) \
{ \
struct generic_callback *__next_cb = (cb)->cb_next; \
CHECK_PTR_NULL(__next_cb, done); \
__next_cb->cb_error = (cb)->cb_error; \
__next_cb->cb_fn(__next_cb); \
}
/* Query the callback status.
* This uses always the first member of the chain!
*/
#define CALLBACK_ERROR(obj) \
((obj)->object_cb ? (obj)->object_cb->cb_error : -EINVAL)
2010-07-23 11:55:18 +00:00
/////////////////////////////////////////////////////////////////////////
// definitions for generic bricks
struct generic_input;
struct generic_output;
struct generic_brick_ops;
struct generic_output_ops;
struct generic_brick_type;
2011-02-23 20:48:06 +00:00
struct generic_switch {
bool button;
bool led_on;
bool led_off;
2011-02-27 14:17:58 +00:00
bool force_off;
int percent_done;
2011-02-23 20:48:06 +00:00
wait_queue_head_t event;
};
2010-08-01 20:21:18 +00:00
#define GENERIC_BRICK(BRICK) \
2011-02-23 20:48:06 +00:00
const char *brick_name; \
2010-08-01 20:21:18 +00:00
const struct BRICK##_brick_type *type; \
struct BRICK##_brick_ops *ops; \
2010-07-23 11:55:18 +00:00
int nr_inputs; \
int nr_outputs; \
int brick_index; /* globally unique */ \
2010-08-01 20:21:18 +00:00
struct BRICK##_input **inputs; \
struct BRICK##_output **outputs; \
2011-02-23 20:48:06 +00:00
struct generic_switch power; \
2011-02-28 18:00:32 +00:00
int (*free)(struct BRICK##_brick *del); \
2010-07-23 11:55:18 +00:00
struct list_head tmp_head; \
struct generic_brick {
GENERIC_BRICK(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_INPUT(BRICK) \
2011-02-23 20:48:06 +00:00
const char *input_name; \
2010-08-01 20:21:18 +00:00
struct BRICK##_brick *brick; \
const struct BRICK##_input_type *type; \
2010-08-04 17:32:04 +00:00
struct BRICK##_output *connect; \
2010-07-30 05:46:22 +00:00
struct list_head input_head; \
2010-07-23 11:55:18 +00:00
struct generic_input {
GENERIC_INPUT(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_OUTPUT(BRICK) \
2011-02-23 20:48:06 +00:00
const char *output_name; \
2010-08-01 20:21:18 +00:00
struct BRICK##_brick *brick; \
2010-08-04 17:32:04 +00:00
const struct BRICK##_output_type *type; \
struct BRICK##_output_ops *ops; \
2010-07-30 05:46:22 +00:00
struct list_head output_head; \
2010-07-23 11:55:18 +00:00
int nr_connected; \
struct generic_output {
GENERIC_OUTPUT(generic);
};
2010-07-30 05:46:22 +00:00
#define GENERIC_OUTPUT_CALL(OUTPUT,OP,ARGS...) \
( \
2010-08-01 20:21:18 +00:00
(void)LOCK_CHECK(OP), \
2010-07-30 05:46:22 +00:00
(OUTPUT) && (OUTPUT)->ops->OP ? \
(OUTPUT)->ops->OP(OUTPUT, ##ARGS) : \
-ENOSYS \
)
#define GENERIC_INPUT_CALL(INPUT,OP,ARGS...) \
( \
2010-08-01 20:21:18 +00:00
(void)LOCK_CHECK(OP), \
2010-07-30 05:46:22 +00:00
(INPUT) && (INPUT)->connect ? \
GENERIC_OUTPUT_CALL((INPUT)->connect, OP, ##ARGS) : \
2010-08-05 15:54:48 +00:00
-ECONNREFUSED \
2010-07-30 05:46:22 +00:00
)
2010-07-23 11:55:18 +00:00
2010-08-01 20:21:18 +00:00
#define GENERIC_BRICK_OPS(BRICK) \
2011-02-23 20:48:06 +00:00
int (*brick_switch)(struct BRICK##_brick *brick); \
2010-07-23 11:55:18 +00:00
struct generic_brick_ops {
GENERIC_BRICK_OPS(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_OUTPUT_OPS(BRICK) \
/*int (*output_start)(struct BRICK##_output *output);*/ \
/*int (*output_stop)(struct BRICK##_output *output);*/ \
2010-07-23 11:55:18 +00:00
struct generic_output_ops {
GENERIC_OUTPUT_OPS(generic)
};
// although possible, *_type should never be extended
2010-08-01 20:21:18 +00:00
#define GENERIC_BRICK_TYPE(BRICK) \
2011-02-23 20:48:06 +00:00
const char *type_name; \
2010-07-23 11:55:18 +00:00
int brick_size; \
int max_inputs; \
int max_outputs; \
2010-08-01 20:21:18 +00:00
const struct BRICK##_input_type **default_input_types; \
2011-02-23 20:48:06 +00:00
const char **default_input_names; \
2010-08-01 20:21:18 +00:00
const struct BRICK##_output_type **default_output_types; \
2011-02-23 20:48:06 +00:00
const char **default_output_names; \
2010-08-01 20:21:18 +00:00
struct BRICK##_brick_ops *master_ops; \
const struct generic_aspect_type **aspect_types; \
2010-08-04 22:38:48 +00:00
const struct BRICK##_input_types **default_type; \
2010-08-01 20:21:18 +00:00
int (*brick_construct)(struct BRICK##_brick *brick); \
int (*brick_destruct)(struct BRICK##_brick *brick); \
2010-07-23 11:55:18 +00:00
struct generic_brick_type {
GENERIC_BRICK_TYPE(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_INPUT_TYPE(BRICK) \
2010-07-23 11:55:18 +00:00
char *type_name; \
int input_size; \
2010-08-01 20:21:18 +00:00
int (*input_construct)(struct BRICK##_input *input); \
int (*input_destruct)(struct BRICK##_input *input); \
2010-07-23 11:55:18 +00:00
struct generic_input_type {
GENERIC_INPUT_TYPE(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_OUTPUT_TYPE(BRICK) \
2010-07-23 11:55:18 +00:00
char *type_name; \
int output_size; \
2010-08-01 20:21:18 +00:00
struct BRICK##_output_ops *master_ops; \
2010-08-04 22:38:48 +00:00
int (*output_construct)(struct BRICK##_output *output); \
2010-08-01 20:21:18 +00:00
int (*output_destruct)(struct BRICK##_output *output); \
2010-07-23 11:55:18 +00:00
struct generic_output_type {
GENERIC_OUTPUT_TYPE(generic);
};
int generic_register_brick_type(const struct generic_brick_type *new_type);
int generic_unregister_brick_type(const struct generic_brick_type *old_type);
2011-04-08 09:52:46 +00:00
INLINE void _generic_output_init(struct generic_brick *brick, const struct generic_output_type *type, struct generic_output *output, const char *output_name)
2010-08-10 17:39:30 +00:00
{
output->output_name = output_name;
output->brick = brick;
output->type = type;
output->ops = type->master_ops;
output->nr_connected = 0;
2011-02-27 14:17:58 +00:00
INIT_LIST_HEAD(&output->output_head);
2010-08-10 17:39:30 +00:00
}
2011-04-08 09:52:46 +00:00
INLINE void _generic_output_exit(struct generic_output *output)
2011-03-24 16:05:46 +00:00
{
list_del_init(&output->output_head);
output->output_name = NULL;
output->brick = NULL;
output->type = NULL;
output->ops = NULL;
output->nr_connected = 0;
}
2010-07-23 11:55:18 +00:00
#ifdef _STRATEGY // call this only in strategy bricks, never in ordinary bricks
// you need this only if you circumvent generic_brick_init_full()
2011-04-08 09:52:46 +00:00
INLINE int generic_brick_init(const struct generic_brick_type *type, struct generic_brick *brick, const char *brick_name)
2010-07-23 11:55:18 +00:00
{
brick->brick_index = get_nr();
2010-07-23 11:55:18 +00:00
brick->brick_name = brick_name;
brick->type = type;
brick->ops = type->master_ops;
brick->nr_inputs = 0;
brick->nr_outputs = 0;
2011-02-23 20:48:06 +00:00
brick->power.led_off = true;
init_waitqueue_head(&brick->power.event);
2011-03-24 16:05:46 +00:00
INIT_LIST_HEAD(&brick->tmp_head);
2010-07-23 11:55:18 +00:00
return 0;
}
2011-04-08 09:52:46 +00:00
INLINE void generic_brick_exit(struct generic_brick *brick)
2011-03-24 16:05:46 +00:00
{
list_del_init(&brick->tmp_head);
brick->brick_name = NULL;
brick->type = NULL;
brick->ops = NULL;
brick->nr_inputs = 0;
brick->nr_outputs = 0;
put_nr(brick->brick_index);
2011-03-24 16:05:46 +00:00
}
2011-04-08 09:52:46 +00:00
INLINE int generic_input_init(struct generic_brick *brick, int index, const struct generic_input_type *type, struct generic_input *input, const char *input_name)
2010-07-23 11:55:18 +00:00
{
if (index < 0 || index >= brick->type->max_inputs)
2011-03-01 18:00:14 +00:00
return -EINVAL;
2010-07-23 11:55:18 +00:00
if (brick->inputs[index])
return -EEXIST;
input->input_name = input_name;
input->brick = brick;
input->type = type;
input->connect = NULL;
2011-02-27 14:17:58 +00:00
INIT_LIST_HEAD(&input->input_head);
2010-07-23 11:55:18 +00:00
brick->inputs[index] = input;
brick->nr_inputs++;
return 0;
}
2011-04-08 09:52:46 +00:00
INLINE void generic_input_exit(struct generic_input *input)
2011-03-24 16:05:46 +00:00
{
list_del_init(&input->input_head);
input->input_name = NULL;
input->brick = NULL;
input->type = NULL;
input->connect = NULL;
}
2011-04-08 09:52:46 +00:00
INLINE int generic_output_init(struct generic_brick *brick, int index, const struct generic_output_type *type, struct generic_output *output, const char *output_name)
2010-07-23 11:55:18 +00:00
{
if (index < 0 || index >= brick->type->max_outputs)
return -ENOMEM;
if (brick->outputs[index])
return -EEXIST;
2010-08-10 17:39:30 +00:00
_generic_output_init(brick, type, output, output_name);
2010-07-23 11:55:18 +00:00
brick->outputs[index] = output;
brick->nr_outputs++;
return 0;
}
2011-04-08 09:52:46 +00:00
INLINE int generic_size(const struct generic_brick_type *brick_type)
2010-07-23 11:55:18 +00:00
{
int size = brick_type->brick_size;
int i;
size += brick_type->max_inputs * sizeof(void*);
for (i = 0; i < brick_type->max_inputs; i++) {
size += brick_type->default_input_types[i]->input_size;
}
size += brick_type->max_outputs * sizeof(void*);
for (i = 0; i < brick_type->max_outputs; i++) {
size += brick_type->default_output_types[i]->output_size;
}
return size;
}
/* If possible, use this instead of generic_*_init().
* input_types and output_types may be NULL => use default_*_types
*/
int generic_brick_init_full(
void *data,
int size,
const struct generic_brick_type *brick_type,
const struct generic_input_type **input_types,
const struct generic_output_type **output_types,
2011-02-23 20:48:06 +00:00
const char **names);
2010-07-23 11:55:18 +00:00
int generic_brick_exit_full(
struct generic_brick *brick);
2011-04-08 09:52:46 +00:00
INLINE int generic_connect(struct generic_input *input, struct generic_output *output)
2010-07-23 11:55:18 +00:00
{
BRICK_DBG("generic_connect(input=%p, output=%p)\n", input, output);
2011-03-01 18:00:14 +00:00
if (unlikely(!input || !output))
2010-07-23 11:55:18 +00:00
return -EINVAL;
2011-03-01 18:00:14 +00:00
if (unlikely(input->connect))
2010-07-23 11:55:18 +00:00
return -EEXIST;
2011-03-23 17:58:02 +00:00
if (unlikely(!list_empty(&input->input_head)))
return -EINVAL;
2011-03-01 18:00:14 +00:00
// helps only against the most common errors
if (unlikely(input->brick == output->brick))
return -EDEADLK;
2011-03-23 17:58:02 +00:00
2010-07-23 11:55:18 +00:00
input->connect = output;
2011-03-20 17:38:08 +00:00
output->nr_connected++;
2011-03-23 17:58:02 +00:00
list_add(&input->input_head, &output->output_head);
BRICK_DBG("now nr_connected=%d\n", output->nr_connected);
2010-07-23 11:55:18 +00:00
return 0;
}
2011-04-08 09:52:46 +00:00
INLINE int generic_disconnect(struct generic_input *input)
2010-07-23 11:55:18 +00:00
{
2011-08-25 10:16:32 +00:00
struct generic_output *connect;
2010-07-23 11:55:18 +00:00
BRICK_DBG("generic_disconnect(input=%p)\n", input);
if (!input)
return -EINVAL;
2011-08-25 10:16:32 +00:00
connect = input->connect;
if (connect) {
connect->nr_connected--;
BRICK_DBG("now nr_connected=%d\n", connect->nr_connected);
2010-07-23 11:55:18 +00:00
input->connect = NULL;
2011-02-27 14:17:58 +00:00
list_del_init(&input->input_head);
2010-07-23 11:55:18 +00:00
}
return 0;
}
#endif // _STRATEGY
// simple wrappers for type safety
2010-08-01 20:21:18 +00:00
#define GENERIC_MAKE_FUNCTIONS(BRICK) \
2011-08-25 10:16:32 +00:00
static inline int BRICK##_register_brick_type(void) \
2010-07-23 11:55:18 +00:00
{ \
2010-08-01 20:21:18 +00:00
extern const struct BRICK##_brick_type BRICK##_brick_type; \
extern int BRICK##_brick_nr; \
if (BRICK##_brick_nr >= 0) { \
BRICK_ERR("brick type " #BRICK " is already registered.\n"); \
2010-07-23 11:55:18 +00:00
return -EEXIST; \
} \
2010-08-01 20:21:18 +00:00
BRICK##_brick_nr = generic_register_brick_type((const struct generic_brick_type*)&BRICK##_brick_type); \
return BRICK##_brick_nr < 0 ? BRICK##_brick_nr : 0; \
2010-07-23 11:55:18 +00:00
} \
\
2011-08-25 10:16:32 +00:00
static inline int BRICK##_unregister_brick_type(void) \
2010-07-23 11:55:18 +00:00
{ \
2010-08-01 20:21:18 +00:00
extern const struct BRICK##_brick_type BRICK##_brick_type; \
return generic_unregister_brick_type((const struct generic_brick_type*)&BRICK##_brick_type); \
2010-07-23 11:55:18 +00:00
} \
\
2010-08-01 20:21:18 +00:00
extern const struct BRICK##_brick_type BRICK##_brick_type; \
2010-08-10 17:39:30 +00:00
extern const struct BRICK##_input_type BRICK##_input_type; \
2010-08-01 20:21:18 +00:00
extern const struct BRICK##_output_type BRICK##_output_type; \
2010-07-23 11:55:18 +00:00
\
2011-08-25 10:16:32 +00:00
INLINE void _##BRICK##_output_init(struct BRICK##_brick *brick, struct BRICK##_output *output, char *output_name) \
2010-08-10 17:39:30 +00:00
{ \
_generic_output_init( \
(struct generic_brick*)brick, \
(const struct generic_output_type*)&BRICK##_output_type, \
(struct generic_output*)output, \
output_name); \
} \
\
_STRATEGY_CODE( \
2011-08-25 10:16:32 +00:00
INLINE int BRICK##_brick_init(struct BRICK##_brick *brick, char *brick_name) \
2010-07-23 11:55:18 +00:00
{ \
2010-08-01 20:21:18 +00:00
return generic_brick_init((const struct generic_brick_type*)&BRICK##_brick_type, (struct generic_brick*)brick, brick_name); \
2010-07-23 11:55:18 +00:00
} \
\
2011-08-25 10:16:32 +00:00
INLINE int BRICK##_input_init(struct BRICK##_brick *brick, int index, struct BRICK##_input *input, char *input_name) \
2010-07-23 11:55:18 +00:00
{ \
return generic_input_init( \
(struct generic_brick*)brick, \
index, \
2010-08-01 20:21:18 +00:00
(struct generic_input_type*)&BRICK##_input_type, \
2010-07-23 11:55:18 +00:00
(struct generic_input*)input, \
input_name); \
} \
\
2011-08-25 10:16:32 +00:00
INLINE int BRICK##_output_init(struct BRICK##_brick *brick, int index, struct BRICK##_output *output, char *output_name) \
2010-07-23 11:55:18 +00:00
{ \
return generic_output_init( \
(struct generic_brick*)brick, \
index, \
2010-08-01 20:21:18 +00:00
(const struct generic_output_type*)&BRICK##_output_type, \
2010-07-23 11:55:18 +00:00
(struct generic_output*)output, \
output_name); \
} \
\
)
/* Define a pair of connectable subtypes.
* For type safety, use this for all possible combinations.
* Yes, this may become quadratic in large type systems, but
* (a) thou shalt not define much types,
2010-07-23 11:55:18 +00:00
* (b) these macros generate only definitions, but no additional
* code at runtime.
*/
2010-08-10 17:39:30 +00:00
#define GENERIC_MAKE_CONNECT(INPUT_BRICK,OUTPUT_BRICK) \
2010-07-23 11:55:18 +00:00
\
_STRATEGY_CODE( \
\
2011-04-08 09:52:46 +00:00
INLINE int INPUT_BRICK##_##OUTPUT_BRICK##_connect( \
2010-08-01 20:21:18 +00:00
struct INPUT_BRICK##_input *input, \
struct OUTPUT_BRICK##_output *output) \
2010-07-23 11:55:18 +00:00
{ \
return generic_connect((struct generic_input*)input, (struct generic_output*)output); \
} \
\
2011-04-08 09:52:46 +00:00
INLINE int INPUT_BRICK##_##OUTPUT_BRICK####_disconnect( \
2010-08-01 20:21:18 +00:00
struct INPUT_BRICK##_input *input) \
2010-07-23 11:55:18 +00:00
{ \
return generic_disconnect((struct generic_input*)input); \
} \
)
2010-07-30 05:46:22 +00:00
///////////////////////////////////////////////////////////////////////
// default operations on objects / aspects
extern struct generic_object *generic_alloc(struct generic_brick *brick, struct generic_object_layout *object_layout, const struct generic_object_type *object_type);
extern void generic_free(struct generic_object *object);
extern struct generic_aspect *generic_get_aspect(struct generic_brick *brick, struct generic_object *obj);
2010-07-30 05:46:22 +00:00
2010-08-10 17:39:30 +00:00
#define GENERIC_ASPECT_FUNCTIONS(BRICK,TYPE) \
2010-07-30 05:46:22 +00:00
\
INLINE struct TYPE##_object *BRICK##_alloc_##TYPE(struct BRICK##_brick *brick, struct generic_object_layout *object_layout) \
2010-08-01 20:21:18 +00:00
{ \
return (void*)generic_alloc((struct generic_brick*)brick, object_layout, &TYPE##_type); \
2010-08-04 17:32:04 +00:00
} \
\
INLINE void BRICK##_free_##TYPE(struct TYPE##_object *object) \
2010-08-04 17:32:04 +00:00
{ \
generic_free((struct generic_object*)object); \
2010-08-01 20:21:18 +00:00
} \
2010-08-02 16:31:10 +00:00
\
INLINE struct BRICK##_##TYPE##_aspect *BRICK##_##TYPE##_get_aspect(struct BRICK##_brick *brick, struct TYPE##_object *obj) \
2010-08-02 16:31:10 +00:00
{ \
return (void*)generic_get_aspect((struct generic_brick*)brick, (struct generic_object*)obj); \
2010-08-02 16:31:10 +00:00
} \
\
2010-08-01 20:21:18 +00:00
2010-07-30 05:46:22 +00:00
///////////////////////////////////////////////////////////////////////
2011-02-23 20:48:06 +00:00
// some general helpers
extern void get_lamport(struct timespec *now);
extern void set_lamport(struct timespec *old);
2010-07-30 05:46:22 +00:00
2011-02-28 18:00:32 +00:00
/* Generic interface to simple brick status changes.
2011-02-27 14:17:58 +00:00
*/
extern void set_button(struct generic_switch *sw, bool val, bool force);
2011-02-23 20:48:06 +00:00
extern void set_led_on(struct generic_switch *sw, bool val);
extern void set_led_off(struct generic_switch *sw, bool val);
2011-03-01 09:34:36 +00:00
/*
* "Forced switch off" means that it cannot be switched on again.
*/
extern void set_button_wait(struct generic_brick *brick, bool val, bool force, int timeout);
2011-02-23 20:48:06 +00:00
2011-02-27 14:17:58 +00:00
/* Operations on networks of bricks (wiring graphs).
*
* Switch on => first switch on all predecessors in the wiring graph
* Switch off => first switch off all successors in the wiring graph
*
* Operations on brick networks by multiple threads in parallel are dangerous,
* because the buttons may start flipping.
* There is one exception: when @force is set, only the direction to
* "off" remains possible. This is useful for emergency shutdowns.
*/
2011-02-28 18:00:32 +00:00
typedef enum {
// only one brick instance
BR_ON_ONE, // switch on one brick instance
BR_OFF_ONE, // just switch off (may be switched on again)
BR_KILL_ONE, // forced switch off => may be never switched on again
BR_FREE_ONE, // forced switch off + deallocation (when possible)
// dito, but operating on the whole graph
BR_ON_ALL,
BR_OFF_ALL,
BR_KILL_ALL,
BR_FREE_ALL,
} brick_switch_t;
extern int set_recursive_button(struct generic_brick *brick, brick_switch_t mode, int timeout);
2011-02-27 14:17:58 +00:00
2011-08-25 10:16:32 +00:00
/////////////////////////////////////////////////////////////////////////
// init
extern int init_brick(void);
extern void exit_brick(void);
2010-07-30 11:50:20 +00:00
#endif