lib/flags: introduce compact creation calls

Zero-initialized event groups timed on the monotonic clock is the
most common form used by applications. Allow people to write more
compact code by providing creation calls and static initializers aimed
at building these directly:

- evl_new_flags(), EVL_FLAGS_INITIALIZER() for zero-init event groups
  timed on the monotonic clock.

- evl_new_flags_any() and EVL_FLAGS_ANY_INITIALIZER() usable for any
  initialization form, specifying the clock and init value.
This commit is contained in:
Philippe Gerum 2019-08-18 19:57:17 +02:00
parent bdc3aab501
commit 5af05a5020
6 changed files with 23 additions and 10 deletions

View File

@ -17,7 +17,7 @@
#define __FLAGS_ACTIVE_MAGIC 0xb42bb42b
#define __FLAGS_DEAD_MAGIC 0
int evl_new_flags(struct evl_flags *flg, int clockfd, int initval,
int evl_new_flags_any(struct evl_flags *flg, int clockfd, int initval,
const char *fmt, ...)
{
int fd;
@ -65,7 +65,7 @@ static int check_sanity(struct evl_flags *flg)
int efd;
if (flg->magic == __FLAGS_UNINIT_MAGIC) {
efd = evl_new_flags(flg,
efd = evl_new_flags_any(flg,
flg->uninit.clockfd,
flg->uninit.initval,
flg->uninit.name);

View File

@ -27,7 +27,7 @@ struct evl_flags {
#define __FLAGS_UNINIT_MAGIC 0xfebcfebc
#define EVL_FLAGS_INITIALIZER(__name, __clockfd, __initval) { \
#define EVL_FLAGS_ANY_INITIALIZER(__name, __clockfd, __initval) { \
.magic = __FLAGS_UNINIT_MAGIC, \
.uninit = { \
.name = (__name), \
@ -36,14 +36,20 @@ struct evl_flags {
} \
}
#define EVL_FLAGS_INITIALIZER(__name) { \
EVL_FLAGS_ANY_INITIALIZER(__name, EVL_CLOCK_MONOTONIC, 0)
#ifdef __cplusplus
extern "C" {
#endif
int evl_new_flags(struct evl_flags *flg,
int evl_new_flags_any(struct evl_flags *flg,
int clockfd, int initval,
const char *fmt, ...);
#define evl_new_flags(__flg, __fmt, __args...) \
evl_new_flags_any(__flg, EVL_CLOCK_MONOTONIC, 0, __fmt, ##__args)
int evl_open_flags(struct evl_flags *flg,
const char *fmt, ...);

View File

@ -12,6 +12,7 @@
#include <linux/types.h>
#include <uapi/evl/types.h>
#include <uapi/evl/monitor.h>
#include <uapi/evl/clock.h>
struct evl_flags {
unsigned int magic;
@ -31,7 +32,7 @@ struct evl_flags {
#define __FLAGS_UNINIT_MAGIC 0xfebcfebc
#define EVL_FLAGS_INITIALIZER(__name, __clockfd, __initval) { \
#define EVL_FLAGS_ANY_INITIALIZER(__name, __clockfd, __initval) { \
.magic = __FLAGS_UNINIT_MAGIC, \
.uninit = { \
.name = (__name), \
@ -40,14 +41,20 @@ struct evl_flags {
} \
}
#define EVL_FLAGS_INITIALIZER(__name) { \
EVL_FLAGS_ANY_INITIALIZER(__name, EVL_CLOCK_MONOTONIC, 0)
#ifdef __cplusplus
extern "C" {
#endif
int evl_new_flags(struct evl_flags *flg,
int evl_new_flags_any(struct evl_flags *flg,
int clockfd, int initval,
const char *fmt, ...);
#define evl_new_flags(__flg, __fmt, __args...) \
evl_new_flags_any(__flg, EVL_CLOCK_MONOTONIC, 0, __fmt, ##__args)
int evl_open_flags(struct evl_flags *flg,
const char *fmt, ...);

View File

@ -27,7 +27,7 @@
#define __FLAGS_ACTIVE_MAGIC 0xb42bb42b
#define __FLAGS_DEAD_MAGIC 0
int evl_new_flags(struct evl_flags *flg, int clockfd, int initval,
int evl_new_flags_any(struct evl_flags *flg, int clockfd, int initval,
const char *fmt, ...)
{
struct evl_monitor_attrs attrs;
@ -124,7 +124,7 @@ static int check_sanity(struct evl_flags *flg)
int efd;
if (flg->magic == __FLAGS_UNINIT_MAGIC) {
efd = evl_new_flags(flg,
efd = evl_new_flags_any(flg,
flg->uninit.clockfd,
flg->uninit.initval,
flg->uninit.name);

View File

@ -106,7 +106,7 @@ int main(int argc, char *argv[])
__Tcall_assert(sfd, evl_new_sem(&c.start, EVL_CLOCK_MONOTONIC, 0, name));
name = get_unique_name(EVL_MONITOR_DEV, 2);
__Tcall_assert(ffd, evl_new_flags(&c.flags, EVL_CLOCK_MONOTONIC, 0, name));
__Tcall_assert(ffd, evl_new_flags(&c.flags, name));
new_thread(&receiver, SCHED_FIFO, LOW_PRIO,
flags_receiver, &c);

View File

@ -68,7 +68,7 @@ int main(int argc, char *argv[])
__Tcall_assert(tfd, evl_attach_self("poll-flags:%d", getpid()));
name = get_unique_name(EVL_MONITOR_DEV, 0);
__Tcall_assert(ffd, evl_new_flags(&flags, EVL_CLOCK_MONOTONIC, 0, name));
__Tcall_assert(ffd, evl_new_flags(&flags, name));
__Tcall_assert(pollfd_in, evl_new_poll());
__Tcall_assert(ret, evl_add_pollfd(pollfd_in, ffd, POLLIN));